1313shared_lock = Lock () # Create a Lock for synchronization
1414shared_number_of_connections = manager .Value ('i' , 0 )
1515
16-
17- class GameHandler (pb2_grpc .GameServicer ):
18- def __init__ (self ):
16+ class GrpcAgent :
17+ def __init__ (self , agent_type , uniform_number ) -> None :
18+ self .agent_type : pb2 .AgentType = agent_type
19+ self .uniform_number : int = uniform_number
1920 self .server_params : Union [pb2 .ServerParam , None ] = None
2021 self .player_params : Union [pb2 .PlayerParam , None ] = None
2122 self .player_types : dict [int , pb2 .PlayerType ] = {}
2223 self .debug_mode : bool = False
23-
24- def GetPlayerActions (self , state : pb2 .State , context ):
25- logging .debug (f"GetPlayerActions unum { state .register_response .uniform_number } at { state .world_model .cycle } " )
24+
25+ def GetAction (self , state : pb2 .State ):
26+ if self .agent_type == pb2 .AgentType .Player :
27+ return self .GetPlayerActions (state )
28+ elif self .agent_type == pb2 .AgentType .Coach :
29+ return self .GetCoachActions (state )
30+ elif self .agent_type == pb2 .AgentType .Trainer :
31+ return self .GetTrainerActions (state )
32+
33+ def GetPlayerActions (self , state : pb2 .State ):
2634 actions = []
2735 if state .world_model .game_mode_type == pb2 .GameModeType .PlayOn :
2836 if state .world_model .self .is_goalie :
@@ -41,19 +49,14 @@ def GetPlayerActions(self, state: pb2.State, context):
4149 actions .append (pb2 .PlayerAction (helios_basic_move = pb2 .HeliosBasicMove ()))
4250 else :
4351 actions .append (pb2 .PlayerAction (helios_set_play = pb2 .HeliosSetPlay ()))
44-
45- res = pb2 .PlayerActions (actions = actions )
46- logging .debug (f"GetPlayerActions Done unum { actions } " )
47- return res
48-
49- def GetCoachActions (self , state : pb2 .State , context ):
50- logging .debug (f"GetCoachActions coach at { state .world_model .cycle } " )
52+ return pb2 .PlayerActions (actions = actions )
53+
54+ def GetCoachActions (self , state : pb2 .State ):
5155 actions = []
5256 actions .append (pb2 .CoachAction (do_helios_substitute = pb2 .DoHeliosSubstitute ()))
5357 return pb2 .CoachActions (actions = actions )
54-
55- def GetTrainerActions (self , state : pb2 .State , context ):
56- logging .debug (f"GetTrainerActions trainer at { state .world_model .cycle } " )
58+
59+ def GetTrainerActions (self , state : pb2 .State ):
5760 actions = []
5861 actions .append (
5962 pb2 .TrainerAction (
@@ -70,28 +73,48 @@ def GetTrainerActions(self, state: pb2.State, context):
7073 )
7174 )
7275 return pb2 .TrainerActions (actions = actions )
76+
77+ class GameHandler (pb2_grpc .GameServicer ):
78+ def __init__ (self ):
79+ self .agents : dict [int , GrpcAgent ] = {}
80+
81+ def GetPlayerActions (self , state : pb2 .State , context ):
82+ logging .debug (f"GetPlayerActions unum { state .register_response .uniform_number } at { state .world_model .cycle } " )
83+ res = self .agents [state .register_response .client_id ].GetAction (state )
84+ logging .debug (f"GetPlayerActions Done unum { res } " )
85+ return res
86+
87+ def GetCoachActions (self , state : pb2 .State , context ):
88+ logging .debug (f"GetCoachActions coach at { state .world_model .cycle } " )
89+ res = self .agents [state .register_response .client_id ].GetAction (state )
90+ return res
91+
92+ def GetTrainerActions (self , state : pb2 .State , context ):
93+ logging .debug (f"GetTrainerActions trainer at { state .world_model .cycle } " )
94+ res = self .agents [state .register_response .client_id ].GetAction (state )
95+ return res
7396
7497 def SendServerParams (self , serverParams : pb2 .ServerParam , context ):
7598 logging .debug (f"Server params received unum { serverParams .register_response .uniform_number } " )
76- self .server_params = serverParams
99+ self .agents [ serverParams . register_response . client_id ]. server_params = serverParams
77100 res = pb2 .Empty ()
78101 return res
79102
80103 def SendPlayerParams (self , playerParams : pb2 .PlayerParam , context ):
81104 logging .debug (f"Player params received unum { playerParams .register_response .uniform_number } " )
82- self .player_params = playerParams
105+ self .agents [ playerParams . register_response . client_id ]. player_params = playerParams
83106 res = pb2 .Empty ()
84107 return res
85108
86109 def SendPlayerType (self , playerType : pb2 .PlayerType , context ):
87110 logging .debug (f"Player type received unum { playerType .register_response .uniform_number } " )
88- self .player_types [playerType .id ] = playerType
111+ self .agents [ playerType . register_response . client_id ]. player_types [playerType .id ] = playerType
89112 res = pb2 .Empty ()
90113 return res
91114
92115 def SendInitMessage (self , initMessage : pb2 .InitMessage , context ):
93116 logging .debug (f"Init message received unum { initMessage .register_response .uniform_number } " )
94- self .debug_mode = initMessage .debug_mode
117+ self .agents [ initMessage . register_response . client_id ]. debug_mode = initMessage .debug_mode
95118 res = pb2 .Empty ()
96119 return res
97120
@@ -105,6 +128,7 @@ def Register(self, register_request: pb2.RegisterRequest, context):
105128 team_name = register_request .team_name
106129 uniform_number = register_request .uniform_number
107130 agent_type = register_request .agent_type
131+ self .agents .append (shared_number_of_connections .value , GrpcAgent (agent_type , unuiform_number ))
108132 res = pb2 .RegisterResponse (client_id = shared_number_of_connections .value ,
109133 team_name = team_name ,
110134 uniform_number = uniform_number ,
@@ -114,7 +138,8 @@ def Register(self, register_request: pb2.RegisterRequest, context):
114138 def SendByeCommand (self , register_response : pb2 .RegisterResponse ):
115139 logging .debug (f"Bye command received unum { register_response .uniform_number } " )
116140 with shared_lock :
117- shared_number_of_connections .value -= 1
141+ self .agents .pop (register_response .client_id )
142+
118143 res = pb2 .Empty ()
119144 return res
120145
0 commit comments