예제 #1
0
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
예제 #2
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')
예제 #3
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)
예제 #4
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)
예제 #5
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)
예제 #6
0
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)
예제 #7
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)
예제 #8
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'})