Skip to content

Commit 5ad6460

Browse files
committed
support new version of grpc for server side planner
1 parent adb4eb6 commit 5ad6460

File tree

5 files changed

+330
-79
lines changed

5 files changed

+330
-79
lines changed

idl/service.proto

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// version 0
2+
13
syntax = "proto3";
24

35
package protos;
@@ -758,6 +760,7 @@ message HeliosChainAction {
758760
bool simple_pass = 7;
759761
bool simple_dribble = 8;
760762
bool simple_shoot = 9;
763+
bool server_side_decision = 10;
761764
}
762765

763766
message HeliosBasicOffensive {}
@@ -836,6 +839,7 @@ message PlayerAction {
836839
HeliosSetPlay helios_set_play = 62;
837840
HeliosPenalty helios_penalty = 63;
838841
HeliosCommunicaion helios_communication = 64;
842+
839843
}
840844
}
841845

@@ -1202,6 +1206,59 @@ message PlayerType {
12021206
float player_speed_max = 34;
12031207
}
12041208

1209+
enum RpcActionCategory {
1210+
AC_Hold = 0;
1211+
AC_Dribble = 1;
1212+
AC_Pass = 2;
1213+
AC_Shoot = 3;
1214+
AC_Clear = 4;
1215+
AC_Move = 5;
1216+
AC_NoAction = 6;
1217+
}
1218+
message RpcCooperativeAction {
1219+
RpcActionCategory category = 1;
1220+
int32 index = 2;
1221+
int32 sender_unum = 3;
1222+
int32 target_unum = 4;
1223+
RpcVector2D target_point = 5;
1224+
double first_ball_speed = 6;
1225+
double first_turn_moment = 7;
1226+
double first_dash_power = 8;
1227+
double first_dash_angle_relative = 9;
1228+
int32 duration_step = 10;
1229+
int32 kick_count = 11;
1230+
int32 turn_count = 12;
1231+
int32 dash_count = 13;
1232+
bool final_action = 14;
1233+
string description = 15;
1234+
int32 parent_index = 16;
1235+
}
1236+
1237+
message RpcPredictState {
1238+
int32 spend_time = 1;
1239+
int32 ball_holder_unum = 2;
1240+
RpcVector2D ball_position = 3;
1241+
RpcVector2D ball_velocity = 4;
1242+
double our_defense_line_x = 5;
1243+
double our_offense_line_x = 6;
1244+
}
1245+
1246+
message RpcActionState {
1247+
RpcCooperativeAction action = 1;
1248+
RpcPredictState predict_state = 2;
1249+
double evaluation = 3;
1250+
}
1251+
1252+
message BestPlannerActionRequest {
1253+
RegisterResponse register_response = 1;
1254+
map<int32, RpcActionState> pairs = 2;
1255+
State state = 3;
1256+
}
1257+
1258+
message BestPlannerActionResponse {
1259+
int32 index = 1;
1260+
}
1261+
12051262
message Empty {
12061263
}
12071264

@@ -1215,4 +1272,5 @@ service Game {
12151272
rpc SendPlayerType(PlayerType) returns (Empty) {} //should be PlayerTypes
12161273
rpc Register(RegisterRequest) returns (RegisterResponse) {}
12171274
rpc SendByeCommand(RegisterResponse) returns (Empty) {}
1275+
rpc GetBestPlannerAction(BestPlannerActionRequest) returns (BestPlannerActionResponse) {}
12181276
}

server.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,23 @@
99
import argparse
1010

1111

12-
logging.basicConfig(level=logging.DEBUG)
12+
logging.basicConfig(level=logging.INFO)
13+
#put log into file as well as console
14+
# Create a custom logger
15+
logger = logging.getLogger('my_logger')
16+
logger.setLevel(logging.DEBUG) # Set the minimum logging level
1317

