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
def test_unequal_elo(self): users = [ User(iso='P1', elo=1100, div=3), User(iso='P2', elo=1090, div=3), User(iso='P3', elo=1100, div=3), User(iso='P4', elo=1100, div=3), ] endgame.apply_rating(users, winner='P1') self.assertNotEqual(users[0].elo, users[1].elo) self.assertEqual(users[2].elo, users[3].elo) self.assertNotEqual(users[0].elo, users[2].elo) self.assertNotEqual(users[1].elo, users[2].elo)
def test_unfair(self): users = [ User(iso='P1', elo=1600, div=3), User(iso='P2', elo=1100, div=3), User(iso='P3', elo=1100, div=3), User(iso='P4', elo=1800, div=3), ] endgame.apply_rating(users, winner='P1') self.assertEqual(users[0].elo, 1600 + 6) self.assertEqual(users[1].elo, 1100 - 2) self.assertEqual(users[2].elo, 1100 - 2) self.assertEqual(users[3].elo, 1800 - 24)
def getUser() -> UserAuth: if not current_user.is_authenticated: # fetch anon user user = User(uid=uuid.uuid4(), username=None) setUser(UserAuth(user)) return current_user
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
def load_user(uid): if uid is None: return None if uid == 'None': raise Exception("Very interesting UID provided") user = users.get(uid) if not user: user = User(uid=uuid.UUID(uid), username=None) if user.uid == 'None': raise Exception("ahusdjfsda asdk e ags sd gsrgsthr") return UserAuth(user)
def handle_divisions(user: User, sa): if user.division >= MAX_DIV: # user is already at max division return False if user.elo > 1800 and sa==1: # user has ascended beyond promo elo user.division += 1 return True elif user.elo < 900 and sa==0 and user.division > 0: # user has fallen 1 division below user.division -= 1 return True elif user.elo < 500: # minimum possible elo user.elo = 500
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)
def __init__(self, user: User): User.__init__(self, **user.__dict__) self.id = self.uid self.guest = False
def forceAnonLogin(uid): anon = User(uid=uuid.uuid4(), username=None) setUser(anon)
def __init__(self, client: WSClient=None, **kwargs): User.__init__(self, **kwargs) self.id = kwargs.get('uid') self.client = client
def change_elos(user1: User, user2: User, numA, numB, sA): user1.elo, user2.elo = calculate_elo(user1.elo, user2.elo, aA, numA, numB) handle_divisions(user1, sA) handle_divisions(user2, 1-sA)