diff options
| author | metamuffin <metamuffin@disroot.org> | 2025-10-11 00:24:11 +0200 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2025-10-11 00:24:11 +0200 |
| commit | e68a9eb5e9e15372313f9017be4a2d58fb690bfc (patch) | |
| tree | 690287a2c71d87c2955e992f1e9575166237188a /server/src | |
| parent | 3fe8ba7f1b9fa7e38fa03f55fd898c8ca2a0e996 (diff) | |
| download | hurrycurry-e68a9eb5e9e15372313f9017be4a2d58fb690bfc.tar hurrycurry-e68a9eb5e9e15372313f9017be4a2d58fb690bfc.tar.bz2 hurrycurry-e68a9eb5e9e15372313f9017be4a2d58fb690bfc.tar.zst | |
clippy + fmt; start using if let chains
Diffstat (limited to 'server/src')
| -rw-r--r-- | server/src/commands.rs | 2 | ||||
| -rw-r--r-- | server/src/entity/book.rs | 2 | ||||
| -rw-r--r-- | server/src/entity/campaign.rs | 4 | ||||
| -rw-r--r-- | server/src/entity/conveyor.rs | 4 | ||||
| -rw-r--r-- | server/src/entity/item_portal.rs | 4 | ||||
| -rw-r--r-- | server/src/entity/player_portal.rs | 4 | ||||
| -rw-r--r-- | server/src/entity/tutorial.rs | 18 | ||||
| -rw-r--r-- | server/src/interaction.rs | 282 | ||||
| -rw-r--r-- | server/src/main.rs | 10 | ||||
| -rw-r--r-- | server/src/network/upnp.rs | 14 | ||||
| -rw-r--r-- | server/src/scoreboard.rs | 2 | ||||
| -rw-r--r-- | server/src/server.rs | 41 | ||||
| -rw-r--r-- | server/src/state.rs | 13 |
13 files changed, 197 insertions, 203 deletions
diff --git a/server/src/commands.rs b/server/src/commands.rs index 5eda53b6..0a7b7643 100644 --- a/server/src/commands.rs +++ b/server/src/commands.rs @@ -22,7 +22,7 @@ use crate::{ use anyhow::Result; use clap::{Parser, ValueEnum}; use hurrycurry_bot::algos::ALGO_CONSTRUCTORS; -use hurrycurry_locale::{tre, trm, TrError}; +use hurrycurry_locale::{TrError, tre, trm}; use hurrycurry_protocol::{Character, Menu, Message, PacketC, PlayerClass, PlayerID}; use std::{fmt::Write, time::Duration}; diff --git a/server/src/entity/book.rs b/server/src/entity/book.rs index 4b87aa44..8daec972 100644 --- a/server/src/entity/book.rs +++ b/server/src/entity/book.rs @@ -18,7 +18,7 @@ use super::{Entity, EntityContext}; use anyhow::Result; use hurrycurry_locale::TrError; -use hurrycurry_protocol::{glam::IVec2, Menu, PacketC, PlayerID}; +use hurrycurry_protocol::{Menu, PacketC, PlayerID, glam::IVec2}; #[derive(Debug, Clone)] pub struct Book(pub IVec2); diff --git a/server/src/entity/campaign.rs b/server/src/entity/campaign.rs index fdc169d1..33baf631 100644 --- a/server/src/entity/campaign.rs +++ b/server/src/entity/campaign.rs @@ -19,10 +19,10 @@ use super::{Entity, EntityContext}; use crate::{scoreboard::ScoreboardStore, server::GameServerExt}; use anyhow::Result; use hurrycurry_data::entities::GateCondition; -use hurrycurry_locale::{trm, TrError}; +use hurrycurry_locale::{TrError, trm}; use hurrycurry_protocol::{ - glam::{IVec2, Vec2}, Message, PacketC, PlayerID, TileIndex, + glam::{IVec2, Vec2}, }; #[derive(Debug, Default, Clone)] diff --git a/server/src/entity/conveyor.rs b/server/src/entity/conveyor.rs index e31410e3..9534b045 100644 --- a/server/src/entity/conveyor.rs +++ b/server/src/entity/conveyor.rs @@ -17,8 +17,8 @@ */ use super::{Entity, EntityContext}; use crate::interaction::interact; -use anyhow::{anyhow, bail, Result}; -use hurrycurry_protocol::{glam::IVec2, ItemLocation}; +use anyhow::{Result, anyhow, bail}; +use hurrycurry_protocol::{ItemLocation, glam::IVec2}; #[derive(Debug, Clone)] pub struct Conveyor { diff --git a/server/src/entity/item_portal.rs b/server/src/entity/item_portal.rs index 6035331b..f0472802 100644 --- a/server/src/entity/item_portal.rs +++ b/server/src/entity/item_portal.rs @@ -17,8 +17,8 @@ */ use super::{Entity, EntityContext}; use crate::interaction::interact; -use anyhow::{bail, Result}; -use hurrycurry_protocol::{glam::IVec2, ItemLocation}; +use anyhow::{Result, bail}; +use hurrycurry_protocol::{ItemLocation, glam::IVec2}; #[derive(Debug, Default, Clone)] pub struct ItemPortal { diff --git a/server/src/entity/player_portal.rs b/server/src/entity/player_portal.rs index f70a2f95..1717002a 100644 --- a/server/src/entity/player_portal.rs +++ b/server/src/entity/player_portal.rs @@ -16,8 +16,8 @@ */ use super::{Entity, EntityContext}; -use anyhow::{anyhow, Result}; -use hurrycurry_protocol::{glam::Vec2, PacketC}; +use anyhow::{Result, anyhow}; +use hurrycurry_protocol::{PacketC, glam::Vec2}; #[derive(Debug, Default, Clone)] pub struct PlayerPortal { diff --git a/server/src/entity/tutorial.rs b/server/src/entity/tutorial.rs index bc4e3e7d..69086165 100644 --- a/server/src/entity/tutorial.rs +++ b/server/src/entity/tutorial.rs @@ -19,7 +19,7 @@ use super::{Entity, EntityContext}; use anyhow::Result; use hurrycurry_locale::{TrError, trm}; use hurrycurry_protocol::{ - glam::IVec2, ItemIndex, Message, PacketC, PlayerID, Recipe, RecipeIndex, TileIndex, + ItemIndex, Message, PacketC, PlayerID, Recipe, RecipeIndex, TileIndex, glam::IVec2, }; use log::{debug, warn}; @@ -297,10 +297,10 @@ impl StepContext<'_> { .. } => { for (pos, tile) in self.ent.game.tiles.iter().filter(|(_, t)| t.kind == *tile) { - if let Some(item) = &tile.item { - if item.kind == *input { - return Err((Some(*pos), trm!("s.tutorial.hold_interact"))); - } + if let Some(item) = &tile.item + && item.kind == *input + { + return Err((Some(*pos), trm!("s.tutorial.hold_interact"))); } } if let Some(pos) = self.find_tile(*tile) { @@ -322,10 +322,10 @@ impl StepContext<'_> { } => { for (_pos, tile) in self.ent.game.tiles.iter().filter(|(_, t)| t.kind == *tile) { - if let Some(item) = &tile.item { - if item.kind == *input { - return Err((None, trm!("s.tutorial.wait_finish"))); - } + if let Some(item) = &tile.item + && item.kind == *input + { + return Err((None, trm!("s.tutorial.wait_finish"))); } } if let Some(pos) = self.find_tile(*tile) { diff --git a/server/src/interaction.rs b/server/src/interaction.rs index fef1ca40..5dd7099b 100644 --- a/server/src/interaction.rs +++ b/server/src/interaction.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use hurrycurry_client_lib::{gamedata_index::GamedataIndex, Involvement, Item}; +use hurrycurry_client_lib::{Involvement, Item, gamedata_index::GamedataIndex}; use hurrycurry_protocol::{Gamedata, ItemLocation, PacketC, PlayerID, Recipe, Score, TileIndex}; use log::info; use std::collections::VecDeque; @@ -36,46 +36,45 @@ pub fn interact( packet_out: &mut VecDeque<PacketC>, ) { let _ = automated; //? what was this used for?? - if other.is_none() { - if let Some(item) = this { - if let Some(active) = &mut item.active { - let recipe = &data.recipe(active.recipe); - if recipe.supports_tile(tile) { - if let Recipe::Active { outputs, speed, .. } = recipe { - if edge { - active.speed += speed; - } else { - active.speed -= speed; - active.speed = active.speed.max(0.); // in case of "release without press" when items cool on active tile - } - if active.position >= 1. { - let this_had_item = this.is_some(); - let other_had_item = other.is_some(); - *other = outputs[0].map(|kind| Item { kind, active: None }); - *this = outputs[1].map(|kind| Item { kind, active: None }); - produce( - this_had_item, - other_had_item, - this, - this_loc, - other, - other_loc, - score_changed, - packet_out, - ); - } else { - packet_out.push_back(PacketC::SetProgress { - player, - item: this_loc, - position: active.position, - speed: active.speed, - warn: active.warn, - }); - } - return; - } - } + if other.is_none() + && let Some(item) = this + && let Some(active) = &mut item.active + { + let recipe = &data.recipe(active.recipe); + if recipe.supports_tile(tile) + && let Recipe::Active { outputs, speed, .. } = recipe + { + if edge { + active.speed += speed; + } else { + active.speed -= speed; + active.speed = active.speed.max(0.); // in case of "release without press" when items cool on active tile } + if active.position >= 1. { + let this_had_item = this.is_some(); + let other_had_item = other.is_some(); + *other = outputs[0].map(|kind| Item { kind, active: None }); + *this = outputs[1].map(|kind| Item { kind, active: None }); + produce( + this_had_item, + other_had_item, + this, + this_loc, + other, + other_loc, + score_changed, + packet_out, + ); + } else { + packet_out.push_back(PacketC::SetProgress { + player, + item: this_loc, + position: active.position, + speed: active.speed, + warn: active.warn, + }); + } + return; } } if !edge { @@ -87,48 +86,48 @@ pub fn interact( } match recipe { Recipe::Active { input, speed, .. } => { - if other.is_none() { - if let Some(item) = this { - if item.kind == *input && item.active.is_none() { - info!("start active recipe {ri:?}"); - item.active = Some(Involvement { - player, - recipe: ri, - speed: *speed, - position: 0., - warn: false, - }); - } - } + if other.is_none() + && let Some(item) = this + && item.kind == *input + && item.active.is_none() + { + info!("start active recipe {ri:?}"); + item.active = Some(Involvement { + player, + recipe: ri, + speed: *speed, + position: 0., + warn: false, + }); } - if this.is_none() { - if let Some(item) = &other { - 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; - } - } + if this.is_none() + && let Some(item) = &other + && 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; } } Recipe::Instant { @@ -176,24 +175,25 @@ pub fn interact( }) }); - if can_place && this.is_none() { - if let Some(item) = other.take() { - *this = Some(item); - packet_out.push_back(PacketC::MoveItem { - from: other_loc, - to: this_loc, - }); - return; - } + if can_place + && this.is_none() + && let Some(item) = other.take() + { + *this = Some(item); + packet_out.push_back(PacketC::MoveItem { + from: other_loc, + to: this_loc, + }); + return; } - if other.is_none() { - if let Some(item) = this.take() { - *other = Some(item); - packet_out.push_back(PacketC::MoveItem { - from: this_loc, - to: other_loc, - }); - } + if other.is_none() + && let Some(item) = this.take() + { + *other = Some(item); + packet_out.push_back(PacketC::MoveItem { + from: this_loc, + to: other_loc, + }); } } @@ -225,10 +225,10 @@ pub fn tick_slot( let prev_speed = a.speed; if r.supports_tile(tile) { - if a.speed <= 0. { - if let Recipe::Passive { speed, .. } = &data.recipe(a.recipe) { - a.speed = *speed; - } + if a.speed <= 0. + && let Recipe::Passive { speed, .. } = &data.recipe(a.recipe) + { + a.speed = *speed; } } else if let Some(revert_speed) = r.revert_speed() { a.speed = -revert_speed @@ -241,25 +241,25 @@ pub fn tick_slot( packet_out.push_back(PacketC::ClearProgress { item: slot_loc }); return; } - if a.position >= 1. { - if let Recipe::Passive { output, warn, .. } = &data.recipe(a.recipe) { - *slot = output.map(|kind| Item { kind, active: None }); - score.passive_recipes += 1; - *score_changed = true; - packet_out.push_back(PacketC::SetProgress { - player: None, - warn: *warn, - item: slot_loc, - position: 1., - speed: 0., - }); - packet_out.push_back(PacketC::SetItem { - location: slot_loc, - item: slot.as_ref().map(|i| i.kind), - }); - return; - }; - } + if a.position >= 1. + && let Recipe::Passive { output, warn, .. } = &data.recipe(a.recipe) + { + *slot = output.map(|kind| Item { kind, active: None }); + score.passive_recipes += 1; + *score_changed = true; + packet_out.push_back(PacketC::SetProgress { + player: None, + warn: *warn, + item: slot_loc, + position: 1., + speed: 0., + }); + packet_out.push_back(PacketC::SetItem { + location: slot_loc, + item: slot.as_ref().map(|i| i.kind), + }); + return; + }; a.position += dt * a.speed; a.position = a.position.min(1.); @@ -276,29 +276,27 @@ pub fn tick_slot( } 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 { + if recipe.supports_tile(tile) + && 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; - } - } + && *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; } } } diff --git a/server/src/main.rs b/server/src/main.rs index be0bb005..93147d45 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -15,13 +15,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use anyhow::{bail, Result}; +use anyhow::{Result, bail}; use clap::Parser; use futures_util::{SinkExt, StreamExt}; use hurrycurry_locale::trm; use hurrycurry_protocol::{PacketC, PacketS}; -use hurrycurry_server::{server::Server, ConnectionID}; -use log::{debug, info, trace, warn, LevelFilter}; +use hurrycurry_server::{ConnectionID, server::Server}; +use log::{LevelFilter, debug, info, trace, warn}; use std::{ env::var, net::SocketAddr, path::PathBuf, process::exit, str::FromStr, sync::Arc, time::Duration, @@ -29,7 +29,7 @@ use std::{ use tokio::{ net::TcpListener, spawn, - sync::{broadcast, mpsc::channel, RwLock}, + sync::{RwLock, broadcast, mpsc::channel}, time::interval, }; use tokio_tungstenite::tungstenite::Message; @@ -315,7 +315,7 @@ async fn run(data_path: PathBuf, args: Args) -> anyhow::Result<()> { #[cfg(test)] mod test { use hurrycurry_protocol::{Character, PacketS, PlayerClass, PlayerID}; - use hurrycurry_server::{server::Server, ConnectionID}; + use hurrycurry_server::{ConnectionID, server::Server}; use std::future::Future; use tokio::sync::broadcast; diff --git a/server/src/network/upnp.rs b/server/src/network/upnp.rs index 8ad79588..a443a223 100644 --- a/server/src/network/upnp.rs +++ b/server/src/network/upnp.rs @@ -15,11 +15,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use anyhow::{bail, Result}; +use anyhow::{Result, bail}; use get_if_addrs::get_if_addrs; use igd::{ - aio::{search_gateway, Gateway}, PortMappingProtocol, SearchOptions, + aio::{Gateway, search_gateway}, }; use log::{error, info}; use std::{ @@ -61,11 +61,11 @@ async fn upnp_setup() -> Result<(Gateway, Ipv4Addr)> { info!("IGD address is {}", gateway.addr); for i in get_if_addrs()? { let a = i.addr.ip(); - if !a.is_loopback() { - if let IpAddr::V4(a) = a { - info!("local v4 address is {a}"); - return Ok((gateway, a)); - } + if !a.is_loopback() + && let IpAddr::V4(a) = a + { + info!("local v4 address is {a}"); + return Ok((gateway, a)); } } bail!("no good local address found") diff --git a/server/src/scoreboard.rs b/server/src/scoreboard.rs index e97e22b2..86616c1f 100644 --- a/server/src/scoreboard.rs +++ b/server/src/scoreboard.rs @@ -23,7 +23,7 @@ use serde::{Deserialize, Serialize}; use std::{ cmp::Reverse, collections::HashMap, - fs::{create_dir_all, read_to_string, rename, File}, + fs::{File, create_dir_all, read_to_string, rename}, io::Write, }; diff --git a/server/src/server.rs b/server/src/server.rs index 48439b5f..9141bc4e 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -461,14 +461,14 @@ impl Server { // TODO if holding two, one is destroyed for item in p.items.into_iter().flatten() { let pos = p.movement.position.floor().as_ivec2(); - if let Some(tile) = self.game.tiles.get_mut(&pos) { - if tile.item.is_none() { - self.packet_out.push_back(PacketC::SetItem { - location: ItemLocation::Tile(pos), - item: Some(item.kind), - }); - tile.item = Some(item); - } + if let Some(tile) = self.game.tiles.get_mut(&pos) + && tile.item.is_none() + { + self.packet_out.push_back(PacketC::SetItem { + location: ItemLocation::Tile(pos), + item: Some(item.kind), + }); + tile.item = Some(item); } } self.packet_out @@ -725,10 +725,10 @@ impl Server { self.game .players_spatial_index .query(pos1, 2., |p2, _pos2| { - if p1 != p2 { - if let [Some(a), Some(b)] = self.game.players.get_disjoint_mut([&p1, &p2]) { - a.movement.collide(&mut b.movement, dt) - } + if p1 != p2 + && let [Some(a), Some(b)] = self.game.players.get_disjoint_mut([&p1, &p2]) + { + a.movement.collide(&mut b.movement, dt) } }) }); @@ -765,16 +765,13 @@ impl Server { player.communicate_persist = None; } } - if let Some((pos, hand)) = player.interacting { - if let Some(tile) = self.game.tiles.get(&pos) { - if let Some(item) = &tile.item { - if let Some(involvement) = &item.active { - if involvement.position >= 1. { - players_auto_release.push((*pid, hand)); - } - } - } - } + if let Some((pos, hand)) = player.interacting + && let Some(tile) = self.game.tiles.get(&pos) + && let Some(item) = &tile.item + && let Some(involvement) = &item.active + && involvement.position >= 1. + { + players_auto_release.push((*pid, hand)); } } for (player, hand) in players_auto_release.drain(..) { diff --git a/server/src/state.rs b/server/src/state.rs index 6cdaa8f1..49b2467a 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -16,11 +16,11 @@ */ use crate::{ - server::{AnnounceState, ConnectionData, GameServerExt, Server}, ConnectionID, + server::{AnnounceState, ConnectionData, GameServerExt, Server}, }; use anyhow::Result; -use hurrycurry_locale::{tre, trm, TrError}; +use hurrycurry_locale::{TrError, tre, trm}; use hurrycurry_protocol::{Menu, Message, PacketC, PacketS, PlayerID, VERSION}; use log::{debug, info, trace}; @@ -92,16 +92,15 @@ impl Server { conn: ConnectionID, packet: PacketS, ) -> Result<Vec<PacketC>, TrError> { - if let Some(p) = get_packet_player(&packet) { - if !self + if let Some(p) = get_packet_player(&packet) + && !self .connections .entry(conn) .or_default() .players .contains(&p) - { - return Err(tre!("s.error.packet_sender_invalid")); - } + { + return Err(tre!("s.error.packet_sender_invalid")); } let mut replies = Vec::new(); match &packet { |