|
| 1 | +import socket |
| 2 | +import random |
| 3 | +from argparse import ArgumentParser |
| 4 | +from src.base.Message import * |
| 5 | +from src.World import * |
| 6 | +import src.ClientGreedy as c_greedy |
| 7 | +import src.ClientRandom as c_random |
| 8 | +import src.ClientBest as c_best |
| 9 | +import src.YourClient as c_your |
| 10 | +import signal |
| 11 | +is_run = True |
| 12 | + |
| 13 | + |
| 14 | +def signal_handler(sig, frame): |
| 15 | + global is_run |
| 16 | + print('You pressed Ctrl+C!') |
| 17 | + is_run = False |
| 18 | + |
| 19 | + |
| 20 | +signal.signal(signal.SIGINT, signal_handler) |
| 21 | + |
| 22 | + |
| 23 | +def run(): |
| 24 | + parser = ArgumentParser() |
| 25 | + parser.add_argument("-n", "--name", dest="name", type=str, default='team_name' + str(random.randint(0, 10000)), |
| 26 | + help="Client Name", metavar="NAME") |
| 27 | + parser.add_argument("-c", "--client", dest="client_type", type=str, default='auto', |
| 28 | + help="greedy, random, hand, best, your", metavar="ClientType") |
| 29 | + parser.add_argument("-p", "--port", dest="server_port", type=int, default=20002, |
| 30 | + help="server port", metavar="ServerPort") |
| 31 | + parser.add_argument("-s", "--server", dest="server_address", type=str, default='localhost', |
| 32 | + help="server address", metavar="ServerAddress") |
| 33 | + args = parser.parse_args() |
| 34 | + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 35 | + sock.settimeout(1) |
| 36 | + server_address = (args.server_address, args.server_port) |
| 37 | + world = World() |
| 38 | + message_snd = MessageClientConnectRequest(args.name).build() |
| 39 | + |
| 40 | + while is_run: |
| 41 | + sock.sendto(message_snd, server_address) |
| 42 | + try: |
| 43 | + message_rcv = sock.recvfrom(4096) |
| 44 | + except: |
| 45 | + continue |
| 46 | + message = parse(message_rcv[0]) |
| 47 | + if message.type == 'MessageClientConnectResponse': |
| 48 | + print('my id is ' + str(message.id)) |
| 49 | + world.set_id(message.id, message.ground_config['goal_id']) |
| 50 | + break |
| 51 | + |
| 52 | + while is_run: |
| 53 | + try: |
| 54 | + r = sock.recvfrom(4096) |
| 55 | + except: |
| 56 | + continue |
| 57 | + message = parse(r[0]) |
| 58 | + if message.type == 'MessageClientDisconnect': |
| 59 | + break |
| 60 | + elif message.type == 'MessageClientWorld': |
| 61 | + world.update(message) |
| 62 | + world.print() |
| 63 | + |
| 64 | + if args.client_type == 'greedy' or (args.client_type == 'auto' and world.self_id == 1): |
| 65 | + action = c_greedy.get_action(world) |
| 66 | + elif args.client_type == 'random' or (args.client_type == 'auto' and world.self_id >= 2): |
| 67 | + action = c_random.get_action(world) |
| 68 | + elif args.client_type == 'best': |
| 69 | + action = c_best.get_action(world) |
| 70 | + elif args.client_type == 'your': |
| 71 | + action = c_your.get_action(world) |
| 72 | + elif args.client_type == 'hand': |
| 73 | + action = input('enter action (u or d or l or r:') |
| 74 | + |
| 75 | + sock.sendto(MessageClientAction(string_action=action).build(), server_address) |
| 76 | + |
0 commit comments