diff options
author | metamuffin <metamuffin@disroot.org> | 2024-06-29 19:04:57 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-06-29 19:04:57 +0200 |
commit | fd2c907274095031917e6279db436186d95d74fc (patch) | |
tree | 62e064f445e791c78ada2b68f7e84220f33e76fb /server/src | |
parent | 5f361ffedfd132c5a900ca8dcb1b25fcb21a2e33 (diff) | |
download | hurrycurry-fd2c907274095031917e6279db436186d95d74fc.tar hurrycurry-fd2c907274095031917e6279db436186d95d74fc.tar.bz2 hurrycurry-fd2c907274095031917e6279db436186d95d74fc.tar.zst |
optionally persist messages
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/customer/mod.rs | 17 | ||||
-rw-r--r-- | server/src/game.rs | 24 | ||||
-rw-r--r-- | server/src/protocol.rs | 3 | ||||
-rw-r--r-- | server/src/state.rs | 32 |
4 files changed, 57 insertions, 19 deletions
diff --git a/server/src/customer/mod.rs b/server/src/customer/mod.rs index d1f49655..92e6e94c 100644 --- a/server/src/customer/mod.rs +++ b/server/src/customer/mod.rs @@ -140,6 +140,7 @@ impl DemandState { id, PacketS::Communicate { message: Some(Message::Item(data.demand(demand).from)), + persist: true, }, )); p.state = CustomerState::Waiting { @@ -157,7 +158,13 @@ impl DemandState { debug!("{id:?} waiting"); *timeout -= dt; if *timeout <= 0. { - packets_out.push((id, PacketS::Communicate { message: None })); + packets_out.push(( + id, + PacketS::Communicate { + message: None, + persist: true, + }, + )); let path = find_path( &self.walkable, p.movement.position.as_ivec2(), @@ -191,7 +198,13 @@ impl DemandState { } }); if let Some(pos) = demand_pos { - packets_out.push((id, PacketS::Communicate { message: None })); + packets_out.push(( + id, + PacketS::Communicate { + persist: true, + message: None, + }, + )); for edge in [true, false] { packets_out.push((id, PacketS::Interact { pos, edge })) } diff --git a/server/src/game.rs b/server/src/game.rs index 72f653c4..4f609608 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -56,7 +56,7 @@ pub struct Player { pub last_position_ts: Instant, pub interacting: Option<IVec2>, pub item: Option<Item>, - pub communicate: Option<Message>, + pub communicate_persist: Option<Message>, } pub struct Game { @@ -127,7 +127,7 @@ impl Game { last_position_ts: Instant::now(), character, position: self.data.chef_spawn, - communicate: None, + communicate_persist: None, interacting: None, name: name.clone(), }, @@ -167,10 +167,11 @@ impl Game { item: Some(item.kind), }) } - if let Some(c) = &player.communicate { + if let Some(c) = &player.communicate_persist { out.push(PacketC::Communicate { player: id, message: Some(c.to_owned()), + persist: true, }) } } @@ -223,7 +224,7 @@ impl Game { last_position_ts: Instant::now(), character, position, - communicate: None, + communicate_persist: None, interacting: None, name: name.clone(), }, @@ -392,13 +393,18 @@ impl Game { player.interacting = if edge { Some(pos) } else { None }; } - PacketS::Communicate { message } => { + PacketS::Communicate { message, persist } => { info!("{player:?} message {message:?}"); - if let Some(player) = self.players.get_mut(&player) { - player.communicate = message.clone() + if persist { + if let Some(player) = self.players.get_mut(&player) { + player.communicate_persist = message.clone() + } } - self.packet_out - .push_back(PacketC::Communicate { player, message }) + self.packet_out.push_back(PacketC::Communicate { + player, + message, + persist, + }) } PacketS::ReplaceHand { item } => { let pdata = self diff --git a/server/src/protocol.rs b/server/src/protocol.rs index 24ac2a11..6ed34a65 100644 --- a/server/src/protocol.rs +++ b/server/src/protocol.rs @@ -58,9 +58,11 @@ pub enum PacketS { }, Communicate { message: Option<Message>, + persist: bool, }, #[serde(skip)] + /// For internal use only ReplaceHand { item: Option<ItemIndex>, }, @@ -130,6 +132,7 @@ pub enum PacketC { Communicate { player: PlayerID, message: Option<Message>, + persist: bool, }, Score { points: i64, diff --git a/server/src/state.rs b/server/src/state.rs index ae3388b6..da05f33a 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -21,6 +21,9 @@ enum Command { #[arg(default_value = "small-default-default")] spec: String, }, + Effect { + name: String, + }, End, } @@ -45,13 +48,17 @@ impl State { pub async fn packet_in(&mut self, player: PlayerID, packet: PacketS) -> Result<()> { match &packet { PacketS::Communicate { - message: Some(Message::Text(message)), - } if let Some(command) = message.strip_prefix("/") => { - self.handle_command(Command::try_parse_from( - shlex::split(command) - .ok_or(anyhow!("quoting invalid"))? - .into_iter(), - )?) + message: Some(Message::Text(text)), + persist: false, + } if let Some(command) = text.strip_prefix("/") => { + self.handle_command( + player, + Command::try_parse_from( + shlex::split(command) + .ok_or(anyhow!("quoting invalid"))? + .into_iter(), + )?, + ) .await?; return Ok(()); } @@ -61,7 +68,7 @@ impl State { Ok(()) } - async fn handle_command(&mut self, command: Command) -> Result<()> { + async fn handle_command(&mut self, player: PlayerID, command: Command) -> Result<()> { match command { Command::Start { spec } => { let data = self.index.generate(spec)?; @@ -71,6 +78,15 @@ impl State { self.game .load(self.index.generate("lobby-none-none".to_string())?); } + Command::Effect { name } => { + self.tx + .send(PacketC::Communicate { + player, + message: Some(Message::Effect(name)), + persist: false, + }) + .ok(); + } } Ok(()) } |