def test_visibility_states(): states = [("public", VisibilityState.PUBLIC), ("friends", VisibilityState.FRIENDS)] for string_value, enum_value in states: assert (VisibilityState.from_string(string_value) == enum_value and VisibilityState.to_string(enum_value) == string_value)
def test_visibility_states(): states = [("public", VisibilityState.PUBLIC), ("friends", VisibilityState.FRIENDS)] for string_value, enum_value in states: assert (VisibilityState.from_string(string_value) == enum_value and VisibilityState.to_string(enum_value) == string_value)
def command_game_host(self, message): if not self.able_to_launch_game: raise ClientError( "You are already in a game or haven't run the connectivity test yet" ) if self.connectivity.result.state == ConnectivityState.STUN: self.connectivity.relay_address = Address( *message['relay_address']) assert isinstance(self.player, Player) title = cgi.escape(message.get('title', '')) port = message.get('gameport') visibility = VisibilityState.from_string(message.get('visibility')) if not isinstance(visibility, VisibilityState): # Protocol violation. self.abort("%s sent a nonsense visibility code: %s" % (self.player.login, message.get('visibility'))) return mod = message.get('mod') try: title.encode('ascii') except UnicodeEncodeError: self.sendJSON( dict(command="notice", style="error", text="Non-ascii characters in game name detected.")) return mapname = message.get('mapname') password = message.get('password') game = self.game_service.create_game( **{ 'visibility': VisibilityState.to_string(visibility), 'game_mode': mod.lower(), 'host': self.player, 'name': title if title else self.player.login, 'mapname': mapname, 'password': password }) self.launch_game(game, port, True) server.stats.incr('game.hosted')
def command_game_host(self, message): if not self.able_to_launch_game: raise ClientError("You are already in a game or haven't run the connectivity test yet") if self.connectivity.result.state == ConnectivityState.STUN: self.connectivity.relay_address = Address(*message['relay_address']) assert isinstance(self.player, Player) title = cgi.escape(message.get('title', '')) port = message.get('gameport') visibility = VisibilityState.from_string(message.get('visibility')) if not isinstance(visibility, VisibilityState): # Protocol violation. self.abort("%s sent a nonsense visibility code: %s" % (self.player.login, message.get('visibility'))) return mod = message.get('mod') try: title.encode('ascii') except UnicodeEncodeError: self.sendJSON(dict(command="notice", style="error", text="Non-ascii characters in game name detected.")) return mapname = message.get('mapname') password = message.get('password') game = self.game_service.create_game(**{ 'visibility': VisibilityState.to_string(visibility), 'game_mode': mod.lower(), 'host': self.player, 'name': title if title else self.player.login, 'mapname': mapname, 'password': password }) self.launch_game(game, port, True) server.stats.incr('game.hosted')
async def test_to_dict(game, create_player): await game.clear_data() game.state = GameState.LOBBY players = [( create_player(**info), result, team ) for info, result, team in [ (dict(login='******', id=1, global_rating=Rating(1500, 250.7)), 0, 1), (dict(login='******', id=2, global_rating=Rating(1700, 120.1)), 0, 1), (dict(login='******', id=3, global_rating=Rating(1200, 72.02)), 0, 2), (dict(login='******', id=4, global_rating=Rating(1200, 72.02)), 0, 2), ]] add_connected_players(game, [player for player, _, _ in players]) for player, _, team in players: game.set_player_option(player.id, 'Team', team) game.set_player_option(player.id, 'Army', player.id - 1) game.host = players[0][0] await game.launch() data = game.to_dict() expected = { "command": "game_info", "visibility": VisibilityState.to_string(game.visibility), "password_protected": game.password is not None, "uid": game.id, "title": game.name, "state": 'playing', "featured_mod": game.game_mode, "featured_mod_versions": game.getGamemodVersion(), "sim_mods": game.mods, "mapname": game.map_folder_name, "map_file_path": game.map_file_path, "host": game.host.login, "num_players": len(game.players), "game_type": game.gameType, "max_players": game.max_players, "launched_at": game.launched_at, "teams": { team: [ player.login for player in game.players if game.get_player_option(player.id, 'Team') == team ] for team in game.teams } } assert data == expected
async def test_to_dict(game, create_player): await game.clear_data() game.state = GameState.LOBBY players = [ (create_player(**info), result, team) for info, result, team in [ (dict(login='******', id=1, global_rating=Rating(1500, 250.7)), 0, 1), (dict(login='******', id=2, global_rating=Rating(1700, 120.1)), 0, 1), (dict(login='******', id=3, global_rating=Rating(1200, 72.02)), 0, 2), (dict(login='******', id=4, global_rating=Rating(1200, 72.02)), 0, 2), ] ] add_connected_players(game, [player for player, _, _ in players]) for player, _, team in players: game.set_player_option(player.id, 'Team', team) game.set_player_option(player.id, 'Army', player.id - 1) game.host = players[0][0] await game.launch() data = game.to_dict() expected = { "command": "game_info", "visibility": VisibilityState.to_string(game.visibility), "password_protected": game.password is not None, "uid": game.id, "title": game.name, "state": 'playing', "featured_mod": game.game_mode, "featured_mod_versions": game.getGamemodVersion(), "sim_mods": game.mods, "mapname": game.map_folder_name, "map_file_path": game.map_file_path, "host": game.host.login, "num_players": len(game.players), "game_type": game.gameType, "max_players": game.max_players, "launched_at": game.launched_at, "teams": { team: [player.login for player in game.players if game.get_player_option(player.id, 'Team') == team] for team in game.teams } } assert data == expected
async def test_to_dict(game, player_factory): game.state = GameState.LOBBY players = [(player_factory(f"{i}", player_id=i, global_rating=rating), result, team) for i, (rating, result, team) in enumerate([ (Rating(1500, 250), 0, 1), (Rating(1700, 120), 0, 1), (Rating(1200, 72), 0, 2), (Rating(1200, 72), 0, 2), ], 1)] add_connected_players(game, [player for player, _, _ in players]) for player, _, team in players: game.set_player_option(player.id, 'Team', team) game.set_player_option(player.id, 'Army', player.id - 1) game.host = players[0][0] await game.launch() data = game.to_dict() expected = { "command": "game_info", "visibility": VisibilityState.to_string(game.visibility), "password_protected": game.password is not None, "uid": game.id, "title": game.sanitize_name(game.name), "state": 'playing', "featured_mod": game.game_mode, "sim_mods": game.mods, "mapname": game.map_folder_name, "map_file_path": game.map_file_path, "host": game.host.login, "num_players": len(game.players), "max_players": game.max_players, "launched_at": game.launched_at, "teams": { team: [ player.login for player in game.players if game.get_player_option(player.id, 'Team') == team ] for team in game.teams } } assert data == expected