-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract action from move and fix format
- Loading branch information
Showing
5 changed files
with
133 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//! Ported from https://github.com/software-challenge/backend/blob/be88340f619892fe70c4cbd45e131d5445e883c7/plugin/src/main/kotlin/sc/plugin2024/actions | ||
|
||
use crate::util::{Error, Element, Result}; | ||
|
||
use super::CubeDir; | ||
|
||
/// An action to take during a move. | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub enum Action { | ||
/// Acceleration by the given amount. | ||
Accelerate { | ||
/// The amount to accelerate by. May be negative, but not zero. | ||
acc: i32 | ||
}, | ||
/// Advancement in the direction of movement. | ||
Advance { | ||
/// The number of fields to move. | ||
distance: usize, | ||
}, | ||
/// Nudging another ship. | ||
Push { | ||
/// The direction to nudge in. | ||
direction: CubeDir, | ||
}, | ||
/// A turn of the ship. | ||
Turn { | ||
/// The direction to turn into. | ||
direction: CubeDir, | ||
}, | ||
} | ||
|
||
impl TryFrom<&Element> for Action { | ||
type Error = Error; | ||
|
||
fn try_from(elem: &Element) -> Result<Self> { | ||
match elem.attribute("class")? { | ||
"acceleration" => Ok(Self::Accelerate { acc: elem.attribute("acc")?.parse()? }), | ||
"advance" => Ok(Self::Advance { distance: elem.attribute("distance")?.parse()? }), | ||
"push" => Ok(Self::Push { direction: elem.attribute("direction")?.parse()? }), | ||
"turn" => Ok(Self::Turn { direction: elem.attribute("direction")?.parse()? }), | ||
class => Err(Error::UnknownVariant(format!("Unknown move class: {}", class))), | ||
} | ||
} | ||
} | ||
|
||
impl From<Action> for Element { | ||
fn from(m: Action) -> Self { | ||
let base = Element::new("data"); | ||
match m { | ||
Action::Accelerate { acc } => base | ||
.attribute("class", "acceleration") | ||
.attribute("acc", acc) | ||
.build(), | ||
Action::Advance { distance } => base | ||
.attribute("class", "advance") | ||
.attribute("distance", distance) | ||
.build(), | ||
Action::Push { direction } => base | ||
.attribute("class", "push") | ||
.attribute("direction", direction) | ||
.build(), | ||
Action::Turn { direction } => base | ||
.attribute("class", "turn") | ||
.attribute("direction", direction) | ||
.build(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{util::assert_xml_format, game::{Action, CubeDir}}; | ||
|
||
// TODO: Add parse and/or roundtrip tests | ||
|
||
#[test] | ||
fn test_to_xml() { | ||
assert_xml_format!( | ||
Action::Accelerate { acc: -2 }, | ||
r#"<acceleration acc="-2" />"# | ||
); | ||
|
||
assert_xml_format!( | ||
Action::Advance { distance: 10 }, | ||
r#"<advance distance="10" />"# | ||
); | ||
|
||
assert_xml_format!( | ||
Action::Push { direction: CubeDir::Left }, | ||
r#"<push direction="LEFT" />"# | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters