// Helper to safe-send some message through a (web-)socket:
function send(socket, message) {
- if (socket && socket.readyState == 1)
+ if (!!socket && socket.readyState == 1)
socket.send(JSON.stringify(message));
}
case "askfullgame": {
const pg = obj.page || page; //required for askidentity and askgame
// In cas askfullgame to wrong SID for example, would crash:
- if (clients[pg] && clients[pg][obj.target]) {
+ if (!!clients[pg] && !!clients[pg][obj.target]) {
const tmpIds = Object.keys(clients[pg][obj.target]);
if (obj.target == sid) {
// Targetting myself
case "newchallenge":
case "newgame":
case "deletechallenge":
- case "newmove":
case "resign":
case "abort":
case "drawoffer":
notifyRoom(page, obj.code, {data: obj.data});
break;
+ case "newmove": {
+ const dataWithFrom = {from: [sid,tmpId], data: obj.data};
+ // Special case re-send newmove only to opponent:
+ if (!!obj.target && !!clients[page][obj.target]) {
+ Object.keys(clients[page][obj.target]).forEach(x => {
+ send(
+ clients[page][obj.target][x],
+ Object.assign({code: "newmove"}, dataWithFrom)
+ );
+ });
+ } else {
+ // NOTE: data.from is useful only to opponent
+ notifyRoom(page, "newmove", dataWithFrom);
+ }
+ break;
+ }
+ case "gotmove":
+ if (
+ !!clients[page][obj.target[0]] &&
+ !!clients[page][obj.target[0]][obj.target[1]]
+ ) {
+ send(
+ clients[page][obj.target[0]][obj.target[1]],
+ {code: "gotmove", data: obj.data}
+ );
+ }
+ break;
+
case "result":
// Special case: notify all, 'transroom': Game --> Hall
notifyRoom("/", "result", {gid: obj.gid, score: obj.score});
{
const pg = obj.target[2] || page; //required for identity and game
// NOTE: if in game we ask identity to opponent still in Hall,
- // but leaving Hall, clients[pg] or clients[pg][target] could be ndefined
- if (clients[pg] && clients[pg][obj.target[0]])
+ // but leaving Hall, clients[pg] or clients[pg][target] could be undefined
+ if (!!clients[pg] && !!clients[pg][obj.target[0]])
send(clients[pg][obj.target[0]][obj.target[1]], {code:obj.code, data:obj.data});
break;
}