From c339d4f7785aa10422ddc42ace3c5e05be7964fb Mon Sep 17 00:00:00 2001 From: Jesse Rusak Date: Wed, 1 Dec 2021 19:13:08 -0500 Subject: [PATCH] changes for gamebot --- client/src/Messages.ts | 11 +++++++---- server/src/chat.rs | 30 ++++++++++++++++++++++-------- server/src/lua.rs | 6 +++--- server/src/main.rs | 2 +- server/src/world/state.rs | 4 ++-- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/client/src/Messages.ts b/client/src/Messages.ts index b0bca6d..c5b34fe 100644 --- a/client/src/Messages.ts +++ b/client/src/Messages.ts @@ -1,4 +1,4 @@ -// From server +// To server export class ToServerMessage { type: string; @@ -9,24 +9,26 @@ export class ToServerMessage { export class LoginMessage extends ToServerMessage { username: string; + user_type: string; constructor(username: string) { super("Login") this.username = username; + this.user_type = 'user'; } } export class CommandMessage extends ToServerMessage { text: string; - extra: JSON | null; - constructor(text: string, extra: JSON | null = null) { + constructor(text: string) { super("Command") this.text = text; - this.extra = extra; } } +// There is also a SendMessage type (currently unused in JS) + export class ReloadCodeMessage extends ToServerMessage { constructor() { super("ReloadCode") @@ -44,6 +46,7 @@ export class SaveFileMessage extends ToServerMessage { } } +// From Server export type ToClientMessage = { type: string; }; export type TellMessage = { type: string, content: ChatRowContent }; export type BacklogMessage = { type: string, history: [ChatRowContent] }; diff --git a/server/src/chat.rs b/server/src/chat.rs index 4fd8fa1..8c709ac 100644 --- a/server/src/chat.rs +++ b/server/src/chat.rs @@ -58,18 +58,21 @@ impl ChatSocket { let message: ToServerMessage = serde_json::from_str(&text)?; log::info!("Got message {:?}", message); match message { - ToServerMessage::Login { username } => self.handle_login(&username, ctx), - ToServerMessage::Command { text, extra } => { + ToServerMessage::Login { + username, + user_type, + } => self.handle_login(&username, &user_type, ctx), + ToServerMessage::Command { text } => { let mut payload = HashMap::new(); payload.insert( "message".to_string(), SerializableValue::String(text.to_string()), ); - if let Some(extra) = extra { - payload.insert("extra".to_string(), serde_json::from_value(extra)?); - } self.handle_user_command("command", SerializableValue::Dict(payload)) } + ToServerMessage::SendMessage { name, payload } => { + self.handle_user_command(&name, serde_json::from_value(payload)?) + } ToServerMessage::ReloadCode {} => self.handle_reload(ctx), ToServerMessage::SaveFile { name, content } => { // TODO: this needs way nicer syntax @@ -84,10 +87,17 @@ impl ChatSocket { Ok(()) } - fn handle_login(&mut self, username: &str, ctx: &mut ws::WebsocketContext) { + fn handle_login( + &mut self, + username: &str, + user_type: &str, + ctx: &mut ws::WebsocketContext, + ) { let world_ref = self.app_data.world_ref.clone(); world_ref.write(|world| { - let id = world.get_state_mut().get_or_create_user(username); + let id = world + .get_state_mut() + .get_or_create_user(username, user_type); if let Some(existing_id) = self.self_id { world.remove_chat_connection(existing_id, ctx.address()); @@ -210,10 +220,14 @@ impl ActixMessage for ToClientMessage { enum ToServerMessage { Login { username: String, + user_type: String, // eg "user" or "group" or whatever -- will become $username/live.$user_type }, Command { text: String, - extra: Option, + }, + SendMessage { + name: String, + payload: serde_json::Value, }, ReloadCode {}, SaveFile { diff --git a/server/src/lua.rs b/server/src/lua.rs index 40740cb..3fba053 100644 --- a/server/src/lua.rs +++ b/server/src/lua.rs @@ -279,7 +279,7 @@ impl PackageReference { pub fn new(name: &str) -> ResultAnyError { lazy_static! { // TODO: For now we only support a single package component (i.e. system.foo, not system.foo.bar) - static ref RE: Regex = Regex::new(r"^(?P[[:word:]]+)(/(?P[[:word:]]+))?\.(?P[[:word:]]+)$").unwrap(); + static ref RE: Regex = Regex::new(r"^(?P[-[:word:]]+)(/(?P[-[:word:]]+))?\.(?P[-[:word:]]+)$").unwrap(); } RE.captures(name) @@ -295,8 +295,8 @@ impl PackageReference { return &self.user; } - pub fn for_user(username: &str) -> PackageReference { - PackageReference::new(&format!("{}/live.user", username)).unwrap() + pub fn for_user(username: &str, user_type: &str) -> PackageReference { + PackageReference::new(&format!("{}/live.{}", username, user_type)).unwrap() } pub fn for_room() -> PackageReference { diff --git a/server/src/main.rs b/server/src/main.rs index 1d246ed..ff0ea0e 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -29,7 +29,7 @@ extern crate scoped_tls; extern crate lazy_static; async fn index() -> impl Responder { - HttpResponse::Ok().body("Hello world!") + HttpResponse::Ok().body("Hello from orisa!") } async fn socket( diff --git a/server/src/world/state.rs b/server/src/world/state.rs index 5990af6..85835a3 100644 --- a/server/src/world/state.rs +++ b/server/src/world/state.rs @@ -111,11 +111,11 @@ impl State { self.entrance } - pub fn get_or_create_user(&mut self, username: &str) -> Id { + pub fn get_or_create_user(&mut self, username: &str, user_type: &str) -> Id { if let Some(id) = self.users.get(username) { *id } else { - let id = self.create_object(ObjectKind::for_user(username)); + let id = self.create_object(ObjectKind::for_user(username, user_type)); let entrance = self.entrance(); self.object_mut(id).unwrap().parent = Some(entrance);