Sharkey/src/web/app/common/views/components/othello.game.vue

258 lines
6.4 KiB
Vue
Raw Normal View History

2018-03-07 03:40:40 +01:00
<template>
2018-03-07 09:48:32 +01:00
<div class="root">
2018-03-08 09:57:57 +01:00
<header><b>{{ blackUser.name }}</b>() vs <b>{{ whiteUser.name }}</b>()</header>
<p class="turn" v-if="!iAmPlayer && !game.is_ended">{{ turnUser.name }}のターンです<mk-ellipsis/></p>
<p class="turn" v-if="logPos != logs.length">{{ turnUser.name }}のターン</p>
<p class="turn" v-if="iAmPlayer && !game.is_ended">{{ isMyTurn ? 'あなたのターンです' : '相手のターンです' }}<mk-ellipsis v-if="!isMyTurn"/></p>
<p class="result" v-if="game.is_ended && logPos == logs.length">
<template v-if="game.winner"><b>{{ game.winner.name }}</b>の勝ち{{ game.settings.is_llotheo ? ' (ロセオ)' : '' }}</template>
2018-03-07 10:45:16 +01:00
<template v-else>引き分け</template>
</p>
2018-03-08 09:57:57 +01:00
<div class="board" :style="{ 'grid-template-rows': `repeat(${ game.settings.map.size }, 1fr)`, 'grid-template-columns': `repeat(${ game.settings.map.size }, 1fr)` }">
2018-03-07 09:48:32 +01:00
<div v-for="(stone, i) in o.board"
2018-03-08 10:30:28 +01:00
:class="{ empty: stone == null, none: o.map.data[i] == ' ', myTurn: !game.is_ended && isMyTurn, can: turnUser ? o.canPut(turnUser.id == blackUser.id ? 'black' : 'white', i) : null, prev: o.prevPos == i }"
2018-03-07 09:48:32 +01:00
@click="set(i)"
>
2018-03-08 09:57:57 +01:00
<img v-if="stone == 'black'" :src="`${blackUser.avatar_url}?thumbnail&size=128`" alt="">
<img v-if="stone == 'white'" :src="`${whiteUser.avatar_url}?thumbnail&size=128`" alt="">
2018-03-07 09:48:32 +01:00
</div>
</div>
2018-03-08 09:57:57 +01:00
2018-03-07 13:57:06 +01:00
<p>:{{ o.blackCount }} :{{ o.whiteCount }} 合計:{{ o.blackCount + o.whiteCount }}</p>
2018-03-08 09:57:57 +01:00
2018-03-07 13:57:06 +01:00
<div class="graph">
<div v-for="n in 61 - o.stats.length">
</div>
<div v-for="data in o.stats">
<div :style="{ height: `${ Math.floor(data.b * 100) }%` }"></div>
<div :style="{ height: `${ Math.floor(data.w * 100) }%` }"></div>
</div>
</div>
2018-03-08 09:57:57 +01:00
<div class="player" v-if="game.is_ended">
2018-03-07 13:57:06 +01:00
<el-button type="primary" @click="logPos = 0" :disabled="logPos == 0">%fa:fast-backward%</el-button>
<el-button type="primary" @click="logPos--" :disabled="logPos == 0">%fa:backward%</el-button>
<span>{{ logPos }} / {{ logs.length }}</span>
<el-button type="primary" @click="logPos++" :disabled="logPos == logs.length">%fa:forward%</el-button>
<el-button type="primary" @click="logPos = logs.length" :disabled="logPos == logs.length">%fa:fast-forward%</el-button>
</div>
2018-03-07 03:40:40 +01:00
</div>
</template>
<script lang="ts">
import Vue from 'vue';
2018-03-08 09:57:57 +01:00
import Othello, { Color } from '../../../../../common/othello/core';
2018-03-07 09:48:32 +01:00
2018-03-07 03:40:40 +01:00
export default Vue.extend({
2018-03-08 09:57:57 +01:00
props: ['game', 'connection'],
2018-03-07 13:57:06 +01:00
2018-03-07 03:40:40 +01:00
data() {
return {
2018-03-08 09:57:57 +01:00
o: null as Othello,
2018-03-07 13:57:06 +01:00
logs: [],
2018-03-08 09:57:57 +01:00
logPos: 0
2018-03-07 03:40:40 +01:00
};
},
2018-03-07 13:57:06 +01:00
2018-03-07 09:48:32 +01:00
computed: {
2018-03-07 10:45:16 +01:00
iAmPlayer(): boolean {
2018-03-08 09:57:57 +01:00
return this.game.user1_id == (this as any).os.i.id || this.game.user2_id == (this as any).os.i.id;
2018-03-07 10:45:16 +01:00
},
2018-03-08 09:57:57 +01:00
myColor(): Color {
if (!this.iAmPlayer) return null;
if (this.game.user1_id == (this as any).os.i.id && this.game.black == 1) return 'black';
if (this.game.user2_id == (this as any).os.i.id && this.game.black == 2) return 'black';
return 'white';
2018-03-07 09:48:32 +01:00
},
2018-03-08 09:57:57 +01:00
opColor(): Color {
if (!this.iAmPlayer) return null;
2018-03-07 09:48:32 +01:00
return this.myColor == 'black' ? 'white' : 'black';
2018-03-08 09:57:57 +01:00
},
blackUser(): any {
return this.game.black == 1 ? this.game.user1 : this.game.user2;
},
whiteUser(): any {
return this.game.black == 1 ? this.game.user2 : this.game.user1;
},
turnUser(): any {
if (this.o.turn == 'black') {
return this.game.black == 1 ? this.game.user1 : this.game.user2;
} else if (this.o.turn == 'white') {
return this.game.black == 1 ? this.game.user2 : this.game.user1;
} else {
return null;
}
},
isMyTurn(): boolean {
if (this.turnUser == null) return null;
return this.turnUser.id == (this as any).os.i.id;
2018-03-07 09:48:32 +01:00
}
},
2018-03-07 13:57:06 +01:00
watch: {
logPos(v) {
2018-03-08 09:57:57 +01:00
if (!this.game.is_ended) return;
this.o = new Othello(this.game.settings.map, {
isLlotheo: this.game.settings.is_llotheo
});
2018-03-07 13:57:06 +01:00
this.logs.forEach((log, i) => {
if (i < v) {
2018-03-08 09:57:57 +01:00
this.o.put(log.color, log.pos);
2018-03-07 13:57:06 +01:00
}
});
this.$forceUpdate();
}
},
2018-03-07 09:48:32 +01:00
created() {
2018-03-08 09:57:57 +01:00
this.o = new Othello(this.game.settings.map, {
isLlotheo: this.game.settings.is_llotheo
});
2018-03-07 09:48:32 +01:00
this.game.logs.forEach(log => {
2018-03-08 09:57:57 +01:00
this.o.put(log.color, log.pos);
2018-03-07 09:48:32 +01:00
});
2018-03-07 03:40:40 +01:00
2018-03-07 13:57:06 +01:00
this.logs = this.game.logs;
this.logPos = this.logs.length;
2018-03-07 09:48:32 +01:00
},
2018-03-07 13:57:06 +01:00
2018-03-07 09:48:32 +01:00
mounted() {
2018-03-07 03:40:40 +01:00
this.connection.on('set', this.onSet);
},
2018-03-07 13:57:06 +01:00
2018-03-07 03:40:40 +01:00
beforeDestroy() {
this.connection.off('set', this.onSet);
},
2018-03-07 13:57:06 +01:00
2018-03-07 09:48:32 +01:00
methods: {
set(pos) {
2018-03-08 10:30:28 +01:00
if (this.game.is_ended) return;
if (!this.iAmPlayer) return;
2018-03-07 10:45:16 +01:00
if (!this.isMyTurn) return;
2018-03-08 09:57:57 +01:00
if (!this.o.canPut(this.myColor, pos)) return;
this.o.put(this.myColor, pos);
2018-03-07 09:48:32 +01:00
this.connection.send({
type: 'set',
pos
});
2018-03-08 09:57:57 +01:00
this.checkEnd();
2018-03-07 09:48:32 +01:00
this.$forceUpdate();
},
2018-03-07 13:57:06 +01:00
2018-03-07 09:48:32 +01:00
onSet(x) {
2018-03-07 13:57:06 +01:00
this.logs.push(x);
this.logPos++;
2018-03-08 09:57:57 +01:00
this.o.put(x.color, x.pos);
this.checkEnd();
this.$forceUpdate();
},
2018-03-07 10:45:16 +01:00
2018-03-08 09:57:57 +01:00
checkEnd() {
this.game.is_ended = this.o.isEnded;
if (this.game.is_ended) {
if (this.o.winner == 'black') {
this.game.winner_id = this.game.black == 1 ? this.game.user1_id : this.game.user2_id;
this.game.winner = this.game.black == 1 ? this.game.user1 : this.game.user2;
} else if (this.o.winner == 'white') {
this.game.winner_id = this.game.black == 1 ? this.game.user2_id : this.game.user1_id;
this.game.winner = this.game.black == 1 ? this.game.user2 : this.game.user1;
} else {
this.game.winner_id = null;
this.game.winner = null;
2018-03-07 10:45:16 +01:00
}
2018-03-07 09:48:32 +01:00
}
}
}
2018-03-07 03:40:40 +01:00
});
</script>
2018-03-07 09:48:32 +01:00
<style lang="stylus" scoped>
@import '~const.styl'
.root
text-align center
> header
padding 8px
border-bottom dashed 1px #c4cdd4
2018-03-07 13:57:06 +01:00
> .board
2018-03-07 09:48:32 +01:00
display grid
grid-gap 4px
width 300px
height 300px
margin 0 auto
> div
background transparent
border-radius 6px
overflow hidden
*
pointer-events none
user-select none
&.empty
border solid 2px #f5f5f5
&.empty.can
background #f5f5f5
&.empty.myTurn
border-color #eee
&.can
background #eee
cursor pointer
&:hover
border-color darken($theme-color, 10%)
background $theme-color
&:active
background darken($theme-color, 10%)
2018-03-07 14:25:26 +01:00
&.prev
box-shadow 0 0 0 4px rgba($theme-color, 0.7)
2018-03-08 09:57:57 +01:00
&.none
border-color transparent !important
2018-03-07 09:48:32 +01:00
> img
display block
width 100%
height 100%
2018-03-07 13:57:06 +01:00
> .graph
display grid
grid-template-columns repeat(61, 1fr)
width 300px
height 38px
margin 0 auto 16px auto
> div
&:not(:empty)
background #ccc
> div:first-child
background #333
> div:last-child
background #ccc
> .player
margin-bottom 16px
> span
display inline-block
margin 0 8px
min-width 70px
2018-03-07 09:48:32 +01:00
</style>