import { store } from "@/store";
import { GameStorage } from "@/utils/gameStorage";
import { ppt } from "@/utils/datetime";
+import { notify } from "@/utils/notifications";
import { ajax } from "@/utils/ajax";
import { extractTime } from "@/utils/timeControl";
import { getRandString } from "@/utils/alea";
gameRef: "",
nextIds: [],
game: {}, //passed to BaseGame
+ focus: false,
// virtualClocks will be initialized from true game.clocks
virtualClocks: [],
vr: null, //"variant rules" object initialized from FEN
cleanBeforeDestroy: function() {
clearInterval(this.socketCloseListener);
document.removeEventListener('visibilitychange', this.visibilityChange);
+ window.removeEventListener('focus', this.onFocus);
+ window.removeEventListener('blur', this.onBlur);
if (!!this.askLastate) clearInterval(this.askLastate);
if (!!this.retrySendmove) clearInterval(this.retrySendmove);
if (!!this.clockUpdate) clearInterval(this.clockUpdate);
},
visibilityChange: function() {
// TODO: Use document.hidden? https://webplatform.news/issues/2019-03-27
- this.send(
- document.visibilityState == "visible"
- ? "getfocus"
- : "losefocus"
- );
+ this.focus = (document.visibilityState == "visible");
+ if (!this.focus && !!this.rematchOffer) {
+ this.rematchOffer = "";
+ this.send("rematchoffer", { data: false });
+ // Do not remove rematch offer from (local) storage
+ }
+ this.send(this.focus ? "getfocus" : "losefocus");
+ },
+ onFocus: function() {
+ this.focus = true;
+ this.send("getfocus");
+ },
+ onBlur: function() {
+ this.focus = false;
+ if (!!this.rematchOffer) {
+ this.rematchOffer = "";
+ this.send("rematchoffer", { data: false });
+ }
+ this.send("losefocus");
},
participateInChat: function(p) {
return Object.keys(p.tmpIds).some(x => p.tmpIds[x].focus) && !!p.name;
},
atCreation: function() {
document.addEventListener('visibilitychange', this.visibilityChange);
+ window.addEventListener('focus', this.onFocus);
+ window.addEventListener('blur', this.onBlur);
// 0] (Re)Set variables
this.gameRef = this.$route.params["id"];
// next = next corr games IDs to navigate faster (if applicable)
} else {
this.gotMoveIdx = movePlus.index;
const receiveMyMove = (movePlus.color == this.game.mycolor);
- if (!receiveMyMove && !!this.game.mycolor)
+ const moveColIdx = ["w", "b"].indexOf(movePlus.color);
+ if (!receiveMyMove && !!this.game.mycolor) {
// Notify opponent that I got the move:
this.send("gotmove", {data: movePlus.index, target: data.from});
+ // And myself if I'm elsewhere:
+ if (!this.focus) {
+ notify(
+ "New move",
+ {
+ body:
+ (this.game.players[moveColIdx].name || "@nonymous") +
+ " just played."
+ }
+ );
+ }
+ }
if (movePlus.cancelDrawOffer) {
// Opponent refuses draw
this.drawOffer = "";
}
}
this.$refs["basegame"].play(movePlus.move, "received", null, true);
- const moveColIdx = ["w", "b"].indexOf(movePlus.color);
this.game.clocks[moveColIdx] = movePlus.clock;
this.processMove(
movePlus.move,
});
};
// The active tab can update storage immediately
- if (!document.hidden) updateStorage();
+ if (this.focus) updateStorage();
// Small random delay otherwise
else setTimeout(updateStorage, 500 + 1000 * Math.random());
}
};
if (this.game.type == "live") {
GameStorage.update(this.gameRef, scoreObj);
+ // Notify myself locally if I'm elsewhere:
+ if (!this.focus) {
+ notify(
+ "Game over",
+ { body: score + " : " + scoreMsg }
+ );
+ }
if (!!callback) callback();
}
else this.updateCorrGame(scoreObj, callback);
<script>
import { store } from "@/store";
import { checkChallenge } from "@/data/challengeCheck";
+import { notify } from "@/utils/notifications";
import { ArrayFun } from "@/utils/array";
import { ajax } from "@/utils/ajax";
import params from "@/parameters";
diag: "", //visualizing FEN
memorize: false //put settings in localStorage
},
+ focus: true,
tchallDiag: "",
curChallToAccept: {from: {}},
presetChalls: JSON.parse(localStorage.getItem("presetChalls") || "[]"),
},
created: function() {
document.addEventListener('visibilitychange', this.visibilityChange);
+ window.addEventListener('focus', this.onFocus);
+ window.addEventListener('blur', this.onBlur);
window.addEventListener("beforeunload", this.cleanBeforeDestroy);
if (this.st.variants.length > 0 && this.newchallenge.vid > 0)
this.loadNewchallVariant();
cleanBeforeDestroy: function() {
clearInterval(this.socketCloseListener);
document.removeEventListener('visibilitychange', this.visibilityChange);
+ window.removeEventListener('focus', this.onFocus);
+ window.removeEventListener('blur', this.onBlur);
window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
this.conn.removeEventListener("message", this.socketMessageListener);
this.send("disconnect");
},
visibilityChange: function() {
// TODO: Use document.hidden? https://webplatform.news/issues/2019-03-27
- this.send(
- document.visibilityState == "visible"
- ? "getfocus"
- : "losefocus"
- );
+ this.focus = (document.visibilityState == "visible");
+ this.send(this.focus ? "getfocus" : "losefocus");
+ },
+ onFocus: function() {
+ this.focus = true;
+ this.send("getfocus");
+ },
+ onBlur: function() {
+ this.focus = false;
+ this.send("losefocus");
},
partialResetNewchallenge: function() {
// Reset potential target and custom FEN:
);
setTimeout(
() => {
+ const myIdx = (game.players[0].sid == this.st.user.sid ? 0 : 1);
GameStorage.add(game, (err) => {
// If an error occurred, game is not added: a tab already
// added the game. Maybe a focused one, maybe not.
// ==> Do not play it again.
if (!err && this.st.settings.sound)
new Audio("/sounds/newgame.flac").play().catch(() => {});
+ if (!this.focus) {
+ notify(
+ "New live game",
+ { body: "vs " + game.players[1-myIdx].name || "@nonymous" }
+ );
+ }
this.$router.push("/game/" + gameInfo.id);
});
},
- document.hidden ? 500 + 1000 * Math.random() : 0
+ this.focus ? 500 + 1000 * Math.random() : 0
);
}
}