Skip to content

Commit

Permalink
changes for gamebot
Browse files Browse the repository at this point in the history
  • Loading branch information
jder committed Dec 2, 2021
1 parent 4593500 commit c339d4f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
11 changes: 7 additions & 4 deletions client/src/Messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// From server
// To server
export class ToServerMessage {
type: string;

Expand All @@ -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")
Expand All @@ -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] };
Expand Down
30 changes: 22 additions & 8 deletions server/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -84,10 +87,17 @@ impl ChatSocket {
Ok(())
}

fn handle_login(&mut self, username: &str, ctx: &mut ws::WebsocketContext<Self>) {
fn handle_login(
&mut self,
username: &str,
user_type: &str,
ctx: &mut ws::WebsocketContext<Self>,
) {
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());
Expand Down Expand Up @@ -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<serde_json::Value>,
},
SendMessage {
name: String,
payload: serde_json::Value,
},
ReloadCode {},
SaveFile {
Expand Down
6 changes: 3 additions & 3 deletions server/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl PackageReference {
pub fn new(name: &str) -> ResultAnyError<PackageReference> {
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<user>[[:word:]]+)(/(?P<repo>[[:word:]]+))?\.(?P<package>[[:word:]]+)$").unwrap();
static ref RE: Regex = Regex::new(r"^(?P<user>[-[:word:]]+)(/(?P<repo>[-[:word:]]+))?\.(?P<package>[-[:word:]]+)$").unwrap();
}

RE.captures(name)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions server/src/world/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit c339d4f

Please sign in to comment.