iceshrimp-legacy/src/web/app/common/views/components/othello.game.vue

254 lines
6.8 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">
<header><b>{{ game.black_user.name }}</b>() vs <b>{{ game.white_user.name }}</b>()</header>
2018-03-07 10:45:16 +01:00
<p class="turn" v-if="!iAmPlayer && !isEnded">{{ turn.name }}のターンです<mk-ellipsis/></p>
2018-03-07 13:57:06 +01:00
<p class="turn" v-if="logPos != logs.length">{{ turn.name }}のターン</p>
2018-03-07 10:45:16 +01:00
<p class="turn" v-if="iAmPlayer && !isEnded">{{ isMyTurn ? 'あなたのターンです' : '相手のターンです' }}<mk-ellipsis v-if="!isMyTurn"/></p>
2018-03-07 13:57:06 +01:00
<p class="result" v-if="isEnded && logPos == logs.length">
2018-03-07 10:45:16 +01:00
<template v-if="winner"><b>{{ winner.name }}</b>の勝ち</template>
<template v-else>引き分け</template>
</p>
2018-03-07 13:57:06 +01:00
<div class="board">
2018-03-07 09:48:32 +01:00
<div v-for="(stone, i) in o.board"
2018-03-07 14:25:26 +01:00
:class="{ empty: stone == null, myTurn: isMyTurn, can: o.canReverse(turn.id == game.black_user.id ? 'black' : 'white', i), prev: o.prevPos == i }"
2018-03-07 09:48:32 +01:00
@click="set(i)"
>
<img v-if="stone == 'black'" :src="`${game.black_user.avatar_url}?thumbnail&size=64`" alt="">
<img v-if="stone == 'white'" :src="`${game.white_user.avatar_url}?thumbnail&size=64`" alt="">
</div>
</div>
2018-03-07 13:57:06 +01:00
<p>:{{ o.blackCount }} :{{ o.whiteCount }} 合計:{{ o.blackCount + o.whiteCount }}</p>
<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>
<div class="player" v-if="isEnded">
<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-07 09:48:32 +01:00
import { OthelloGameStream } from '../../scripts/streaming/othello-game';
import Othello from '../../../../../common/othello';
2018-03-07 03:40:40 +01:00
export default Vue.extend({
props: ['game'],
2018-03-07 13:57:06 +01:00
2018-03-07 03:40:40 +01:00
data() {
return {
2018-03-07 09:48:32 +01:00
o: new Othello(),
2018-03-07 13:57:06 +01:00
logs: [],
logPos: 0,
2018-03-07 09:48:32 +01:00
turn: null,
2018-03-07 10:45:16 +01:00
isMyTurn: null,
isEnded: false,
winner: null,
2018-03-07 09:48:32 +01:00
connection: null
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 {
return this.game.black_user_id == (this as any).os.i.id || this.game.white_user_id == (this as any).os.i.id;
},
2018-03-07 09:48:32 +01:00
myColor(): string {
return this.game.black_user_id == (this as any).os.i.id ? 'black' : 'white';
},
opColor(): string {
return this.myColor == 'black' ? 'white' : 'black';
}
},
2018-03-07 13:57:06 +01:00
watch: {
logPos(v) {
if (!this.isEnded) return;
this.o = new Othello();
this.turn = this.game.black_user;
this.logs.forEach((log, i) => {
if (i < v) {
this.o.set(log.color, log.pos);
if (log.color == 'black' && this.o.getPattern('white').length > 0) {
this.turn = this.game.white_user;
}
if (log.color == 'black' && this.o.getPattern('white').length == 0) {
this.turn = this.game.black_user;
}
if (log.color == 'white' && this.o.getPattern('black').length > 0) {
this.turn = this.game.black_user;
}
if (log.color == 'white' && this.o.getPattern('black').length == 0) {
this.turn = this.game.white_user;
}
}
});
this.$forceUpdate();
}
},
2018-03-07 09:48:32 +01:00
created() {
this.game.logs.forEach(log => {
this.o.set(log.color, log.pos);
});
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 10:45:16 +01:00
this.turn = this.game.turn_user_id == this.game.black_user_id ? this.game.black_user : this.game.white_user;
this.isMyTurn = this.game.turn_user_id == (this as any).os.i.id;
this.isEnded = this.game.is_ended;
this.winner = this.game.winner;
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() {
this.connection = new OthelloGameStream((this as any).os.i, this.game);
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 09:48:32 +01:00
this.connection.close();
2018-03-07 03:40:40 +01:00
},
2018-03-07 13:57:06 +01:00
2018-03-07 09:48:32 +01:00
methods: {
set(pos) {
2018-03-07 10:45:16 +01:00
if (!this.isMyTurn) return;
2018-03-07 09:48:32 +01:00
if (!this.o.canReverse(this.myColor, pos)) return;
this.o.set(this.myColor, pos);
if (this.o.getPattern(this.opColor).length > 0) {
2018-03-07 10:45:16 +01:00
this.isMyTurn = !this.isMyTurn;
this.turn = this.myColor == 'black' ? this.game.white_user : this.game.black_user;
} else if (this.o.getPattern(this.myColor).length == 0) {
this.isEnded = true;
2018-03-07 13:57:06 +01:00
this.winner = this.o.blackCount == this.o.whiteCount ? null : this.o.blackCount > this.o.whiteCount ? this.game.black_user : this.game.white_user;
2018-03-07 09:48:32 +01:00
}
this.connection.send({
type: 'set',
pos
});
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-07 09:48:32 +01:00
this.o.set(x.color, x.pos);
2018-03-07 13:57:06 +01:00
2018-03-07 10:45:16 +01:00
if (this.o.getPattern('black').length == 0 && this.o.getPattern('white').length == 0) {
this.isEnded = true;
2018-03-07 13:57:06 +01:00
this.winner = this.o.blackCount == this.o.whiteCount ? null : this.o.blackCount > this.o.whiteCount ? this.game.black_user : this.game.white_user;
2018-03-07 10:45:16 +01:00
} else {
if (this.iAmPlayer && this.o.getPattern(this.myColor).length > 0) {
this.isMyTurn = true;
}
if (x.color == 'black' && this.o.getPattern('white').length > 0) {
this.turn = this.game.white_user;
}
if (x.color == 'black' && this.o.getPattern('white').length == 0) {
this.turn = this.game.black_user;
}
if (x.color == 'white' && this.o.getPattern('black').length > 0) {
this.turn = this.game.black_user;
}
if (x.color == 'white' && this.o.getPattern('black').length == 0) {
this.turn = this.game.white_user;
}
2018-03-07 09:48:32 +01:00
}
this.$forceUpdate();
}
}
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-template-rows repeat(8, 1fr)
grid-template-columns repeat(8, 1fr)
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-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>