def do_genstats(args, config):
    count = args.count
    size = args.size
    ships = ["AAAAA", "BBBB", "CCC", "DD", "DD", "SSS", "SSS"]
    # Create a board which contains a count of the number of time
    # a space was used.
    count_board = [[0] * size for i in range(size)]
    for i in range(0, count):
        layout = BoardLayout.generate(size=size, ships=ships)
        board = layout.render()
        for row in range(0, size):
            for col in range(0, size):
                if board[row][col] != '-':
                    count_board[row][col] += 1

    print("Percentages Board")
    print("-----------------")

    # Print the board of percentages.
    print("  ", end=' ')
    for i in range(0, size):
        print("  {}".format(chr(ord('A') + i)), end=' ')
    print()

    for row in range(0, size):
        print("%2d" % (row + 1), end=' ')
        for space in count_board[row]:
            print("%3.0f" % (float(space) / float(count) * 100, ), end=' ')
        print()

    print()
    print("Total Games Created: {}".format(count))
    def test_join_game_valid(self):
        layout = BoardLayout.generate(self.test_ships)
        nonces = create_nonces(10)

        # Send join payload to tp

        join_req = create_join_payload('join_game', layout, nonces)
        self.validator.send(
            self.player_1.create_tp_process_request(join_req)
        )

        # give state back to tp

        self.send_state_to_tp('join_game', {
            'Ships': self.test_ships,
            'State': 'NEW'
        })

        # mock state set
        LOGGER.debug('state update')
        self.update_state('join_game', {
            'Ships': self.test_ships,
            'TargetBoard1': [['?'] * 10 for _ in range(10)],
            'Player1': self.public_key_1,
            'HashedBoard1': layout.render_hashed(nonces),
            'State': 'NEW'
        })

        self.expect_ok()
Пример #3
0
def do_genstats(args, config):
    count = args.count
    size = args.size
    ships = ["AAAAA", "BBBB", "CCC", "DD", "DD", "SSS", "SSS"]
    # Create a board which contains a count of the number of time
    # a space was used.
    count_board = [[0] * size for i in range(size)]
    for i in xrange(0, count):
        layout = BoardLayout.generate(size=size, ships=ships)
        board = layout.render()
        for row in xrange(0, size):
            for col in xrange(0, size):
                if board[row][col] != '-':
                    count_board[row][col] += 1

    print("Percentages Board")
    print("-----------------")

    # Print the board of percentages.
    print("  ", end=' ')
    for i in xrange(0, size):
        print("  {}".format(chr(ord('A') + i)), end=' ')
    print()

    for row in xrange(0, size):
        print("%2d" % (row + 1), end=' ')
        for space in count_board[row]:
            print("%3.0f" % (float(space) / float(count) * 100,), end=' ')
        print()

    print()
    print("Total Games Created: {}".format(count))
Пример #4
0
def do_genstats(args, config):
    count = args.count
    size = args.size

    # Create a board which contains a count of the number of time
    # a space was used.
    count_board = [[0] * size for i in range(size)]
    for i in xrange(0, count):
        layout = BoardLayout.generate(size=size)
        board = layout.render()
        for row in xrange(0, size):
            for col in xrange(0, size):
                if board[row][col] != '-':
                    count_board[row][col] += 1

    print "Percentages Board"
    print "-----------------"

    # Print the board of percentages.
    print "  ",
    for i in xrange(0, size):
        print "  {}".format(chr(ord('A') + i)),
    print

    for row in xrange(0, size):
        print "%2d" % (row + 1),
        for space in count_board[row]:
            print "%3.0f" % (float(space) / float(count) * 100, ),
        print

    print
    print "Total Games Created: {}".format(count)
Пример #5
0
def do_genstats(args, config):
    count = args.count
    size = args.size

    # Create a board which contains a count of the number of time
    # a space was used.
    count_board = [[0] * size for i in range(size)]
    for i in xrange(0, count):
        layout = BoardLayout.generate(size=size)
        board = layout.render()
        for row in xrange(0, size):
            for col in xrange(0, size):
                if board[row][col] != '-':
                    count_board[row][col] += 1

    print "Percentages Board"
    print "-----------------"

    # Print the board of percentages.
    print "  ",
    for i in xrange(0, size):
        print "  {}".format(chr(ord('A') + i)),
    print

    for row in xrange(0, size):
        print "%2d" % (row + 1),
        for space in count_board[row]:
            print "%3.0f" % (float(space) / float(count) * 100,),
        print

    print
    print "Total Games Created: {}".format(count)
