<script>
import { getSquareId, getSquareFromId } from "@/utils/squareId";
import { ArrayFun } from "@/utils/array";
+import { store } from "@/store";
export default {
name: 'my-board',
props: ["vr","lastMove","analyze","orientation","userColor","vname"],
data: function () {
return {
- hints: (!localStorage["hints"] ? true : localStorage["hints"] === "1"),
- bcolor: localStorage["bcolor"] || "lichess", //lichess, chesscom or chesstempo
possibleMoves: [], //filled after each valid click/dragstart
choices: [], //promotion pieces, or checkered captures... (as moves)
selectedPiece: null, //moving piece (or clicked piece)
incheck: [],
start: {}, //pixels coordinates + id of starting square (click or drag)
+ settings: store.state.settings,
};
},
render(h) {
);
// Create board element (+ reserves if needed by variant or mode)
const lm = this.lastMove;
- const showLight = this.hints && this.vname != "Dark";
+ const showLight = this.settings.highlight && this.vname != "Dark";
const gameDiv = h(
'div',
{
)
);
}
- if (this.hints && hintSquares[ci][cj])
+ if (this.settings.hints && hintSquares[ci][cj])
{
elems.push(
h(
['board'+sizeY]: true,
'light-square': (i+j)%2==0,
'dark-square': (i+j)%2==1,
- [this.bcolor]: true,
+ [this.settings.bcolor]: true,
'in-shadow': this.vname=="Dark" && !this.analyze
&& (!this.userColor
|| !this.vr.enlightened[this.userColor][ci][cj]),
updateSettings: function(event) {
const propName =
event.target.id.substr(3).replace(/^\w/, c => c.toLowerCase())
- localStorage[propName] = ["bcolor","sound"].includes(propName)
+ let value = (["bcolor","sound"].includes(propName)
? event.target.value
- : event.target.checked;
+ : event.target.checked);
+ if (propName == "sound")
+ value = parseInt(value);
+ store.updateSetting(propName, value);
},
},
};
"&page=" + encodeURIComponent(page));
// Settings initialized with values from localStorage
this.state.settings = {
- bcolor: localStorage["bcolor"] || "lichess",
- sound: parseInt(localStorage["sound"]) || 2,
- hints: parseInt(localStorage["hints"]) || 1,
- coords: !!eval(localStorage["coords"]),
- highlight: !!eval(localStorage["highlight"]),
- sqSize: parseInt(localStorage["sqSize"]),
+ bcolor: localStorage.getItem("bcolor") || "lichess",
+ sound: parseInt(localStorage.getItem("sound")) || 1,
+ hints: localStorage.getItem("hints") == "true",
+ highlight: localStorage.getItem("highlight") == "true",
};
this.socketCloseListener = () => {
// Next line may fail at first, but should retry and eventually success (TODO?)
: "en");
this.setTranslations();
},
+ updateSetting: function(propName, value) {
+ this.state.settings[propName] = value;
+ localStorage.setItem(propName, value);
+ },
setTranslations: async function() {
// Import translations from "./translations/$lang.js"
const tModule = await import("@/translations/" + this.state.lang + ".js");
"Database error:": "Database error:",
"Download PGN": "Download PGN",
"Draw": "Draw",
+ "Draw offer only in your turn": "Draw offer only in your turn",
"Email": "Email",
"Email sent!": "Email sent!",
"Empty message": "Empty message",
gid: gameId,
newObj:
{
+ // Some fields may be undefined:
chat: obj.chat,
- move: obj.move, //may be undefined...
+ move: obj.move,
fen: obj.fen,
score: obj.score,
scoreMsg: obj.scoreMsg,
}
else if (this.drawOffer == "") //no effect if drawOffer == "sent"
{
+ if (this.game.mycolor != this.vr.turn)
+ return alert(this.st.tr["Draw offer only in your turn"]);
if (!confirm(this.st.tr["Offer draw?"]))
return;
this.drawOffer = "sent";
if (sid != this.st.user.sid)
this.st.conn.send(JSON.stringify({code:"drawoffer", target:sid}));
});
- GameStorage.update(this.gameRef.id, {drawOffer: true});
+ GameStorage.update(this.gameRef.id, {drawOffer: this.game.mycolor});
}
},
abortGame: function() {
}
if (L >= 1)
game.initime[L%2] = game.moves[L-1].played;
- if (game.drawOffer)
- this.drawOffer = "received";
}
// Now that we used idx and played, re-format moves as for live games
game.moves = game.moves.map( (m) => {
}
}
}
+
+
+
+ // TODO: (and also when receiving / sending a move ?)
+// if (!!game.drawOffer)
+// {
+// if (game.drawOffer == "w")
+// {
+// if (myIdx == 0)
+// {
+// this.drawOffer = "sent";
+
+
+
+
+
this.game = Object.assign({},
game,
// NOTE: assign mycolor here, since BaseGame could also be VS computer
scoreMsg varchar,
timeControl varchar,
created datetime, --used only for DB cleaning
- drawOffer boolean,
+ drawOffer character,
foreign key (vid) references Variants(id)
);
* score: varchar (result)
* scoreMsg: varchar ("Time", "Mutual agreement"...)
* created: datetime
- * drawOffer: boolean
+ * drawOffer: char ('w','b' or '' for none)
*
* Structure table Players:
* gid: ref game id
"INSERT INTO Games"
+ " (vid, fenStart, fen, score, timeControl, created, drawOffer)"
+ " VALUES (" + vid + ",'" + fen + "','" + fen + "','*','"
- + timeControl + "'," + Date.now() + "," + false + ")";
+ + timeControl + "'," + Date.now() + ",'')";
db.run(query, function(err) {
if (!!err)
return cb(err);
let modifs = "";
if (!!obj.message)
modifs += "message = message || ' ' || '" + obj.message + "',";
- if ([true,false].includes(obj.drawOffer))
+ // NOTE: if drawOffer is true, we should check that it's player's turn
+ // A bit overcomplicated. Let's trust the client on that for now...
+ if (!!obj.drawOffer)
modifs += "drawOffer = " + obj.drawOffer + ",";
if (!!obj.fen)
modifs += "fen = '" + obj.fen + "',";
GameModel.update(gid, obj, (err) => {
if (!!err)
return res.json(err);
- // Notify opponent if he enabled notifications:
- GameModel.getPlayers(gid, (err2,players) => {
- if (!!err2)
- return res.json(err);
- const oppid = (players[0].id == req.userId ? players[1].id : players[0].id);
- UserModel.tryNotify(oppid,
- "New move in game: " + params.siteURL + "/game/" + gid);
- });
+ if (!!obj.move || !!obj.score)
+ {
+ // Notify opponent if he enabled notifications:
+ GameModel.getPlayers(gid, (err2,players) => {
+ if (!err2)
+ {
+ const oppid = (players[0].id == req.userId
+ ? players[1].id
+ : players[0].id);
+ const messagePrefix = (!!obj.move
+ ? "New move in game: "
+ : "Game ended: ");
+ UserModel.tryNotify(oppid,
+ messagePrefix + params.siteURL + "/game/" + gid);
+ }
+ });
+ }
res.json({});
});
});