From 6e269c6b1fe57374067005b4c0828516c5b5e029 Mon Sep 17 00:00:00 2001 From: Rusty Striker Date: Fri, 25 Jul 2025 17:36:19 +0300 Subject: [PATCH] impl some more stuff --- assets/web/tavern.js | 18 ++++++++++++++++++ src/api/login.rs | 1 + src/api/mod.rs | 1 + src/lib.rs | 2 ++ src/main.rs | 2 +- 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/assets/web/tavern.js b/assets/web/tavern.js index 8be0268..366434a 100644 --- a/assets/web/tavern.js +++ b/assets/web/tavern.js @@ -5,11 +5,13 @@ class Tavern { this.msgs = []; this.connected = false; this.loggedIn = false; + this.admin = false; this.socket.onmessage = (m) => { m = JSON.parse(m.data); console.log(m); if (m.login) { this.socket.loggedIn = m.login.success; + this.admin = m.login.admin; this.call(this.onlogin, this.socket.loggedIn); } if (m.message) { @@ -105,6 +107,22 @@ class Tavern { if (!this.connected || this.loggedIn) { return; } this.socket.send(JSON.stringify('get_scene_list')) } + spawn_token = (scene, character, x, y, img_path, visible_to_players) => { + if (!this.connected || this.loggedIn) { return; } + this.socket.send(JSON.stringify({ + spawn_token: { + map_id: scene, character: character, x: x, y: y, img_path: img_path, visible_to_players: visible_to_players + } + })); + } + create_scene = (title) => { + if (!this.connected || this.loggedIn) { return; } + this.socket.send(JSON.stringify({ create_scene: { title: title } })); + } + shutdown = () => { + if (!this.connected || this.loggedIn) { return; } + this.socket.send(JSON.stringify('shutdown')); + } } const tavern = new Tavern(); diff --git a/src/api/login.rs b/src/api/login.rs index e365193..b115557 100644 --- a/src/api/login.rs +++ b/src/api/login.rs @@ -9,6 +9,7 @@ pub struct LoginRequest { #[derive(Serialize, Clone, Debug)] pub struct LoginResult { pub success: bool, + pub admin: bool, pub username: String, // TODO: Figure out what the user needs on successful login to reduce traffic } diff --git a/src/api/mod.rs b/src/api/mod.rs index 53f1b2c..426f7b8 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -44,6 +44,7 @@ pub enum Request { amount: usize, from: usize, }, + /// TODO: Perhaps remove it, as the client should auto get it on login and use GetChatHistory when needed GetLastMessages { amount: usize, }, diff --git a/src/lib.rs b/src/lib.rs index ff07bea..ef99e42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,6 +101,7 @@ impl GameServer { SendTo::User(id.clone()), api::Response::Login(api::login::LoginResult { success: true, + admin, username: login.username.clone(), }), )); @@ -112,6 +113,7 @@ impl GameServer { SendTo::User(id.clone()), api::Response::Login(api::login::LoginResult { success: false, + admin: false, username: login.username, }), )); diff --git a/src/main.rs b/src/main.rs index 27c08b6..295a9a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -130,7 +130,7 @@ async fn handle_socket( b = brecv.recv() => { println!("{} trying to log in: {:?}", &temp_id, &b); if let Ok((to_id, msg)) = b { - if let Response::Login(open_tavern::api::login::LoginResult { success, username }) = &msg { + if let Response::Login(open_tavern::api::login::LoginResult { success, username, admin: _ }) = &msg { let to_id = to_id.should_send(&temp_id); if to_id && *success && id.as_ref().map(|id| id == username).unwrap_or(false) { _ = socket.send(Message::Text(serde_json::to_string(&msg).unwrap_or_default().into())).await;