cargo fmt
This commit is contained in:
parent
b7ab700a57
commit
218ebc3b8f
3 changed files with 18 additions and 19 deletions
|
@ -7,16 +7,16 @@ pub mod protocol;
|
||||||
pub mod rules;
|
pub mod rules;
|
||||||
pub mod serializer;
|
pub mod serializer;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
pub use rules::{PwState, Dispatch};
|
|
||||||
pub use protocol::CommandError;
|
|
||||||
pub use config::Config as PwConfig;
|
pub use config::Config as PwConfig;
|
||||||
|
pub use protocol::CommandError;
|
||||||
|
pub use rules::{Dispatch, PwState};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub struct PlanetWars {
|
pub struct PlanetWars {
|
||||||
/// Game state
|
/// Game state
|
||||||
state: rules::PwState,
|
state: rules::PwState,
|
||||||
/// Map planet names to their ids
|
/// Map planet names to their ids
|
||||||
planet_map: HashMap<String, usize>
|
planet_map: HashMap<String, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PlanetWars {
|
impl PlanetWars {
|
||||||
|
@ -28,7 +28,7 @@ impl PlanetWars {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|p| (p.name.clone(), p.id))
|
.map(|p| (p.name.clone(), p.id))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
PlanetWars { state, planet_map }
|
PlanetWars { state, planet_map }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,9 +58,8 @@ impl PlanetWars {
|
||||||
pub fn execute_command(
|
pub fn execute_command(
|
||||||
&mut self,
|
&mut self,
|
||||||
player_num: usize,
|
player_num: usize,
|
||||||
cmd: &protocol::Command
|
cmd: &protocol::Command,
|
||||||
) -> Result<(), CommandError>
|
) -> Result<(), CommandError> {
|
||||||
{
|
|
||||||
let dispatch = self.parse_command(player_num, cmd)?;
|
let dispatch = self.parse_command(player_num, cmd)?;
|
||||||
self.state.dispatch(&dispatch);
|
self.state.dispatch(&dispatch);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -69,13 +68,15 @@ impl PlanetWars {
|
||||||
/// Check the given command for validity.
|
/// Check the given command for validity.
|
||||||
/// If it is valid, return an internal representation of the dispatch
|
/// If it is valid, return an internal representation of the dispatch
|
||||||
/// described by the command.
|
/// described by the command.
|
||||||
pub fn parse_command(&self, player_id: usize, cmd: &protocol::Command)
|
pub fn parse_command(
|
||||||
-> Result<Dispatch, CommandError>
|
&self,
|
||||||
{
|
player_id: usize,
|
||||||
|
cmd: &protocol::Command,
|
||||||
|
) -> Result<Dispatch, CommandError> {
|
||||||
let origin_id = *self
|
let origin_id = *self
|
||||||
.planet_map
|
.planet_map
|
||||||
.get(&cmd.origin)
|
.get(&cmd.origin)
|
||||||
.ok_or(CommandError::OriginDoesNotExist)?;
|
.ok_or(CommandError::OriginDoesNotExist)?;
|
||||||
|
|
||||||
let target_id = *self
|
let target_id = *self
|
||||||
.planet_map
|
.planet_map
|
||||||
|
@ -108,4 +109,4 @@ impl PlanetWars {
|
||||||
pub fn execute_dispatch(&mut self, dispatch: &Dispatch) {
|
pub fn execute_dispatch(&mut self, dispatch: &Dispatch) {
|
||||||
self.state.dispatch(dispatch);
|
self.state.dispatch(dispatch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,8 +50,7 @@ pub struct Dispatch {
|
||||||
|
|
||||||
impl PwState {
|
impl PwState {
|
||||||
pub fn dispatch(&mut self, dispatch: &Dispatch) {
|
pub fn dispatch(&mut self, dispatch: &Dispatch) {
|
||||||
let distance = self.planets[dispatch.origin]
|
let distance = self.planets[dispatch.origin].distance(&self.planets[dispatch.target]);
|
||||||
.distance(&self.planets[dispatch.target]);
|
|
||||||
|
|
||||||
let origin = &mut self.planets[dispatch.origin];
|
let origin = &mut self.planets[dispatch.origin];
|
||||||
origin.fleets[0].ship_count -= dispatch.ship_count;
|
origin.fleets[0].ship_count -= dispatch.ship_count;
|
||||||
|
|
|
@ -47,8 +47,7 @@ impl<'a> Serializer<'a> {
|
||||||
/// rotated based on the number offset for this serializer.
|
/// rotated based on the number offset for this serializer.
|
||||||
fn player_num(&self, player_id: usize) -> usize {
|
fn player_num(&self, player_id: usize) -> usize {
|
||||||
let num_players = self.state.players.len();
|
let num_players = self.state.players.len();
|
||||||
let rotated_id =
|
let rotated_id = (player_id + num_players - self.player_num_offset) % num_players;
|
||||||
(player_id + num_players - self.player_num_offset) % num_players;
|
|
||||||
// protocol player ids start at 1
|
// protocol player ids start at 1
|
||||||
return rotated_id + 1;
|
return rotated_id + 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue