impl some more stuff

This commit is contained in:
Rusty Striker 2025-07-25 17:36:19 +03:00
parent 0d99649e56
commit 6e269c6b1f
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
5 changed files with 23 additions and 1 deletions

View file

@ -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();

View file

@ -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
}

View file

@ -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,
},

View file

@ -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,
}),
));

View file

@ -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;