Exemple #1
0
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 _set_up_game(l_countries, adict):
    _delete_all()

    world = World(current='P1', wid=WID, map='autotest_map')
    l_users = []

    for i, country in enumerate(l_countries):
        country.name = 'Country ' + country.iso
        country.wid = world.wid
        country.order = i + 1

        l_users.append(
            User(iso=country.iso,
                 email='*****@*****.**',
                 password='******',
                 token='t',
                 salt='s',
                 username='******' + country.iso,
                 wid=world.wid,
                 elo=1100,
                 division=1))

    conn_graph = {}
    l_areas = {}

    cid = lambda x, y: 'a{}{}'.format(x, y)

    for y in range(0, 4):
        for x in range(0, 4):
            area = adict[x][y]
            area.id = cid(x, y)
            area.wid = world.wid
            area.iso2 = area.iso

            if area.build in ('barr', 'house', 'cita'):
                area.tile = 'city'

            l_areas[area.id] = area
            conn_graph[area.id] = [
                cid(x - 1, y),
                cid(x + 1, y),
                cid(x, y - 1),
                cid(x, y + 1),
            ]

    service.switch_conn_graph(conn_graph)

    worlds.save(world)
    countries.save_all(l_countries)
    areas.save_all(l_areas.values())
    users.save_all(l_users)

    # reset entities
    countries.calculate_pop(WID)

    return world, l_users, l_countries, l_areas
Exemple #3
0
    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')
Exemple #4
0
    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)
Exemple #5
0
    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)
Exemple #6
0
    def new_world(self):
        user = getUser()

        if not user.username:
            return "Login first"

        world = World(name="Test world", map='hu_test', max_rounds=None)
        worlds.save(world)

        start_world(world, AI=True)

        user.wid = world.wid
        user.iso = 'UK'
        users.save(world)

        return 'New world created: {}'.format(world.wid)
Exemple #7
0
def create_world_entities(world: World, AI=False):
    l_countries, l_areas, l_options = load_gtml("game/maps/{}.gtml".format(world.map))

    world.max_players = len(l_countries)

    country_pops = defaultdict(int)
    for area in l_areas:
        area.wid = world.wid
        area.iso2 = area.iso

        if area.unit:
            country_pops[area.iso] -= 1
        if area.build == 'barr':
            country_pops[area.iso] += 3
        elif area.build in ('cita','house'):
            country_pops[area.iso] += 1

    if 'initial_order' in l_options:
        if l_options['initial_order'] == 'random':
            # randomized order
            orders = list(range(len(l_countries)))
            random.shuffle(orders)
        else:
            # map-defined starting order
            isos = l_options['initial_order'].split(',')
            orders = [isos.index(c.iso) for c in l_countries]
    else:
        # normal order 0,1,2...
        orders = list(range(len(l_countries)))

    for i, country in enumerate(l_countries):
        country.wid = world.wid
        country.ai = AI and i > 0
        country.pop = country_pops[country.iso]
        country.order = orders[i]

    # Number of cities in a map:
    # CEIL(N_MAX_PLAYERS * ((N_START_SHIELDS-1)/3))
    shields_avg = sum(c.shields for c in l_countries) / len(l_countries)
    city_count = sum(1 for el in filter(lambda a: a.tile == 'city', l_areas))
    max_city_count = math.ceil(world.max_players * (shields_avg+1)/3)

    if city_count > max_city_count:
        print("Warning: ", "Max city count exceeded: {} / {}".format(city_count, max_city_count))
        #raise Exception()

    return l_countries, l_areas
def _set_up_match(l_countries, l_areas):
    _delete_all()

    world = World(wid=WID,
                  current='P1',
                  name="Test World",
                  map='autotest_map',
                  max_rounds=None)
    l_users = []

    for i, country in enumerate(l_countries):
        country.name = 'Country ' + country.iso
        country.wid = world.wid
        country.order = i + 1

        l_users.append(
            User(iso=country.iso,
                 email='*****@*****.**',
                 password='******',
                 token='t',
                 salt='s',
                 username='******' + country.iso,
                 wid=world.wid,
                 elo=1100,
                 division=1))

    for area in l_areas:
        area.wid = world.wid
        area.iso2 = area.iso

    # todo: later: add conn graph to GTML
    service.switch_conn_graph({})

    worlds.save(world)
    countries.save_all(l_countries)
    areas.save_all(l_areas)
    users.save_all(l_users)

    # reset entities
    countries.calculate_pop(WID)
Exemple #9
0
    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)
Exemple #10
0
    def start(self):
        if not request.args['uids']:
            return ApiResponse({'result': False, 'no': -1})

        uids = request.args['uids'].split(',')
        world = worlds.get(self.WID)
        l_users = users.list(uids)

        # delete world
        if world is not None:
            worlds.delete(world)

            h = histories.get(self.WID)
            if h is not None:
                histories.delete(h)

        # create world
        world = World(wid=self.WID,
                      name="Playtest world",
                      map=self.MAP,
                      max_rounds=None)

        isos = load_isos('game/maps/{}.gtml'.format(world.map))
        if len(isos) < len(l_users):
            return ApiResponse({'result': False, 'no': len(isos)})

        # force-enter users:
        for user, iso in zip(l_users, isos):
            user.wid = world.wid
            user.iso = iso

        # Save everything and start the game
        worlds.save(world)
        start_world(world, AI=True)
        users.save_all(l_users)

        return ApiResponse({'result': 'OK'})