async def action_greet(cls, data: dict, player: Player, game: Game): if player.ready: await cls.write_error(player, 'you are greeted before') return try: name = data['name'] except KeyError: await cls.write_error(player, 'name is undefined') return if not isinstance(name, str): await cls.write_error(player, 'name has wrong type') return name = name.strip() if not name: await cls.write_error(player, 'name is blank') return if len(name) > 10: await cls.write_error(player, 'name is too long (max is 10)') return player.set_nick(name) data = messages.get_world_data(player, game) await cls.write_ok(player, **data)
async def action_shoot(cls, data: dict, player: Player, game: Game): with await game.step_lock: if not await cls.can_has_action(game, player): return player.set_shot() await cls.set_had_action(player, game) await cls.write_ok(player)
def make_player(had_action=False, x=1, y=2): player = Player(x=x, y=y, player_id=0) player.had_action = had_action connection = PlayerConnection(None, None) connection.write = CoroutineMock(spec=connection.write) player.set_connection(connection) return player
async def test_unfreeze_players(): game = Game() game.alive_players = [Player(0, 128, 128), Player(1, 128, 128)] logic = TickLogicPart(game) await logic.unfreeze_players() assert not game.alive_players[0].is_freeze assert not game.alive_players[1].is_freeze
async def action_set_speed(cls, data: dict, player: Player, game: Game): speed = data.get('speed') if not isinstance(speed, int): await cls.write_error(player, 'speed has wrong value') return with await game.step_lock: if not await cls.can_has_action(game, player): return player.set_speed(speed) await cls.set_had_action(player, game) await cls.write_ok(player, speed=player.speed)
def test_render_players(pygame): game = Game() game.alive_players = [ Player(0, 32, 32), Player(1, 64, 32), ] drawer = Drawer(game) drawer._blit = Mock(spec=drawer._blit) drawer._render_players() assert drawer._blit.call_args_list == [ call('IMG_PLAYER_1_1', game.alive_players[0]), call('IMG_PLAYER_2_1', game.alive_players[1]), ]
def test_messages_get_monster_data(): player = Player(x=1, y=2, player_id=0) player.direction = Direction.UP player.speed = 2 assert messages.get_monster_serialized_data(player, action='socek') == dict( id=player.id.hex, type='player', status='data', action='socek', is_freeze=False, direction='up', speed=2, parent=None, position={'x': 1, 'y': 2}, )
async def action_rotate(cls, data: dict, player: Player, game: Game): raw_direction = data.get('direction', '').lower() try: direction = Direction(raw_direction) except ValueError: await cls.write_error(player, 'unknown direction') return with await game.step_lock: if not await cls.can_has_action(game, player): return player.set_direction(direction) await cls.set_had_action(player, game) await cls.write_ok(player, direction=direction.value, position=player.get_position())
def test_messages_get_move_data(): player = Player(x=1, y=2, player_id=0) assert messages.get_monster_serialized_move_data(player) == dict( id=player.id.hex, status='data', action='move', position={'x': 1, 'y': 2}, )
def load_map(self, name): data_map = MapMaker.load_data_from_name(name) MapMaker(self, data_map).make() self.players = [ Player(player_id, *self.player_spawns[player_id]) for player_id in range(2) ] self.alive_players = self.players[:] # copy list
def test_get_info_label_from_freezed(pygame): drawer = Drawer(Game()) player = Player(0, 0, 0) player.set_connection(object()) player.set_freeze() assert drawer._get_info_label(player) == 'FREEZE'
def test_messages_get_world_data(): game = Game() player = Player(x=1, y=1, player_id=0) game.alive_players = [player] game.npcs = [NPC(2, 2)] game.bullets = [Bullet(3, 3)] game.walls = [TinyWall(x=4, y=4), Water(x=5, y=5)] game.coins = [Coin(x=4, y=4), Coin(x=5, y=5)] assert messages.get_world_data(player, game) == dict( id=player.id.hex, cords=[ dict(type='player', id=player.id.hex, position={ 'x': 1, 'y': 1 }), dict(type='npc', id=game.npcs[0].id.hex, position={ 'x': 2, 'y': 2 }), dict(type='bullet', id=game.bullets[0].id.hex, position={ 'x': 3, 'y': 3 }), dict(type='tinywall', id=game.walls[0].id.hex, position={ 'x': 4, 'y': 4 }), dict(type='water', id=game.walls[1].id.hex, position={ 'x': 5, 'y': 5 }), dict(type='coin', id=game.coins[0].id.hex, position={ 'x': 4, 'y': 4 }), dict(type='coin', id=game.coins[1].id.hex, position={ 'x': 5, 'y': 5 }), ])
async def test_set_old_position(): game = Game() game.bullets = [Bullet(128, 128)] game.alive_players = [Player(x=128, y=128, player_id=0)] game.npcs = [NPC(128, 128)] for monster in game.get_monsters_chain(): monster.set_position(42, 24) await SetOldPositionLogicPart(game).do_it() for monster in game.bullets + game.alive_players + game.npcs: assert monster.old_position.x == 42 assert monster.old_position.y == 24
def test_render_text(pygame): game = Game() game.npcs = [NPC(0, 0), NPC(1, 1)] game.players = [Player(0, 32, 32), Player(1, 64, 32)] game.players[0].set_nick('1234') game.players[0].score = 100 game.time_left = 125 game.npcs_left = 3 drawer = Drawer(game) drawer._render_label = Mock(spec=drawer._render_label) drawer._render_text() assert drawer._render_label.call_args_list == [ call('title', 'BATTLE CITY AI', (0, 00)), call('npc_left', 'NPCs left: 003', (0, 40)), call('npc', 'NPCs in area: 002', (0, 80)), call('time', 'Time left: 125', (0, 120)), call('not-ready', 'NOT READY', (0, 180)), call('p-1', '1234 000100', (0, 240), drawer.PLAYER_COLORS[0]), call('p-info-1', 'WAIT', (0, 260), drawer.PLAYER_COLORS[0]), call('p-2', 'P1 000000', (0, 280), drawer.PLAYER_COLORS[1]), call('p-info-2', 'WAIT', (0, 300), drawer.PLAYER_COLORS[1]), ]
def test_render_players_with_freeze(pygame): game = Game() game.alive_players = [ Player(0, 32, 32), ] game.alive_players[0].set_freeze() drawer = Drawer(game) drawer._blit = Mock(spec=drawer._blit) drawer._render_players() assert drawer._blit.call_args_list == [ call('IMG_PLAYER_1_1', game.alive_players[0]), call('FREEZE', game.alive_players[0]), ]
def test_messages_get_bullet_data(): player = Player(x=1, y=2, player_id=0) bullet = Bullet(x=1, y=2) bullet.set_direction(direction=Direction.UP) bullet.set_parent(player) assert messages.get_monster_serialized_data(bullet, action='socek') == dict( id=bullet.id.hex, type='bullet', status='data', action='socek', is_freeze=False, direction='up', speed=8, parent=player.id.hex, position={'x': 1, 'y': 2}, )
async def test_move(): game = Game() # default direction is UP game.bullets = [Bullet(128, 128)] game.alive_players = [Player(x=128, y=128, player_id=0)] # but default direction of NPC is DOWN game.npcs = [NPC(128, 128)] game.alive_players[0].set_speed(2) game.npcs[0].set_speed(2) await MoveLogicPart(game).do_it() assert game.alive_players[0].get_position() == {'x': 128, 'y': 128 - 2} assert game.npcs[0].get_position() == {'x': 128, 'y': 128 + 2} assert game.bullets[0].get_position() == {'x': 128, 'y': 128 - 8}
def test_messages_get_over_game_data(): player_a = Player(x=1, y=2, player_id=0) player_b = Player(x=1, y=2, player_id=1) player_a.score = 20 player_b.score = 40 game = Game() game.alive_players = [player_a, player_b] assert messages.get_over_game_data(game) == dict( status='game', action='over', winner=player_b.id.hex, )
async def set_had_action(player: Player, game: Game): player.set_had_action() data = messages.get_monster_serialized_data(player) await game.broadcast(data)
def test_get_info_label_from_not_connected_player(pygame): drawer = Drawer(Game()) player = Player(0, 0, 0) assert drawer._get_info_label(player) == 'WAIT'
def test_get_info_label_from_killed_player(pygame): drawer = Drawer(Game()) player = Player(0, 0, 0) player.set_game_over() assert drawer._get_info_label(player) == 'KILLED'
def test_get_info_label_from_connected_player(pygame): drawer = Drawer(Game()) player = Player(0, 0, 0) player.set_connection(object()) assert drawer._get_info_label(player) == ''