|
| 1 | +from concurrent import futures |
| 2 | +from time import sleep |
| 3 | +import service_pb2_grpc as pb2_grpc |
| 4 | +import service_pb2 as pb2 |
| 5 | +from typing import Union |
| 6 | +from multiprocessing import Manager, Lock |
| 7 | +import logging |
| 8 | +import grpc |
| 9 | + |
| 10 | +logging.basicConfig(level=logging.DEBUG) |
| 11 | + |
| 12 | +manager = Manager() |
| 13 | +shared_lock = Lock() # Create a Lock for synchronization |
| 14 | +shared_number_of_connections = manager.Value('i', 0) |
| 15 | + |
| 16 | + |
| 17 | +class GameHandler(pb2_grpc.GameServicer): |
| 18 | + def __init__(self): |
| 19 | + self.server_params: Union[pb2.ServerParam, None] = None |
| 20 | + self.player_params: Union[pb2.PlayerParam, None] = None |
| 21 | + self.player_types: dict[int, pb2.PlayerType] = {} |
| 22 | + 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}") |
| 26 | + actions = [] |
| 27 | + if state.world_model.game_mode_type == pb2.GameModeType.PlayOn: |
| 28 | + if state.world_model.self.is_goalie: |
| 29 | + actions.append(pb2.PlayerAction(helios_goalie=pb2.HeliosGoalie())) |
| 30 | + elif state.world_model.self.is_kickable: |
| 31 | + actions.append(pb2.PlayerAction(helios_chain_action=pb2.HeliosChainAction(lead_pass=True, |
| 32 | + direct_pass=True, |
| 33 | + through_pass=True, |
| 34 | + simple_pass=True, |
| 35 | + short_dribble=True, |
| 36 | + long_dribble=True, |
| 37 | + simple_shoot=True, |
| 38 | + simple_dribble=True, |
| 39 | + cross=True))) |
| 40 | + else: |
| 41 | + actions.append(pb2.PlayerAction(helios_basic_move=pb2.HeliosBasicMove())) |
| 42 | + else: |
| 43 | + 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}") |
| 51 | + actions = [] |
| 52 | + actions.append(pb2.CoachAction(do_helios_substitute=pb2.DoHeliosSubstitute())) |
| 53 | + 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}") |
| 57 | + actions = [] |
| 58 | + actions.append( |
| 59 | + pb2.TrainerAction( |
| 60 | + do_move_ball=pb2.DoMoveBall( |
| 61 | + position=pb2.RpcVector2D( |
| 62 | + x=0, |
| 63 | + y=0 |
| 64 | + ), |
| 65 | + velocity=pb2.RpcVector2D( |
| 66 | + x=0, |
| 67 | + y=0 |
| 68 | + ), |
| 69 | + ) |
| 70 | + ) |
| 71 | + ) |
| 72 | + return pb2.TrainerActions(actions=actions) |
| 73 | + |
| 74 | + def SendServerParams(self, serverParams: pb2.ServerParam, context): |
| 75 | + logging.debug(f"Server params received unum {serverParams.register_response.uniform_number}") |
| 76 | + self.server_params = serverParams |
| 77 | + res = pb2.Empty() |
| 78 | + # logging.debug(f"Server params Done unum {serverParams.register_response.uniform_number}") |
| 79 | + return res |
| 80 | + |
| 81 | + def SendPlayerParams(self, playerParams: pb2.PlayerParam, context): |
| 82 | + logging.debug(f"Player params received unum {playerParams.register_response.uniform_number}") |
| 83 | + self.player_params = playerParams |
| 84 | + res = pb2.Empty() |
| 85 | + return res |
| 86 | + |
| 87 | + def SendPlayerType(self, playerType: pb2.PlayerType, context): |
| 88 | + logging.debug(f"Player type received unum {playerType.register_response.uniform_number}") |
| 89 | + self.player_types[playerType.id] = playerType |
| 90 | + res = pb2.Empty() |
| 91 | + return res |
| 92 | + |
| 93 | + def SendInitMessage(self, initMessage: pb2.InitMessage, context): |
| 94 | + logging.debug(f"Init message received unum {initMessage.register_response.uniform_number}") |
| 95 | + self.debug_mode = initMessage.debug_mode |
| 96 | + res = pb2.Empty() |
| 97 | + return res |
| 98 | + |
| 99 | + def Register(self, register_request: pb2.RegisterRequest, context): |
| 100 | + logging.debug(f"received register request from team_name: {register_request.team_name} " |
| 101 | + f"unum: {register_request.uniform_number} " |
| 102 | + f"agent_type: {register_request.agent_type}") |
| 103 | + with shared_lock: |
| 104 | + shared_number_of_connections.value += 1 |
| 105 | + logging.debug(f"Number of connections {shared_number_of_connections.value}") |
| 106 | + team_name = register_request.team_name |
| 107 | + uniform_number = register_request.uniform_number |
| 108 | + agent_type = register_request.agent_type |
| 109 | + res = pb2.RegisterResponse(client_id=shared_number_of_connections.value, |
| 110 | + team_name=team_name, |
| 111 | + uniform_number=uniform_number, |
| 112 | + agent_type=agent_type) |
| 113 | + return res |
| 114 | + |
| 115 | + def SendByeCommand(self, register_response: pb2.RegisterResponse): |
| 116 | + logging.debug(f"Bye command received unum {register_response.uniform_number}") |
| 117 | + with shared_lock: |
| 118 | + shared_number_of_connections.value -= 1 |
| 119 | + res = pb2.Empty() |
| 120 | + return res |
| 121 | + |
| 122 | +def serve(port): |
| 123 | + server = grpc.server(futures.ThreadPoolExecutor(max_workers=22)) |
| 124 | + game_service = GameHandler() |
| 125 | + pb2_grpc.add_GameServicer_to_server(game_service, server) |
| 126 | + server.add_insecure_port(f'[::]:{port}') |
| 127 | + server.start() |
| 128 | + logging.info(f"Starting server on port {port}") |
| 129 | + |
| 130 | + server.wait_for_termination() |
| 131 | + |
| 132 | + # while True: |
| 133 | + # with shared_lock: |
| 134 | + # logging.debug(f"Waiting for connections {shared_number_of_connections.value}") |
| 135 | + # if shared_number_of_connections.value != 0: |
| 136 | + # break |
| 137 | + # sleep(1) |
| 138 | + |
| 139 | +# def serve(port): |
| 140 | +# handler = GameHandler() |
| 141 | +# processor = Game.Processor(handler) |
| 142 | +# transport = TSocket.TServerSocket(host='0.0.0.0', port=port) |
| 143 | +# tfactory = TTransport.TBufferedTransportFactory() |
| 144 | +# pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 145 | + |
| 146 | +# server = PFProcessServer(processor, transport, tfactory, pfactory) |
| 147 | +# # server = TThreadedServer(processor, transport, tfactory, pfactory) |
| 148 | + |
| 149 | +# logging.info(f"Starting server on port {port}") |
| 150 | +# try: |
| 151 | +# server.serve() |
| 152 | +# except KeyboardInterrupt: |
| 153 | +# server.stop() |
| 154 | +# print("Stopping server") |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == '__main__': |
| 158 | + serve(50051) |
0 commit comments