Пример #6
0
def do_join(args, config):
    name = args.name

    url = config.get('DEFAULT', 'url')
    key_file = config.get('DEFAULT', 'key_file')

    data = load_data(config)

    client_for_state = BattleshipClient(base_url=url, keyfile=key_file)
    state = client_for_state.get_all_store_objects()
    if name not in state:
        raise BattleshipException(
            "No such game: {}".format(name)
        )
    game = state[name]
    ships = game['Ships']

    if name not in data['games']:
        new_layout = BoardLayout.generate(ships=ships)
        data['games'][name] = {}
        data['games'][name]['layout'] = new_layout.serialize()
        data['games'][name]['nonces'] = create_nonces(new_layout.size)

        home = os.path.expanduser("~")

        username = config.get('DEFAULT', 'username')

        data_file = os.path.join(home,
                                 ".sawtooth",
                                 "battleship-{}.data".format(username))
        with open(data_file + ".new", 'w') as fd:
            json.dump(data, fd, sort_keys=True, indent=4)
        if os.name == 'nt':
            if os.path.exists(data_file):
                os.remove(data_file)
        os.rename(data_file + ".new", data_file)
    else:
        print("Board and nonces already defined for game, reusing...")

    layout = BoardLayout.deserialize(data['games'][name]['layout'])
    nonces = data['games'][name]['nonces']

    hashed_board = layout.render_hashed(nonces)

    client = BattleshipClient(base_url=url, keyfile=key_file)
    client.join(name=name, board=hashed_board)

    if args.wait:
        client.wait_for_commit()
Пример #7
0
def do_join(args, config):
    name = args.name

    url = config.get('DEFAULT', 'url')
    key_file = config.get('DEFAULT', 'key_file')

    data = load_data(config)

    client_for_state = BattleshipClient(base_url=url, keyfile=key_file)
    state = client_for_state.get_all_store_objects()
    if name not in state:
        raise BattleshipException(
            "No such game: {}".format(name)
        )
    game = state[name]
    ships = game['Ships']

    if name not in data['games']:
        new_layout = BoardLayout.generate(ships=ships)
        data['games'][name] = {}
        data['games'][name]['layout'] = new_layout.serialize()
        data['games'][name]['nonces'] = create_nonces(new_layout.size)

        home = os.path.expanduser("~")

        username = config.get('DEFAULT', 'username')

        data_file = os.path.join(home,
                                 ".sawtooth",
                                 "battleship-{}.data".format(username))
        with open(data_file + ".new", 'w') as fd:
            json.dump(data, fd, sort_keys=True, indent=4)
        if os.name == 'nt':
            if os.path.exists(data_file):
                os.remove(data_file)
        os.rename(data_file + ".new", data_file)
    else:
        print("Board and nonces already defined for game, reusing...")

    layout = BoardLayout.deserialize(data['games'][name]['layout'])
    nonces = data['games'][name]['nonces']

    hashed_board = layout.render_hashed(nonces)

    client = BattleshipClient(base_url=url, keyfile=key_file)
    client.join(name=name, board=hashed_board)

    if args.wait:
        client.wait_for_commit()
    def test_join_nonexistent_game(self):
        layout = BoardLayout.generate(self.test_ships)
        nonces = create_nonces(10)

        # Send join payload to tp
        join_req = create_join_payload('nonexistent_game', layout, nonces)
        self.validator.send(
            self.player_1.create_tp_process_request(join_req)
        )

        # give state back to tp

        self.send_state_to_tp('nonexistent_game')

        self.expect_invalid()
def do_genstats(args, config):
    count = args.count
    size = args.size
    ships = ["AAAAA", "BBBB", "CCC", "DD", "DD", "SSS", "SSS"]
    # Create a board which contains a count of the number of time
    # a space was used.
    count_board = [[0] * size for i in range(size)]
    for i in range(0, count):
        layout = BoardLayout.generate(size=size, ships=ships)
        board = layout.render()
        for row in range(0, size):
            for col in range(0, size):
                if board[row][col] != '-':
                    count_board[row][col] += 1

    print("Percentages Board")
    print("-----------------")

    # Print the board of percentages.
    print("  ", end=' ')
    def test_join_game_too_many_players(self):
        layout = BoardLayout.generate(self.test_ships)
        nonces = create_nonces(10)

        # Send join payload to tp
        join_req = create_join_payload('full_game', layout, nonces)
        self.validator.send(
            self.player_1.create_tp_process_request(join_req)
        )

        # give state back to tp

        self.send_state_to_tp('full_game', {
            'Ships': self.test_ships,
            'TargetBoard1': [['?'] * 10 for _ in range(10)],
            'TargetBoard2': [['?'] * 10 for _ in range(10)],
            'Player1': self.public_key_1,
            'Player2': self.public_key_2,
            'HashedBoard1': layout.render_hashed(nonces),
            'HashedBoard2': layout.render_hashed(nonces),
            'State': 'P1-NEXT'
        })

        self.expect_invalid()