1
+ import re
1
2
from typing import List
2
3
from socha import _socha
3
4
from socha .api .protocol .protocol import (
4
5
Board ,
5
- Cards ,
6
6
Room ,
7
- ScPlugin2025Hare ,
7
+ Hare ,
8
8
State ,
9
9
Data ,
10
10
)
@@ -20,13 +20,15 @@ def map_board(protocol_board: Board) -> _socha.Board:
20
20
"""
21
21
track : List [_socha .Field ] = []
22
22
23
- for field in protocol_board .fields :
23
+ for field in protocol_board .field_value :
24
24
if field == 'START' :
25
25
track .append (_socha .Field .Start )
26
26
elif field == 'MARKET' :
27
27
track .append (_socha .Field .Market )
28
28
elif field == 'HARE' :
29
29
track .append (_socha .Field .Hare )
30
+ elif field == 'HEDGEHOG' :
31
+ track .append (_socha .Field .Hedgehog )
30
32
elif field == 'CARROTS' :
31
33
track .append (_socha .Field .Carrots )
32
34
elif field == 'POSITION_1' :
@@ -37,47 +39,47 @@ def map_board(protocol_board: Board) -> _socha.Board:
37
39
track .append (_socha .Field .Salad )
38
40
elif field == 'GOAL' :
39
41
track .append (_socha .Field .Goal )
42
+ else :
43
+ raise ValueError (f'Unknown field type: { field } ' )
40
44
41
45
return _socha .Board (track = track )
42
46
43
47
44
- def map_xml_to_card (cards : Cards ) -> List [_socha .Card ]:
45
- return_cards : List [_socha .Card ] = []
46
- for card in cards .card :
47
- if card == 'EAT_SALAD' :
48
- return_cards .append (_socha .Card .EatSalad )
49
- elif card == 'HURRY_AHEAD' :
50
- return_cards .append (_socha .Card .HurryAhead )
51
- elif card == 'FALL_BACK' :
52
- return_cards .append (_socha .Card .FallBack )
53
- elif card == 'SWAP_CARROTS' :
54
- return_cards .append (_socha .Card .SwapCarrots )
55
- return return_cards
56
-
57
-
58
- def map_card_to_xml (cards : List [_socha .Card ]) -> None | Cards :
59
- if not cards :
60
- return None
61
- return_cards : Cards = Cards ()
62
- for card in cards :
63
- if card == _socha .Card .EatSalad :
64
- return_cards .sc_plugin2025_card .append ('EAT_SALAD' )
65
- elif card == _socha .Card .FallBack :
66
- return_cards .sc_plugin2025_card .append ('FALL_BACK' )
67
- elif card == _socha .Card .HurryAhead :
68
- return_cards .sc_plugin2025_card .append ('HURRY_AHEAD' )
69
- elif card == _socha .Card .SwapCarrots :
70
- return_cards .sc_plugin2025_card .append ('SWAP_CARROTS' )
71
- return return_cards
72
-
73
-
74
- def handle_move (move_response : _socha .Move ):
48
+ def map_card_to_string (card : _socha .Card ) -> str :
49
+ if card == _socha .Card .EatSalad :
50
+ return 'EAT_SALAD'
51
+ elif card == _socha .Card .HurryAhead :
52
+ return 'HURRY_AHEAD'
53
+ elif card == _socha .Card .FallBack :
54
+ return 'FALL_BACK'
55
+ elif card == _socha .Card .SwapCarrots :
56
+ return 'SWAP_CARROTS'
57
+ else :
58
+ raise ValueError (f'Unknown card type: { card } ' )
59
+
60
+
61
+ def map_string_to_card (card : str ) -> _socha .Card :
62
+ card = re .sub (r'[^A-Za-z0-9_]' , '' , card )
63
+
64
+ if card == 'EAT_SALAD' :
65
+ return _socha .Card .EatSalad
66
+ elif card == 'HURRY_AHEAD' :
67
+ return _socha .Card .HurryAhead
68
+ elif card == 'FALL_BACK' :
69
+ return _socha .Card .FallBack
70
+ elif card == 'SWAP_CARROTS' :
71
+ return _socha .Card .SwapCarrots
72
+ else :
73
+ raise ValueError (f'Unknown card type: { card } ' )
74
+
75
+
76
+ def handle_move (move_response : _socha .Move ) -> Data :
75
77
if isinstance (move_response .action , _socha .Advance ):
76
78
advance : _socha .Advance = move_response .action
77
79
return Data (
78
80
class_value = 'advance' ,
79
81
distance = advance .distance ,
80
- cards = map_card_to_xml ( advance .cards ) ,
82
+ card = [ map_card_to_string ( card ) for card in advance .cards ] ,
81
83
)
82
84
elif isinstance (move_response .action , _socha .EatSalad ):
83
85
return Data (class_value = 'eatsalad' )
@@ -86,6 +88,8 @@ def handle_move(move_response: _socha.Move):
86
88
return Data (class_value = 'exchangecarrots' , value = exchangeCarrots .amount )
87
89
elif isinstance (move_response .action , _socha .FallBack ):
88
90
return Data (class_value = 'fallback' )
91
+ else :
92
+ raise ValueError (f'Unknown move response action: { move_response .action } ' )
89
93
90
94
91
95
def message_to_state (message : Room ) -> _socha .GameState :
@@ -101,19 +105,23 @@ def message_to_state(message: Room) -> _socha.GameState:
101
105
"""
102
106
state : State = message .data .class_binding
103
107
104
- def create_hare (hare : ScPlugin2025Hare ) -> _socha .Hare :
108
+ def create_hare (hare : Hare ) -> _socha .Hare :
105
109
return _socha .Hare (
106
- cards = map_xml_to_card (cards = hare .cards ),
110
+ cards = [map_string_to_card (card ) for card in hare .cards .card ]
111
+ if hare .cards
112
+ else [],
107
113
carrots = hare .carrots ,
108
114
position = hare .position ,
109
- salad_eaten = hare .salad_eaten ,
115
+ last_move = _socha .Move (action = hare .last_action .class_binding )
116
+ if hare .last_action
117
+ else None ,
110
118
salads = hare .salads ,
111
119
team = _socha .TeamEnum .One if hare .team == 'ONE' else _socha .TeamEnum .Two ,
112
120
)
113
121
114
122
return _socha .GameState (
115
123
board = map_board (state .board ),
116
- player_one = create_hare (state .sc_plugin2025_hare [0 ]),
117
- player_two = create_hare (state .sc_plugin2025_hare [1 ]),
124
+ player_one = create_hare (state .hare [0 ]),
125
+ player_two = create_hare (state .hare [1 ]),
118
126
turn = state .turn ,
119
127
)
0 commit comments