aboutsummaryrefslogtreecommitdiff
path: root/planetwars-rules/src
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2021-12-21 21:47:48 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2021-12-21 21:47:48 +0100
commit218ebc3b8f5024a0813f8378b83d91dbb9aa7db2 (patch)
tree4ce205ae721fd0696ec16ea8b8b7f46bfeb36787 /planetwars-rules/src
parentb7ab700a57fb1155d4824f24fe9cc55111c977fe (diff)
downloadplanetwars.dev-218ebc3b8f5024a0813f8378b83d91dbb9aa7db2.tar.xz
planetwars.dev-218ebc3b8f5024a0813f8378b83d91dbb9aa7db2.zip
cargo fmt
Diffstat (limited to 'planetwars-rules/src')
-rw-r--r--planetwars-rules/src/lib.rs31
-rw-r--r--planetwars-rules/src/rules.rs3
-rw-r--r--planetwars-rules/src/serializer.rs3
3 files changed, 18 insertions, 19 deletions
diff --git a/planetwars-rules/src/lib.rs b/planetwars-rules/src/lib.rs
index 29c7b28..48034ee 100644
--- a/planetwars-rules/src/lib.rs
+++ b/planetwars-rules/src/lib.rs
@@ -7,16 +7,16 @@ pub mod protocol;
pub mod rules;
pub mod serializer;
-use std::collections::HashMap;
-pub use rules::{PwState, Dispatch};
-pub use protocol::CommandError;
pub use config::Config as PwConfig;
+pub use protocol::CommandError;
+pub use rules::{Dispatch, PwState};
+use std::collections::HashMap;
pub struct PlanetWars {
/// Game state
state: rules::PwState,
/// Map planet names to their ids
- planet_map: HashMap<String, usize>
+ planet_map: HashMap<String, usize>,
}
impl PlanetWars {
@@ -28,7 +28,7 @@ impl PlanetWars {
.iter()
.map(|p| (p.name.clone(), p.id))
.collect();
-
+
PlanetWars { state, planet_map }
}
@@ -58,9 +58,8 @@ impl PlanetWars {
pub fn execute_command(
&mut self,
player_num: usize,
- cmd: &protocol::Command
- ) -> Result<(), CommandError>
- {
+ cmd: &protocol::Command,
+ ) -> Result<(), CommandError> {
let dispatch = self.parse_command(player_num, cmd)?;
self.state.dispatch(&dispatch);
return Ok(());
@@ -69,13 +68,15 @@ impl PlanetWars {
/// Check the given command for validity.
/// If it is valid, return an internal representation of the dispatch
/// described by the command.
- pub fn parse_command(&self, player_id: usize, cmd: &protocol::Command)
- -> Result<Dispatch, CommandError>
- {
+ pub fn parse_command(
+ &self,
+ player_id: usize,
+ cmd: &protocol::Command,
+ ) -> Result<Dispatch, CommandError> {
let origin_id = *self
- .planet_map
- .get(&cmd.origin)
- .ok_or(CommandError::OriginDoesNotExist)?;
+ .planet_map
+ .get(&cmd.origin)
+ .ok_or(CommandError::OriginDoesNotExist)?;
let target_id = *self
.planet_map
@@ -108,4 +109,4 @@ impl PlanetWars {
pub fn execute_dispatch(&mut self, dispatch: &Dispatch) {
self.state.dispatch(dispatch);
}
-} \ No newline at end of file
+}
diff --git a/planetwars-rules/src/rules.rs b/planetwars-rules/src/rules.rs
index 7c13e58..587098f 100644
--- a/planetwars-rules/src/rules.rs
+++ b/planetwars-rules/src/rules.rs
@@ -50,8 +50,7 @@ pub struct Dispatch {
impl PwState {
pub fn dispatch(&mut self, dispatch: &Dispatch) {
- let distance = self.planets[dispatch.origin]
- .distance(&self.planets[dispatch.target]);
+ let distance = self.planets[dispatch.origin].distance(&self.planets[dispatch.target]);
let origin = &mut self.planets[dispatch.origin];
origin.fleets[0].ship_count -= dispatch.ship_count;
diff --git a/planetwars-rules/src/serializer.rs b/planetwars-rules/src/serializer.rs
index 380433e..7eb2e01 100644
--- a/planetwars-rules/src/serializer.rs
+++ b/planetwars-rules/src/serializer.rs
@@ -47,8 +47,7 @@ impl<'a> Serializer<'a> {
/// rotated based on the number offset for this serializer.
fn player_num(&self, player_id: usize) -> usize {
let num_players = self.state.players.len();
- let rotated_id =
- (player_id + num_players - self.player_num_offset) % num_players;
+ let rotated_id = (player_id + num_players - self.player_num_offset) % num_players;
// protocol player ids start at 1
return rotated_id + 1;
}