18+
# Create handlers: one for console, one for file
19+
console_handler = logging.StreamHandler()
20+
file_handler = logging.FileHandler('logfile.log')
21+
22+
# Set logging level for each handler
23+
console_handler.setLevel(logging.ERROR)
24+
file_handler.setLevel(logging.INFO)
25+
26+
# Add the handlers to the logger
27+
logger.addHandler(console_handler)
28+
logger.addHandler(file_handler)
1429

1530
class GrpcAgent:
1631
def __init__(self, agent_type, uniform_number) -> None:
@@ -40,9 +55,10 @@ def GetPlayerActions(self, state: pb2.State):
4055
through_pass=True,
4156
simple_pass=True,
4257
short_dribble=True,
43-
long_dribble=True,
58+
long_dribble=False,
4459
simple_shoot=True,
4560
simple_dribble=True,
61+
server_side_decision=True,
4662
cross=True)))
4763
else:
4864
actions.append(pb2.PlayerAction(helios_basic_move=pb2.HeliosBasicMove()))
@@ -125,15 +141,15 @@ def Register(self, register_request: pb2.RegisterRequest, context):
125141
f"agent_type: {register_request.agent_type}")
126142
with self.shared_lock:
127143
self.shared_number_of_connections.value += 1
128-
logging.debug(f"Number of connections {self.shared_number_of_connections.value}")
129-
team_name = register_request.team_name
130-
uniform_number = register_request.uniform_number
131-
agent_type = register_request.agent_type
132-
self.agents[self.shared_number_of_connections.value] = GrpcAgent(agent_type, uniform_number)
133-
res = pb2.RegisterResponse(client_id=self.shared_number_of_connections.value,
134-
team_name=team_name,
135-
uniform_number=uniform_number,
136-
agent_type=agent_type)
144+
logging.debug(f"Number of connections {self.shared_number_of_connections.value}")
145+
team_name = register_request.team_name
146+
uniform_number = register_request.uniform_number
147+
agent_type = register_request.agent_type
148+
self.agents[self.shared_number_of_connections.value] = GrpcAgent(agent_type, uniform_number)
149+
res = pb2.RegisterResponse(client_id=self.shared_number_of_connections.value,
150+
team_name=team_name,
151+
uniform_number=uniform_number,
152+
agent_type=agent_type)
137153
return res
138154

139155
def SendByeCommand(self, register_response: pb2.RegisterResponse, context):
@@ -143,6 +159,22 @@ def SendByeCommand(self, register_response: pb2.RegisterResponse, context):
143159

144160
res = pb2.Empty()
145161
return res
162+
163+
def GetBestPlannerAction(self, pairs: pb2.BestPlannerActionRequest, context):
164+
logger.error(f"GetBestPlannerAction cycle:{pairs.state.world_model.cycle} pairs:{len(pairs.pairs)} unum:{pairs.state.register_response.uniform_number}")
165+
pairs_list: list[int, pb2.RpcActionState] = [(k, v) for k, v in pairs.pairs.items()]
166+
pairs_list.sort(key=lambda x: x[0])
167+
168+
# for p in pairs_list:
169+
# pair: pb2.RpcActionStatePair = p[1]
170+
# logger.info(f"i:{p[0]} p:{pair.action.parent_index} {p[1].action.description} to {p[1].action.target_unum} in ({round(p[1].action.target_point.x, 2)},{round(p[1].action.target_point.y, 2)}) e:{round(p[1].evaluation,2)}")
171+
172+
best_action = max(pairs_list, key=lambda x: -1000 if x[1].action.parent_index != -1 else x[1].predict_state.ball_position.x)
173+
174+
logger.error(f"Best action: {best_action[0]} {best_action[1].action.description} to {best_action[1].action.target_unum} in ({round(best_action[1].action.target_point.x, 2)},{round(best_action[1].action.target_point.y, 2)}) e:{round(best_action[1].evaluation,2)}")
175+
176+
res = pb2.BestPlannerActionResponse(index=best_action[0])
177+
return res
146178

