Esempio n. 1
0
def run_game(game, dockers, args, sock_file):
    '''
    This contains the logic that needs to be cleaned up at the end of a game
    If there is something that needs to be cleaned up add it in the try catch
    loop surrounding the catch loop surrounding the call of the function
    '''

    # Start the unix stream server
    server.start_server(sock_file, game, dockers)

    if args['use_viewer']:
        viewer_server = server.start_viewer_server(PORT, game)

    # Start the docker instances
    for player_key in dockers:
        docker_inst = dockers[player_key]
        docker_inst.start()
        for player_ in game.players:
            if player_['id'] == player_key:
                player = player_['player']
                break
        if player.planet == bc.Planet.Earth:
            planet = 'earth'
        else:
            planet = 'mars'
        if player.team == bc.Team.Blue:
            team = 'blue'
        else:
            team = 'red'
        name = f'[{planet}:{team}]'
        logger = Logger(name)
        docker_inst.stream_logs(line_action=logger)
        player_['logger'] = logger

    # Wait until all the code is done then clean up
    while not game.game_over:
        time.sleep(1)

    print(game.disconnected)
    print("Dumping matchfile")
    match_ptr = open("/player/" + str(args['replay_filename']), mode='w')
    match_file = {}
    match_file['message'] = game.viewer_messages
    if not game.disconnected:
        if bc.Team.Red == game.manager.winning_team():
            winner = 'player1'
        else:
            winner = 'player2'
    else:
        winner = game.winner


    match_file['metadata'] = {'player1': args['dir_p1'][8:],
            'player2' : args['dir_p2'][8:], 'winner': winner}
    json.dump(match_file, match_ptr)
    match_ptr.close()
    if args['use_viewer']:
        viewer_server.shutdown()

    return winner
Esempio n. 2
0
def run_game(game, dockers, args, sock_file, scrimmage=False):
    '''
    This contains the logic that needs to be cleaned up at the end of a game
    If there is something that needs to be cleaned up add it in the try catch
    loop surrounding the catch loop surrounding the call of the function
    '''

    # Start the unix stream server
    main_server = server.start_server(sock_file, game, dockers)

    viewer_server = server.start_viewer_server(
        PORT, game) if args['use_viewer'] else None

    try:
        # Start the docker instances
        for player_key in dockers:
            docker_inst = dockers[player_key]
            docker_inst.start()
            for player_ in game.players:
                if player_['id'] == player_key:
                    player = player_['player']
                    break
            if player.planet == bc.Planet.Earth:
                planet = 'earth'
            else:
                planet = 'mars'
            if player.team == bc.Team.Blue:
                team = 'blue'
            else:
                team = 'red'

            name = '[{}:{}]'.format(planet, team)
            logger = Logger(name,
                            print=(not args['terminal_viewer']
                                   and not scrimmage))
            docker_inst.stream_logs(line_action=logger)
            player_['logger'] = logger

        # Wait until all the code is done then clean up
        while not game.game_over:
            time.sleep(0.1)

    finally:
        main_server.shutdown()
        try:
            main_server.server_close()
        except e:
            print(e)

        if viewer_server is not None:
            viewer_server.shutdown()

    match_file = {}
    match_file['message'] = game.viewer_messages
    if not game.disconnected:
        if bc.Team.Red == game.manager.winning_team():
            winner = 'player1'
        else:
            winner = 'player2'
    else:
        winner = game.winner

    match_file['metadata'] = {
        'player1': 'player1' if scrimmage else args['dir_p1'][8:],
        'player2': 'player2' if scrimmage else args['dir_p2'][8:],
        'winner': winner
    }

    if args['docker']:
        match_output = abspath(
            os.path.join('/player', str(args['replay_filename'])))
    else:
        match_output = args['replay_filename']
        if not os.path.isabs(match_output):
            match_output = abspath(os.path.join('..', str(match_output)))

    if not scrimmage:
        print("Saving replay to", match_output)

        match_ptr = open(match_output, 'w')
        json.dump(match_file, match_ptr)
        match_ptr.close()

        return winner
    else:
        return winner, match_file
def run_game(game, dockers, args, sock_file):
    '''
    This contains the logic that needs to be cleaned up at the end of a game
    If there is something that needs to be cleaned up add it in the try catch
    loop surrounding the catch loop surrounding the call of the function
    '''

    # Start the unix stream server
    main_server = server.start_server(sock_file, game, dockers)

    viewer_server = server.start_viewer_server(PORT, game) if args['use_viewer'] else None

    # Start the docker instances
    for player_key in dockers:
        docker_inst = dockers[player_key]
        docker_inst.start()
        for player_ in game.players:
            if player_['id'] == player_key:
                player = player_['player']
                break
        if player.planet == bc.Planet.Earth:
            planet = 'earth'
        else:
            planet = 'mars'
        if player.team == bc.Team.Blue:
            team = 'blue'
        else:
            team = 'red'
        name = f'[{planet}:{team}]'
        logger = Logger(name)
        docker_inst.stream_logs(line_action=logger)
        player_['logger'] = logger

    # Wait until all the code is done then clean up
    while not game.game_over:
        time.sleep(0.1)

    print('Killing game server.')
    main_server.shutdown()
    try:
        main_server.server_close()
    except e:
        print(e)

    match_file = {}
    match_file['message'] = game.viewer_messages
    if not game.disconnected:
        if bc.Team.Red == game.manager.winning_team():
            winner = 'player1'
        else:
            winner = 'player2'
    else:
        winner = game.winner

    match_file['metadata'] = {
        'player1': args['dir_p1'][8:],
        'player2': args['dir_p2'][8:],
        'winner': winner
    }

    if args['docker']:
        match_output = abspath(os.path.join('/player', str(args['replay_filename'])))
    else:
        match_output = args['replay_filename']
        if not os.path.isabs(match_output):
            match_output = abspath(os.path.join('..', str(match_output)))

    print("Dumping matchfile to", match_output)
    match_ptr = open(match_output, 'w')
    json.dump(match_file, match_ptr)
    match_ptr.close()
    if viewer_server is not None:
        viewer_server.shutdown()

    return winner