token movement now works (and syncs between devices)

This commit is contained in:
Rusty Striker 2024-10-09 20:23:02 +03:00
parent be6dd7c0e4
commit 95c2595279
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
6 changed files with 65 additions and 11 deletions

View file

@ -1,3 +1,4 @@
use api::game_actions::SpawnToken;
use game::{chat_message::ChatMessage, Game, GameImpl};
use tokio::sync::{broadcast, mpsc};
@ -87,7 +88,7 @@ impl GameServer {
for (i, (path, x, y)) in self.tokens.iter().enumerate() {
let bits = std::fs::read(path).expect("FAILED READING TOKEN IMAGE");
let img = base64::Engine::encode(&base64::prelude::BASE64_STANDARD, &bits);
_ = broadcast.send((Some(id.clone()), api::Response::SpawnToken { token_id: i, x: *x, y: *y, img }));
_ = broadcast.send((Some(id.clone()), api::Response::SpawnToken(SpawnToken { token_id: i, x: *x, y: *y, img })));
}
},
api::Request::SpawnToken { x, y, img_path } => {
@ -95,11 +96,15 @@ impl GameServer {
self.tokens.push((img_path.clone(), x, y));
let bits = std::fs::read(img_path).expect("FAILED READING TOKEN IMAGE");
let img = base64::Engine::encode(&base64::prelude::BASE64_STANDARD, &bits);
_ = broadcast.send((Some(id.clone()), api::Response::SpawnToken { token_id, x, y, img }));
_ = broadcast.send((Some(id.clone()), api::Response::SpawnToken(SpawnToken { token_id, x, y, img })));
},
api::Request::MoveToken { token_id, x, y } => {
// TODO: add check to make sure the actor is authorized to move the token
_ = broadcast.send((None, api::Response::MoveToken { token_id, x, y }));
if token_id < self.tokens.len() {
self.tokens[token_id].1 = x;
self.tokens[token_id].2 = y;
_ = broadcast.send((None, api::Response::MoveToken { token_id, x, y }));
}
},
api::Request::Quit => _ = broadcast.send((None, api::Response::Quit { id })),
api::Request::Kick(id) => _ = broadcast.send((Some(id), api::Response::Shutdown)),