def test_weather(self): ########################################################################### # Change to raising handler for unit-testing set_prequire_handler(raising_prequire_handler) temps, precips, winds = ([60, 70, 80, 70], [1, 2, 3, 4], [Wind(10, Direction.NNW)] * 4) climate = Climate(temps, precips, winds) atmos = Atmosphere(climate) # First season should be winter first_season = iter(Season).next() self.assertEqual(climate.temperature(first_season), atmos.temperature()) self.assertEqual(climate.precip(first_season), atmos.precip()) self.assertEqual(climate.wind(first_season), atmos.wind()) # Check access controls self.assertRaises(ProgramError, atmos.cycle_turn, None, None, None) grant_access(self, Atmosphere.ALLOW_CYCLE_TURN) # Brute-force check of all seasons for idx, season in enumerate(Season): atmos.cycle_turn([], Location(0, 0), season) self.assertEqual(climate.temperature(season), atmos.temperature()) self.assertEqual(climate.precip(season), atmos.precip()) self.assertEqual(climate.wind(season), atmos.wind()) self.assertEqual(climate.temperature(season), temps[idx]) self.assertEqual(climate.precip(season), precips[idx]) self.assertEqual(climate.wind(season), winds[idx])
def __init__(self, args): ########################################################################### urequire(len(args) == 1, self.name(), " takes one argument") self.__spell_cls = SpellFactory.get(args[0]) grant_access(self, Player.ALLOW_LEARN)
def test_talents(self): ########################################################################### from player import Player from configuration import Configuration Configuration._create() player = Player() talents = Talents(player) tier1_spell_name = "hot" another_tier1_spell_name = "cold" tier2_spell_name = "fire" high_tier_spell_name = "quake" not_a_spell = "lol" # Give ourselves ability to add exp to player grant_access(self, Player.ALLOW_GAIN_EXP) # Get player up to level 10 for i in xrange(9): exp_needed = player.next_level_cost() - player.exp() player.gain_exp(exp_needed) self.assertEqual(player.level(), 10) # Should not be able to learn fire 1 (don't know hot 1) self.assertRaises(UserError, talents.add, tier2_spell_name) # Should not be able to learn quake 1 (not high enough level) self.assertRaises(UserError, talents.add, high_tier_spell_name) # Should not be able to learn an invalid spell self.assertRaises(UserError, talents.add, not_a_spell) # Learn hot up to max for i in xrange(talents._MAX_SPELL_LEVEL): talents.add(tier1_spell_name) # Should not be able to learn tier1 spell again, maxed out self.assertRaises(UserError, talents.add, tier1_spell_name) # Should be able to learn fire now for i in xrange(talents._MAX_SPELL_LEVEL): talents.add(tier2_spell_name) # Should not be able to learn a different tier1 spell, out of points self.assertRaises(UserError, talents.add, another_tier1_spell_name) # Check iteration known_spells = [] for spell_spec in talents: known_spells.append(spell_spec) # Check that we know the spells we've learned for spell_name in [tier1_spell_name, tier2_spell_name]: for spell_level in xrange(1, talents._MAX_SPELL_LEVEL): self.assertIn( (spell_name, spell_level), talents ) #self.assertIn( (spell_name, spell_level), known_spells ) self.assertEqual(len(known_spells), 2)
def __init_impl(self, world): ########################################################################### cls = self.__class__ self.__tech_level = cls.__STARTING_TECH_LEVEL self.__tech_points = 0 self.__next_tech_level_cost = cls.__FIRST_TECH_LEVEL_COST self.__world = world grant_access(self, City.ALLOW_CYCLE_TURN)
def test_geology(self): ########################################################################### # Change to raising handler for unit-testing set_prequire_handler(raising_prequire_handler) grant_access(self, Geology.ALLOW_CYCLE_TURN) plate_movement = 3 # Test that we cannot create instances of Geology self.assertRaises(ProgramError, Geology, plate_movement) # Check that is_geological works self.assertTrue(is_geological(DrawMode.MAGMA)) subd = Subducting(plate_movement) # Check turn cycling subd.cycle_turn() subd.cycle_turn() tension1 = subd.tension() magma1 = subd.magma() subd.cycle_turn() subd.cycle_turn() self.assertLess(tension1, subd.tension()) self.assertLess(magma1, subd.magma()) for i in xrange(500): subd.cycle_turn() self.assertLess(subd.tension(), 1.0) self.assertLess(subd.magma(), 1.0) # Check transform trans = Transform(plate_movement) magma_orig = trans.magma() trans.cycle_turn() trans.cycle_turn() tension1 = trans.tension() self.assertEqual(magma_orig, trans.magma()) trans.cycle_turn() trans.cycle_turn() self.assertLess(tension1, trans.tension()) self.assertEqual(magma_orig, trans.magma()) # Check inactive inactive = Inactive() magma_orig = inactive.magma() tension_orig = inactive.tension() for i in xrange(100): inactive.cycle_turn() self.assertEqual(magma_orig, inactive.magma()) self.assertEqual(tension_orig, inactive.tension())
def test_world(self): ########################################################################### from configuration import Configuration from world_factory import create_world Configuration._create() world = create_world() grant_access(self, World.ALLOW_CYCLE_TURN) # Test cycle turn time1_val = int(world.time().season()) world.cycle_turn() time2 = world.time() self.assertEqual(time1_val + 1, int(time2.season()))
def __init_impl(self, name, location): ########################################################################### self.__name = name self.__rank = 1 self.__population = City._CITY_STARTING_POP self.__next_rank_pop = self.__population * City._CITY_RANK_UP_MULTIPLIER self.__prod_bank = 0.0 self.__location = location self.__defense = 1 self.__famine = False from world_tile import WorldTile, allow_build_infra from world import World grant_access(self, WorldTile.ALLOW_WORK) grant_access(self, World.ALLOW_PLACE_CITY) allow_build_infra(self)
def test_player(self): ########################################################################### # Change to raising handler for unit-testing set_prequire_handler(raising_prequire_handler) Configuration._create() player = Player() spell = "hot" exp_needed = player.next_level_cost() - player.exp() self.assertRaises(ProgramError, player.gain_exp, exp_needed) grant_access(self, Player.ALLOW_GAIN_EXP) player.gain_exp(exp_needed) self.assertEqual(player.level(), 2) from spell_factory import SpellFactory hot_spell_1 = SpellFactory.create_spell(spell, 1) # TODO - Uncomment once Talents are implemented #self.assertRaises(UserError, player.verify_cast, hot_spell_1) self.assertRaises(ProgramError, player.learn, spell) grant_access(self, Player.ALLOW_LEARN) player.learn(spell) player.verify_cast(hot_spell_1) self.assertRaises(ProgramError, player.cast, hot_spell_1) grant_access(self, Player.ALLOW_CAST) player.cast(hot_spell_1) self.assertEqual(player.mana(), player.max_mana() - hot_spell_1.cost())
def __init__(self): ########################################################################### """ Do not call. Use free function to get a handle to an Engine """ # Dump imports here to avoid circular imports from interface import create_interface from world_factory import create_world from player import Player from player_ai import PlayerAI from world import World grant_access(self, Player.ALLOW_CYCLE_TURN) grant_access(self, PlayerAI.ALLOW_CYCLE_TURN) grant_access(self, World.ALLOW_CYCLE_TURN) # This should only be invoked through _instance which should only # be invoked through engine(). We cannot use the check_access system # here because that system cannot be used to grant access to a free # function. check_callers(["_instance", "engine"]) self.__interface = create_interface() self.__world = create_world() self.__player = Player() self.__ai_player = PlayerAI(self.__world) self.__quit = False
# Internal-only # _DEFAULT_WORLD_CONFIG = "1" # Hardcoded world 1 ############################################################################### class _WorldFactoryBase(object): ############################################################################### """ Only used to allow all subclasses to have create-city access """ pass grant_access(_WorldFactoryBase, World.ALLOW_PLACE_CITY) ############################################################################### class _WorldFactoryHardcoded(_WorldFactoryBase): ############################################################################### ########################################################################### def __init__(self, config): ########################################################################### self.__worldnum = int(config) ########################################################################### def create(self): ########################################################################### if (self.__worldnum == 1):
else: prequire(extra_args is not None and len(extra_args) == 1, "Expect spell name in extra_args") spell_name = extra_args[0] spell = SpellFactory.create_spell(spell_name) help_str = cls._GRAPHICAL_USAGE help_str += "\nDescription of %s spell:\n" % spell_name help_str += spell.info() + "\n" help_str += "Player has skill level %d in this spell\n" % \ engine().player().spell_skill(spell_name) return help_str grant_access(_CastCommand, Player.ALLOW_CAST) grant_access(_CastCommand, Player.ALLOW_GAIN_EXP) ############################################################################### class _LearnCommand(Command): ############################################################################### # Class variables _NAME = "learn" _ALIASES = ("l",) _TEXT_USAGE = \ """%s <spell-name> Player learns spell of type <spell-name>. If spell already known, then skill in this spell is increased by one. """ % _NAME _GRAPHICAL_USAGE = \
self.__cities.append(city) tile = self.tile(location) tile.place_city(city) ########################################################################### def __remove_city_impl_world(self, city): ########################################################################### check_access(self.ALLOW_REMOVE_CITY) self.__cities.remove(city) self.tile(city.location()).remove_city() grant_access(World, WorldTile.ALLOW_CYCLE_TURN) # # Tests # ############################################################################### class TestWorld(unittest.TestCase): ############################################################################### ########################################################################### def test_world(self): ########################################################################### from configuration import Configuration from world_factory import create_world