def loads(self, pid, data): # load the data into a dictionary and then set a Pug object's fields data_dict = json.loads(data) pug = Pug.Pug() for key in data_dict: if (key == u'player_votes' or key == u'_players' or key == u'player_stats' or key == u'game_stats' or key == u'end_stats'): # these keys are dictionaries, so we want to do them slightly # different. i.e convert unicode keys back to longs tmp = {} for itemkey in data_dict[key]: tmp[long(itemkey)] = data_dict[key][itemkey] data_dict[key] = tmp elif key == u"teams": # we should convert teams back to a set! tmp = {} for team in data_dict[key]: tmp[team] = set(data_dict[key][team]) data_dict[key] = tmp setattr(pug, str(key), data_dict[key]) pug.id = pid return pug
def test_helpers(): print "Testing helper methods" pug = Pug.Pug() pug.add_player(1L, "1", PlayerStats()) pug._add_to_team("blue", 1L) assert pug.player_team(1L) == "blue" assert pug.has_player(1L) == True assert pug.player_list() == [ 1L ] assert pug.is_admin(1L) == True assert pug.player_role(1L) is None pug.medics["blue"] = 1L assert pug.player_role(1L) == "Medic" assert pug.player_name(1L) == "1" assert pug.get_state_string() == "GATHERING_PLAYERS" assert pug.teams_done == False assert pug.full == False assert pug.player_count == 1 assert pug.game_started == False assert pug.game_over == False assert pug.password is None assert pug.has_disconnects == False assert pug.replacement_required == False assert pug.replacement_timed_out == False assert pug.map_available(Pug.AVAILABLE_MAPS[0]) == True assert len(pug.map_votes) == 0
def setUp(self): self.server = Server.Server("TF2") self.server.rcon_connection = RconConnection() self.server.ip = "202.138.3.55" self.server.port = 27045 self.server.rcon_password = "******" self.pug = Pug.Pug(pid=1) # add some players to the pug for stats testing... self.pug.add_player(76561197960265729, "1", Pug.PlayerStats()) self.pug.add_player(76561197960265730, "2", Pug.PlayerStats()) self.pug.add_player(76561197960265731, "3", Pug.PlayerStats()) for i in xrange(9): stat = Pug.PlayerStats() self.pug.add_player(i, str(i), stat) self.pug.begin_map_vote() self.pug.end_map_vote() self.pug.shuffle_teams() self.server.reserve(self.pug) self.server.prepare() self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket.connect(('127.0.0.1', self.server.log_port))
def test_shuffle_teams(): print "Testing team creation (shuffle)" pug = Pug.Pug() # build a team with predictable stats & outcome. 1L and 2L are medics pug.add_player(1L, "1", PlayerStats(rating = 1600, games_since_medic = 5)) pug.add_player(2L, "2", PlayerStats(rating = 1600, games_since_medic = 5)) pug.add_player(3L, "3", PlayerStats(rating = 1800)) pug.add_player(4L, "4", PlayerStats(rating = 1800)) pug.add_player(5L, "5", PlayerStats(rating = 1770)) pug.add_player(6L, "6", PlayerStats(rating = 1750)) pug.add_player(7L, "7", PlayerStats(rating = 1700)) pug.add_player(8L, "8", PlayerStats(rating = 1900)) pug.add_player(9L, "9", PlayerStats(rating = 1500)) pug.add_player(10L, "10", PlayerStats(rating = 1650)) pug.add_player(11L, "11", PlayerStats(rating = 1620)) pug.add_player(12L, "12", PlayerStats(rating = 1680)) pug.shuffle_teams() assert pug.state == Pug.states["TEAMS_SHUFFLED"] # 2 potential team lineups for each team due to medics being randomly # shuffled red_team1 = set([1L, 3L, 5L, 7L, 11L, 12L]) red_team2 = set([2L, 3L, 5L, 7L, 11L, 12L]) blue_team1 = set([1L, 4L, 6L, 8L, 9L, 10L]) blue_team2 = set([2L, 4L, 6L, 8L, 9L, 10L]) assert 1L in pug.medics.values() and 2L in pug.medics.values() assert pug.teams["blue"] == blue_team1 or pug.teams["blue"] == blue_team2 assert pug.teams["red"] == red_team1 or pug.teams["red"] == red_team2
def test_add_player(): print "Testing add_player" pug = Pug.Pug() pug.add_player(1L, "1", PlayerStats()) assert 1L in pug._players assert 1L in pug.player_stats assert 1L in pug.game_stats
def test_update_end_stats(): print "Testing end of game stat update" pug = Pug.Pug() start = PlayerStats(kills = 1, deaths = 1, losses = 1, winstreak = 2) pug.add_player(1L, "1", start) pug._add_to_team("blue", 1L) pug.begin_game() pug.update_game_stat(1L, "kills", 1) pug.update_game_stat(1L, "deaths", 1) pug.update_score("blue", 1) pug.set_player_rating(1L, 1600) game = PlayerStats(kills = 1, deaths = 1, wins = 1, games_played = 1, games_since_medic = 1, winstreak = 3, rating = 1600) end = PlayerStats(kills = 2, deaths = 2, wins = 1, losses = 1, games_played = 1, games_since_medic = 1, winstreak = 3, rating = 1600) pug.update_end_stats() # now we do our asserts assert pug.player_stats[1L] == start assert pug.game_stats[1L] == game assert pug.end_stats[1L] == end
def create_pug(self, player_id, player_name, size=12, pug_map=None, custom_id=None, restriction=None): """ This method is used to create a new pug. Size and map are optional. If the player is already in a pug, is banned, or does not meet the restriction themselves, an exception is raised. :param player_id The ID of the player to add :param player_name The name of the player to add :param size The size of the pug (max number of players) :param map The map the pug will be on. If none, it means a vote will occur once the pug is full. :param custom_id A custom ID to assign to the pug. Useful for clients to distinguish pugs if necessary. :param restriction A rating restriction to enforce. If a positive number is given, players must have >= that rating. If a negative number is given, players must have < that rating. :return Pug The newly created pug """ if pug_map is not None and (not Pug.Pug.map_available(pug_map)): raise InvalidMapException("Invalid map specified") pug = Pug.Pug(size=size, pmap=pug_map, custom_id=custom_id, restriction=restriction) # try to add the player to the newly created pug. if the player is # banned, restricted, or in another pug, _add_player will raise an # exception. The pug is not flushed to the database by _add_player. self._add_player(pug, player_id, player_name) # if we've reached here, player was successfully added to the pug. # check if we can get a server or not. if not, raise an exception and # escape before we add the pug to the internal list and flush it. server = self.server_manager.allocate(pug) # If the server returned is None, there are no servers available. # Therefore, we raise an exception. Else, code continues and player # gets added/pug gets flushed if server is None: raise NoAvailableServersException("No more servers are available") self._pugs.append(pug) self._flush_pug(pug) # prepare the server for pug (empty it, set pw, update pug id, etc) self.server_manager.prepare(server) return pug
def fill_pug(): pug = Pug.Pug() for i in xrange(1,13): pug.add_player(i, str(i), PlayerStats()) assert pug.full == True return pug
def test_map_vote(): print "Testing map vote with player vote" pug = Pug.Pug() pug.add_player(1, str(1), PlayerStats()) pug.begin_map_vote() assert pug.state == Pug.states["MAP_VOTING"] last_map = "" for m in pug.maps: pug.vote_map(1, m) assert pug.player_votes[1] == m assert len(pug.map_votes) > 0 assert pug.map_votes.most_common(1)[0] == (m, 1) last_map = m pug.end_map_vote() assert pug.state == Pug.states["MAPVOTE_COMPLETED"] assert pug.map == last_map #------------------------------------------------------ print "Testing map vote with no vote" pug = Pug.Pug() pug.begin_map_vote() assert pug.state == Pug.states["MAP_VOTING"] pug.end_map_vote() assert pug.state == Pug.states["MAPVOTE_COMPLETED"] assert pug.map is not None #------------------------------------------------------ print "Testing map vote with forced map" pug = Pug.Pug() pug.force_map("cp_granary") pug.begin_map_vote() assert pug.state == Pug.states["MAPVOTE_COMPLETED"] assert pug.map == "cp_granary" pug = Pug.Pug() pug.begin_map_vote() assert pug.state == Pug.states["MAP_VOTING"] pug.force_map("cp_granary") pug.end_map_vote() assert pug.state == Pug.states["MAPVOTE_COMPLETED"] assert pug.map == "cp_granary"
def test_player_restriction(): pug = Pug.Pug() print "Testing player restriction. Min rating: 100" pug.player_restriction = 100 assert pug.player_restricted(120) == False assert pug.player_restricted(100) == False assert pug.player_restricted(90) == True pug.player_restriction = -100 assert pug.player_restricted(100) == True assert pug.player_restricted(120) == True assert pug.player_restricted(90) == False
def test_remove_player(): print "Testing remove_player" pug = Pug.Pug() pug.add_player(1L, "1", PlayerStats()) # we need to make sure the map vote is removed too pug.begin_map_vote() pug.vote_map(1L, "cp_badlands") pug.remove_player(1L) assert 1L not in pug._players assert 1L not in pug.player_stats assert 1L not in pug.game_stats assert 1L not in pug.player_votes # test replacement trigger pug = fill_pug() pug.shuffle_teams() state = pug.state player_team = pug.player_team(1L) pug.remove_player(1L) assert pug.full == False assert pug.state == Pug.states["REPLACEMENT_REQUIRED"] assert pug._previous_state == state assert len(pug.teams[player_team]) == pug.size/2 - 1 leaver_ids = [ p["id"] for p in pug.leaver_record ] assert 1L in leaver_ids replace_time = pug.replacement_time replace_timeout = pug.replacement_timeout prev_state = pug._previous_state # test double remove (i.e. need 2 replacements) player_team = pug.player_team(2L) pug.remove_player(2L) assert pug.full == False assert pug.state == Pug.states["REPLACEMENT_REQUIRED"] assert pug.replacement_time == replace_time assert pug.replacement_timeout == replace_timeout assert pug._previous_state == prev_state leaver_ids = [ p["id"] for p in pug.leaver_record ] assert 2L in leaver_ids
def test_update_game_stats(): print "Testing game stat update" pug = Pug.Pug() pug.add_player(1L, "1", PlayerStats()) pug.begin_game() assert pug.game_stats[1L] == PlayerStats() assert pug.game_stats[1L] is pug._get_game_stats(1L) assert pug.game_stats[1L]["kills"] == 0 pug.update_game_stat(1L, "kills", 1) assert pug.game_stats[1L]["kills"] == 1 pug.update_game_stat(1L, "kills", 1) assert pug.game_stats[1L]["kills"] == 2 pug.update_game_stat(1L, "kills", 10, increment = False) assert pug.game_stats[1L]["kills"] == 10
def test_update_score(): print "Testing team score update" pug = Pug.Pug() assert pug.game_scores["blue"] == pug.game_scores["red"] == 0 # Ensure no update before pug starts pug.update_score("blue", 2) pug.update_score("red", 3) assert pug.game_scores["blue"] == pug.game_scores["red"] == 0 pug.begin_game() assert pug.state == Pug.states["GAME_STARTED"] pug.update_score("blue", 2) assert pug.game_scores["blue"] == 2 assert pug.game_scores["red"] == 0 pug.update_score("red", 3) assert pug.game_scores["blue"] == 2 assert pug.game_scores["red"] == 3
def test_add_to_team(): print "Testing add_to_team" pug = Pug.Pug() pug.add_player(1L, str(1L), PlayerStats()) pug.add_player(2L, str(2L), PlayerStats()) pug.add_player(3L, str(3L), PlayerStats()) pug.add_player(4L, str(4L), PlayerStats()) pug._add_to_team("blue", 1L) assert pug.teams["blue"] == set([ 1L ]) assert pug.team_ratings["blue"] == PlayerStats()["rating"] assert pug.player_team(1L) == "blue" pug._add_to_team("blue", [2L, 3L]) assert pug.teams["blue"] == set([ 1L, 2L, 3L ]) assert pug.team_ratings["blue"] == PlayerStats()["rating"]*3 pug._add_to_team("blue", [ 4L ]) assert pug.teams["blue"] == set([ 1L, 2L, 3L, 4L ]) assert pug.team_ratings["blue"] == PlayerStats()["rating"]*4
def test_remove_from_team(): print "Testing remove_from_team" pug = Pug.Pug() pug.add_player(1L, str(1L), PlayerStats()) pug.add_player(2L, str(2L), PlayerStats()) pug.add_player(3L, str(3L), PlayerStats()) pug.add_player(4L, str(4L), PlayerStats()) assert pug.team_ratings["blue"] == 0 pug._add_to_team("blue", [ 1L, 2L, 3L, 4L ]) pug._remove_from_team("blue", [ 1L ]) assert pug.teams["blue"] == set([ 2L, 3L, 4L ]) assert pug.team_ratings["blue"] == PlayerStats()["rating"]*3 pug._remove_from_team("blue", 2L) assert pug.teams["blue"] == set([ 3L, 4L ]) assert pug.team_ratings["blue"] == PlayerStats()["rating"]*2 pug._remove_from_team("blue", [ 3L, 4L ]) assert pug.teams["blue"] == set([]) assert pug.team_ratings["blue"] == 0
from entities import Pug, Server from pprint import pprint s = Server.Server("TF2") p = Pug.Pug(pid = 1) p.add_player(76561197960265729, "1", Pug.PlayerStats()) p.add_player(76561197960265730, "2", Pug.PlayerStats()) p.add_player(76561197960265731, "3", Pug.PlayerStats()) p.begin_game() #pprint(p.game_stats) s.reserve(p) s.prepare() log = s._log_interface kill = 'L 10/01/2012 - 22:20:45: "1<0><[U:1:1]><Blue>" killed "2<2><[U:1:2]><Red>" with "scattergun" (attacker_position "-1803 129 236") (victim_position "-1767 278 218")' log._dispatch_parse(kill) pprint(p.game_stats)
settings.db_port) conn = psycopg2.pool.SimpleConnectionPool(dsn=dsn, minconn=1, maxconn=1) dbif = PSQLDatabaseInterface(conn) PLAYER_IDS = [1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L] api_key = "123abc" server_group = 1 player_stats = {} for cid in PLAYER_IDS: player_stats[cid] = Pug.PlayerStats() pugs = [Pug.Pug()] # we want to use the various methods supplied in PSQLDatabaseInterface and # see if they have the correct output def test_user_info(): # single user print "User info for '%s':" % api_key user = dbif.get_user_info(api_key) pprint(user) # all users print "All users:" users = dbif.get_user_info() pprint(users)