#!/usr/bin/env python3

# Project: VUT FIT SUI Project - Dice Wars
# Authors:
#   - Josef Kolář      <xkolar71@stud.fit.vutbr.cz>
#   - Dominik Harmim   <xharmi00@stud.fit.vutbr.cz>
#   - Petr Kapoun      <xkapou04@stud.fit.vutbr.cz>
#   - Jindřich Šesták  <xsesta05@stud.fit.vutbr.cz>
# Year: 2020
# Description: Generates game configurations.

import random
import sys
from argparse import ArgumentParser
import time
from signal import signal, SIGCHLD

from utils import run_ai_only_game, BoardDefinition

parser = ArgumentParser(prog='Dice_Wars')
parser.add_argument('-p', '--port', help="Server port", type=int, default=5005)
parser.add_argument('-a', '--address', help="Server address", default='127.0.0.1')

procs = []


def signal_handler():
    """ Handler for SIGCHLD signal that terminates server and clients. """
    for p in procs:
        try:
            p.kill()
        except ProcessLookupError:
            pass


PLAYING_AIs = [
    'xkolar71_orig',
    'xkolar71_2',
    'xkolar71_3',
    'xkolar71_4',
]


def board_definitions():
    while True:
        random.seed(int(time.time()))
        yield BoardDefinition(random.randint(1, 10 ** 10), random.randint(1, 10 ** 10), random.randint(1, 10 ** 10))


def main():
    args = parser.parse_args()

    signal(SIGCHLD, signal_handler)

    boards_played = 0
    try:
        for board_definition in board_definitions():
            boards_played += 1

            run_ai_only_game(
                args.port, args.address, procs, PLAYING_AIs,
                board_definition,
                fixed=random.randint(1, 10 ** 10),
                client_seed=random.randint(1, 10 ** 10),
                debug=True, logdir='logs',
            )
            print(f'Played {boards_played} games.', file=sys.stderr)

    except (Exception, KeyboardInterrupt) as e:
        sys.stderr.write("Breaking the tournament because of {}\n".format(repr(e)))
        for p in procs:
            p.kill()
        raise


if __name__ == '__main__':
    main()

[((20, 9, 20, 41), 'argparse.ArgumentParser', 'ArgumentParser', (), '', False, 'from argparse import ArgumentParser\n'), ((53, 4, 53, 35), 'signal.signal', 'signal', ({(53, 11, 53, 18): 'SIGCHLD', (53, 20, 53, 34): 'signal_handler'}, {}), '(SIGCHLD, signal_handler)', False, 'from signal import signal, SIGCHLD\n'), ((46, 24, 46, 35), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n'), ((47, 30, 47, 57), 'random.randint', 'random.randint', ({(47, 45, 47, 46): '(1)', (47, 48, 47, 56): '(10 ** 10)'}, {}), '(1, 10 ** 10)', False, 'import random\n'), ((47, 59, 47, 86), 'random.randint', 'random.randint', ({(47, 74, 47, 75): '(1)', (47, 77, 47, 85): '(10 ** 10)'}, {}), '(1, 10 ** 10)', False, 'import random\n'), ((47, 88, 47, 115), 'random.randint', 'random.randint', ({(47, 103, 47, 104): '(1)', (47, 106, 47, 114): '(10 ** 10)'}, {}), '(1, 10 ** 10)', False, 'import random\n'), ((63, 22, 63, 49), 'random.randint', 'random.randint', ({(63, 37, 63, 38): '(1)', (63, 40, 63, 48): '(10 ** 10)'}, {}), '(1, 10 ** 10)', False, 'import random\n'), ((64, 28, 64, 55), 'random.randint', 'random.randint', ({(64, 43, 64, 44): '(1)', (64, 46, 64, 54): '(10 ** 10)'}, {}), '(1, 10 ** 10)', False, 'import random\n')]