55from socha ._socha import Field , FieldType , Move , TeamEnum , CubeCoordinates , GameState
66from socha .api .protocol .protocol import Acceleration , Actions , Advance , Push , Turn , Board , Data , Water , Sandbank , Island , Passenger , Goal
77
8+ from python .socha .api .protocol .protocol import Room
9+
10+
11+ def _convert_field (field ) -> _socha .Field :
12+ if isinstance (field , Water ):
13+ return Field (FieldType .Water , None )
14+ elif isinstance (field , Sandbank ):
15+ return Field (FieldType .Sandbank , None )
16+ elif isinstance (field , Island ):
17+ return Field (FieldType .Island , None )
18+ elif isinstance (field , Passenger ):
19+ return Field (FieldType .Passenger , _socha .Passenger (direction_from_string (field .direction ), field .passenger ))
20+ elif isinstance (field , Goal ):
21+ return Field (FieldType .Goal , None )
22+
23+
24+ def _convert_field_array (field_array ) -> List [_socha .Field ]:
25+ return [_convert_field (field ) for field in field_array .field ]
26+
27+
28+ def _convert_segment (segment ) -> _socha .Segment :
29+ con_fields = [_convert_field_array (field_array )
30+ for field_array in segment .field_array ]
31+ con_center = _socha .CubeCoordinates (q = segment .center .q , r = segment .center .r )
32+ return _socha .Segment (direction = direction_from_string (segment .direction ), center = con_center , fields = con_fields )
33+
834
935def _convert_board (protocol_board : Board ) -> _socha .Board :
10- """
11- Converts a protocol Board to a usable game board for using in the logic.
12- :param protocol_board: A Board object in protocol format
13- :type protocol_board: Board
14- :return: A Board object in the format used by the game logic
15- :rtype: penguins.Board
16- """
17- con_segments : List [_socha .Segment ] = []
18- for segment in protocol_board .segment :
19- con_fields : List [List [_socha .Field ]] = []
20- for field_array in segment .field_array :
21- con_row : List [_socha .Field ] = []
22- for field in field_array .field :
23- if isinstance (field , Water ):
24- con_row .append (Field (FieldType .Water , None ))
25- elif isinstance (field , Sandbank ):
26- con_row .append (Field (FieldType .Sandbank , None ))
27- elif isinstance (field , Island ):
28- con_row .append (Field (FieldType .Island , None ))
29- elif isinstance (field , Passenger ):
30- con_row .append (Field (
31- FieldType .Passenger , _socha .Passenger (direction_from_string (field .direction ), field .passenger )))
32- elif isinstance (field , Goal ):
33- con_row .append (Field (FieldType .Goal , None ))
34- con_fields .append (con_row )
35- con_center : _socha .CubeCoordinates = CubeCoordinates (
36- q = segment .center .q , r = segment .center .r )
37- con_segments .append (_socha .Segment (direction = direction_from_string (
38- segment .direction ), center = con_center , fields = con_fields ))
36+ con_segments = [_convert_segment (segment )
37+ for segment in protocol_board .segment ]
3938 return _socha .Board (
4039 segments = con_segments ,
4140 next_direction = direction_from_string (protocol_board .next_direction )
@@ -106,7 +105,7 @@ def _merge_advances(actions):
106105 return new_actions
107106
108107
109- def if_last_game_state (message , last_game_state ):
108+ def if_last_game_state (message : Room , last_game_state ):
110109 try :
111110 last_game_state .board = _convert_board (
112111 message .data .class_binding .board )
@@ -120,27 +119,18 @@ def if_last_game_state(message, last_game_state):
120119 return last_game_state .perform_move (last_move )
121120 except Exception as e :
122121 logging .warning (
123- f"An error occurred: { e } . Returning the last game state without changes." )
124- return last_game_state
125-
126-
127- def if_not_last_game_state (message ) -> GameState :
128- first_position = CubeCoordinates (
129- q = message .data .class_binding .ship [0 ].position .q , r = message .data .class_binding .ship [0 ].position .r )
130- first_team = TeamEnum .One if message .data .class_binding .ship [
131- 0 ].team == "ONE" else TeamEnum .Two
132- first_team = _socha .Ship (position = first_position , team = first_team )
133-
134- second_position = CubeCoordinates (
135- q = message .data .class_binding .ship [1 ].position .q , r = message .data .class_binding .ship [1 ].position .r )
136- second_team = TeamEnum .One if message .data .class_binding .ship [
137- 1 ].team == "ONE" else TeamEnum .Two
138- second_team = _socha .Ship (position = second_position , team = second_team )
139-
140- return GameState (
141- board = _convert_board (message .data .class_binding .board ),
142- turn = message .data .class_binding .turn ,
143- current_ship = first_team ,
144- other_ship = second_team ,
145- last_move = None ,
146- )
122+ f"An error occurred: { e } ." )
123+ return if_not_last_game_state (message )
124+
125+
126+ def if_not_last_game_state (message : Room ) -> GameState :
127+ ships = [
128+ _socha .Ship (position = CubeCoordinates (q = ship .position .q , r = ship .position .r ),
129+ team = TeamEnum .One if ship .team == "ONE" else TeamEnum .Two )
130+ for ship in message .data .class_binding .ship
131+ ]
132+ return GameState (board = _convert_board (message .data .class_binding .board ),
133+ turn = message .data .class_binding .turn ,
134+ current_ship = ships [0 ],
135+ other_ship = ships [1 ],
136+ last_move = None )
0 commit comments