Skip to content

Commit 3487d2d

Browse files
committed
update formation file
1 parent 0efa2a9 commit 3487d2d

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed

server.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from src.sample_coach_agent import SampleCoachAgent
1414
from src.sample_player_agent import SamplePlayerAgent
1515
from src.sample_trainer_agent import SampleTrainerAgent
16+
import traceback
1617

1718

1819
console_logging_level = logging.INFO
@@ -51,8 +52,7 @@ def GetAction(self, state: pb2.State):
5152
return self.GetTrainerActions(state)
5253
except Exception as e:
5354
self.logger.error(f"Error in GetAction: {e}")
54-
import traceback
55-
traceback.print_exc()
55+
self.logger.error(traceback.format_exc())
5656
return pb2.PlayerActions()
5757

5858
def GetPlayerActions(self, state: pb2.State):
@@ -81,16 +81,31 @@ def GetTrainerActions(self, state: pb2.State):
8181
return pb2.TrainerActions(actions=self.agent.get_actions())
8282

8383
def SetServerParams(self, server_params: pb2.ServerParam):
84-
self.logger.debug(f"Server params received unum {server_params.register_response.uniform_number}")
85-
self.agent.set_server_params(server_params)
84+
try:
85+
self.logger.debug(f"Server params received unum {server_params.register_response.uniform_number}")
86+
self.agent.set_server_params(server_params)
87+
except Exception as e:
88+
self.logger.error(f"Error in GetAction: {e}")
89+
self.logger.error(traceback.format_exc())
90+
return pb2.PlayerActions()
8691

8792
def SetPlayerParams(self, player_params: pb2.PlayerParam):
88-
self.logger.debug(f"Player params received unum {player_params.register_response.uniform_number}")
89-
self.agent.set_player_params(player_params)
93+
try:
94+
self.logger.debug(f"Player params received unum {player_params.register_response.uniform_number}")
95+
self.agent.set_player_params(player_params)
96+
except Exception as e:
97+
self.logger.error(f"Error in GetAction: {e}")
98+
self.logger.error(traceback.format_exc())
99+
return pb2.PlayerActions()
90100

91101
def SetPlayerType(self, player_type: pb2.PlayerType):
92-
self.logger.debug(f"Player type received unum {player_type.register_response.uniform_number}")
93-
self.agent.set_player_types(player_type)
102+
try:
103+
self.logger.debug(f"Player type received unum {player_type.register_response.uniform_number}")
104+
self.agent.set_player_types(player_type)
105+
except Exception as e:
106+
self.logger.error(f"Error in GetAction: {e}")
107+
self.logger.error(traceback.format_exc())
108+
return pb2.PlayerActions()
94109

95110
class GameHandler(pb2_grpc.GameServicer):
96111
def __init__(self, shared_lock, shared_number_of_connections) -> None:
@@ -158,8 +173,7 @@ def Register(self, register_request: pb2.RegisterRequest, context):
158173
return register_response
159174
except Exception as e:
160175
main_logger.error(f"Error in Register: {e}")
161-
import traceback
162-
traceback.print_exc()
176+
main_logger.error(traceback.format_exc())
163177
return pb2.RegisterResponse()
164178

165179
def SendByeCommand(self, register_response: pb2.RegisterResponse, context):

src/sample_player_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self, logger) -> None:
1010
super().__init__(logger)
1111
self.logger.info('SamplePlayerAgent created')
1212
self.decision_maker = DecisionMaker()
13-
self.strategy = FormationStrategy()
13+
self.strategy = FormationStrategy(self.logger)
1414
self.wm: WorldModel = None
1515

1616
def update_actions(self, wm:WorldModel):

src/strategy/formation_file.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,45 @@
22
from pyrusgeom.geom_2d import *
33
from enum import Enum
44
from pyrusgeom.soccer_math import min_max
5+
import logging
6+
from abc import ABC, abstractmethod
57

68
class FormationType(Enum):
79
Static = 's'
810
DelaunayTriangulation2 = 'D'
911

