def testRandom(self): class Strategy(object): def roll(self): return 123 a = Abilities() a.random(Strategy()) for name, value in a: self.assertIn(name, Abilities.all) self.assertEquals(123, value)
def dump(self): print 'Monster: ' + self.name print 'HP: ' + str(self.hp) print 'AC: ' + str(self.ac) print 'Initialive: ' + str(self.initiative) print 'Damage: ' + str(self.damage) print 'Speed: ' + str(self.speed) print 'Abilities:' for name in Abilities.all: print ' ' + name + ': ' + str(self.abilities[name]) + ' / ' + str( Abilities.modifier(self.abilities[name]))
def testModifier(self): self.assertEquals(-5, Abilities.modifier(0)) self.assertEquals(-5, Abilities.modifier(1)) self.assertEquals(-3, Abilities.modifier(5)) self.assertEquals(0, Abilities.modifier(10)) self.assertEquals(4, Abilities.modifier(19)) self.assertEquals(5, Abilities.modifier(20))
def _doAttack(self, me, other, weapon=None): if weapon: if not isinstance(weapon, Weapon): raise ValueError('You can only attack with weapons') ability = Abilities.STRENGTH if not weapon.isRanged else Abilities.DEXTERITY modifier = Abilities.modifier(me.abilities[ability]) threatRange = weapon.threatRange damages = weapon.damage criticalDmg = weapon.critical else: modifier = 0 threatRange = 20 damages = me.damage criticalDmg = 2 print '--' print '-- %s attacks %s' % (me.name, other.name) (touch, critical, critical_miss) = self.touch(threatRange, modifier, other.ac) if critical_miss: print '-- Critical miss' return if not touch: print '-- Miss' return dmg = damages.calc() totalDmg = dmg + modifier if not critical: print '-- Damage dealt: %s + %s = %s' % (dmg, modifier, totalDmg) else: totalDmg *= criticalDmg print '-- Critical damage dealt: %s * (%s + %s) = %s' % ( criticalDmg, dmg, modifier, totalDmg) other.hp -= totalDmg if other.hp > 0: print '-- %s HP: %s' % (str(other.name), other.hp) else: print '-- %s is dead' % other.name
def __init__(self, name, damage, abilityScores=False, initiative=1, speed=1, ac=1, description='', hp=1, firstStrike=False): super(Monster, self).__init__(name, abilityScores=abilityScores, initiative=initiative, ac=ac, hp=hp, firstStrike=firstStrike) self.description = description self.damage = damage self.abilities = Abilities(abilityScores) self.speed = speed
def testInit(self): a = Abilities() for name in Abilities.all: self.assertEquals(0, a[name])
def testRandomWrongStrategy(self): with self.assertRaises(ValueError): a = Abilities() a.random(self)
def testIter(self): a = Abilities([123, 123, 123, 123, 123, 123]) for name, value in a: self.assertIn(name, Abilities.all) self.assertEquals(123, value)
def testSetItemError(self): with self.assertRaises(IndexError): a = Abilities() a['foobar'] = 0
def testGetSetItem(self): a = Abilities() self.assertEquals(0, a['Strength']) a['Strength'] = 10 self.assertEquals(10, a['Strength'])
def testKeys(self): a = Abilities() self.assertItemsEqual(a.keys(), Abilities.all)
def testInitWithInvalidDefaults(self): with self.assertRaises(ValueError): a = Abilities([1])
def testInitWithDefaults(self): a = Abilities(scores=[1, 2, 3, 4, 5, 6]) counter = 1 for name in Abilities.all: self.assertEquals(counter, a[name]) counter += 1