Skip to content

Commit

Permalink
fix(hui): protocol and logic issues
Browse files Browse the repository at this point in the history
  • Loading branch information
maxblan committed Jun 25, 2024
1 parent 13ff013 commit 7ad5daa
Show file tree
Hide file tree
Showing 24 changed files with 955 additions and 345 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "_socha"
version = "3.2.1"
version = "3.3.0"
edition = "2021"

[lib]
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ build-backend = "maturin"

[project]
name = "socha"
version = "3.2.1"
version = "3.3.0"
authors = [{ name = "maxblan", email = "[email protected]" }]
description = "Dies ist das Paket für die Software-Challenge Germany 2025. In dieser Saison wird das Spiel 'Hase und Igel' sein."
description = "Dieses Paket ist für die Software-Challenge Germany 2025, bei der in dieser Saison das Spiel 'Hase und Igel' im Mittelpunkt steht."
readme = "README.md"
requires-python = ">=3.10"
dependencies = ["xsdata==22.9"]
Expand Down
14 changes: 7 additions & 7 deletions python/socha/_socha.pyi
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from enum import Enum
from typing import List, Optional

class Card:
class Card(Enum):
FallBack: int = 0
HurryAhead: int = 1
EatSalad: int = 2
SwapCarrots: int = 3

def __init__(self) -> None: ...
def moves(self) -> bool: ...
def play(self, state: GameState) -> None: ...
def perform(self, state: GameState) -> None: ...

class Advance:
distance: int
Expand All @@ -29,6 +29,7 @@ class ExchangeCarrots:
def perform(self, state: GameState) -> None: ...

class FallBack:
def __init__(self) -> None: ...
def perform(self, state: GameState) -> None: ...

class Field(Enum):
Expand Down Expand Up @@ -62,7 +63,7 @@ class Hare:
position: int
salads: int
carrots: int
salad_eaten: bool
last_move: Optional[Move]
cards: List[Card]

def __init__(
Expand All @@ -71,14 +72,14 @@ class Hare:
cards: Optional[List[Card]] = None,
carrots: Optional[int] = None,
salads: Optional[int] = None,
salad_eaten: Optional[bool] = None,
last_move: Optional[Move] = None,
position: Optional[int] = None,
) -> None: ...
def is_in_goal(self) -> bool: ...
def can_enter_goal(self) -> bool: ...
def advance_by(self, state: GameState, distance: int) -> None: ...
def exchange_carrots(self, state: GameState, carrots: int) -> None: ...
def consume_carrots(self, carrots: int) -> None: ...
def consume_carrots(self, state: GameState, carrots: int) -> None: ...
def eat_salad(self, state: GameState) -> None: ...
def move_to_field(self, state: GameState, new_position: int) -> None: ...
def get_fall_back(self, state: GameState) -> Optional[int]: ...
Expand All @@ -103,9 +104,8 @@ class GameState:
) -> None: ...
def perform_move(self, move: Move) -> GameState: ...
def clone_current_player(self) -> Hare: ...
def update_current_player(self, player: Hare) -> None: ...
def clone_other_player(self) -> Hare: ...
def update_other_player(self, player: Hare) -> None: ...
def update_player(self, player: Hare) -> None: ...

