def bad_messages_test(): army1 = MockArmy('dragon') army2 = MockArmy('salamander') game = Game(MockScenario([army1, army2]), [], lambda: 0) flat = game.do({ 'name': 'build', 'army_name': 'dragon', 'unit_name': 'footman', 'at': { 'x': [], 'y': 'not a number' } }) print(flat) assert flat == {'scenario': 'a serialized scenario', 'timestamp': 0} flat = game.do({ 'name': 'nonsense', 'at': { 'x': -1, 'y': 0 } }) assert flat == {'scenario': 'a serialized scenario', 'timestamp': 0} flat = game.do({ 'name': 'move', }) assert flat == {'scenario': 'a serialized scenario', 'timestamp': 0} flat = game.do({ 'name': 'end_turn', }) assert flat == {'scenario': 'a serialized scenario', 'timestamp': 0} flat = game.do({ 'name': 'attack', }) assert flat == {'scenario': 'a serialized scenario', 'timestamp': 0} game.build(army1, 'footman', 'here') flat = game.do({ 'name': 'move', 'army_name': 'dragon', 'unit_id': 'footman', 'to': { 'x': 0, 'y': 0 } }) flat = game.do({ 'name': 'attack', 'attacker': 'footman', 'defending_army': 'salamander', 'defender_id': 'footman' }) assert flat == {'scenario': 'a serialized scenario', 'timestamp': 0} flat = game.do({ 'name': 'the chacha' }) assert flat == {'scenario': 'a serialized scenario', 'timestamp': 0} flat = game.do({ }) assert flat == {'scenario': 'a serialized scenario', 'timestamp': 0}
def move2_test(): army1 = MockArmy('dragon') army2 = MockArmy('salamander') path_finder = MockPathFinder(MockBoard(), [1, 2, 3], 10, True) game = Game(MockScenario([army1, army2]), path_finder) unit = MockUnit(100, 'dragon') game.move(unit, "There") assert unit.pos != "There"
def wrong_turn_move_test(): army1 = MockArmy('dragon') army3 = MockArmy('rat', False) path_finder = MockPathFinder(MockBoard(), [1, 2, 3], 10) game = Game(MockScenario([army1, army3]), path_finder) unit = MockUnit(10, 'rat') game.move(unit, "A new place") assert unit.pos == 0
def build_test(): army1 = MockArmy('dragon') army2 = MockArmy('salamander') game = Game(MockScenario([army1, army2]), []) game.build(army1, 'footman', 'here') assert army1.units == ['footman here'] game.build(army1, 'footman', 'again') assert army1.units == ['footman here', 'footman again'] game.build(army1, 'footman', 'not here') assert army1.units == ['footman here', 'footman again']
def attack_test(): army1 = MockArmy('dragon') army2 = MockArmy('salamander') game = Game(MockScenario([army1, army2]), []) unit1 = MockUnit(100, 'dragon') unit2 = MockUnit(10, 'salamander') unit3 = MockUnit(100, 'dragon') game.attack(unit1, unit2) assert unit2.health == 90 game.attack(unit1, unit3) assert unit3.health == 100
def load_scenario(scenario_metadata, loader): armor_data = loader.load(scenario_metadata["armor_data"]) weapon_data = loader.load(scenario_metadata["weapon_data"]) transport_data = loader.load(scenario_metadata["transport_data"]) building_data = loader.load(scenario_metadata["building_data"]) unit_data = loader.load(scenario_metadata["unit_data"]) tile_data = loader.load(scenario_metadata["tile_data"]) army_data = loader.load(scenario_metadata["army_data"]) board_data = loader.load(scenario_metadata["board_data"]) layout_data = loader.load(scenario_metadata["layout_data"]) building_factory = BuildingFactory(building_data) unit_factory_factory = UnitFactoryFactory({ 'dragon': { 'units': unit_data, 'transports': transport_data, 'weapons': weapon_data, 'armor': armor_data }, 'salamander': { 'units': unit_data, 'transports': transport_data, 'weapons': weapon_data, 'armor': armor_data }, }) tile_factory = TileFactory(tile_data) army_factory = ArmyFactory(army_data, unit_factory_factory, building_factory) board_factory = BoardFactory(board_data, tile_factory) scenario = ScenarioBuilder(board_factory, army_factory) scenario.set_board(layout_data['board']) for army in layout_data['armies']: army_name = army['name'] scenario.add_army(army_name) for unit in army['units']: scenario.add_unit(army_name, unit) for building in army['buildings']: scenario.add_building(army_name, building) scenario.set_starting_money(army['money'], army_name) scenario = scenario.pop_instance() # I don't love this, it's sort of like glue path_finder = PathFinder(scenario.get_board()) return Game(scenario, path_finder)
def find_unit_test(): army1 = MockArmy('dragon') army1.buy_unit("best", "1") army2 = MockArmy('salamander') army3 = MockArmy('rat') army3.buy_unit("test", "0") army4 = MockArmy('phoenix') army4.buy_unit("stressed", "4") game = Game(MockScenario([army1, army2, army3, army4]), []) unit = game._find_unit('test 0') assert unit == "rat" unit = game._find_unit('best 1') assert unit == "dragon" unit = game._find_unit('stressed 4') assert unit == "phoenix"
def move_test(): army1 = MockArmy('dragon') army3 = MockArmy('rat') path_finder = MockPathFinder(MockBoard(), [1, 2, 3], 10) game = Game(MockScenario([army1, army3]), path_finder) # out of range move unit = MockUnit(5, 'dragon') game.move(unit, "A new place") assert unit.pos != "A new place" # in range move unit = MockUnit(10, 'rat') game.move(unit, "A new place") assert unit.pos == "A new place" # occupied move game.move(unit, "not here") assert unit.pos == "A new place"