def test_fire_wrong_turn(self):
        layout = BoardLayout(10)
        layout.append(
            ShipPosition(text='AA', row=0, column=0, orientation='horizontal'))
        nonces = create_nonces(10)

        # Send join payload to tp
        fire_req = create_fire_payload('invalid_row', '1', 'Z')

        self.validator.send(
            self.player_1.create_tp_process_request(fire_req)
        )

        # give state back to tp

        self.send_state_to_tp('invalid_row', {
            'Ships': ['AA'],
            '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': 'P2-NEXT'
        })

        self.expect_invalid()
    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()
Ejemplo n.º 3
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()
Ejemplo n.º 4
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 test_fire_game_won(self):
        # Create test board
        layout = BoardLayout(10)
        layout.append(
            ShipPosition(text='AA', row=0, column=0, orientation='horizontal'))
        nonces = create_nonces(10)

        # Player 2 fires
        fire_req = \
            create_fire_payload('fire_game', '1', 'B', 'A', nonces[0][1])

        self.validator.send(
            self.player_2.create_tp_process_request(fire_req))

        target_board1 = [['?'] * 10 for _ in range(10)]
        target_board1[0][0] = 'H'

        self.send_state_to_tp('fire_game', {
            'Ships': ['AA'],
            'TargetBoard1': target_board1,
            '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': 'P2-NEXT',
            'LastFireRow': '1',
            'LastFireColumn': 'B'
        }, self.player_2)

        target_board1[0][1] = 'H'

        self.update_state('fire_game', {
            'Ships': ['AA'],
            'TargetBoard1': target_board1,
            '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),
            'LastFireRow': '1',
            'LastFireColumn': 'B',
            'State': 'P1-WIN'
        }, self.player_2)

        self.expect_ok()
    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()