import random def rollForInitiative(s): 'return a hero guy' guys = list(s.query(HeroGuy, sort=HeroGuy.DEX.descending, limit=2)) # leaving out 0 case for simplicity if len(guys) == 1: return guys[0] elif len(guys) == 2: winningDEX = guys[0].DEX topGuy = s.findFirst(HeroGuy, HeroGuy.DEX == winningDEX, sort=HeroGuy.storeID.descending) bottomGuy = s.findFirst(HeroGuy, HeroGuy.DEX == winningDEX, sort=HeroGuy.storeID.ascending) spinTheWheel = random.randint(bottomGuy.storeID, topGuy.storeID) return s.findFirst(HeroGuy, AND(HeroGuy.DEX == winningDEX, HeroGuy.storeID <= spinTheWheel)) assert 0, 'wtf'