Пример #1
0
    def test_match(self):
        c, a, calls = load_gtml('testapp/content/real_match/init.gtml')
        _set_up_game(c, a)

        print("\nROUND 1")
        c, a, calls = load_gtml('testapp/content/real_match/round1.gtml')
        self._assert_calls(*_execute(calls, resp_format='list'))
        self._assert_map(c, a)

        print("\nROUND 2")
        c, a, calls = load_gtml('testapp/content/real_match/round2.gtml')
        self._assert_calls(*_execute(calls, resp_format='list'))
        self._assert_map(c, a)

        print("\nROUND 3")
        c, a, calls = load_gtml('testapp/content/real_match/round3.gtml')
        self._assert_calls(*_execute(calls, resp_format='list'))
        self._assert_map(c, a)

        print("\nROUND 4")
        c, a, calls = load_gtml('testapp/content/real_match/round4.gtml')
        self._assert_calls(*_execute(calls, resp_format='list'))
        self._assert_map(c, a)

        # todo: assert victory & endgame & match history

        _finish()
Пример #2
0
def create_world():
    default_map = 'hu_test'

    world = service.create(map=default_map)

    l_countries, _, _ = load_gtml("game/maps/{}.gtml".format(world.map), skip=('AREAS', 'TEST_CALLS'))

    return world, l_countries
Пример #3
0
    def test_emperor_round(self):
        c, a, calls = load_gtml('testapp/content/edge_match/init.gtml')
        _set_up_match(c, a)

        # todo: somehow put into map file later
        service.switch_conn_graph({
            "AR01": ["AR02", "AR03"],
            "AR02": ["AR01", "AR03"],
            "AR03": ["AR01", "AR02", "ARR04"],
        })

        c, a, calls = load_gtml('testapp/content/edge_match/round1.gtml')
        self._assert_calls(*_execute(calls, resp_format='list'))
        self._assert_map(c)

        c, a, calls = load_gtml('testapp/content/edge_match/round2.gtml')
        self._assert_calls(*_execute(calls, resp_format='list'))
        self._assert_map(c)

        _finish()
Пример #4
0
def join_world(user: User, world: World, players, iso=None):
    l_countries, _, _ = load_gtml("game/maps/{}.gtml".format(world.map), skip=('AREAS', 'TEST_CALLS'))
    isos = [c.iso for c in l_countries]

    if user.wid and user.wid != world.wid:
        return False

    if world.rounds:
        # game has already started
        return False

    # attempt reconnect
    if user.iso:
        if user.wid == world.wid and user.iso in players:
            if players[user.iso]['username'] == user.username:
                # reconnect was successful
                return True

        # someone took our place or we had bad credentials in the first place
        user.wid = None
        user.iso = None
        users.set_world(user.uid, None, None)
        return False
    else:
        # otherwise, we are connecting

        if len(players) == world.max_players:
            return False

        if iso:
            # see if selected iso is valid
            if iso not in isos:
                return False

            if iso in players and players[iso]['username'] != user.username:
                return False

            user.iso = iso
        else:
            # find the first empty slot
            for iso in isos:
                if iso not in players:
                    user.iso = iso
                    break

        if not user.iso:
            # couldn't find an iso
            return False

        user.wid = world.wid
        users.set_world(user.uid, user.wid, user.iso)

        return True
Пример #5
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
Пример #6
0
def _set_up_gtml(filename):
    adict, l_countries = load_gtml(filename)

    return _set_up_game(adict, l_countries)