misskey/src/client/app/common/views/components/games/reversi/reversi.game.vue

463 lines
12 KiB
Vue
Raw Normal View History

2018-03-07 03:40:40 +01:00
<template>
2018-08-03 08:59:24 +02:00
<div class="xqnhankfuuilcwvhgsopeqncafzsquya">
2018-08-05 06:40:26 +02:00
<button class="go-index" v-if="selfNav" @click="goIndex">%fa:arrow-left%</button>
2018-08-04 15:28:01 +02:00
<header><b><router-link :to="blackUser | userPage">{{ blackUser | userName }}</router-link></b>(%i18n:common.reversi.black%) vs <b><router-link :to="whiteUser | userPage">{{ whiteUser | userName }}</router-link></b>(%i18n:common.reversi.white%)</header>
2018-03-08 09:57:57 +01:00
2018-08-05 13:57:24 +02:00
<div style="overflow: hidden; line-height: 28px;">
2018-08-03 14:11:53 +02:00
<p class="turn" v-if="!iAmPlayer && !game.isEnded">{{ '%i18n:common.reversi.turn-of%'.replace('{}', $options.filters.userName(turnUser)) }}<mk-ellipsis/></p>
<p class="turn" v-if="logPos != logs.length">{{ '%i18n:common.reversi.past-turn-of%'.replace('{}', $options.filters.userName(turnUser)) }}</p>
2018-06-23 14:56:47 +02:00
<p class="turn1" v-if="iAmPlayer && !game.isEnded && !isMyTurn">%i18n:common.reversi.opponent-turn%<mk-ellipsis/></p>
<p class="turn2" v-if="iAmPlayer && !game.isEnded && isMyTurn" v-animate-css="{ classes: 'tada', iteration: 'infinite' }">%i18n:common.reversi.my-turn%</p>
2018-03-29 07:48:47 +02:00
<p class="result" v-if="game.isEnded && logPos == logs.length">
2018-08-04 15:28:01 +02:00
<template v-if="game.winner">
<span>{{ '%i18n:common.reversi.won%'.replace('{}', $options.filters.userName(game.winner)) }}</span>
<span v-if="game.surrendered != null"> (%i18n:@surrendered%)</span>
</template>
2018-06-23 14:56:47 +02:00
<template v-else>%i18n:common.reversi.drawn%</template>
2018-03-10 05:22:20 +01:00
</p>
</div>
2018-03-08 09:57:57 +01:00
<div class="board">
<div class="labels-x" v-if="this.$store.state.settings.reversiBoardLabels">
2018-06-23 15:25:22 +02:00
<span v-for="i in game.settings.map[0].length">{{ String.fromCharCode(64 + i) }}</span>
</div>
<div class="flex">
<div class="labels-y" v-if="this.$store.state.settings.reversiBoardLabels">
<div v-for="i in game.settings.map.length">{{ i }}</div>
</div>
<div class="cells" :style="cellsStyle">
<div v-for="(stone, i) in o.board"
:class="{ empty: stone == null, none: o.map[i] == 'null', isEnded: game.isEnded, myTurn: !game.isEnded && isMyTurn, can: turnUser ? o.canPut(turnUser.id == blackUser.id, i) : null, prev: o.prevPos == i }"
@click="set(i)"
2018-06-23 15:25:22 +02:00
:title="`${String.fromCharCode(65 + o.transformPosToXy(i)[0])}${o.transformPosToXy(i)[1] + 1}`">
2018-07-24 16:43:14 +02:00
<img v-if="stone === true" :src="blackUser.avatarUrl" alt="">
<img v-if="stone === false" :src="whiteUser.avatarUrl" alt="">
</div>
</div>
2018-06-24 06:18:22 +02:00
<div class="labels-y" v-if="this.$store.state.settings.reversiBoardLabels">
<div v-for="i in game.settings.map.length">{{ i }}</div>
</div>
</div>
<div class="labels-x" v-if="this.$store.state.settings.reversiBoardLabels">
<span v-for="i in game.settings.map[0].length">{{ String.fromCharCode(64 + i) }}</span>
2018-03-07 09:48:32 +01:00
</div>
</div>
2018-03-08 09:57:57 +01:00
2018-08-02 00:36:25 +02:00
<p class="status"><b>{{ '%i18n:common.reversi.this-turn%'.split('{}')[0] }}{{ logPos }}{{ '%i18n:common.reversi.this-turn%'.split('{}')[1] }}</b> %i18n:common.reversi.black%:{{ o.blackCount }} %i18n:common.reversi.white%:{{ o.whiteCount }} %i18n:common.reversi.total%:{{ o.blackCount + o.whiteCount }}</p>
2018-03-08 09:57:57 +01:00
2018-08-04 15:28:01 +02:00
<div class="actions" v-if="!game.isEnded && iAmPlayer">
<form-button @click="surrender">%i18n:@surrender%</form-button>
</div>
2018-03-29 07:48:47 +02:00
<div class="player" v-if="game.isEnded">
2018-03-15 07:02:15 +01:00
<el-button-group>
<el-button type="primary" @click="logPos = 0" :disabled="logPos == 0">%fa:angle-double-left%</el-button>
<el-button type="primary" @click="logPos--" :disabled="logPos == 0">%fa:angle-left%</el-button>
</el-button-group>
2018-03-07 13:57:06 +01:00
<span>{{ logPos }} / {{ logs.length }}</span>
2018-03-15 07:02:15 +01:00
<el-button-group>
<el-button type="primary" @click="logPos++" :disabled="logPos == logs.length">%fa:angle-right%</el-button>
<el-button type="primary" @click="logPos = logs.length" :disabled="logPos == logs.length">%fa:angle-double-right%</el-button>
</el-button-group>
2018-03-07 13:57:06 +01:00
</div>
2018-08-12 18:25:50 +02:00
<div class="info">
<p v-if="game.settings.isLlotheo">%i18n:@is-llotheo%</p>
<p v-if="game.settings.loopedBoard">%i18n:@looped-map%</p>
<p v-if="game.settings.canPutEverywhere">%i18n:@can-put-everywhere%</p>
</div>
2018-03-07 03:40:40 +01:00
</div>
</template>
<script lang="ts">
import Vue from 'vue';
2018-03-11 09:23:59 +01:00
import * as CRC32 from 'crc-32';
2018-07-11 06:47:01 +02:00
import Reversi, { Color } from '../../../../../../../games/reversi/core';
import { url } from '../../../../../config';
2018-03-07 09:48:32 +01:00
2018-03-07 03:40:40 +01:00
export default Vue.extend({
2018-08-05 06:40:26 +02:00
props: {
initGame: {
type: Object,
require: true
},
connection: {
type: Object,
require: true
},
selfNav: {
type: Boolean,
require: true
}
},
2018-03-07 13:57:06 +01:00
2018-03-07 03:40:40 +01:00
data() {
return {
2018-03-11 09:23:59 +01:00
game: null,
2018-06-17 01:10:54 +02:00
o: null as Reversi,
2018-03-07 13:57:06 +01:00
logs: [],
2018-03-11 09:23:59 +01:00
logPos: 0,
pollingClock: 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 {
2018-05-27 06:49:09 +02:00
if (!this.$store.getters.isSignedIn) return false;
return this.game.user1Id == this.$store.state.i.id || this.game.user2Id == this.$store.state.i.id;
2018-03-07 10:45:16 +01:00
},
2018-08-04 15:28:01 +02:00
2018-03-08 09:57:57 +01:00
myColor(): Color {
if (!this.iAmPlayer) return null;
2018-05-27 06:49:09 +02:00
if (this.game.user1Id == this.$store.state.i.id && this.game.black == 1) return true;
if (this.game.user2Id == this.$store.state.i.id && this.game.black == 2) return true;
return false;
2018-03-07 09:48:32 +01:00
},
2018-08-04 15:28:01 +02:00
2018-03-08 09:57:57 +01:00
opColor(): Color {
if (!this.iAmPlayer) return null;
return this.myColor === true ? false : true;
2018-03-08 09:57:57 +01:00
},
2018-08-04 15:28:01 +02:00
2018-03-08 09:57:57 +01:00
blackUser(): any {
return this.game.black == 1 ? this.game.user1 : this.game.user2;
},
2018-08-04 15:28:01 +02:00
2018-03-08 09:57:57 +01:00
whiteUser(): any {
return this.game.black == 1 ? this.game.user2 : this.game.user1;
},
2018-08-04 15:28:01 +02:00
2018-03-08 09:57:57 +01:00
turnUser(): any {
if (this.o.turn === true) {
2018-03-08 09:57:57 +01:00
return this.game.black == 1 ? this.game.user1 : this.game.user2;
} else if (this.o.turn === false) {
2018-03-08 09:57:57 +01:00
return this.game.black == 1 ? this.game.user2 : this.game.user1;
} else {
return null;
}
},
2018-08-04 15:28:01 +02:00
2018-03-08 09:57:57 +01:00
isMyTurn(): boolean {
if (!this.iAmPlayer) return false;
if (this.turnUser == null) return false;
2018-05-27 06:49:09 +02:00
return this.turnUser.id == this.$store.state.i.id;
},
2018-08-04 15:28:01 +02:00
cellsStyle(): any {
return {
2018-07-30 13:13:30 +02:00
'grid-template-rows': `repeat(${this.game.settings.map.length}, 1fr)`,
'grid-template-columns': `repeat(${this.game.settings.map[0].length}, 1fr)`
};
2018-03-07 09:48:32 +01:00
}
},
2018-03-07 13:57:06 +01:00
watch: {
logPos(v) {
2018-03-29 07:48:47 +02:00
if (!this.game.isEnded) return;
2018-06-17 01:10:54 +02:00
this.o = new Reversi(this.game.settings.map, {
2018-03-29 07:48:47 +02:00
isLlotheo: this.game.settings.isLlotheo,
canPutEverywhere: this.game.settings.canPutEverywhere,
loopedBoard: this.game.settings.loopedBoard
2018-03-08 09:57:57 +01:00
});
2018-03-07 13:57:06 +01:00
this.logs.forEach((log, i) => {
if (i < v) {
2018-03-15 07:02:15 +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-11 09:23:59 +01:00
this.game = this.initGame;
2018-06-17 01:10:54 +02:00
this.o = new Reversi(this.game.settings.map, {
2018-03-29 07:48:47 +02:00
isLlotheo: this.game.settings.isLlotheo,
canPutEverywhere: this.game.settings.canPutEverywhere,
loopedBoard: this.game.settings.loopedBoard
2018-03-08 09:57:57 +01:00
});
2018-03-07 09:48:32 +01:00
this.game.logs.forEach(log => {
2018-03-15 07:02:15 +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-11 09:23:59 +01:00
// 通信を取りこぼしてもいいように定期的にポーリングさせる
2018-03-29 07:48:47 +02:00
if (this.game.isStarted && !this.game.isEnded) {
2018-03-11 09:23:59 +01:00
this.pollingClock = setInterval(() => {
const crc32 = CRC32.str(this.logs.map(x => x.pos.toString()).join(''));
this.connection.send({
type: 'check',
crc32
});
2018-03-11 23:53:58 +01:00
}, 3000);
2018-03-11 09:23:59 +01:00
}
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-11 09:23:59 +01:00
this.connection.on('rescue', this.onRescue);
2018-08-04 15:28:01 +02:00
this.connection.on('ended', this.onEnded);
2018-03-07 03:40:40 +01:00
},
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-11 09:23:59 +01:00
this.connection.off('rescue', this.onRescue);
2018-08-04 15:28:01 +02:00
this.connection.off('ended', this.onEnded);
2018-03-11 09:23:59 +01:00
clearInterval(this.pollingClock);
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-29 07:48:47 +02:00
if (this.game.isEnded) return;
2018-03-08 10:30:28 +01:00
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-10 17:55:46 +01:00
// サウンドを再生する
2018-05-20 19:13:39 +02:00
if (this.$store.state.device.enableSounds) {
2018-06-17 01:10:54 +02:00
const sound = new Audio(`${url}/assets/reversi-put-me.mp3`);
2018-05-20 19:13:39 +02:00
sound.volume = this.$store.state.device.soundVolume;
2018-03-10 17:55:46 +01:00
sound.play();
}
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-10 17:55:46 +01:00
// サウンドを再生する
2018-05-20 19:13:39 +02:00
if (this.$store.state.device.enableSounds && x.color != this.myColor) {
2018-06-17 01:10:54 +02:00
const sound = new Audio(`${url}/assets/reversi-put-you.mp3`);
2018-05-20 19:13:39 +02:00
sound.volume = this.$store.state.device.soundVolume;
2018-03-10 17:55:46 +01:00
sound.play();
}
2018-03-08 09:57:57 +01:00
},
2018-03-07 10:45:16 +01:00
2018-08-04 15:28:01 +02:00
onEnded(x) {
this.game = x.game;
},
2018-03-08 09:57:57 +01:00
checkEnd() {
2018-03-29 07:48:47 +02:00
this.game.isEnded = this.o.isEnded;
if (this.game.isEnded) {
if (this.o.winner === true) {
2018-03-29 07:48:47 +02:00
this.game.winnerId = this.game.black == 1 ? this.game.user1Id : this.game.user2Id;
2018-03-08 09:57:57 +01:00
this.game.winner = this.game.black == 1 ? this.game.user1 : this.game.user2;
} else if (this.o.winner === false) {
2018-03-29 07:48:47 +02:00
this.game.winnerId = this.game.black == 1 ? this.game.user2Id : this.game.user1Id;
2018-03-08 09:57:57 +01:00
this.game.winner = this.game.black == 1 ? this.game.user2 : this.game.user1;
} else {
2018-03-29 07:48:47 +02:00
this.game.winnerId = null;
2018-03-08 09:57:57 +01:00
this.game.winner = null;
2018-03-07 10:45:16 +01:00
}
2018-03-07 09:48:32 +01:00
}
2018-03-11 09:23:59 +01:00
},
// 正しいゲーム情報が送られてきたとき
onRescue(game) {
this.game = game;
2018-06-17 01:10:54 +02:00
this.o = new Reversi(this.game.settings.map, {
2018-03-29 07:48:47 +02:00
isLlotheo: this.game.settings.isLlotheo,
canPutEverywhere: this.game.settings.canPutEverywhere,
loopedBoard: this.game.settings.loopedBoard
2018-03-11 09:23:59 +01:00
});
this.game.logs.forEach(log => {
this.o.put(log.color, log.pos, true);
2018-03-11 09:23:59 +01:00
});
this.logs = this.game.logs;
this.logPos = this.logs.length;
this.checkEnd();
this.$forceUpdate();
2018-08-04 15:28:01 +02:00
},
surrender() {
(this as any).api('games/reversi/games/surrender', {
gameId: this.game.id
});
2018-08-05 06:40:26 +02:00
},
goIndex() {
this.$emit('go-index');
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'
2018-08-03 08:59:24 +02:00
root(isDark)
2018-03-07 09:48:32 +01:00
text-align center
2018-08-05 06:40:26 +02:00
> .go-index
position absolute
top 0
left 0
z-index 1
width 42px
height 42px
2018-03-07 09:48:32 +01:00
> header
padding 8px
2018-08-03 08:59:24 +02:00
border-bottom dashed 1px isDark ? #4c5761 : #c4cdd4
2018-03-07 09:48:32 +01:00
2018-08-04 15:28:01 +02:00
a
color inherit
2018-03-07 13:57:06 +01:00
> .board
2018-06-26 11:53:06 +02:00
width calc(100% - 16px)
max-width 500px
2018-03-07 09:48:32 +01:00
margin 0 auto
2018-06-24 06:18:22 +02:00
$label-size = 16px
$gap = 4px
> .labels-x
height $label-size
2018-06-24 06:18:22 +02:00
padding 0 $label-size
display flex
> *
flex 1
display flex
align-items center
justify-content center
2018-06-24 06:18:22 +02:00
font-size 12px
&:first-child
margin-left -($gap / 2)
&:last-child
margin-right -($gap / 2)
> .flex
display flex
> .labels-y
width $label-size
display flex
flex-direction column
> *
flex 1
display flex
align-items center
justify-content center
2018-06-24 06:18:22 +02:00
font-size 12px
&:first-child
margin-top -($gap / 2)
&:last-child
margin-bottom -($gap / 2)
> .cells
flex 1
display grid
grid-gap $gap
> div
background transparent
border-radius 6px
overflow hidden
2018-03-07 09:48:32 +01:00
*
pointer-events none
user-select none
2018-03-07 09:48:32 +01:00
&.empty
2018-08-03 08:59:24 +02:00
border solid 2px isDark ? #51595f : #eee
2018-03-07 09:48:32 +01:00
&.empty.can
2018-08-03 08:59:24 +02:00
background isDark ? #51595f : #eee
2018-03-07 09:48:32 +01:00
&.empty.myTurn
2018-08-03 08:59:24 +02:00
border-color isDark ? #6a767f : #ddd
2018-03-07 09:48:32 +01:00
&.can
2018-08-03 08:59:24 +02:00
background isDark ? #51595f : #eee
cursor pointer
2018-03-07 09:48:32 +01:00
&:hover
border-color darken($theme-color, 10%)
background $theme-color
2018-03-07 09:48:32 +01:00
&:active
background darken($theme-color, 10%)
2018-03-07 09:48:32 +01:00
&.prev
box-shadow 0 0 0 4px rgba($theme-color, 0.7)
2018-03-07 14:25:26 +01:00
&.isEnded
2018-08-03 08:59:24 +02:00
border-color isDark ? #6a767f : #ddd
2018-03-08 14:11:08 +01:00
&.none
border-color transparent !important
2018-03-08 09:57:57 +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
2018-03-16 15:38:25 +01:00
> .status
margin 0
padding 16px 0
2018-08-04 15:28:01 +02:00
> .actions
padding-bottom 16px
2018-03-07 13:57:06 +01:00
> .player
2018-03-15 07:02:15 +01:00
padding-bottom 32px
2018-03-07 13:57:06 +01:00
> span
display inline-block
margin 0 8px
min-width 70px
2018-08-03 08:59:24 +02:00
.xqnhankfuuilcwvhgsopeqncafzsquya[data-darkmode]
root(true)
.xqnhankfuuilcwvhgsopeqncafzsquya:not([data-darkmode])
root(false)
2018-03-07 09:48:32 +01:00
</style>