serialize actions in enum

This commit is contained in:
Gregor Zeitlinger 2023-09-23 14:21:56 +02:00
commit 968ea198d2
15 changed files with 82 additions and 172 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ dist/
.vscode/
.history/
*.cmd/
*.result.json

View file

@ -73,7 +73,7 @@ impl Action {
}
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub enum CombatAction {
PlayActionCard(Option<String>),
RemoveCasualties(Vec<u32>),

View file

@ -4,7 +4,7 @@ use crate::{
game::Game, playing_actions::ActionType, position::Position, resource_pile::ResourcePile,
};
#[derive(Serialize, Deserialize, PartialEq, Eq)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum CustomAction {
ConstructWonder {
city_position: Position,

View file

@ -303,20 +303,15 @@ impl Game {
Playing => {
let action = action.playing().expect("action should be a playing action");
if self.can_redo()
&& serde_json::from_str::<PlayingAction>(
self.action_log[self.action_log_index]
.as_playing_action()
.expect("undone actions should be playing actions"),
)
.expect("action should be deserializable")
&& self.action_log[self.action_log_index]
.as_playing_action()
.expect("undone actions should be playing actions")
== action
{
self.redo(player_index);
return;
}
self.add_action_log_item(ActionLogItem::Playing(
serde_json::to_string(&action).expect("playing action should be serializable"),
));
self.add_action_log_item(ActionLogItem::Playing(action.clone()));
action.execute(self, player_index);
}
StatusPhase(phase) => {
@ -324,10 +319,7 @@ impl Game {
.status_phase()
.expect("action should be a status phase action");
assert!(phase == action.phase(), "Illegal action");
self.add_action_log_item(ActionLogItem::StatusPhase(
serde_json::to_string(&action)
.expect("status phase action should be serializable"),
));
self.add_action_log_item(ActionLogItem::StatusPhase(action.clone()));
action.execute(self, player_index);
}
Movement {
@ -337,9 +329,7 @@ impl Game {
let action = action
.movement()
.expect("action should be a movement action");
self.add_action_log_item(ActionLogItem::Movement(
serde_json::to_string(&action).expect("movement action should be serializable"),
));
self.add_action_log_item(ActionLogItem::Movement(action.clone()));
self.execute_movement_action(
action,
player_index,
@ -356,9 +346,7 @@ impl Game {
let action = action
.cultural_influence_resolution()
.expect("action should be a cultural influence resolution action");
self.add_action_log_item(ActionLogItem::CulturalInfluenceResolution(
serde_json::to_string(&action).expect("playing action should be serializable"),
));
self.add_action_log_item(ActionLogItem::CulturalInfluenceResolution(action));
self.execute_cultural_influence_resolution_action(
action,
roll_boost_cost,
@ -380,9 +368,7 @@ impl Game {
can_retreat,
} => {
let action = action.combat().expect("action should be a combat action");
self.add_action_log_item(ActionLogItem::Combat(
serde_json::to_string(&action).expect("combat action should be serializable"),
));
self.add_action_log_item(ActionLogItem::Combat(action.clone()));
self.execute_combat_action(
action,
initiation,
@ -404,10 +390,7 @@ impl Game {
let action = action
.place_settler()
.expect("action should be place_settler action");
self.add_action_log_item(ActionLogItem::PlaceSettler(
serde_json::to_string(&action)
.expect("place settler action should be serializable"),
));
self.add_action_log_item(ActionLogItem::PlaceSettler(action));
self.execute_place_settler_action(
action,
player_index,
@ -421,10 +404,14 @@ impl Game {
fn undo(&mut self, player_index: usize) {
match &self.action_log[self.action_log_index - 1] {
ActionLogItem::Playing(action) => serde_json::from_str::<PlayingAction>(action).expect("log item variant of type playing action should contain a serialized playing action").undo(self, player_index),
ActionLogItem::Playing(action) => action.clone().undo(self, player_index),
ActionLogItem::StatusPhase(_) => panic!("status phase actions can't be undone"),
ActionLogItem::Movement(action) => self.undo_movement_action(serde_json::from_str::<MovementAction>(action).expect("log item variant of type movement action should contain a serialized movement action"), player_index),
ActionLogItem::CulturalInfluenceResolution(action) => self.undo_cultural_influence_resolution_action(serde_json::from_str::<bool>(action).expect("cultural influence resolution log item should contain a serialized boolean representing the confirmation action")),
ActionLogItem::Movement(action) => {
self.undo_movement_action(action.clone(), player_index);
}
ActionLogItem::CulturalInfluenceResolution(action) => {
self.undo_cultural_influence_resolution_action(*action);
}
ActionLogItem::Combat(_action) => unimplemented!("retreat can't yet be undone"),
ActionLogItem::PlaceSettler(_action) => panic!("placing a settler can't be undone"),
}
@ -435,17 +422,13 @@ impl Game {
fn redo(&mut self, player_index: usize) {
let action_log_item = &self.action_log[self.action_log_index];
self.log.push(log::format_action_log_item(
&action_log_item.as_action(),
&action_log_item.clone().as_action(),
self,
));
match action_log_item {
ActionLogItem::Playing(action) => serde_json::from_str::<PlayingAction>(action)
.expect("action should be deserializable")
.execute(self, player_index),
ActionLogItem::Playing(action) => action.clone().execute(self, player_index),
ActionLogItem::StatusPhase(_) => panic!("status phase actions can't be redone"),
ActionLogItem::Movement(action) => {
let action = serde_json::from_str::<MovementAction>(action)
.expect("movement action should be deserializable");
let Movement {
movement_actions_left,
moved_units,
@ -456,14 +439,13 @@ impl Game {
);
};
self.execute_movement_action(
action,
action.clone(),
player_index,
*movement_actions_left,
moved_units.clone(),
);
}
ActionLogItem::CulturalInfluenceResolution(action) => {
let action = serde_json::from_str::<bool>(action).expect("action should be a serialized boolean representing the cultural influence resolution confirmation action");
let CulturalInfluenceResolution {
roll_boost_cost,
target_player_index,
@ -474,7 +456,7 @@ impl Game {
panic!("cultural influence resolution actions can only be redone if the game is in a cultural influence resolution state");
};
self.execute_cultural_influence_resolution_action(
action,
*action,
*roll_boost_cost,
*target_player_index,
*target_city_position,
@ -641,7 +623,6 @@ impl Game {
fn undo_cultural_influence_resolution_action(&mut self, action: bool) {
let cultural_influence_attempt_action = self.action_log[self.action_log_index - 2].as_playing_action().expect("any log item previous to a cultural influence resolution action log item should a cultural influence attempt action log item");
let cultural_influence_attempt_action = serde_json::from_str::<PlayingAction>(cultural_influence_attempt_action).expect("any log item previous to a cultural influence resolution action log item should a cultural influence attempt action log item");
let PlayingAction::InfluenceCultureAttempt {
starting_city_position: _,
target_player_index,

View file

@ -17,19 +17,19 @@ use crate::{
#[derive(Serialize, Deserialize, Clone)]
pub enum ActionLogItem {
Playing(String),
StatusPhase(String),
Movement(String),
CulturalInfluenceResolution(String),
Combat(String),
PlaceSettler(String),
Playing(PlayingAction),
StatusPhase(StatusPhaseAction),
Movement(MovementAction),
CulturalInfluenceResolution(bool),
Combat(CombatAction),
PlaceSettler(Position),
}
impl ActionLogItem {
#[must_use]
pub fn as_playing_action(&self) -> Option<&str> {
pub fn as_playing_action(&self) -> Option<PlayingAction> {
if let Self::Playing(v) = self {
Some(v)
Some(v.clone())
} else {
None
}
@ -41,14 +41,16 @@ impl ActionLogItem {
///
/// Panics if variant does'nt match with it's contents
#[must_use]
pub fn as_action(&self) -> Action {
pub fn as_action(self) -> Action {
match self {
Self::Playing(action) => Action::Playing(serde_json::from_str::<PlayingAction>(action).expect("data should be a serialized playing action")),
Self::StatusPhase(action) => Action::StatusPhase(serde_json::from_str::<StatusPhaseAction>(action).expect("data should be a serialized status phase action")),
Self::Movement(action) => Action::Movement(serde_json::from_str::<MovementAction>(action).expect("data should be a serialized movement action")),
Self::CulturalInfluenceResolution(action) => Action::CulturalInfluenceResolution(serde_json::from_str::<bool>(action).expect("data should be a serialized boolean representing cultural influence resolution confirmation action")),
Self::Combat(action) => Action::Combat(serde_json::from_str::<CombatAction>(action).expect("data should be a serialized combat action")),
Self::PlaceSettler(action) => Action::PlaceSettler(serde_json::from_str::<Position>(action).expect("data should be a serialized place settler action")),
Self::Playing(action) => Action::Playing(action),
Self::StatusPhase(action) => Action::StatusPhase(action),
Self::Movement(action) => Action::Movement(action),
Self::CulturalInfluenceResolution(action) => {
Action::CulturalInfluenceResolution(action)
}
Self::Combat(action) => Action::Combat(action),
Self::PlaceSettler(action) => Action::PlaceSettler(action),
}
}
}

View file

@ -19,7 +19,7 @@ use crate::{
unit::UnitType,
};
#[derive(Serialize, Deserialize, PartialEq, Eq)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum PlayingAction {
Advance {
advance: String,

View file

@ -10,13 +10,13 @@ use crate::{
resource_pile::ResourcePile,
};
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct ChangeGovernmentType {
pub new_government: String,
pub additional_advances: Vec<String>,
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub enum StatusPhaseAction {
CompleteObjectives(Vec<String>),
FreeAdvance(String),

View file

@ -370,7 +370,7 @@ impl Display for Units {
}
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub enum MovementAction {
Move {
units: Vec<u32>,

View file

@ -361,7 +361,7 @@ fn assert_eq_game_json(
expected_path: &str,
message: &str,
) {
if expected == actual {
if expected.trim() == actual.trim() {
return;
}
let file_path = format!("tests{SEPARATOR}test_games{SEPARATOR}{test}.result.json");

View file

@ -464,17 +464,7 @@
},
"starting_player_index": 0,
"current_player_index": 1,
"action_log": [
{
"Playing": "{\"Advance\":{\"advance\":\"Tactics\",\"payment\":{\"food\":0,\"wood\":0,\"ore\":0,\"ideas\":2,\"gold\":0,\"mood_tokens\":0,\"culture_tokens\":0}}}"
},
{
"Playing": "{\"Construct\":{\"city_position\":{\"q\":2,\"r\":0},\"city_piece\":\"Fortress\",\"payment\":{\"food\":1,\"wood\":1,\"ore\":1,\"ideas\":0,\"gold\":0,\"mood_tokens\":0,\"culture_tokens\":0},\"port_position\":null,\"temple_bonus\":null}}"
},
{
"Playing": "\"EndTurn\""
}
],
"action_log": [],
"action_log_index": 3,
"log": [
"The game has started",
@ -574,4 +564,4 @@
}
}
]
}
}

View file

@ -466,16 +466,20 @@
"current_player_index": 1,
"action_log": [
{
"Playing": "{\"Advance\":{\"advance\":\"Tactics\",\"payment\":{\"food\":0,\"wood\":0,\"ore\":0,\"ideas\":2,\"gold\":0,\"mood_tokens\":0,\"culture_tokens\":0}}}"
},
{
"Playing": "{\"Construct\":{\"city_position\":{\"q\":2,\"r\":0},\"city_piece\":\"Fortress\",\"payment\":{\"food\":1,\"wood\":1,\"ore\":1,\"ideas\":0,\"gold\":0,\"mood_tokens\":0,\"culture_tokens\":0},\"port_position\":null,\"temple_bonus\":null}}"
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "{\"InfluenceCultureAttempt\":{\"starting_city_position\":{\"q\":2,\"r\":-1},\"target_player_index\":0,\"target_city_position\":{\"q\":2,\"r\":0},\"city_piece\":\"Fortress\"}}"
"Playing": {
"InfluenceCultureAttempt": {
"starting_city_position": {
"q": 2,
"r": -1
},
"target_player_index": 0,
"target_city_position": {
"q": 2,
"r": 0
},
"city_piece": "Fortress"
}
}
}
],
"action_log_index": 4,
@ -579,4 +583,4 @@
}
}
]
}
}

View file

@ -466,35 +466,7 @@
},
"starting_player_index": 0,
"current_player_index": 0,
"action_log": [
{
"Playing": "{\"Advance\":{\"advance\":\"Tactics\",\"payment\":{\"food\":0,\"wood\":0,\"ore\":0,\"ideas\":2,\"gold\":0,\"mood_tokens\":0,\"culture_tokens\":0}}}"
},
{
"Playing": "{\"Construct\":{\"city_position\":{\"q\":2,\"r\":0},\"city_piece\":\"Fortress\",\"payment\":{\"food\":1,\"wood\":1,\"ore\":1,\"ideas\":0,\"gold\":0,\"mood_tokens\":0,\"culture_tokens\":0},\"port_position\":null,\"temple_bonus\":null}}"
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "{\"InfluenceCultureAttempt\":{\"starting_city_position\":{\"q\":2,\"r\":-1},\"target_player_index\":0,\"target_city_position\":{\"q\":2,\"r\":0},\"city_piece\":\"Fortress\"}}"
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "\"EndTurn\""
}
],
"action_log": [],
"action_log_index": 9,
"log": [
"The game has started",
@ -609,4 +581,4 @@
}
}
]
}
}

View file

@ -469,34 +469,9 @@
"current_player_index": 1,
"action_log": [
{
"Playing": "{\"Advance\":{\"advance\":\"Tactics\",\"payment\":{\"food\":0,\"wood\":0,\"ore\":0,\"ideas\":2,\"gold\":0,\"mood_tokens\":0,\"culture_tokens\":0}}}"
},
{
"Playing": "{\"Construct\":{\"city_position\":{\"q\":2,\"r\":0},\"city_piece\":\"Fortress\",\"payment\":{\"food\":1,\"wood\":1,\"ore\":1,\"ideas\":0,\"gold\":0,\"mood_tokens\":0,\"culture_tokens\":0},\"port_position\":null,\"temple_bonus\":null}}"
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "{\"InfluenceCultureAttempt\":{\"starting_city_position\":{\"q\":2,\"r\":-1},\"target_player_index\":0,\"target_city_position\":{\"q\":2,\"r\":0},\"city_piece\":\"Fortress\"}}"
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "\"EndTurn\""
},
{
"Playing": "\"EndTurn\""
},
{
"StatusPhase": "{\"FreeAdvance\":\"Storage\"}"
"StatusPhase": {
"FreeAdvance": "Storage"
}
}
],
"action_log_index": 10,
@ -615,4 +590,4 @@
}
}
]
}
}

View file

@ -470,20 +470,7 @@
},
"starting_player_index": 0,
"current_player_index": 0,
"action_log": [
{
"Playing": "\"MoveUnits\""
},
{
"Movement": "{\"Move\":{\"units\":[4],\"destination\":{\"q\":1,\"r\":1}}}"
},
{
"Movement": "\"Stop\""
},
{
"Playing": "\"MoveUnits\""
}
],
"action_log": [],
"action_log_index": 4,
"log": [
"The game has started",
@ -604,4 +591,4 @@
}
}
]
}
}

View file

@ -474,19 +474,17 @@
"current_player_index": 0,
"action_log": [
{
"Playing": "\"MoveUnits\""
},
{
"Movement": "{\"Move\":{\"units\":[4],\"destination\":{\"q\":1,\"r\":1}}}"
},
{
"Movement": "\"Stop\""
},
{
"Playing": "\"MoveUnits\""
},
{
"Movement": "{\"Move\":{\"units\":[4],\"destination\":{\"q\":1,\"r\":2}}}"
"Movement": {
"Move": {
"units": [
4
],
"destination": {
"q": 1,
"r": 2
}
}
}
}
],
"action_log_index": 5,
@ -622,4 +620,4 @@
}
}
]
}
}