def test_new(self): world = World() players = ['P1', 'P2', 'P3', 'P4'] tb = TurnBox(world, players) tb.start() self.assertEqual(list(tb.deque), players) self.assertEqual(tb.world.current, 'P1')
def start_world(world, AI=False): l_countries, l_areas = create_world_entities(world, AI=AI) # set first player as current tb = TurnBox(world, list(map(lambda c: c.iso, sorted(l_countries, key=lambda c: c.order)))) tb.start() # save entities countries.save_all(l_countries) areas.save_all(l_areas) worlds.save(world)
def end_turn(world: World, curr: Country, countries: dict): """ :param world: game world :param curr: the country that ends the turn :param countries: all countries in the world, in order of turn :return: """ if world.current != curr.iso: raise TurnException("not_your_turn") if len(countries) <= 1: raise TurnException("small_party") tb = TurnBox(world, list(countries.keys())) next_iso = tb.next() while countries.get( next_iso) is not None and countries[next_iso].shields <= 0: # skip over eliminated countries (but keep the turn number) next_iso = tb.next() world.turns -= 1 if next_iso == curr.iso: raise TurnException("small_party") world.has_moved = False if next_iso is None: # round ends, restart turn resp = _end_round(world, countries) if resp.emperor: # new round starts counting from the new emperor tb.start(resp.emperor.iso) isos = tb.current_playerIds() # reassign country orders based on tb isos: for country in countries.values(): country.order = isos.index(country.iso) resp.orders[country.iso] = country.order db_countries.save(country, commit=False) else: # otherwise, we continue as usual tb.start() # order & isos list stays as before. # we can change country orders, emperor status, gold and db_countries.session.commit() return resp if world.current is not None: # start 35 sec timeout start_timeout(world) return None
def test_restart(self): world = World(current='P3', turns=6, rounds=3) players = ['P1', 'P2', 'P3', 'P4'] tb = TurnBox(world, players) tb.start() self.assertEqual(list(tb.deque), ['P3', 'P4', 'P1', 'P2']) self.assertEqual(tb.world.current, 'P3') self.assertEqual(tb.next(), 'P4') self.assertEqual(tb.world.turns, 8) self.assertEqual(tb.world.rounds, 3)
def test_reset(self): world = World() players = ['P1', 'P2', 'P3', 'P4'] tb = TurnBox(world, players) # NEW ROUND self.assertEqual(tb.start(), 'P1') self.assertEqual(tb.next(), 'P2') self.assertEqual(tb.next(), 'P3') self.assertEqual(tb.next(), 'P4') self.assertEqual(tb.world.turns, 4) self.assertEqual(tb.next(), None) self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.world.turns, 4) # FRESH START - NEW ROUND new_start = 'P3' self.assertEqual(tb.start(new_start), 'P3') self.assertEqual(tb.world.turns, 5) self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.next(), 'P4') self.assertEqual(tb.world.turns, 6) self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.next(), 'P1') self.assertEqual(tb.world.turns, 7) self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.next(), 'P2') self.assertEqual(tb.world.turns, 8) self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.next(), None) self.assertEqual(tb.world.turns, 8) self.assertEqual(tb.world.rounds, 3) self.assertEqual(tb.next(), None) self.assertEqual(tb.world.turns, 8) self.assertEqual(tb.world.rounds, 3)
def test_cyclic(self): world = World() players = ['P1', 'P2', 'P3', 'P4'] tb = TurnBox(world, players) # NEW ROUND self.assertEqual(tb.world.rounds, 0) self.assertEqual(tb.start(), 'P1') self.assertEqual(tb.world.rounds, 1) self.assertEqual(tb.world.turns, 1) self.assertEqual(tb.next(), 'P2') self.assertEqual(tb.world.rounds, 1) self.assertEqual(tb.world.turns, 2) self.assertEqual(tb.next(), 'P3') self.assertEqual(tb.world.rounds, 1) self.assertEqual(tb.world.turns, 3) self.assertEqual(tb.next(), 'P4') self.assertEqual(tb.world.rounds, 1) self.assertEqual(tb.world.turns, 4) self.assertEqual(tb.next(), None) self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.world.turns, 4) self.assertEqual(tb.next(), None) self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.world.turns, 4) # NEW ROUND self.assertEqual(tb.start(), 'P1') self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.world.turns, 5) self.assertEqual(tb.next(), 'P2') self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.world.turns, 6) self.assertEqual(tb.next(), 'P3') self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.world.turns, 7) self.assertEqual(tb.next(), 'P4') self.assertEqual(tb.world.rounds, 2) self.assertEqual(tb.world.turns, 8) self.assertEqual(tb.next(), None) self.assertEqual(tb.world.rounds, 3) self.assertEqual(tb.world.turns, 8) self.assertEqual(tb.next(), None) self.assertEqual(tb.world.rounds, 3) self.assertEqual(tb.world.turns, 8)