Exemplo n.º 1
0
def parse_header(header):
    game_state = GameState()
    base_featureset = FeatureSet()

    for line in header.split("\n"):
        line = line.strip()
        if line.startswith("info,"):
            try:
                _, key, value = csv_split(line)
            except Exception:
                logging.error("Choked on line: %s" % line)
                raise

            if key in ["visteam", "hometeam"]:
                setattr(GameState, key, value)

            fs_key = "game_%s" % key
            if fs_key in FeatureSet.__slots__:
                setattr(FeatureSet, fs_key, value)

    return game_state, base_featureset
Exemplo n.º 2
0
def main():
    input_queue = queue.Queue()

    input_thread = threading.Thread(target=add_input, args=(input_queue, ))
    input_thread.daemon = True
    input_thread.start()

    outs, ins = [], []

    turn = 0
    total_turns = 0
    prev_turn = -1

    pygame.key.set_repeat(500, 30)

    if len(sys.argv) > 1 and sys.argv[1] == '-s':
        d = os.path.dirname(os.path.realpath(__file__))
        fname = os.path.join(d, "logs", "commands_{:.0f}".format(time.time()))
        cf = open(fname, "w")
    else:
        cf = None

    global sz
    while True:
        while not input_queue.empty():
            line = input_queue.get()
            if line.startswith("->"):
                if cf is not None:
                    cf.write(line)
                outs.append(parse_line(line[3:].strip()))
            if line.startswith("<-"):
                if cf is not None:
                    cf.write(line)
                ins.append(parse_line(line[3:].strip()))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    sz += 1
                if event.key == pygame.K_2 and sz > 1:
                    sz -= 1
                if event.key == pygame.K_o:
                    if turn > 1:
                        turn -= 1
                if event.key == pygame.K_p:
                    if turn + 1 < total_turns:
                        turn += 1

        total_turns = min(len(ins), len(outs))
        if total_turns == 0:
            continue

        try:
            gs = GameState(ins[turn])
        except:
            if turn == 0 and total_turns > 1:
                turn += 1
                gs = GameState(ins[turn])

        screen.fill(0)
        if turn != prev_turn:
            print("=" * 20)
            print("turn {}/{}".format(turn, total_turns))
            print("sent -> {}".format(outs[turn]))
            print("got  <- {}".format(ins[turn]))
            prev_turn = turn

        last_text_pos_y = 40
        for sh in gs.ships:
            if sh.player_type == gs.my_type:
                color = (0, 192, 0)
            else:
                color = (192, 0, 0)

            screen_pos = to_screen(sh.pos)
            if sh.player_type == DEFENDER_ID:
                pygame.draw.circle(screen, color, screen_pos, 15, 2)
            else:
                pygame.draw.rect(
                    screen, color,
                    (screen_pos[0] - 15, screen_pos[1] - 15, 30, 30), 2)
            pygame.draw.line(screen, color, screen_pos,
                             to_screen(sh.pos + sh.speed), 2)

            screen.blit(font.render(str(sh.ship_id), 1, color),
                        (screen_pos[0] - 35, screen_pos[1] - 12))
            screen.blit(
                small_font.render(
                    "{}+{}".format(sh.pos, sh.speed).replace(" ", ""), 1,
                    color), (screen_pos[0] + 20, screen_pos[1] - 6))

            text = '(id:{}, energy:{}, shoot_energy:{}, rest:{}, health:{}, tired:{}/{})'.format(
                sh.ship_id, sh.energy, sh.shoot_energy, sh.rest, sh.health,
                sh.tiredness, sh.tiredness_limit)

            for m in sh.prev_moves:
                text += "  " + str(m)
                if m.move_type == 2:
                    target_pos = to_screen(m.pos())
                    pygame.draw.line(screen, color, screen_pos, target_pos,
                                     1 + m.args[2] // 10)

            text_info = font_ship_info.render(text, 1, color)
            screen.blit(text_info, (10, last_text_pos_y))
            last_text_pos_y += 24

        border_color = (200, 200, 200)
        draw_centered_rect(border_color, gs.world_size, 2)
        draw_centered_rect(border_color, gs.planet_size, 0)  # fill

        tt = font.render(
            'turn {}/{}; sz = {}; finished = {}'.format(
                turn, total_turns, sz, gs.game_finished), 1, (192, 192, 192))
        # print(dir(tt.get_rect()))
        screen.blit(tt, (10, 5))

        pygame.display.update()