Beispiel #1
0
 def test_toggle_ready(self):
     from lobbypy.models import Player, Lobby
     player = self.session.query(Player).first()
     lobby = Lobby('Lobby', player, '', '', '')
     lobby.set_team(player, 0)
     self.session.add(lobby)
     transaction.commit()
     from lobbypy.controllers import toggle_ready
     lobby = self.session.merge(lobby)
     player = self.session.merge(player)
     toggle_ready(self.session, lobby, player)
     transaction.commit()
     lobby = self.session.merge(lobby)
     player = self.session.merge(player)
     self.assertTrue(lobby.teams[0].players[0].ready)
Beispiel #2
0
 def test_set_class(self):
     from lobbypy.models import Player, Lobby
     player = self.session.query(Player).first()
     lobby = Lobby('Lobby', player, '', '', '')
     lobby.set_team(player, 0)
     self.session.add(lobby)
     transaction.commit()
     from lobbypy.controllers import set_class
     lobby = self.session.merge(lobby)
     player = self.session.merge(player)
     set_class(self.session, lobby, player, 1)
     transaction.commit()
     lobby = self.session.merge(lobby)
     player = self.session.merge(player)
     self.assertEquals(lobby.teams[0].players[0].cls, 1)
 def _makeLobby(self, name='test', owner=None, server_address='test',
         game_map='test', password='******', teams=lambda: []):
     from lobbypy.models import Lobby
     if owner is None:
         from lobbypy.models import Player
         owner = Player('')
     return Lobby(name, owner, server_address, game_map, password, teams)
Beispiel #4
0
 def on_create_lobby(self, name, server_address, rcon_password, game_map):
     """Create and join lobby"""
     assert g.player
     assert not self.lobby_id
     # Check server
     if current_app.config.get('RCON_CHECK_SERVER', True):
         try:
             sr = connect_rcon(server_address, rcon_password)
             sq = connect_query(server_address)
         except RconException:
             return False, 'bad_pass'
         except Exception:
             return False, 'server_issue'
         else:
             if not check_map(sr):
                 return False, 'map_dne'
             # TODO: ask if you want to kick players
             if not check_players(sq):
                 return False, 'players'
             sr.close()
             sq.close()
     # Leave or delete old lobbies
     lobby_deletes = leave_or_delete_all_lobbies(g.player)
     # TODO: pull/generate password from list
     lobby = Lobby(name, g.player, server_address, rcon_password, game_map, 'password')
     lobby.join(g.player)
     db.session.add(lobby)
     db.session.commit()
     # Send event to redis
     self.broadcast_event('/lobby/', 'create', make_lobby_item_dict(lobby))
     # Send leave or deletes
     [self.broadcast_leave_or_delete(*l_d) for l_d in lobby_deletes]
     # Update ACL
     self.add_acl_method('on_set_team')
     self.add_acl_method('on_leave')
     self.del_acl_method('on_create_lobby')
     self.del_acl_method('on_join')
     self.add_acl_method('on_kick')
     self.add_acl_method('on_set_lobby_name')
     self.add_acl_method('on_set_team_name')
     # Set lobby id and start listening on redis
     self.lobby_id = lobby.id
     self.listener_job = self.spawn(self.listener, '/lobby/%d' % lobby.id)
     current_app.logger.info('Player %d created Lobby %d' % (g.player.id,
         lobby.id))
     return True, lobby.id