class RulesEngine:
@staticmethod
Expand Down
88 changes: 48 additions & 40 deletions python/socha/api/networking/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import re
from typing import List
from socha import _socha
from socha.api.protocol.protocol import (
Board,
Cards,
Room,
ScPlugin2025Hare,
Hare,
State,
Data,
)
Expand All @@ -20,13 +20,15 @@ def map_board(protocol_board: Board) -> _socha.Board:
"""
track: List[_socha.Field] = []

for field in protocol_board.fields:
for field in protocol_board.field_value:
if field == 'START':
track.append(_socha.Field.Start)
elif field == 'MARKET':
track.append(_socha.Field.Market)
elif field == 'HARE':
track.append(_socha.Field.Hare)
elif field == 'HEDGEHOG':
track.append(_socha.Field.Hedgehog)
elif field == 'CARROTS':
track.append(_socha.Field.Carrots)
elif field == 'POSITION_1':
Expand All @@ -37,47 +39,47 @@ def map_board(protocol_board: Board) -> _socha.Board:
track.append(_socha.Field.Salad)
elif field == 'GOAL':
track.append(_socha.Field.Goal)
else:
raise ValueError(f'Unknown field type: {field}')

return _socha.Board(track=track)


def map_xml_to_card(cards: Cards) -> List[_socha.Card]:
return_cards: List[_socha.Card] = []
for card in cards.card:
if card == 'EAT_SALAD':
return_cards.append(_socha.Card.EatSalad)
elif card == 'HURRY_AHEAD':
return_cards.append(_socha.Card.HurryAhead)
elif card == 'FALL_BACK':
return_cards.append(_socha.Card.FallBack)
elif card == 'SWAP_CARROTS':
return_cards.append(_socha.Card.SwapCarrots)
return return_cards


def map_card_to_xml(cards: List[_socha.Card]) -> None | Cards:
if not cards:
return None
return_cards: Cards = Cards()
for card in cards:
if card == _socha.Card.EatSalad:
return_cards.sc_plugin2025_card.append('EAT_SALAD')
elif card == _socha.Card.FallBack:
return_cards.sc_plugin2025_card.append('FALL_BACK')
elif card == _socha.Card.HurryAhead:
return_cards.sc_plugin2025_card.append('HURRY_AHEAD')
elif card == _socha.Card.SwapCarrots:
return_cards.sc_plugin2025_card.append('SWAP_CARROTS')
return return_cards


def handle_move(move_response: _socha.Move):
def map_card_to_string(card: _socha.Card) -> str:
if card == _socha.Card.EatSalad:
return 'EAT_SALAD'
elif card == _socha.Card.HurryAhead:
return 'HURRY_AHEAD'
elif card == _socha.Card.FallBack:
return 'FALL_BACK'
elif card == _socha.Card.SwapCarrots:
return 'SWAP_CARROTS'
else:
raise ValueError(f'Unknown card type: {card}')


def map_string_to_card(card: str) -> _socha.Card:
card = re.sub(r'[^A-Za-z0-9_]', '', card)

if card == 'EAT_SALAD':
return _socha.Card.EatSalad
elif card == 'HURRY_AHEAD':
return _socha.Card.HurryAhead
elif card == 'FALL_BACK':
return _socha.Card.FallBack
elif card == 'SWAP_CARROTS':
return _socha.Card.SwapCarrots
else:
raise ValueError(f'Unknown card type: {card}')


def handle_move(move_response: _socha.Move) -> Data:
if isinstance(move_response.action, _socha.Advance):
advance: _socha.Advance = move_response.action
return Data(
class_value='advance',
distance=advance.distance,
cards=map_card_to_xml(advance.cards),
card=[map_card_to_string(card) for card in advance.cards],
)
elif isinstance(move_response.action, _socha.EatSalad):
return Data(class_value='eatsalad')
Expand All @@ -86,6 +88,8 @@ def handle_move(move_response: _socha.Move):
return Data(class_value='exchangecarrots', value=exchangeCarrots.amount)
elif isinstance(move_response.action, _socha.FallBack):
return Data(class_value='fallback')
else:
raise ValueError(f'Unknown move response action: {move_response.action}')


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

def create_hare(hare: ScPlugin2025Hare) -> _socha.Hare:
def create_hare(hare: Hare) -> _socha.Hare:
return _socha.Hare(
cards=map_xml_to_card(cards=hare.cards),
cards=[map_string_to_card(card) for card in hare.cards.card]
if hare.cards
else [],
carrots=hare.carrots,
position=hare.position,
salad_eaten=hare.salad_eaten,
last_move=_socha.Move(action=hare.last_action.class_binding)
if hare.last_action
else None,
salads=hare.salads,
team=_socha.TeamEnum.One if hare.team == 'ONE' else _socha.TeamEnum.Two,
)

return _socha.GameState(
board=map_board(state.board),
player_one=create_hare(state.sc_plugin2025_hare[0]),
player_two=create_hare(state.sc_plugin2025_hare[1]),
player_one=create_hare(state.hare[0]),
player_two=create_hare(state.hare[1]),
turn=state.turn,
)
Loading

0 comments on commit 7ad5daa

Please sign in to comment.