147179
def serve(port, shared_lock, shared_number_of_connections):
148180
server = grpc.server(futures.ThreadPoolExecutor(max_workers=22))

service_pb2.py

Lines changed: 80 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service_pb2.pyi

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ class AgentType(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
9191
PlayerT: _ClassVar[AgentType]
9292
CoachT: _ClassVar[AgentType]
9393
TrainerT: _ClassVar[AgentType]
94+
95+
class RpcActionCategory(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
96+
__slots__ = ()
97+
AC_Hold: _ClassVar[RpcActionCategory]
98+
AC_Dribble: _ClassVar[RpcActionCategory]
99+
AC_Pass: _ClassVar[RpcActionCategory]
100+
AC_Shoot: _ClassVar[RpcActionCategory]
101+
AC_Clear: _ClassVar[RpcActionCategory]
102+
AC_Move: _ClassVar[RpcActionCategory]
103+
AC_NoAction: _ClassVar[RpcActionCategory]
94104
NARROW: ViewWidth
95105
NORMAL: ViewWidth
96106
WIDE: ViewWidth
@@ -159,6 +169,13 @@ MODE_MAX: GameModeType
159169
PlayerT: AgentType
160170
CoachT: AgentType
161171
TrainerT: AgentType
172+
AC_Hold: RpcActionCategory
173+
AC_Dribble: RpcActionCategory
174+
AC_Pass: RpcActionCategory
175+
AC_Shoot: RpcActionCategory
176+
AC_Clear: RpcActionCategory
177+
AC_Move: RpcActionCategory
178+
AC_NoAction: RpcActionCategory
162179

163180
class RpcVector2D(_message.Message):
164181
__slots__ = ("x", "y", "dist", "angle")
@@ -1280,7 +1297,7 @@ class HeliosShoot(_message.Message):
12801297
def __init__(self) -> None: ...
12811298

12821299
class HeliosChainAction(_message.Message):
1283-
__slots__ = ("direct_pass", "lead_pass", "through_pass", "short_dribble", "long_dribble", "cross", "simple_pass", "simple_dribble", "simple_shoot")
1300+
__slots__ = ("direct_pass", "lead_pass", "through_pass", "short_dribble", "long_dribble", "cross", "simple_pass", "simple_dribble", "simple_shoot", "server_side_decision")
12841301
DIRECT_PASS_FIELD_NUMBER: _ClassVar[int]
12851302
LEAD_PASS_FIELD_NUMBER: _ClassVar[int]
12861303
THROUGH_PASS_FIELD_NUMBER: _ClassVar[int]
@@ -1290,6 +1307,7 @@ class HeliosChainAction(_message.Message):
12901307
SIMPLE_PASS_FIELD_NUMBER: _ClassVar[int]
12911308
SIMPLE_DRIBBLE_FIELD_NUMBER: _ClassVar[int]
12921309
SIMPLE_SHOOT_FIELD_NUMBER: _ClassVar[int]
1310+
SERVER_SIDE_DECISION_FIELD_NUMBER: _ClassVar[int]
12931311
direct_pass: bool
12941312
lead_pass: bool
12951313
through_pass: bool
@@ -1299,7 +1317,8 @@ class HeliosChainAction(_message.Message):
12991317
simple_pass: bool
13001318
simple_dribble: bool
13011319
simple_shoot: bool
1302-
def __init__(self, direct_pass: bool = ..., lead_pass: bool = ..., through_pass: bool = ..., short_dribble: bool = ..., long_dribble: bool = ..., cross: bool = ..., simple_pass: bool = ..., simple_dribble: bool = ..., simple_shoot: bool = ...) -> None: ...
1320+
server_side_decision: bool
1321+
def __init__(self, direct_pass: bool = ..., lead_pass: bool = ..., through_pass: bool = ..., short_dribble: bool = ..., long_dribble: bool = ..., cross: bool = ..., simple_pass: bool = ..., simple_dribble: bool = ..., simple_shoot: bool = ..., server_side_decision: bool = ...) -> None: ...
13031322

13041323
class HeliosBasicOffensive(_message.Message):
13051324
__slots__ = ()
@@ -2145,6 +2164,91 @@ class PlayerType(_message.Message):
21452164
player_speed_max: float
21462165
def __init__(self, register_response: _Optional[_Union[RegisterResponse, _Mapping]] = ..., id: _Optional[int] = ..., stamina_inc_max: _Optional[float] = ..., player_decay: _Optional[float] = ..., inertia_moment: _Optional[float] = ..., dash_power_rate: _Optional[float] = ..., player_size: _Optional[float] = ..., kickable_margin: _Optional[float] = ..., kick_rand: _Optional[float] = ..., extra_stamina: _Optional[float] = ..., effort_max: _Optional[float] = ..., effort_min: _Optional[float] = ..., kick_power_rate: _Optional[float] = ..., foul_detect_probability: _Optional[float] = ..., catchable_area_l_stretch: _Optional[float] = ..., unum_far_length: _Optional[float] = ..., unum_too_far_length: _Optional[float] = ..., team_far_length: _Optional[float] = ..., team_too_far_length: _Optional[float] = ..., player_max_observation_length: _Optional[float] = ..., ball_vel_far_length: _Optional[float] = ..., ball_vel_too_far_length: _Optional[float] = ..., ball_max_observation_length: _Optional[float] = ..., flag_chg_far_length: _Optional[float] = ..., flag_chg_too_far_length: _Optional[float] = ..., flag_max_observation_length: _Optional[float] = ..., kickable_area: _Optional[float] = ..., reliable_catchable_dist: _Optional[float] = ..., max_catchable_dist: _Optional[float] = ..., real_speed_max: _Optional[float] = ..., player_speed_max2: _Optional[float] = ..., real_speed_max2: _Optional[float] = ..., cycles_to_reach_max_speed: _Optional[int] = ..., player_speed_max: _Optional[float] = ...) -> None: ...
21472166

2167+
class RpcCooperativeAction(_message.Message):
2168+
__slots__ = ("category", "index", "sender_unum", "target_unum", "target_point", "first_ball_speed", "first_turn_moment", "first_dash_power", "first_dash_angle_relative", "duration_step", "kick_count", "turn_count", "dash_count", "final_action", "description", "parent_index")
2169+
CATEGORY_FIELD_NUMBER: _ClassVar[int]
2170+
INDEX_FIELD_NUMBER: _ClassVar[int]
2171+
SENDER_UNUM_FIELD_NUMBER: _ClassVar[int]
2172+
TARGET_UNUM_FIELD_NUMBER: _ClassVar[int]
2173+
TARGET_POINT_FIELD_NUMBER: _ClassVar[int]
2174+
FIRST_BALL_SPEED_FIELD_NUMBER: _ClassVar[int]
2175+
FIRST_TURN_MOMENT_FIELD_NUMBER: _ClassVar[int]
2176+
FIRST_DASH_POWER_FIELD_NUMBER: _ClassVar[int]
2177+
FIRST_DASH_ANGLE_RELATIVE_FIELD_NUMBER: _ClassVar[int]
2178+
DURATION_STEP_FIELD_NUMBER: _ClassVar[int]
2179+
KICK_COUNT_FIELD_NUMBER: _ClassVar[int]
2180+
TURN_COUNT_FIELD_NUMBER: _ClassVar[int]
2181+
DASH_COUNT_FIELD_NUMBER: _ClassVar[int]
2182+
FINAL_ACTION_FIELD_NUMBER: _ClassVar[int]
2183+
DESCRIPTION_FIELD_NUMBER: _ClassVar[int]
2184+
PARENT_INDEX_FIELD_NUMBER: _ClassVar[int]
2185+
category: RpcActionCategory
2186+
index: int
2187+
sender_unum: int
2188+
target_unum: int
2189+
target_point: RpcVector2D
2190+
first_ball_speed: float
2191+
first_turn_moment: float
2192+
first_dash_power: float
2193+
first_dash_angle_relative: float
2194+
duration_step: int
2195+
kick_count: int
2196+
turn_count: int
2197+
dash_count: int
2198+
final_action: bool
2199+
description: str
2200+
parent_index: int
2201+
def __init__(self, category: _Optional[_Union[RpcActionCategory, str]] = ..., index: _Optional[int] = ..., sender_unum: _Optional[int] = ..., target_unum: _Optional[int] = ..., target_point: _Optional[_Union[RpcVector2D, _Mapping]] = ..., first_ball_speed: _Optional[float] = ..., first_turn_moment: _Optional[float] = ..., first_dash_power: _Optional[float] = ..., first_dash_angle_relative: _Optional[float] = ..., duration_step: _Optional[int] = ..., kick_count: _Optional[int] = ..., turn_count: _Optional[int] = ..., dash_count: _Optional[int] = ..., final_action: bool = ..., description: _Optional[str] = ..., parent_index: _Optional[int] = ...) -> None: ...
2202+
2203+
class RpcPredictState(_message.Message):
2204+
__slots__ = ("spend_time", "ball_holder_unum", "ball_position", "ball_velocity", "our_defense_line_x", "our_offense_line_x")
2205+
SPEND_TIME_FIELD_NUMBER: _ClassVar[int]
2206+
BALL_HOLDER_UNUM_FIELD_NUMBER: _ClassVar[int]
2207+
BALL_POSITION_FIELD_NUMBER: _ClassVar[int]
2208+
BALL_VELOCITY_FIELD_NUMBER: _ClassVar[int]
2209+
OUR_DEFENSE_LINE_X_FIELD_NUMBER: _ClassVar[int]
2210+
OUR_OFFENSE_LINE_X_FIELD_NUMBER: _ClassVar[int]
2211+
spend_time: int
2212+
ball_holder_unum: int
2213+
ball_position: RpcVector2D
2214+
ball_velocity: RpcVector2D
2215+
our_defense_line_x: float
2216+
our_offense_line_x: float
2217+
def __init__(self, spend_time: _Optional[int] = ..., ball_holder_unum: _Optional[int] = ..., ball_position: _Optional[_Union[RpcVector2D, _Mapping]] = ..., ball_velocity: _Optional[_Union[RpcVector2D, _Mapping]] = ..., our_defense_line_x: _Optional[float] = ..., our_offense_line_x: _Optional[float] = ...) -> None: ...
2218+
2219+
class RpcActionState(_message.Message):
2220+
__slots__ = ("action", "predict_state", "evaluation")
2221+
ACTION_FIELD_NUMBER: _ClassVar[int]
2222+
PREDICT_STATE_FIELD_NUMBER: _ClassVar[int]
2223+
EVALUATION_FIELD_NUMBER: _ClassVar[int]
2224+
action: RpcCooperativeAction
2225+
predict_state: RpcPredictState
2226+
evaluation: float
2227+
def __init__(self, action: _Optional[_Union[RpcCooperativeAction, _Mapping]] = ..., predict_state: _Optional[_Union[RpcPredictState, _Mapping]] = ..., evaluation: _Optional[float] = ...) -> None: ...
2228+
2229+
class BestPlannerActionRequest(_message.Message):
2230+
__slots__ = ("register_response", "pairs", "state")
2231+
class PairsEntry(_message.Message):
2232+
__slots__ = ("key", "value")
2233+
KEY_FIELD_NUMBER: _ClassVar[int]
2234+
VALUE_FIELD_NUMBER: _ClassVar[int]
2235+
key: int
2236+
value: RpcActionState
2237+
def __init__(self, key: _Optional[int] = ..., value: _Optional[_Union[RpcActionState, _Mapping]] = ...) -> None: ...
2238+
REGISTER_RESPONSE_FIELD_NUMBER: _ClassVar[int]
2239+
PAIRS_FIELD_NUMBER: _ClassVar[int]
2240+
STATE_FIELD_NUMBER: _ClassVar[int]
2241+
register_response: RegisterResponse
2242+
pairs: _containers.MessageMap[int, RpcActionState]
2243+
state: State
2244+
def __init__(self, register_response: _Optional[_Union[RegisterResponse, _Mapping]] = ..., pairs: _Optional[_Mapping[int, RpcActionState]] = ..., state: _Optional[_Union[State, _Mapping]] = ...) -> None: ...
2245+
2246+
class BestPlannerActionResponse(_message.Message):
2247+
__slots__ = ("index",)
2248+
INDEX_FIELD_NUMBER: _ClassVar[int]
2249+
index: int
2250+
def __init__(self, index: _Optional[int] = ...) -> None: ...
2251+
21482252
class Empty(_message.Message):
21492253
__slots__ = ()
21502254
def __init__(self) -> None: ...

service_pb2_grpc.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ def __init__(self, channel):
8484
request_serializer=service__pb2.RegisterResponse.SerializeToString,
8585
response_deserializer=service__pb2.Empty.FromString,
8686
_registered_method=True)
87+
self.GetBestPlannerAction = channel.unary_unary(
88+
'/protos.Game/GetBestPlannerAction',
89+
request_serializer=service__pb2.BestPlannerActionRequest.SerializeToString,
90+
response_deserializer=service__pb2.BestPlannerActionResponse.FromString,
91+
_registered_method=True)
8792

8893

8994
class GameServicer(object):
@@ -143,6 +148,12 @@ def SendByeCommand(self, request, context):
143148
context.set_details('Method not implemented!')
144149
raise NotImplementedError('Method not implemented!')
145150

151+
def GetBestPlannerAction(self, request, context):
152+
"""Missing associated documentation comment in .proto file."""
153+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
154+
context.set_details('Method not implemented!')
155+
raise NotImplementedError('Method not implemented!')
156+
146157

147158
def add_GameServicer_to_server(servicer, server):
148159
rpc_method_handlers = {
@@ -191,6 +202,11 @@ def add_GameServicer_to_server(servicer, server):
191202
request_deserializer=service__pb2.RegisterResponse.FromString,
192203
response_serializer=service__pb2.Empty.SerializeToString,
193204
),
205+
'GetBestPlannerAction': grpc.unary_unary_rpc_method_handler(
206+
servicer.GetBestPlannerAction,
207+
request_deserializer=service__pb2.BestPlannerActionRequest.FromString,
208+
response_serializer=service__pb2.BestPlannerActionResponse.SerializeToString,
209+
),
194210
}
195211
generic_handler = grpc.method_handlers_generic_handler(
196212
'protos.Game', rpc_method_handlers)
@@ -444,3 +460,30 @@ def SendByeCommand(request,
444460
timeout,
445461
metadata,
446462
_registered_method=True)
463+
464+
@staticmethod
465+
def GetBestPlannerAction(request,
466+
target,
467+
options=(),
468+
channel_credentials=None,
469+
call_credentials=None,
470+
insecure=False,
471+
compression=None,
472+
wait_for_ready=None,
473+
timeout=None,
474+
metadata=None):
475+
return grpc.experimental.unary_unary(
476+
request,
477+
target,
478+
'/protos.Game/GetBestPlannerAction',
479+
service__pb2.BestPlannerActionRequest.SerializeToString,
480+
service__pb2.BestPlannerActionResponse.FromString,
481+
options,
482+
channel_credentials,
483+
insecure,
484+
call_credentials,
485+
compression,
486+
wait_for_ready,
487+
timeout,
488+
metadata,
489+
_registered_method=True)

0 commit comments

Comments
 (0)