Beispiel #5
0
 def _makeLobby(self,
                owner=None,
                name='Lobby',
                server_address='server:9999',
                game_map='cp_lololol',
                password='******'):
     from lobbypy.models import Lobby
     if owner is None:
         from lobbypy.models import Player
         owner = Player('-1')
     return Lobby(name, owner, server_address, game_map, password, teams=[])
 def test_on_leave(self, magic_make, magic_item_make, magic_broadcast):
     instance = self._makeOne()
     o = Player('')
     l = Lobby('', o, '', '', '', '')
     p = Player('0')
     l.join(p)
     db.session.add(o)
     db.session.add(l)
     db.session.add(p)
     db.session.commit()
     instance.lobby_id = l.id
     instance.ctx.g.player = p
     instance.allowed_methods = set(['on_leave',
         'recv_connect', 'on_set_team'])
     instance.listener_job = MagicMock()
     instance.on_leave()
     self.assertEqual(instance.allowed_methods,
             set(['on_join', 'on_create_lobby', 'recv_connect']))
     self.assertTrue(instance.lobby_id is None)
     self.assertEqual(len(l.spectators), 0)
     instance.listener_job.kill.assert_called_once()
 def test_on_set_team(self, magic_make, magic_item_make, magic_broadcast):
     instance = self._makeOne()
     o = Player('')
     l = Lobby('', o, '', '', '', '')
     t = Team('Red')
     l.teams.append(t)
     p = Player('0')
     l.join(p)
     db.session.add(o)
     db.session.add(l)
     db.session.add(t)
     db.session.add(p)
     db.session.commit()
     instance.lobby_id = l.id
     instance.ctx.g.player = p
     instance.allowed_methods = set(['on_leave',
         'recv_connect', 'on_set_team'])
     instance.on_set_team(0)
     self.assertEqual(instance.allowed_methods, set(['on_leave',
         'recv_connect', 'on_set_class', 'on_toggle_ready', 'on_set_team']))
     self.assertEqual(l.teams[0].players[0].player, p)
 def test_on_get_lobby_listing(self, magic_emit,
         magic_make_lobby_dict, magic_Lobby):
     from lobbypy.models import Lobby, Player
     p = Player('0')
     lobbies = [
             Lobby('A', p, '', '', '', teams=[]),
             Lobby('B', p, '', '', '', teams=[]),
             ]
     magic_Lobby.query.all.return_value = lobbies
     lobbies_dict = [
             {'name': 'A'},
             {'name': 'B'},
             ]
     json_rvs = list(lobbies_dict)
     def side_effect(*args, **kwargs):
         return json_rvs.pop(0)
     magic_make_lobby_dict.side_effect = side_effect
     instance = self._makeOne()
     rvs = instance.on_get_lobby_listing()
     self.assertTrue(rvs[0])
     self.assertEqual(rvs[1], lobbies_dict)
Beispiel #9
0
 def post(self):
     o_id = request.form['owner_id']
     name = request.form['name']
     server_address = request.form['server_address']
     game_map = request.form['game_map']
     password = request.form['password']
     o = Player.query.get(o_id)
     if not o:
         abort(405)
     l = Lobby(name, o, server_address, game_map, password)
     db.session.add(l)
     db.session.commit()
     return jsonify(201, id=l.id)
Beispiel #10
0
 def test_with_owned_lobbies(self):
     from lobbypy.models import Player, Lobby
     p = Player('0')
     db.session.add(p)
     l = Lobby('test', p, 'test', 'test', 'test', teams=[])
     db.session.add(l)
     db.session.commit()
     rv = self._callFUT(p)
     l_dels = []
     for l_del in rv:
         l_dels.append(l_del)
     self.assertEqual(len(l_dels), 1)
     self.assertEqual(l_dels[0], (l, True))
Beispiel #11
0
 def test_with_speced_lobbies(self):
     from lobbypy.models import Player, Lobby
     o = Player('1')
     p = Player('0')
     db.session.add(o)
     db.session.add(p)
     l = Lobby('test', o, 'test', 'test', 'test', teams=[])
     l.spectators.append(p)
     db.session.add(l)
     db.session.commit()
     rv = self._callFUT(p)
     l_dels = []
     for l_del in rv:
         l_dels.append(l_del)
     self.assertEqual(len(l_dels), 1)
     self.assertEqual(l_dels[0], (l, False))
Beispiel #12
0
 def test_with_teamed_lobbies(self):
     from lobbypy.models import Player, Lobby, Team
     o = Player('1')
     p = Player('0')
     db.session.add(o)
     db.session.add(p)
     l = Lobby('test', o, 'test', 'test', 'test', teams=[])
     t = Team('Red')
     l.teams.append(t)
     l.teams[0].join(p)
     db.session.add(l)
     db.session.commit()
     rv = self._callFUT(p)
     l_dels = []
     for l_del in rv:
         l_dels.append(l_del)
     self.assertEqual(len(l_dels), 1)
     self.assertEqual(l_dels[0], (l, False))