<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")
div Names: {{ game.players[0].name }} - {{ game.players[1].name }}
div(v-if="game.score=='*'") 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
textarea(v-if="game.score=='*'" v-model="corrMsg")
Chat(:players="game.players")
</template>
-<!--
-// ==> après, implémenter/vérifier les passages de challenges + parties en cours
-// observer,
--->
-
<script>
import BaseGame from "@/components/BaseGame.vue";
import Chat from "@/components/Chat.vue";
// Always add myself to players' list
const my = this.st.user;
this.people.push({sid:my.sid, id:my.id, name:my.name});
- if (!!this.$route.params["id"])
- {
- this.gameRef.id = this.$route.params["id"];
- this.gameRef.rid = this.$route.query["rid"];
- this.loadGame();
- }
- // TODO: mode analyse (/analyze/Atomic/rn
- // ... fen = query[], vname=params[] ...
+ this.gameRef.id = this.$route.params["id"];
+ this.gameRef.rid = this.$route.query["rid"]; //may be undefined
+ if (!this.gameRef.rid)
+ this.loadGame(); //local or corr: can load from now
// 0.1] Ask server for room composition:
- const funcPollClients = () => {
+ const initialize = () => {
+ // Poll clients + load game if stored remotely
this.st.conn.send(JSON.stringify({code:"pollclients"}));
+ if (!!this.gameRef.rid)
+ this.loadGame();
};
if (!!this.st.conn && this.st.conn.readyState == 1) //1 == OPEN state
- funcPollClients();
+ initialize();
else //socket not ready yet (initial loading)
- this.st.conn.onopen = funcPollClients;
+ this.st.conn.onopen = initialize;
this.st.conn.onmessage = this.socketMessageListener;
const socketCloseListener = () => {
store.socketCloseListener(); //reinitialize connexion (in store.js)
case "identity":
{
let player = this.people.find(p => p.sid == data.user.sid);
+ // NOTE: sometimes player.id fails because player is undefined...
+ // Probably because the event was meant for Hall?
+ if (!player)
+ return;
player.id = data.user.id;
player.name = data.user.name;
// Sending last state only for live games: corr games are complete
{
// Minimal game informations:
id: this.game.id,
- players: this.game.players.map(p => p.name),
+ players: this.game.players.map(p => { return {name:p.name}; }),
vid: this.game.vid,
timeControl: this.game.timeControl,
};
this.game.mycolor=="w" ? "1-0" : "0-1", "Resign");
break;
case "abort":
- this.$refs["basegame"].endGame("?", "Abort: " + data.msg);
+ this.$refs["basegame"].endGame("?", "Abort");
break;
case "draw":
this.$refs["basegame"].endGame("1/2", "Mutual agreement");
this.drawOffer = "received";
break;
case "askfullgame":
- // TODO: just give game; observers are listed here anyway:
- // ==> mark request SID as someone to send moves to
- // NOT to all people array: our opponent can send moves too!
+ // TODO: use data.id to retrieve game in indexedDB (but for now only one running game so OK)
+ this.st.conn.send(JSON.stringify({code:"fullgame", game:this.game, target:data.from}));
break;
case "fullgame":
- // and when receiving answer just call loadGame(received_game)
this.loadGame(data.game);
break;
// TODO: drawaccepted (click draw button before sending move
// TODO: ignore if preventDrawOffer is set; otherwise show modal box with option "prevent future offers"
// if accept: send message "draw"
},
- 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);
- const oppsid = this.getOppSid();
- if (!!oppsid)
+ abortGame: function() {
+ if (!confirm(this.st.tr["Terminate game?"]))
+ return;
+ // Next line will trigger a "gameover" event, bubbling up till here
+ this.$refs["basegame"].endGame("?", "Abort");
+ this.people.forEach(p => {
+ if (p.sid != this.st.user.sid)
{
this.st.conn.send(JSON.stringify({
code: "abort",
- msg: message,
- target: oppsid,
+ target: p.sid,
}));
}
- }
+ });
},
resign: function(e) {
if (!confirm("Resign the game?"))
[ game.players[1], game.players[0] ];
}
// corr game: needs to compute the clocks + initime
+ // NOTE: clocks in seconds, initime in milliseconds
game.clocks = [tc.mainTime, tc.mainTime];
game.initime = [0, 0];
const L = game.moves.length;
for (let i=2; i<L; i++)
{
addTime[i%2] += tc.increment -
- (game.moves[i].played - game.moves[i-1].played);
+ (game.moves[i].played - game.moves[i-1].played) / 1000;
}
for (let i=0; i<=1; i++)
game.clocks[i] += addTime[i];
);
};
if (!!game)
- return afterRetrival(game);
+ return afterRetrieval(game);
if (!!this.gameRef.rid)
{
// Remote live game
+ // (TODO: send game ID as well, and receiver would pick the corresponding
+ // game in his current games; if allowed to play several)
this.st.conn.send(JSON.stringify(
{code:"askfullgame", target:this.gameRef.rid}));
// (send moves updates + resign/abort/draw actions)
obj[key] = move[key];
return obj;
}, {});
- // Send move ("newmove" event) to opponent(s) (if ours)
+ // Send move ("newmove" event) to people in the room (if our turn)
let addTime = 0;
if (move.color == this.game.mycolor)
{
if (this.game.type == "corr")
sendMove.message = this.corrMsg;
const oppsid = this.getOppSid();
- if (!!oppsid)
- {
- this.st.conn.send(JSON.stringify({
- code: "newmove",
- target: oppsid,
- move: sendMove,
- }));
- }
+ this.people.forEach(p => {
+ if (p.sid != this.st.user.sid)
+ {
+ this.st.conn.send(JSON.stringify({
+ code: "newmove",
+ target: p.sid,
+ move: sendMove,
+ }));
+ }
+ });
if (this.game.type == "corr" && this.corrMsg != "")
{
// Add message to last move in BaseGame:
gameOver: function(score) {
this.game.mode = "analyze";
this.game.score = score;
- GameStorage.update(this.gameRef.id, { score: score });
+ const myIdx = this.game.players.findIndex(p => {
+ return p.sid == this.st.user.sid || p.uid == this.st.user.id;
+ });
+ if (myIdx >= 0) //OK, I play in this game
+ GameStorage.update(this.gameRef.id, { score: score });
},
},
};