Skip to content

Commit

Permalink
feat(utils): add logging and exception handling to if_last_game_state…
Browse files Browse the repository at this point in the history
… function
  • Loading branch information
maxblan committed Jan 7, 2024
1 parent e81cb1f commit 8ca5a26
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions python/socha/api/networking/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ast import List
import logging

from socha import _socha
from socha._socha import Field, FieldType, Move, TeamEnum, CubeCoordinates, GameState
Expand Down Expand Up @@ -106,15 +107,21 @@ def _merge_advances(actions):


def if_last_game_state(message, last_game_state):
last_game_state.board = _convert_board(message.data.class_binding.board)
actions = message.data.class_binding.last_move.actions.actions
new_actions = _merge_advances([_socha.Accelerate(acc=a.acc) if isinstance(a, Acceleration)
else _socha.Advance(distance=a.distance) if isinstance(a, Advance)
else _socha.Push(direction=direction_from_string(a.direction)) if isinstance(a, Push)
else _socha.Turn(direction=direction_from_string(a.direction)) for a in actions])

last_move = Move(actions=new_actions)
return last_game_state.perform_move(last_move)
try:
last_game_state.board = _convert_board(
message.data.class_binding.board)
actions = message.data.class_binding.last_move.actions.actions
new_actions = _merge_advances([_socha.Accelerate(acc=a.acc) if isinstance(a, Acceleration)
else _socha.Advance(distance=a.distance) if isinstance(a, Advance)
else _socha.Push(direction=direction_from_string(a.direction)) if isinstance(a, Push)
else _socha.Turn(direction=direction_from_string(a.direction)) for a in actions])

last_move = Move(actions=new_actions)
return last_game_state.perform_move(last_move)
except Exception as e:
logging.warning(
f"An error occurred: {e}. Returning the last game state without changes.")
return last_game_state


def if_not_last_game_state(message) -> GameState:
Expand Down

0 comments on commit 8ca5a26

Please sign in to comment.