Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Husbandry #140

Merged
merged 10 commits into from
Jan 26, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
husbandry
  • Loading branch information
zeitlinger committed Jan 26, 2025
commit 72a7af15008da010e0555d2fce2d2795972339ee
16 changes: 11 additions & 5 deletions server/src/game.rs
Original file line number Diff line number Diff line change
@@ -305,16 +305,13 @@ impl Game {
return;
}

// both for redo and for executing the action
self.players[player_index].take_events(|events, player| {
events.on_execute_action.trigger(player, &action, &());
});

if matches!(action, Action::Redo) {
assert!(self.can_redo(), "no action can be redone");
self.redo(player_index);
return;
}
let copy = action.clone();

self.log.push(log::format_action_log_item(&action, self));
self.add_action_log_item(action.clone());
match self.state.clone() {
@@ -372,9 +369,16 @@ impl Game {
}
Finished => panic!("actions can't be executed when the game is finished"),
}
self.on_execute_or_redo(&copy, player_index);
check_for_waste(self, player_index);
}

fn on_execute_or_redo(&mut self, action: &Action, player_index: usize) {
self.players[player_index].take_events(|events, player| {
events.on_execute_action.trigger(player, &action, &());
});
}

fn undo(&mut self, player_index: usize) {
let action = &self.action_log[self.action_log_index - 1];

@@ -411,6 +415,7 @@ impl Game {

fn redo(&mut self, player_index: usize) {
let action_log_item = &self.action_log[self.action_log_index];
let copy = action_log_item.clone();
self.log
.push(log::format_action_log_item(&action_log_item.clone(), self));
match action_log_item {
@@ -455,6 +460,7 @@ impl Game {
Action::Redo => panic!("redo action can't be redone"),
}
self.action_log_index += 1;
self.on_execute_or_redo(&copy, player_index);
check_for_waste(self, player_index);
}

17 changes: 17 additions & 0 deletions server/tests/game_api_tests.rs
Original file line number Diff line number Diff line change
@@ -830,6 +830,23 @@ fn test_collect() {
);
}

#[test]
fn test_collect_husbandry() {
// todo: test that the action is legal it can't be done again
test_action(
"collect_husbandry",
Action::Playing(Collect(playing_actions::Collect {
city_position: Position::from_offset("B3"),
collections: vec![
(Position::from_offset("B5"), ResourcePile::food(1)),
],
})),
0,
true,
false,
);
}

#[test]
fn test_collect_free_economy() {
test_action(
Loading