12+
class FormationIndexData:
13+
def __init__(self, ball, players):
14+
self._ball: list[float] = ball
15+
self._players: list[list[float]] = players
16+
17+
def ball(self) -> list[float]:
18+
return self._ball
19+
20+
def players(self) -> list[list[float]]:
21+
return self._players
22+
23+
class IFormationFileReader(ABC):
24+
@abstractmethod
25+
def read_file(self, path):
26+
pass
27+
28+
class OldStaticFormationFileReader(IFormationFileReader):
29+
def read_file(self, lines):
30+
players = []
31+
for i in range(len(lines)):
32+
if i == 0 or lines[i].startswith('#'):
33+
continue
34+
player = lines[i].split()
35+
players.append([float(player[2]), float(player[3])])
36+
37+
return FormationIndexData(None, players)
38+
39+
1040

1141
class FormationFile:
12-
def __init__(self, path):
42+
def __init__(self, path, logger: logging.Logger):
43+
self._logger = logger
1344
self._balls = []
1445
self._players = []
1546
self._triangles = []
@@ -25,7 +56,11 @@ def read_file(self, path):
2556
if lines[0].find('Static') < 0:
2657
self._formation_type = FormationType.DelaunayTriangulation2
2758
if self._formation_type == FormationType.Static:
28-
self.read_static(lines)
59+
data = OldStaticFormationFileReader().read_file(lines)
60+
players = data.players()
61+
# self._players = data.players()
62+
for i in range(11):
63+
self._target_players[i + 1] = Vector2D(float(players[i][0]), float(players[i][1]))
2964
else:
3065
self.read_delaunay(lines)
3166

src/strategy/formation_strategy.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,24 @@ class Situation(Enum):
1515
PenaltyKick_Situation = 4
1616

1717
class Formation:
18-
def __init__(self, path):
19-
self.before_kick_off_formation: FormationFile = FormationFile(f'{path}/before_kick_off.conf')
20-
self.defense_formation: FormationFile = FormationFile(f'{path}/defense_formation.conf')
21-
self.offense_formation: FormationFile = FormationFile(f'{path}/offense_formation.conf')
22-
self.goalie_kick_opp_formation: FormationFile = FormationFile(f'{path}/goalie_kick_opp_formation.conf')
23-
self.goalie_kick_our_formation: FormationFile = FormationFile(f'{path}/goalie_kick_our_formation.conf')
24-
self.kickin_our_formation: FormationFile = FormationFile(f'{path}/kickin_our_formation.conf')
25-
self.setplay_opp_formation: FormationFile = FormationFile(f'{path}/setplay_opp_formation.conf')
26-
self.setplay_our_formation: FormationFile = FormationFile(f'{path}/setplay_our_formation.conf')
18+
def __init__(self, path, logger: logging.Logger):
19+
self.before_kick_off_formation: FormationFile = FormationFile(f'{path}/before_kick_off.conf', logger)
20+
self.defense_formation: FormationFile = FormationFile(f'{path}/defense_formation.conf', logger)
21+
self.offense_formation: FormationFile = FormationFile(f'{path}/offense_formation.conf', logger)
22+
self.goalie_kick_opp_formation: FormationFile = FormationFile(f'{path}/goalie_kick_opp_formation.conf', logger)
23+
self.goalie_kick_our_formation: FormationFile = FormationFile(f'{path}/goalie_kick_our_formation.conf', logger)
24+
self.kickin_our_formation: FormationFile = FormationFile(f'{path}/kickin_our_formation.conf', logger)
25+
self.setplay_opp_formation: FormationFile = FormationFile(f'{path}/setplay_opp_formation.conf', logger)
26+
self.setplay_our_formation: FormationFile = FormationFile(f'{path}/setplay_our_formation.conf', logger)
2727

2828
class FormationStrategy(IPositionStrategy):
29-
def __init__(self):
29+
def __init__(self, logger: logging.Logger):
30+
self.logger = logger
3031
self.formations: dict[str, Formation] = {}
31-
self.formations['4-3-3'] = Formation('src/formations/4-3-3')
32-
self.selected_formation_name = '4-3-3'
32+
self.formations['4-3-3'] = Formation('src/formations/4-3-3', logger)
33+
# self.formations['4-3-3-cyrus-base'] = Formation('src/formations/4-3-3-cyrus-base')
34+
# self.selected_formation_name = '4-3-3-cyrus-base' # '4-3-3'
35+
self.selected_formation_name = '4-3-3' # '4-3-3'
3336

3437
self._poses: dict[int, Vector2D] = {(i, Vector2D(0, 0)) for i in range(11)}
3538
self.current_situation = Situation.Offense_Situation

0 commit comments

Comments
 (0)