From fac8af2866827e2b2b27d8efbab8e24b06739e56 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 8 Mar 2022 18:56:06 +0100 Subject: Allow "query" calls as Observer and during replays. --- public/common/play.js | 17 +++++++++++++++-- server.js | 29 ++++++++++++++++++----------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/public/common/play.js b/public/common/play.js index 0fa432a..1ebc9dd 100644 --- a/public/common/play.js +++ b/public/common/play.js @@ -553,11 +553,19 @@ function send_action(verb, noun) { return false; } +let replay_query = null; + function send_query(q, param) { if (param !== undefined) - send_message("query", [q, param]); + if (replay_query) + replay_query(q, param); + else + send_message("query", [q, param]); else - send_message("query", q); + if (replay_query) + replay_query(q, undefined); + else + send_message("query", q); } function confirm_resign() { @@ -649,6 +657,11 @@ async function init_replay() { } } + replay_query = function (query, params) { + let reply = rules.query(s, player, query, params); + on_reply(query, reply); + } + let ss; for (p = 0; p < replay.length; ++p) { replay[p].arguments = JSON.parse(replay[p].arguments); diff --git a/server.js b/server.js index f97a0ed..5cb934b 100644 --- a/server.js +++ b/server.js @@ -1759,7 +1759,7 @@ function on_restart(socket, scenario) { } } -function handle_message(socket, cmd, arg) { +function handle_player_message(socket, cmd, arg) { switch (cmd) { case 'action': on_action(socket, arg[0], arg[1]); break; case 'query': on_query(socket, arg); break; @@ -1773,6 +1773,12 @@ function handle_message(socket, cmd, arg) { } } +function handle_observer_message(socket, cmd, arg) { + switch (cmd) { + case 'query': on_query(socket, arg); break; + } +} + wss.on('connection', (socket, req, client) => { let u = url.parse(req.url, true); if (u.pathname !== '/play-socket') @@ -1829,16 +1835,17 @@ wss.on('connection', (socket, req, client) => { broadcast_presence(socket.game_id); }); - if (socket.role !== "Observer") { - socket.on('message', (data) => { - try { - let [ cmd, arg ] = JSON.parse(data); - handle_message(socket, cmd, arg); - } catch (err) { - send_message(socket, 'error', err); - } - }); - } + socket.on('message', (data) => { + try { + let [ cmd, arg ] = JSON.parse(data); + if (socket.role !== "Observer") + handle_player_message(socket, cmd, arg); + else + handle_observer_message(socket, cmd, arg); + } catch (err) { + send_message(socket, 'error', err); + } + }); broadcast_presence(socket.game_id); send_state(socket, get_game_state(socket.game_id)); -- cgit v1.2.3