def test_fire_wrong_turn(self): game_name = "wrong_turn" nonces = create_nonces(10) self.validator.send( self.player_1.create_tp_process_request({ "Action": "FIRE", "Name": game_name, "Row": "A", "Column": "1" })) self.respond_to_get_with_state( game_name, { "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": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "State": "P2-NEXT", }, ) self.validator.expect( self.player_1.create_tp_response("INVALID_TRANSACTION"))
def test_join_game_too_many_players(self): game_name = "full_game" nonces = create_nonces(10) self.validator.send( self.player_1.create_tp_process_request({ "Action": "JOIN", "Name": game_name, "Board": self.layout.render_hashed(nonces) })) self.respond_to_get_with_state( game_name, { "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": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "State": "P1-NEXT", }, ) self.validator.expect( self.player_1.create_tp_response("INVALID_TRANSACTION"))
def test_fire_wrong_turn(self): game_name = "wrong_turn" nonces = create_nonces(10) self.validator.send( self.player_1.create_tp_process_request( {"Action": "FIRE", "Name": game_name, "Row": "A", "Column": "1"} ) ) self.respond_to_get_with_state( game_name, { "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": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "State": "P2-NEXT", }, ) self.validator.expect(self.player_1.create_tp_response("INVALID_TRANSACTION"))
def test_join_game_too_many_players(self): game_name = "full_game" nonces = create_nonces(10) self.validator.send( self.player_1.create_tp_process_request( {"Action": "JOIN", "Name": game_name, "Board": self.layout.render_hashed(nonces)} ) ) self.respond_to_get_with_state( game_name, { "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": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "State": "P1-NEXT", }, ) self.validator.expect(self.player_1.create_tp_response("INVALID_TRANSACTION"))
def test_fire_game_won(self): game_name = "fire_game" nonces = create_nonces(10) # Player 2 fires self.validator.send( self.player_2.create_tp_process_request( { "Action": "FIRE", "Name": game_name, "Row": "A", "Column": "2", "RevealSpace": "A", "RevealNonce": nonces[0][1], } ) ) target_board1 = [["?"] * 10 for _ in range(10)] target_board1[0][0] = "H" for _ in range(1): self.respond_to_get_with_state( game_name, { "Ships": ["AA"], "TargetBoard1": target_board1, "TargetBoard2": [["?"] * 10 for _ in range(10)], "Player1": self.public_key_1, "Player2": self.public_key_2, "HashedBoard1": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "State": "P2-NEXT", "LastFireRow": "A", "LastFireColumn": "2", }, self.player_2, ) target_board1[0][1] = "H" self.respond_to_set_with_state( game_name, { "HashedBoard1": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "LastFireColumn": "2", "LastFireRow": "A", "Player1": self.public_key_1, "Player2": self.public_key_2, "Ships": ["AA"], "State": "P2-WIN", "TargetBoard1": target_board1, "TargetBoard2": [["?"] * 10 for _ in range(10)], }, self.player_2, ) self.validator.expect(self.player_1.create_tp_response("OK"))
def test_fire_miss_ship(self): game_name = "fire_game" nonces = create_nonces(10) self.validator.send( self.player_2.create_tp_process_request({ "Action": "FIRE", "Name": "fire_game", "Row": "B", "Column": "1", "RevealSpace": "-", "RevealNonce": nonces[1][0], })) self.respond_to_get_with_state( game_name, { "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": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "State": "P2-NEXT", "LastFireRow": "B", "LastFireColumn": "1", }, ) target_board1 = [["?"] * 10 for _ in range(10)] target_board1[1][0] = "M" self.respond_to_set_with_state( game_name, { "Ships": ["AA"], "TargetBoard1": target_board1, "TargetBoard2": [["?"] * 10 for _ in range(10)], "Player1": self.public_key_1, "Player2": self.public_key_2, "HashedBoard1": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "LastFireRow": "B", "LastFireColumn": "1", "State": "P1-NEXT", }, ) self.validator.expect(self.player_1.create_tp_response("OK"))
def test_join_nonexistent_game(self): game_name = "nonexistent_game" nonces = create_nonces(10) self.validator.send( self.player_1.create_tp_process_request( {"Action": "JOIN", "Name": game_name, "Board": self.layout.render_hashed(nonces)} ) ) self.respond_to_get_with_state(game_name, None) self.validator.expect(self.player_1.create_tp_response("INVALID_TRANSACTION"))
def test_join_nonexistent_game(self): game_name = "nonexistent_game" nonces = create_nonces(10) self.validator.send( self.player_1.create_tp_process_request({ "Action": "JOIN", "Name": game_name, "Board": self.layout.render_hashed(nonces) })) self.respond_to_get_with_state(game_name, None) self.validator.expect( self.player_1.create_tp_response("INVALID_TRANSACTION"))
def test_fire_first_move(self): game_name = "fire_game" nonces = create_nonces(10) # Player 1 fires self.validator.send( self.player_1.create_tp_process_request({ "Action": "FIRE", "Name": game_name, "Row": "B", "Column": "1" })) self.respond_to_get_with_state( game_name, { "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": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "State": "P1-NEXT", }, ) self.respond_to_set_with_state( game_name, { "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": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "LastFireRow": "B", "LastFireColumn": "1", "State": "P2-NEXT", }, ) self.validator.expect(self.player_1.create_tp_response("OK"))
def test_join_game_valid(self): game_name = "join_game" nonces = create_nonces(10) # Send join payload to tp self.validator.send( self.player_1.create_tp_process_request({ "Action": "JOIN", "Name": game_name, "Board": self.layout.render_hashed(nonces) })) self.respond_to_get_with_state( game_name, { "HashedBoard1": [], "HashedBoard2": [], "Ships": ["AA"], "State": "NEW", "TargetBoard1": [], "TargetBoard2": [], }, ) self.respond_to_set_with_state( game_name, { "HashedBoard1": self.layout.render_hashed(nonces), "HashedBoard2": [], "LastFireColumn": None, "LastFireRow": None, "Player1": self.public_key_1, "Player2": None, "Ships": ["AA"], "State": "NEW", "TargetBoard1": [["?"] * 10 for _ in range(10)], "TargetBoard2": [], }, ) self.validator.expect(self.player_1.create_tp_response("OK"))
def test_fire_first_move(self): game_name = "fire_game" nonces = create_nonces(10) # Player 1 fires self.validator.send( self.player_1.create_tp_process_request( {"Action": "FIRE", "Name": game_name, "Row": "B", "Column": "1"} ) ) self.respond_to_get_with_state( game_name, { "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": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "State": "P1-NEXT", }, ) self.respond_to_set_with_state( game_name, { "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": self.layout.render_hashed(nonces), "HashedBoard2": self.layout.render_hashed(nonces), "LastFireRow": "B", "LastFireColumn": "1", "State": "P2-NEXT", }, ) self.validator.expect(self.player_1.create_tp_response("OK"))
def test_join_game_valid(self): game_name = "join_game" nonces = create_nonces(10) # Send join payload to tp self.validator.send( self.player_1.create_tp_process_request( {"Action": "JOIN", "Name": game_name, "Board": self.layout.render_hashed(nonces)} ) ) self.respond_to_get_with_state( game_name, { "HashedBoard1": [], "HashedBoard2": [], "Ships": ["AA"], "State": "NEW", "TargetBoard1": [], "TargetBoard2": [], }, ) self.respond_to_set_with_state( game_name, { "HashedBoard1": self.layout.render_hashed(nonces), "HashedBoard2": [], "LastFireColumn": None, "LastFireRow": None, "Player1": self.public_key_1, "Player2": None, "Ships": ["AA"], "State": "NEW", "TargetBoard1": [["?"] * 10 for _ in range(10)], "TargetBoard2": [], }, ) self.validator.expect(self.player_1.create_tp_response("OK"))