From f157c847bb5f69f7f1c6a0ef8aec98de470da81b Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 15 Apr 2023 15:07:15 -0700 Subject: [PATCH] Include next mora in greeting event --- demo/shiritori.js | 15 +++++++++++---- src/server.rs | 4 +++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/demo/shiritori.js b/demo/shiritori.js index f43f5bf..0f819e6 100644 --- a/demo/shiritori.js +++ b/demo/shiritori.js @@ -19,18 +19,25 @@ function displayWord(_word, end, delay) { div.scrollTop = div.offsetHeight; } +function updateInput(data) { + if (data.next_mora !== null) { + let waiting = data.author === id; + input.placeholder = waiting ? "Waiting for other players..." : `${data.next_mora}…`; + input.disabled = waiting; + if (!waiting) input.focus(); + } +} + ws.onmessage = e => { const { event, data } = JSON.parse(e.data); switch (event) { case "greeting": id = data.id; + updateInput(data); break; case "word": - let waiting = data.author === id; displayWord(data.word, true, 0); - input.placeholder = waiting ? "Waiting for other players..." : `${data.next_mora}…`; - input.disabled = waiting; - if (!waiting) input.focus(); + updateInput(data); break; case "history": console.log(data); diff --git a/src/server.rs b/src/server.rs index 122de05..c5d4f1f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -26,6 +26,7 @@ impl MessageResponse { enum MessageResponseData { Greeting { id: u64, + next_mora: Option, }, Word { author: u64, @@ -165,7 +166,8 @@ impl Server { fn handle_connection(&mut self, client_id: u64, responder: Responder) -> Result<(), ServerError> { println!("A client connected with id #{}", client_id); responder.send(MessageResponseData::Greeting { - id: client_id + id: client_id, + next_mora: self.next_mora.clone(), }.into_message()); responder.send(MessageResponseData::History { words: self.database.load_words_before(self.database.last_word_id + 1)?