summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-06-29 20:26:02 +0200
committermetamuffin <metamuffin@disroot.org>2024-06-29 20:26:02 +0200
commit04a4902d1054a2e8776107c99ef83496b6050b60 (patch)
tree45d4bd14ec4a17e0accd060bf7211ee421418a34 /server/src
parent03c46a18d3a283ee737688c4c65bb5ef1d6ba1dc (diff)
downloadhurrycurry-04a4902d1054a2e8776107c99ef83496b6050b60.tar
hurrycurry-04a4902d1054a2e8776107c99ef83496b6050b60.tar.bz2
hurrycurry-04a4902d1054a2e8776107c99ef83496b6050b60.tar.zst
server message for command errors
Diffstat (limited to 'server/src')
-rw-r--r--server/src/main.rs16
-rw-r--r--server/src/protocol.rs3
-rw-r--r--server/src/state.rs33
3 files changed, 35 insertions, 17 deletions
diff --git a/server/src/main.rs b/server/src/main.rs
index bf69f295..68927eaa 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -151,13 +151,17 @@ async fn run() -> anyhow::Result<()> {
}
};
debug!("<- {id:?} {packet:?}");
- if let Err(e) = state.write().await.packet_in(id, packet).await {
- warn!("client error: {e}");
- let _ = error_tx
- .send(PacketC::Error {
+ let packet_out = match state.write().await.packet_in(id, packet).await {
+ Ok(packets) => packets,
+ Err(e) => {
+ warn!("client error: {e}");
+ vec![PacketC::Error {
message: format!("{e}"),
- })
- .await;
+ }]
+ }
+ };
+ for packet in packet_out {
+ let _ = error_tx.send(packet).await;
}
}
Message::Close(_) => break,
diff --git a/server/src/protocol.rs b/server/src/protocol.rs
index 6ed34a65..39bc0887 100644
--- a/server/src/protocol.rs
+++ b/server/src/protocol.rs
@@ -134,6 +134,9 @@ pub enum PacketC {
message: Option<Message>,
persist: bool,
},
+ ServerMessage {
+ text: String,
+ },
Score {
points: i64,
demands_failed: usize,
diff --git a/server/src/state.rs b/server/src/state.rs
index da05f33a..41ffce20 100644
--- a/server/src/state.rs
+++ b/server/src/state.rs
@@ -45,26 +45,37 @@ impl State {
let _ = self.tx.send(p);
}
}
- pub async fn packet_in(&mut self, player: PlayerID, packet: PacketS) -> Result<()> {
+ pub async fn packet_in(&mut self, player: PlayerID, packet: PacketS) -> Result<Vec<PacketC>> {
match &packet {
PacketS::Communicate {
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(());
+ match self.handle_command_parse(player, command).await {
+ Ok(()) => return Ok(vec![]),
+ Err(e) => {
+ return Ok(vec![PacketC::ServerMessage {
+ text: format!("{e}"),
+ }]);
+ }
+ }
}
_ => (),
}
self.game.packet_in(player, packet)?;
+ Ok(vec![])
+ }
+
+ async fn handle_command_parse(&mut self, player: PlayerID, command: &str) -> Result<()> {
+ self.handle_command(
+ player,
+ Command::try_parse_from(
+ shlex::split(command)
+ .ok_or(anyhow!("quoting invalid"))?
+ .into_iter(),
+ )?,
+ )
+ .await?;
Ok(())
}