diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/bot/src/algos/customer.rs | 7 | ||||
-rw-r--r-- | server/bot/src/algos/frank.rs | 10 | ||||
-rw-r--r-- | server/bot/src/algos/mod.rs | 7 | ||||
-rw-r--r-- | server/bot/src/algos/test.rs | 10 | ||||
-rw-r--r-- | server/bot/src/main.rs | 2 | ||||
-rw-r--r-- | server/client-lib/src/lib.rs | 31 | ||||
-rw-r--r-- | server/src/data/index.rs | 13 | ||||
-rw-r--r-- | server/src/data/mod.rs | 6 | ||||
-rw-r--r-- | server/src/entity/bot.rs | 2 | ||||
-rw-r--r-- | server/src/interaction.rs | 105 | ||||
-rw-r--r-- | server/src/server.rs | 2 | ||||
-rw-r--r-- | server/src/state.rs | 11 |
12 files changed, 88 insertions, 118 deletions
diff --git a/server/bot/src/algos/customer.rs b/server/bot/src/algos/customer.rs index 61a79dbc..1b1315c6 100644 --- a/server/bot/src/algos/customer.rs +++ b/server/bot/src/algos/customer.rs @@ -25,7 +25,9 @@ use log::info; use rand::{random, seq::IndexedRandom, thread_rng}; #[derive(Debug, Clone)] +#[derive(Default)] pub enum Customer { + #[default] New, Entering { path: Path, @@ -50,11 +52,6 @@ pub enum Customer { }, } -impl Default for Customer { - fn default() -> Self { - Customer::New - } -} impl BotAlgo for Customer { fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput { diff --git a/server/bot/src/algos/frank.rs b/server/bot/src/algos/frank.rs index 980d8c08..95718d4c 100644 --- a/server/bot/src/algos/frank.rs +++ b/server/bot/src/algos/frank.rs @@ -35,13 +35,13 @@ pub struct Frank { const MESSAGES: &[fn(&str) -> String] = &[ |_name| format!("Work faster, {_name}!"), - |_name| format!("Quick! There is no time for chit-chat!"), - |_name| format!("Look what a mess you've made! Clean this up immediatly!"), - |_name| format!("Don't let the customers starve!"), + |_name| "Quick! There is no time for chit-chat!".to_string(), + |_name| "Look what a mess you've made! Clean this up immediatly!".to_string(), + |_name| "Don't let the customers starve!".to_string(), |_name| format!("You are wasting me money, {_name}!"), - |_name| format!("I'm not paying you to stand around!"), + |_name| "I'm not paying you to stand around!".to_string(), |_name| format!("Get back to work, {_name}!"), - |_name| format!("You're FIRED!"), + |_name| "You're FIRED!".to_string(), ]; impl BotAlgo for Frank { diff --git a/server/bot/src/algos/mod.rs b/server/bot/src/algos/mod.rs index bb1d615b..dea31406 100644 --- a/server/bot/src/algos/mod.rs +++ b/server/bot/src/algos/mod.rs @@ -16,18 +16,19 @@ */ mod customer; +mod frank; mod simple; mod test; mod waiter; -pub mod frank; pub use customer::Customer; +pub use frank::Frank; pub use simple::Simple; pub use test::Test; pub use waiter::Waiter; -pub use frank::Frank; -pub const ALGO_CONSTRUCTORS: &'static [(&'static str, fn() -> crate::DynBotAlgo)] = &[ +#[allow(clippy::type_complexity)] +pub const ALGO_CONSTRUCTORS: &[(&str, fn() -> crate::DynBotAlgo)] = &[ ("test", || Box::new(Test::default())), ("simple", || Box::new(Simple::default())), ("waiter", || Box::new(Waiter::default())), diff --git a/server/bot/src/algos/test.rs b/server/bot/src/algos/test.rs index 73368de2..7cfafc29 100644 --- a/server/bot/src/algos/test.rs +++ b/server/bot/src/algos/test.rs @@ -43,12 +43,10 @@ impl BotAlgo for Test { interact: None, ..Default::default() }; - } else { - if let Some((item, near)) = find_demand(game) { - info!("demand {item:?} near {near}"); - if let Some(path) = find_path_to_neighbour(&game.walkable, pos.as_ivec2(), near) { - self.path = Some(path); - } + } else if let Some((item, near)) = find_demand(game) { + info!("demand {item:?} near {near}"); + if let Some(path) = find_path_to_neighbour(&game.walkable, pos.as_ivec2(), near) { + self.path = Some(path); } } BotInput::default() diff --git a/server/bot/src/main.rs b/server/bot/src/main.rs index bb30cb68..d4d21d35 100644 --- a/server/bot/src/main.rs +++ b/server/bot/src/main.rs @@ -76,7 +76,7 @@ fn main() -> Result<()> { .iter() .find(|(n, _)| n == &args.algo) .map(|(_, c)| c()) - .expect(&format!("unknown algo {:?}", args.algo)), + .unwrap_or_else(|| panic!("unknown algo {:?}", args.algo)), }), PacketC::Error { message } => { warn!("server error message: {message}"); diff --git a/server/client-lib/src/lib.rs b/server/client-lib/src/lib.rs index b4cb0724..fc0f8ae6 100644 --- a/server/client-lib/src/lib.rs +++ b/server/client-lib/src/lib.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#![feature(map_many_mut)] +#![feature(map_many_mut, let_chains)] pub mod network; pub mod spatial_index; @@ -60,6 +60,7 @@ pub struct Player { pub movement: MovementBase, } +#[derive(Default)] pub struct Game { pub data: Arc<Gamedata>, pub tiles: HashMap<IVec2, Tile>, @@ -73,22 +74,6 @@ pub struct Game { pub score: Score, } -impl Default for Game { - fn default() -> Self { - Self { - data: Default::default(), - tiles: HashMap::new(), - walkable: HashSet::new(), - players: HashMap::new(), - players_spatial_index: SpatialIndex::default(), - end: None, - lobby: false, - environment_effects: HashSet::new(), - score: Score::default(), - } - } -} - impl Game { pub fn apply_packet(&mut self, packet: PacketC) { match packet { @@ -176,10 +161,10 @@ impl Game { message, timeout, } => { - if let Some(timeout) = &timeout { - if let Some(player) = self.players.get_mut(&player) { - player.communicate_persist = message.to_owned().map(|m| (m, *timeout)); - } + if let Some(timeout) = &timeout + && let Some(player) = self.players.get_mut(&player) + { + player.communicate_persist = message.to_owned().map(|m| (m, *timeout)); } } PacketC::Score(score) => { @@ -211,14 +196,14 @@ impl Game { .update_entry(pid, player.movement.position); } - for (_, player) in &mut self.players { + for player in self.players.values_mut() { if let Some(item) = &mut player.item { if let Some(active) = &mut item.active { active.position += active.speed; } } } - for (_, tile) in &mut self.tiles { + for tile in self.tiles.values_mut() { if let Some(item) = &mut tile.item { if let Some(active) = &mut item.active { active.position += active.speed; diff --git a/server/src/data/index.rs b/server/src/data/index.rs index 1c206d83..e056cfc0 100644 --- a/server/src/data/index.rs +++ b/server/src/data/index.rs @@ -11,14 +11,11 @@ impl GamedataIndex { self.recipe_passive_by_input.clear(); for (ri, r) in data.recipes() { - match r { - Recipe::Passive { input, .. } => { - self.recipe_passive_by_input - .entry(*input) - .or_default() - .push(ri); - } - _ => (), + if let Recipe::Passive { input, .. } = r { + self.recipe_passive_by_input + .entry(*input) + .or_default() + .push(ri); } } } diff --git a/server/src/data/mod.rs b/server/src/data/mod.rs index 246f695e..91b32a42 100644 --- a/server/src/data/mod.rs +++ b/server/src/data/mod.rs @@ -152,14 +152,12 @@ impl DataIndex { &self .read_recipes( map_in - .recipes - .as_ref() - .map(|a| a.as_str()) + .recipes.as_deref() .unwrap_or("default"), ) .await?, )?; - Ok(build_data(&self.maps, map.to_string(), map_in, recipes_in)?) + build_data(&self.maps, map.to_string(), map_in, recipes_in) } } diff --git a/server/src/entity/bot.rs b/server/src/entity/bot.rs index cc67f640..79ca97f1 100644 --- a/server/src/entity/bot.rs +++ b/server/src/entity/bot.rs @@ -55,7 +55,7 @@ impl<T: BotAlgo> Entity for BotDriver<T> { }) } - let input = self.algo.tick(self.id, &c.game, c.dt); + let input = self.algo.tick(self.id, c.game, c.dt); if input.leave { info!("leave {:?}", self.id); c.packet_in.push_back(PacketS::Leave { player: self.id }); diff --git a/server/src/interaction.rs b/server/src/interaction.rs index 0721fa81..3a950fba 100644 --- a/server/src/interaction.rs +++ b/server/src/interaction.rs @@ -22,6 +22,7 @@ use std::collections::VecDeque; use crate::data::index::GamedataIndex; +#[allow(clippy::too_many_arguments)] pub fn interact( data: &Gamedata, edge: bool, @@ -108,32 +109,30 @@ pub fn interact( } if this.is_none() { if let Some(item) = &other { - if item.kind == *input { - if item.active.is_none() { - let mut item = other.take().unwrap(); - info!("start active recipe {ri:?}"); - item.active = Some(Involvement { - player, - recipe: ri, - speed: *speed, - position: 0., - warn: false, - }); - *this = Some(item); - score.active_recipes += 1; - packet_out.push_back(PacketC::MoveItem { - from: other_loc, - to: this_loc, - }); - packet_out.push_back(PacketC::SetProgress { - player, - item: this_loc, - position: 0., - speed: *speed, - warn: false, - }); - return; - } + if item.kind == *input && item.active.is_none() { + let mut item = other.take().unwrap(); + info!("start active recipe {ri:?}"); + item.active = Some(Involvement { + player, + recipe: ri, + speed: *speed, + position: 0., + warn: false, + }); + *this = Some(item); + score.active_recipes += 1; + packet_out.push_back(PacketC::MoveItem { + from: other_loc, + to: this_loc, + }); + packet_out.push_back(PacketC::SetProgress { + player, + item: this_loc, + position: 0., + speed: *speed, + warn: false, + }); + return; } } } @@ -194,7 +193,6 @@ pub fn interact( from: this_loc, to: other_loc, }); - return; } } } @@ -209,6 +207,7 @@ pub enum TickEffect { Produce, } +#[allow(clippy::too_many_arguments)] pub fn tick_slot( dt: f32, data: &Gamedata, @@ -273,34 +272,31 @@ pub fn tick_slot( warn: a.warn, item: slot_loc, }); - return; } - } else { - if let Some(recipes) = data_index.recipe_passive_by_input.get(&item.kind) { - for &ri in recipes { - let recipe = data.recipe(ri); - if recipe.supports_tile(tile) { - if let Recipe::Passive { - input, warn, speed, .. - } = recipe - { - if *input == item.kind { - item.active = Some(Involvement { - player: None, - recipe: ri, - position: 0., - warn: *warn, - speed: *speed, - }); - packet_out.push_back(PacketC::SetProgress { - player: None, - position: 0., - speed: *speed, - warn: *warn, - item: slot_loc, - }); - return; - } + } else if let Some(recipes) = data_index.recipe_passive_by_input.get(&item.kind) { + for &ri in recipes { + let recipe = data.recipe(ri); + if recipe.supports_tile(tile) { + if let Recipe::Passive { + input, warn, speed, .. + } = recipe + { + if *input == item.kind { + item.active = Some(Involvement { + player: None, + recipe: ri, + position: 0., + warn: *warn, + speed: *speed, + }); + packet_out.push_back(PacketC::SetProgress { + player: None, + position: 0., + speed: *speed, + warn: *warn, + item: slot_loc, + }); + return; } } } @@ -309,6 +305,7 @@ pub fn tick_slot( } } +#[allow(clippy::too_many_arguments)] fn produce( player: Option<PlayerID>, this_had_item: bool, diff --git a/server/src/server.rs b/server/src/server.rs index fd7db5b5..01460cb9 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -399,7 +399,7 @@ impl Server { let last_position_update = self .last_movement_update .entry(player) - .or_insert_with(|| Instant::now()); + .or_insert_with(Instant::now); let dt = last_position_update.elapsed(); *last_position_update += dt; let diff = pos - pd.movement.position; diff --git a/server/src/state.rs b/server/src/state.rs index e368317c..b9a43f6b 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -76,15 +76,12 @@ impl Server { self.packet_in(packet, &mut replies)?; for p in &replies { - match p { - PacketC::Joined { id } => { - self.connections.entry(conn).or_default().insert(*id); - } - _ => (), + if let PacketC::Joined { id } = p { + self.connections.entry(conn).or_default().insert(*id); } } - if self.count_chefs() <= 0 && !self.game.lobby { + if self.count_chefs() == 0 && !self.game.lobby { self.tx .send(PacketC::ServerMessage { text: "Game was aborted due to a lack of players".to_string(), @@ -97,7 +94,7 @@ impl Server { pub async fn disconnect(&mut self, conn: ConnectionID) { if let Some(players) = self.connections.get(&conn) { - for player in players.to_owned() { + for player in players.clone() { let _ = self.packet_in_outer(conn, PacketS::Leave { player }).await; } } |