diff options
Diffstat (limited to 'server/src/state.rs')
-rw-r--r-- | server/src/state.rs | 40 |
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: {:?}", |