<template lang="pug">
.row
.col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
+ input#modalAbort.modal(type="checkbox")
+ div(role="dialog" aria-labelledby="abortBoxTitle")
+ .card.smallpad.small-modal.text-center
+ label.modal-close(for="modalAbort")
+ h3#abortBoxTitle.section {{ st.tr["Terminate game?"] }}
+ button(@click="abortGame") {{ st.tr["Sorry I have to go"] }}
+ button(@click="abortGame") {{ st.tr["Game seems over"] }}
+ button(@click="abortGame") {{ st.tr["Game is too boring"] }}
BaseGame(:game="game" :vr="vr" ref="basegame"
@newmove="processMove" @gameover="gameOver")
- .button-group(v-if="game.mode!='analyze'")
+ // TODO: show players names + clocks state
+ div Time: {{ virtualClocks[0] }} - {{ virtualClocks[1] }}
+ .button-group(v-if="game.mode!='analyze' && game.score=='*'")
button(@click="offerDraw") Draw
- button(@click="abortGame") Abort
+ button(@click="() => abortGame()") Abort
button(@click="resign") Resign
div(v-if="game.mode=='corr'")
textarea(v-show="score=='*' && vr.turn==game.mycolor" v-model="corrMsg")
div(v-show="cursor>=0") {{ moves[cursor].message }}
</template>
+<!--
+// TODO: movelist dans basegame et chat ici
+// se limiter à 2 joueurs pour l'instant au moins tout en restant général
+// ==> après, implémenter/vérifier les passages de challenges + parties en cours
+// observer,
+// + problèmes, habiller et publier. (+ corr...)
+
+// refactor players.forEach(...) into sendTo(opponent, ...)
+-->
+
<script>
import BaseGame from "@/components/BaseGame.vue";
//import Chat from "@/components/Chat.vue";
//import MoveList from "@/components/MoveList.vue";
import { store } from "@/store";
-import { GameStorage } from "@/utils/storage";
+import { GameStorage } from "@/utils/gameStorage";
+import { ppt } from "@/utils/datetime";
export default {
name: 'my-game',
rid: ""
},
game: { }, //passed to BaseGame
+ virtualClocks: [0, 0], //initialized with true game.clocks
vr: null, //"variant rules" object initialized from FEN
drawOfferSent: false, //did I just ask for draw? (TODO: use for button style)
people: [ ], //potential observers (TODO)
this.loadGame();
}
},
+ "game.clocks": function(newState) {
+ this.virtualClocks = newState.map(s => ppt(s));
+ const currentTurn = this.vr.turn;
+ const colorIdx = ["w","b","g","r"].indexOf(currentTurn);
+ let countdown = newState[colorIdx] -
+ (Date.now() - this.game.initime[colorIdx])/1000;
+ const myTurn = (currentTurn == this.game.mycolor);
+ let clockUpdate = setInterval(() => {
+ if (countdown <= 0 || this.vr.turn != currentTurn)
+ {
+ clearInterval(clockUpdate);
+ if (countdown <= 0 && myTurn)
+ {
+ this.$refs["basegame"].endGame(
+ this.game.mycolor=="w" ? "0-1" : "1-0", "Time");
+ this.game.players.forEach(p => {
+ if (p.sid != this.st.user.sid)
+ {
+ this.st.conn.send(JSON.stringify({
+ code: "timeover",
+ target: p.sid,
+ }));
+ }
+ });
+ }
+ }
+ else
+ {
+ // TODO: with Vue 3, just do this.virtualClocks[colorIdx] = ppt(--countdown)
+ this.$set(this.virtualClocks, colorIdx, ppt(Math.max(0, --countdown)));
+ }
+ }, 1000);
+ },
},
created: function() {
if (!!this.$route.params["id"])
switch (data.code)
{
case "newmove":
- // TODO: observer on dark games must see all board ? Or alternate ? (seems better)
- // ...or just see nothing as on buho21
// NOTE: next call will trigger processMove()
this.$refs["basegame"].play(data.move,
"receive", this.game.vname!="Dark" ? "animate" : null);
// TODO: ignore if preventDrawOffer is set; otherwise show modal box with option "prevent future offers"
// if accept: send message "draw"
},
- abortGame: function() {
- if (!confirm("Abort the game?"))
- return;
-
-// Abort possible à tout moment avec message
-// Sorry I have to go / Game seems over / Game is not interesting
-
- //+ bouton "abort" avec score == "?" + demander confirmation pour toutes ces actions,
- //send message: "gameOver" avec score "?"
- // ==> BaseGame must listen to game.score change, and call "endgame(score)" in this case
+ abortGame: function(event) {
+ let modalBox = document.getElementById("modalAbort");
+ if (!event)
+ {
+ // First call show options:
+ modalBox.checked = true;
+ }
+ else
+ {
+ modalBox.checked = false; //decision made: box disappear
+ const message = event.target.innerText;
+ // Next line will trigger a "gameover" event, bubbling up till here
+ this.$refs["basegame"].endGame("?", "Abort: " + message);
+ this.game.players.forEach(p => {
+ if (!!p.sid && p.sid != this.st.user.sid)
+ {
+ this.st.conn.send(JSON.stringify({
+ code: "abort",
+ msg: message,
+ target: p.sid,
+ }));
+ }
+ });
+ }
},
resign: function(e) {
if (!confirm("Resign the game?"))
}
});
// Next line will trigger a "gameover" event, bubbling up till here
- this.$refs["basegame"].endGame(this.game.mycolor=="w" ? "0-1" : "1-0");
+ this.$refs["basegame"].endGame(
+ this.game.mycolor=="w" ? "0-1" : "1-0", "Resign");
},
- // 4 cases for loading a game:
- // - from localStorage (one running game I play)
- // - from indexedDB (one completed live game)
+ // 3 cases for loading a game:
+ // - from indexedDB (running or completed live game I play)
// - from server (one correspondance game I play[ed] or not)
// - from remote peer (one live game I don't play, finished or not)
- loadGame: function() {
- GameStorage.get(this.gameRef, async (game) => {
+ loadGame: function(game) {
+ const afterRetrieval = async (game) => {
+ const vModule = await import("@/variants/" + game.vname + ".js");
+ window.V = vModule.VariantRules;
+ this.vr = new V(game.fen);
this.game = Object.assign({},
game,
// NOTE: assign mycolor here, since BaseGame could also bs VS computer
{mycolor: [undefined,"w","b"][1 + game.players.findIndex(
p => p.sid == this.st.user.sid)]},
);
- const vModule = await import("@/variants/" + game.vname + ".js");
- window.V = vModule.VariantRules;
- this.vr = new V(game.fen);
- // Post-processing: decorate each move with current FEN:
- // (to be able to jump to any position quickly)
- game.moves.forEach(move => {
- // NOTE: this is doing manually what BaseGame.play() achieve...
- // but in a lighter "fast-forward" way
- move.color = this.vr.turn;
- this.vr.play(move);
- move.fen = this.vr.getFen();
+ };
+ if (!!game)
+ return afterRetrival(game);
+ if (!!this.gameRef.rid)
+ {
+ // TODO: just send a game request message to the remote player,
+ // and when receiving answer just call loadGame(received_game)
+ // + remote peer should have registered us as an observer
+ // (send moves updates + resign/abort/draw actions)
+ return;
+ }
+ else
+ {
+ GameStorage.get(this.gameRef.id, async (game) => {
+ afterRetrieval(game);
});
- this.vr.re_init(game.fen);
- });
+ }
// // Poll all players except me (if I'm playing) to know online status.
// // --> Send ping to server (answer pong if players[s] are connected)
// if (this.gameInfo.players.some(p => p.sid == this.st.user.sid))
return obj;
}, {});
// Send move ("newmove" event) to opponent(s) (if ours)
- // (otherwise move.elapsed is supposed to be already transmitted)
let addTime = undefined;
if (move.color == this.game.mycolor)
{
- const elapsed = Date.now() - GameStorage.getInitime();
+ const elapsed = Date.now() - this.game.initime[colorIdx];
+ // elapsed time is measured in milliseconds
+ addTime = this.game.increment - elapsed/1000;
this.game.players.forEach(p => {
if (p.sid != this.st.user.sid)
{
this.st.conn.send(JSON.stringify({
code: "newmove",
target: p.sid,
- move: Object.assign({}, filtered_move, {elapsed: elapsed}),
+ move: Object.assign({}, filtered_move, {addTime: addTime}),
}));
}
});
- move.elapsed = elapsed;
- // elapsed time is measured in milliseconds
- addTime = this.game.increment - elapsed/1000;
}
- GameStorage.update({
+ else
+ addTime = move.addTime; //supposed transmitted
+ const nextIdx = ["w","b","g","r"].indexOf(this.vr.turn);
+ GameStorage.update(this.gameRef.id,
+ {
colorIdx: colorIdx,
+ nextIdx: nextIdx,
move: filtered_move,
fen: move.fen,
addTime: addTime,
- initime: (this.vr.turn == this.game.mycolor), //my turn now?
});
+ // Also update current game object:
+ this.game.moves.push(move);
+ this.game.fen = move.fen;
+ //TODO: just this.game.clocks[colorIdx] += (!!addTime ? addTime : 0);
+ this.$set(this.game.clocks, colorIdx,
+ this.game.clocks[colorIdx] + (!!addTime ? addTime : 0));
+ this.game.initime[nextIdx] = Date.now();
},
// TODO: this update function should also work for corr games
gameOver: function(score) {
this.game.mode = "analyze";
- GameStorage.update({
+ GameStorage.update(this.gameRef.id,
+ {
score: score,
});
},