aboutsummaryrefslogtreecommitdiff
path: root/server/src/state.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-07-07 23:03:07 +0200
committermetamuffin <metamuffin@disroot.org>2024-07-07 23:03:07 +0200
commitcd6e8f3886d764847f92ad75d397e2f2f6ad930a (patch)
treec99e141bc51c2004cdde1b0660ed10b2696f04d8 /server/src/state.rs
parentfd46def1ebc10d1f2ee4f4447f33e2dfb35986d2 (diff)
downloadhurrycurry-cd6e8f3886d764847f92ad75d397e2f2f6ad930a.tar
hurrycurry-cd6e8f3886d764847f92ad75d397e2f2f6ad930a.tar.bz2
hurrycurry-cd6e8f3886d764847f92ad75d397e2f2f6ad930a.tar.zst
add option to download map source
Diffstat (limited to 'server/src/state.rs')
-rw-r--r--server/src/state.rs40
1 files changed, 32 insertions, 8 deletions
diff --git a/server/src/state.rs b/server/src/state.rs
index cd763b18..8f33b6d4 100644
--- a/server/src/state.rs
+++ b/server/src/state.rs
@@ -6,7 +6,7 @@ use crate::{
protocol::{Message, PacketC, PacketS, PlayerID},
};
use anyhow::{anyhow, bail, Result};
-use clap::Parser;
+use clap::{Parser, ValueEnum};
use log::debug;
use tokio::sync::broadcast::Sender;
@@ -25,6 +25,11 @@ enum Command {
#[arg(default_value = "420")]
timer: u64,
},
+ Download {
+ #[arg(value_enum)]
+ r#type: DownloadType,
+ name: String,
+ },
List,
Effect {
name: String,
@@ -33,13 +38,20 @@ enum Command {
End,
}
+#[derive(ValueEnum, Clone)]
+enum DownloadType {
+ Map,
+ Recipes,
+ Demand,
+}
+
impl State {
- pub fn new(tx: Sender<PacketC>) -> Result<Self> {
+ pub async fn new(tx: Sender<PacketC>) -> Result<Self> {
let mut index = DataIndex::default();
index.reload()?;
let mut game = Game::new();
- game.load(index.generate("lobby-none-none".to_string())?, None);
+ game.load(index.generate("lobby-none-none".to_string()).await?, None);
Ok(Self { game, index, tx })
}
@@ -51,8 +63,10 @@ impl State {
text: format!("Game finished. You reached {} points.", self.game.points),
})
.ok();
- self.game
- .load(self.index.generate("lobby-none-none".to_string())?, None);
+ self.game.load(
+ self.index.generate("lobby-none-none".to_string()).await?,
+ None,
+ );
}
while let Some(p) = self.game.packet_out() {
debug!("-> {p:?}");
@@ -97,7 +111,7 @@ impl State {
async fn handle_command(&mut self, player: PlayerID, command: Command) -> Result<()> {
match command {
Command::Start { spec, timer } => {
- let data = self.index.generate(spec)?;
+ let data = self.index.generate(spec).await?;
self.game.load(data, Some(Duration::from_secs(timer)));
}
Command::End => {
@@ -113,12 +127,22 @@ impl State {
),
})
.ok();
- self.game
- .load(self.index.generate("lobby-none-none".to_string())?, None);
+ self.game.load(
+ self.index.generate("lobby-none-none".to_string()).await?,
+ None,
+ );
}
Command::Reload => {
self.index.reload()?;
}
+ Command::Download { r#type, name } => {
+ let source = match r#type {
+ DownloadType::Map => self.index.read_map(&name).await,
+ DownloadType::Recipes => self.index.read_recipes(&name).await,
+ DownloadType::Demand => self.index.read_demands(&name).await,
+ }?;
+ bail!("{source}");
+ }
Command::List => {
bail!(
"Maps: {:?}\nDemands: {:?}\nRecipes: {:?}",