- const tmpId_idx = Math.floor(Math.random() * tmpIds.length);
- send(
- clients[pg][obj.target][tmpIds[tmpId_idx]].socket,
- { code: obj.code, from: [sid,tmpId,page] }
- );
+ if (tmpIds.length > 0) {
+ const ttmpId = tmpIds[Math.floor(Math.random() * tmpIds.length)];
+ send(
+ clients[pg][obj.target][ttmpId].socket,
+ { code: obj.code, from: [sid,tmpId,page] }
+ );
+ }
+ }
+ break;
+ }
+
+ // Special situation of the previous "case":
+ // Full game can be asked to any observer.
+ case "askfullgame": {
+ if (!!clients[page]) {
+ let sids = Object.keys(clients[page]).filter(k => k != sid);
+ if (sids.length > 0) {
+ // Pick a SID at random in this set, and ask full game:
+ const rid = sids[Math.floor(Math.random() * sids.length)];
+ // ..to a random tmpId:
+ const tmpIds = Object.keys(clients[page][rid]);
+ const rtmpId = tmpIds[Math.floor(Math.random() * tmpIds.length)];
+ send(
+ clients[page][rid][rtmpId].socket,
+ { code: "askfullgame", from: [sid,tmpId] }
+ );
+ }