aboutsummaryrefslogtreecommitdiff
path: root/server/src/state.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/state.rs')
-rw-r--r--server/src/state.rs151
1 files changed, 2 insertions, 149 deletions
diff --git a/server/src/state.rs b/server/src/state.rs
index ad9eeb1c..ed1a7488 100644
--- a/server/src/state.rs
+++ b/server/src/state.rs
@@ -15,56 +15,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-use crate::{entity::bot::BotDriver, server::Server, ConnectionID};
-use anyhow::{anyhow, bail, Result};
-use clap::{Parser, ValueEnum};
-use hurrycurry_bot::algos::ALGO_CONSTRUCTORS;
+use crate::{server::Server, ConnectionID};
+use anyhow::{bail, Result};
use hurrycurry_protocol::{Message, PacketC, PacketS, PlayerID};
use log::{debug, trace};
-use std::time::Duration;
-
-#[derive(Parser)]
-#[clap(multicall = true)]
-enum Command {
- /// Start a new game
- Start {
- /// Gamedata specification
- #[arg(default_value = "junior")]
- spec: String,
- /// Duration in seconds
- #[arg(default_value = "420")]
- timer: u64,
- },
- /// Abort the current game
- End,
- /// Download recipe/map's source declaration
- Download {
- /// Resource kind
- #[arg(value_enum)]
- r#type: DownloadType,
- /// Name
- name: String,
- },
- /// List all recipes and maps
- List,
- /// Send an effect
- Effect { name: String },
- /// Send an item
- Item { name: String },
- /// Reload the resource index
- ReloadIndex,
- #[clap(alias = "summon-bot", alias = "spawn-bot")]
- CreateBot { algo: String, name: Option<String> },
- /// Reload the current map
- #[clap(alias = "r")]
- Reload,
-}
-
-#[derive(ValueEnum, Clone)]
-enum DownloadType {
- Map,
- Recipes,
-}
impl Server {
pub async fn tick_outer(&mut self, dt: f32) -> anyhow::Result<()> {
@@ -148,107 +102,6 @@ impl Server {
}
self.connections.remove(&conn);
}
-
- async fn handle_command_parse(&mut self, player: PlayerID, command: &str) -> Result<()> {
- for line in command.split("\n") {
- self.handle_command(
- player,
- Command::try_parse_from(
- shlex::split(line)
- .ok_or(anyhow!("quoting invalid"))?
- .into_iter(),
- )?,
- )
- .await?;
- }
- Ok(())
- }
-
- async fn handle_command(&mut self, player: PlayerID, command: Command) -> Result<()> {
- match command {
- Command::Start { spec, timer } => {
- let data = self.index.generate(&spec).await?;
- self.load(data, Some(Duration::from_secs(timer)));
- }
- Command::End => {
- self.tx
- .send(PacketC::ServerMessage {
- text: format!(
- "Game was aborted by {}.",
- self.game
- .players
- .get(&player)
- .ok_or(anyhow!("player missing"))?
- .name
- ),
- })
- .ok();
- self.load(self.index.generate("lobby").await?, None);
- }
- Command::Reload => {
- if self.count_chefs() > 1 {
- bail!("must be at most one player to reload");
- }
- self.load(
- self.index.generate(&self.game.data.current_map).await?,
- None,
- );
- }
- Command::ReloadIndex => {
- 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,
- }?;
- bail!("{source}");
- }
- Command::List => {
- bail!(
- "Maps: {:?}\nRecipes: {:?}",
- self.index.maps.keys().collect::<Vec<_>>(),
- self.index.recipes
- )
- }
- Command::Effect { name } => {
- self.tx
- .send(PacketC::Communicate {
- player,
- message: Some(Message::Effect(name)),
- timeout: None,
- })
- .ok();
- }
- Command::Item { name } => {
- let item = self
- .game
- .data
- .get_item_by_name(&name)
- .ok_or(anyhow!("unknown item"))?;
- self.tx
- .send(PacketC::Communicate {
- player,
- message: Some(Message::Item(item)),
- timeout: None,
- })
- .ok();
- }
- Command::CreateBot { algo, name } => {
- let (aname, cons) = ALGO_CONSTRUCTORS
- .iter()
- .find(|(name, _)| *name == algo.as_str())
- .ok_or(anyhow!("algo name unknown"))?;
- let algo = cons();
- self.entities.push(Box::new(BotDriver::new(
- format!("{}-bot", name.unwrap_or((*aname).to_owned())),
- 51,
- algo,
- )));
- }
- }
- Ok(())
- }
}
fn get_packet_player(packet: &PacketS) -> Option<PlayerID> {