Skip to content

Commit 51ba81d

Browse files
author
maxblan
committed
Refactor action perform_unchecked methods
1 parent 26af619 commit 51ba81d

File tree

7 files changed

+0
-135
lines changed

7 files changed

+0
-135
lines changed

python/socha/api/networking/utils.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,6 @@ def handle_move(move_response):
8383

8484

8585
def _merge_advances(actions):
86-
"""
87-
Merges consecutive Advance actions into a single action by adding their distances.
88-
This is a workaround for handling multiple Advance actions in a sequence.
89-
90-
Args:
91-
actions (list): A list of actions.
92-
93-
Returns:
94-
list: A new list of actions where consecutive Advance actions have been merged.
95-
96-
Note:
97-
This function modifies the input list 'actions' in-place.
98-
"""
9986
new_actions = []
10087
for i in range(len(actions) - 1):
10188
if isinstance(actions[i], _socha.Advance) and isinstance(actions[i + 1], _socha.Advance):

src/plugin/actions.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,4 @@ impl Action {
4848
Action::Turn(turn) => turn.perform(game_state).map(|ship| (Some(ship), None)),
4949
}
5050
}
51-
52-
pub fn perform_unchecked(&self, game_state: &mut GameState) -> (Option<Ship>, Option<Ship>) {
53-
match self {
54-
Action::Accelerate(accelerate) =>
55-
(Some(accelerate.perform_unchecked(game_state)), None),
56-
Action::Advance(advance) => (Some(advance.perform_unchecked(game_state)), None),
57-
Action::Push(push) => {
58-
let (ship1, ship2) = push.perform_unchecked(game_state);
59-
(Some(ship1), Some(ship2))
60-
}
61-
Action::Turn(turn) => (Some(turn.perform_unchecked(game_state)), None),
62-
}
63-
}
6451
}

src/plugin/actions/accelerate.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,6 @@ impl Accelerate {
8282
return ship.clone();
8383
}
8484

85-
pub fn perform_unchecked(&self, state: &GameState) -> Ship {
86-
debug!("perform_unchecked() called with acc: {} and game state: {:?}", self.acc, state);
87-
let mut ship: Ship = state.current_ship.clone();
88-
89-
let new_ship = self.accelerate_unchecked(&mut ship);
90-
91-
debug!("Ship accelerated successfully");
92-
new_ship
93-
}
94-
9585
fn accelerate_unchecked(&self, ship: &mut Ship) -> Ship {
9686
debug!("accelerate_unchecked() called with ship: {:?}", ship);
9787
let used_coal: i32 = self.acc.abs() - ship.free_acc;

src/plugin/actions/advance.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,6 @@ impl Advance {
6767
Ok(true)
6868
}
6969

70-
pub fn perform_unchecked(&self, state: &GameState) -> Ship {
71-
debug!("Performing unchecked advance with distance: {}", self.distance);
72-
let mut ship = state.current_ship.clone();
73-
74-
let advance_info = state.calculate_advance_info(
75-
&ship.position,
76-
&ship.direction,
77-
ship.movement
78-
);
79-
80-
ship.update_position(self.distance, advance_info);
81-
debug!("Unchecked advance completed: {:?}", ship);
82-
ship
83-
}
84-
8570
fn __repr__(&self) -> PyResult<String> {
8671
Ok(format!("Advance({})", self.distance))
8772
}

src/plugin/actions/push.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,6 @@ impl Push {
8686
Ok((current_ship, other_ship))
8787
}
8888

89-
pub fn perform_unchecked(&self, state: &GameState) -> (Ship, Ship) {
90-
debug!("Performing unchecked push with direction: {}", self.direction);
91-
let mut current_ship: Ship = state.current_ship.clone();
92-
let mut other_ship: Ship = state.other_ship.clone();
93-
94-
current_ship.movement -= 1;
95-
96-
let push_to: CubeCoordinates = current_ship.position + self.direction.vector();
97-
98-
other_ship.position = push_to;
99-
100-
if let Some(field) = state.board.get(&push_to) {
101-
if field.field_type == FieldType::Sandbank {
102-
other_ship.speed = 1;
103-
other_ship.movement = 1;
104-
}
105-
}
106-
107-
other_ship.free_turns += 1;
108-
109-
debug!("Unchecked push completed and other ship status: {:?}", other_ship);
110-
(current_ship, other_ship)
111-
}
112-
11389
fn __repr__(&self) -> PyResult<String> {
11490
Ok(format!("Push({})", self.direction))
11591
}

src/plugin/actions/turn.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,6 @@ impl Turn {
6060
self.direction.turn_count_to(self.direction.clone()).abs().saturating_sub(ship.free_turns)
6161
}
6262

63-
pub fn perform_unchecked(&self, state: &GameState) -> Ship {
64-
debug!("Performing unchecked turn with direction: {}", self.direction);
65-
let mut current_ship: Ship = state.current_ship.clone();
66-
67-
let turn_count: i32 = current_ship.direction.turn_count_to(self.direction);
68-
let used_coal: i32 = turn_count.abs() - current_ship.free_turns;
69-
70-
current_ship.free_turns = std::cmp::max(current_ship.free_turns - turn_count.abs(), 0);
71-
if used_coal > 0 {
72-
current_ship.coal -= used_coal;
73-
}
74-
75-
current_ship.direction = self.direction;
76-
77-
debug!("Unchecked turn completed and ship status: {:?}", current_ship);
78-
current_ship
79-
}
80-
8163
fn __repr__(&self) -> PyResult<String> {
8264
Ok(format!("Turn({})", self.direction))
8365
}

src/plugin/game_state.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -272,48 +272,6 @@ impl GameState {
272272
Ok(new_state)
273273
}
274274

275-
pub fn perform_move_unchecked(&self, move_: Move) -> GameState {
276-
let mut new_state = self.clone();
277-
debug!("Current ship before move: {:?}", new_state.current_ship);
278-
debug!("Other ship before move: {:?}", new_state.other_ship);
279-
280-
for action in &move_.actions {
281-
match action.perform_unchecked(&mut new_state) {
282-
(Some(current_ship), Some(other_ship)) => {
283-
new_state.current_ship = current_ship;
284-
new_state.other_ship = other_ship;
285-
}
286-
(Some(current_ship), None) => {
287-
new_state.current_ship = current_ship;
288-
}
289-
(None, Some(_)) => {}
290-
(None, None) => {}
291-
}
292-
}
293-
294-
new_state.pick_up_passenger_current_ship();
295-
new_state.current_ship.points = new_state
296-
.ship_points(new_state.current_ship)
297-
.expect("Could not calculate ship points");
298-
299-
if move_.actions.iter().any(|a| matches!(a, Action::Push(_))) {
300-
new_state.other_ship.points = new_state
301-
.ship_points(new_state.other_ship)
302-
.expect("Could not calculate other ship's points");
303-
if new_state.other_ship.speed == 1 {
304-
new_state.pick_up_passenger_other_ship();
305-
}
306-
}
307-
308-
new_state.last_move = Some(move_);
309-
new_state.advance_turn();
310-
311-
debug!("Current ship after move: {:?}", new_state.current_ship);
312-
debug!("Other ship after move: {:?}", new_state.other_ship);
313-
314-
new_state
315-
}
316-
317275
pub fn advance_turn(&mut self) {
318276
let current_ship: &mut Ship = &mut self.current_ship;
319277

0 commit comments

Comments
 (0)