Skip to content

Commit 7ad5daa

Browse files
committed
fix(hui): protocol and logic issues
1 parent 13ff013 commit 7ad5daa

24 files changed

+955
-345
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "_socha"
3-
version = "3.2.1"
3+
version = "3.3.0"
44
edition = "2021"
55

66
[lib]

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ build-backend = "maturin"
44

55
[project]
66
name = "socha"
7-
version = "3.2.1"
7+
version = "3.3.0"
88
authors = [{ name = "maxblan", email = "[email protected]" }]
9-
description = "Dies ist das Paket für die Software-Challenge Germany 2025. In dieser Saison wird das Spiel 'Hase und Igel' sein."
9+
description = "Dieses Paket ist für die Software-Challenge Germany 2025, bei der in dieser Saison das Spiel 'Hase und Igel' im Mittelpunkt steht."
1010
readme = "README.md"
1111
requires-python = ">=3.10"
1212
dependencies = ["xsdata==22.9"]

python/socha/_socha.pyi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from enum import Enum
22
from typing import List, Optional
33

4-
class Card:
4+
class Card(Enum):
55
FallBack: int = 0
66
HurryAhead: int = 1
77
EatSalad: int = 2
88
SwapCarrots: int = 3
99

1010
def __init__(self) -> None: ...
1111
def moves(self) -> bool: ...
12-
def play(self, state: GameState) -> None: ...
12+
def perform(self, state: GameState) -> None: ...
1313

1414
class Advance:
1515
distance: int
@@ -29,6 +29,7 @@ class ExchangeCarrots:
2929
def perform(self, state: GameState) -> None: ...
3030

3131
class FallBack:
32+
def __init__(self) -> None: ...
3233
def perform(self, state: GameState) -> None: ...
3334

3435
class Field(Enum):
@@ -62,7 +63,7 @@ class Hare:
6263
position: int
6364
salads: int
6465
carrots: int
65-
salad_eaten: bool
66+
last_move: Optional[Move]
6667
cards: List[Card]
6768

6869
def __init__(
@@ -71,14 +72,14 @@ class Hare:
7172
cards: Optional[List[Card]] = None,
7273
carrots: Optional[int] = None,
7374
salads: Optional[int] = None,
74-
salad_eaten: Optional[bool] = None,
75+
last_move: Optional[Move] = None,
7576
position: Optional[int] = None,
7677
) -> None: ...
7778
def is_in_goal(self) -> bool: ...
7879
def can_enter_goal(self) -> bool: ...
7980
def advance_by(self, state: GameState, distance: int) -> None: ...
8081
def exchange_carrots(self, state: GameState, carrots: int) -> None: ...
81-
def consume_carrots(self, carrots: int) -> None: ...
82+
def consume_carrots(self, state: GameState, carrots: int) -> None: ...
8283
def eat_salad(self, state: GameState) -> None: ...
8384
def move_to_field(self, state: GameState, new_position: int) -> None: ...
8485
def get_fall_back(self, state: GameState) -> Optional[int]: ...
@@ -103,9 +104,8 @@ class GameState:
103104
) -> None: ...
104105
def perform_move(self, move: Move) -> GameState: ...
105106
def clone_current_player(self) -> Hare: ...
106-
def update_current_player(self, player: Hare) -> None: ...
107107
def clone_other_player(self) -> Hare: ...
108-
def update_other_player(self, player: Hare) -> None: ...
108+
def update_player(self, player: Hare) -> None: ...
109109

110110
class RulesEngine:
111111
@staticmethod

python/socha/api/networking/utils.py

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import re
12
from typing import List
23
from socha import _socha
34
from socha.api.protocol.protocol import (
45
Board,
5-
Cards,
66
Room,
7-
ScPlugin2025Hare,
7+
Hare,
88
State,
99
Data,
1010
)
@@ -20,13 +20,15 @@ def map_board(protocol_board: Board) -> _socha.Board:
2020
"""
2121
track: List[_socha.Field] = []
2222

23-
for field in protocol_board.fields:
23+
for field in protocol_board.field_value:
2424
if field == 'START':
2525
track.append(_socha.Field.Start)
2626
elif field == 'MARKET':
2727
track.append(_socha.Field.Market)
2828
elif field == 'HARE':
2929
track.append(_socha.Field.Hare)
30+
elif field == 'HEDGEHOG':
31+
track.append(_socha.Field.Hedgehog)
3032
elif field == 'CARROTS':
3133
track.append(_socha.Field.Carrots)
3234
elif field == 'POSITION_1':
@@ -37,47 +39,47 @@ def map_board(protocol_board: Board) -> _socha.Board:
3739
track.append(_socha.Field.Salad)
3840
elif field == 'GOAL':
3941
track.append(_socha.Field.Goal)
42+
else:
43+
raise ValueError(f'Unknown field type: {field}')
4044

4145
return _socha.Board(track=track)
4246

4347

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:
7577
if isinstance(move_response.action, _socha.Advance):
7678
advance: _socha.Advance = move_response.action
7779
return Data(
7880
class_value='advance',
7981
distance=advance.distance,
80-
cards=map_card_to_xml(advance.cards),
82+
card=[map_card_to_string(card) for card in advance.cards],
8183
)
8284
elif isinstance(move_response.action, _socha.EatSalad):
8385
return Data(class_value='eatsalad')
@@ -86,6 +88,8 @@ def handle_move(move_response: _socha.Move):
8688
return Data(class_value='exchangecarrots', value=exchangeCarrots.amount)
8789
elif isinstance(move_response.action, _socha.FallBack):
8890
return Data(class_value='fallback')
91+
else:
92+
raise ValueError(f'Unknown move response action: {move_response.action}')
8993

9094

9195
def message_to_state(message: Room) -> _socha.GameState:
@@ -101,19 +105,23 @@ def message_to_state(message: Room) -> _socha.GameState:
101105
"""
102106
state: State = message.data.class_binding
103107

104-
def create_hare(hare: ScPlugin2025Hare) -> _socha.Hare:
108+
def create_hare(hare: Hare) -> _socha.Hare:
105109
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 [],
107113
carrots=hare.carrots,
108114
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,
110118
salads=hare.salads,
111119
team=_socha.TeamEnum.One if hare.team == 'ONE' else _socha.TeamEnum.Two,
112120
)
113121

114122
return _socha.GameState(
115123
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]),
118126
turn=state.turn,
119127
)

0 commit comments

Comments
 (0)