]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/submission.py
Simplify the should_do_weapon_stats function.
[xonotic/xonstat.git] / xonstat / views / submission.py
index a7ca8d29ea7f20e280647bec16e48c47be70d4de..fbfb960f1b455f9145c8bf320e1be53c84f503bc 100644 (file)
@@ -1,10 +1,10 @@
 import calendar
+import collections
 import datetime
 import logging
 import re
 
 import pyramid.httpexceptions
-import sqlalchemy.sql.expression as expr
 from sqlalchemy import Sequence
 from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
 from xonstat.elo import EloProcessor
@@ -16,79 +16,253 @@ from xonstat.util import strip_colors, qfont_decode, verify_request, weapon_map
 log = logging.getLogger(__name__)
 
 
-def parse_stats_submission(body):
-    """
-    Parses the POST request body for a stats submission
-    """
-    # storage vars for the request body
-    game_meta = {}
-    events = {}
-    players = []
-    teams = []
+class Submission(object):
+    """Parses an incoming POST request for stats submissions."""
+
+    def __init__(self, body, headers):
+        # a copy of the HTTP headers
+        self.headers = headers
+
+        # a copy of the HTTP POST body
+        self.body = body
+
+        # the submission code version (from the server)
+        self.version = None
+
+        # the revision string of the server
+        self.revision = None
+
+        # the game type played
+        self.game_type_cd = None
+
+        # the active game mod
+        self.mod = None
+
+        # the name of the map played
+        self.map_name = None
+
+        # unique identifier (string) for a match on a given server
+        self.match_id = None
+
+        # the name of the server
+        self.server_name = None
+
+        # the number of cvars that were changed to be different than default
+        self.impure_cvar_changes = None
+
+        # the port number the game server is listening on
+        self.port_number = None
+
+        # how long the game lasted
+        self.duration = None
+
+        # which ladder is being used, if any
+        self.ladder = None
+
+        # players involved in the match (humans, bots, and spectators)
+        self.players = []
+
+        # raw team events
+        self.teams = []
+
+        # the parsing deque (we use this to allow peeking)
+        self.q = collections.deque(self.body.split("\n"))
+
+        ############################################################################################
+        # Below this point are fields useful in determining if the submission is valid or
+        # performance optimizations that save us from looping over the events over and over again.
+        ############################################################################################
+
+        # humans who played in the match
+        self.humans = []
+
+        # bots who played in the match
+        self.bots = []
+
+        # distinct weapons that we have seen fired
+        self.weapons = set()
 
-    # we're not in either stanza to start
-    in_P = in_Q = False
+        # has a human player fired a shot?
+        self.human_fired_weapon = False
 
-    for line in body.split('\n'):
+        # does any human have a non-zero score?
+        self.human_nonzero_score = False
+
+        # does any human have a fastest cap?
+        self.human_fastest = False
+
+    def next_item(self):
+        """Returns the next key:value pair off the queue."""
         try:
-            (key, value) = line.strip().split(' ', 1)
-
-            # Server (S) and Nick (n) fields can have international characters.
-            if key in 'S' 'n':
-                value = unicode(value, 'utf-8')
-
-            if key not in 'P' 'Q' 'n' 'e' 't' 'i':
-                game_meta[key] = value
-
-            if key == 'Q' or key == 'P':
-                #log.debug('Found a {0}'.format(key))
-                #log.debug('in_Q: {0}'.format(in_Q))
-                #log.debug('in_P: {0}'.format(in_P))
-                #log.debug('events: {0}'.format(events))
-
-                # check where we were before and append events accordingly
-                if in_Q and len(events) > 0:
-                    #log.debug('creating a team (Q) entry')
-                    teams.append(events)
-                    events = {}
-                elif in_P and len(events) > 0:
-                    #log.debug('creating a player (P) entry')
-                    players.append(events)
-                    events = {}
-
-                if key == 'P':
-                    #log.debug('key == P')
-                    in_P = True
-                    in_Q = False
-                elif key == 'Q':
-                    #log.debug('key == Q')
-                    in_P = False
-                    in_Q = True
-
-                events[key] = value
-
-            if key == 'e':
-                (subkey, subvalue) = value.split(' ', 1)
-                events[subkey] = subvalue
-            if key == 'n':
-                events[key] = value
-            if key == 't':
-                events[key] = value
+            items = self.q.popleft().strip().split(' ', 1)
+            if len(items) == 1:
+                # Some keys won't have values, like 'L' records where the server isn't actually
+                # participating in any ladders. These can be safely ignored.
+                return None, None
+            else:
+                return items
         except:
-            # no key/value pair - move on to the next line
-            pass
-
-    # add the last entity we were working on
-    if in_P and len(events) > 0:
-        players.append(events)
-    elif in_Q and len(events) > 0:
-        teams.append(events)
+            return None, None
+
+    def add_weapon_fired(self, sub_key):
+        """Adds a weapon to the set of weapons fired during the match (a set)."""
+        self.weapons.add(sub_key.split("-")[1])
+
+    @staticmethod
+    def is_human_player(player):
+        """
+        Determines if a given set of events correspond with a non-bot
+        """
+        return not player['P'].startswith('bot')
+
+    @staticmethod
+    def played_in_game(player):
+        """
+        Determines if a given set of player events correspond with a player who
+        played in the game (matches 1 and scoreboardvalid 1)
+        """
+        return 'matches' in player and 'scoreboardvalid' in player
+
+    def parse_player(self, key, pid):
+        """Construct a player events listing from the submission."""
+
+        # all of the keys related to player records
+        player_keys = ['i', 'n', 't', 'e']
+
+        player = {key: pid}
+
+        player_fired_weapon = False
+        player_nonzero_score = False
+        player_fastest = False
+
+        # Consume all following 'i' 'n' 't'  'e' records
+        while len(self.q) > 0:
+            (key, value) = self.next_item()
+            if key is None and value is None:
+                continue
+            elif key == 'e':
+                (sub_key, sub_value) = value.split(' ', 1)
+                player[sub_key] = sub_value
+
+                if sub_key.endswith("cnt-fired"):
+                    player_fired_weapon = True
+                    self.add_weapon_fired(sub_key)
+                elif sub_key == 'scoreboard-score' and int(sub_value) != 0:
+                    player_nonzero_score = True
+                elif sub_key == 'scoreboard-fastest':
+                    player_fastest = True
+            elif key == 'n':
+                player[key] = unicode(value, 'utf-8')
+            elif key in player_keys:
+                player[key] = value
+            else:
+                # something we didn't expect - put it back on the deque
+                self.q.appendleft("{} {}".format(key, value))
+                break
+
+        played = self.played_in_game(player)
+        human = self.is_human_player(player)
+
+        if played and human:
+            self.humans.append(player)
+
+            if player_fired_weapon:
+                self.human_fired_weapon = True
+
+            if player_nonzero_score:
+                self.human_nonzero_score = True
+
+            if player_fastest:
+                self.human_fastest = True
+
+        elif played and not human:
+            self.bots.append(player)
+
+        self.players.append(player)
+
+    def parse_team(self, key, tid):
+        """Construct a team events listing from the submission."""
+        team = {key: tid}
+
+        # Consume all following 'e' records
+        while len(self.q) > 0 and self.q[0].startswith('e'):
+            (_, value) = self.next_item()
+            (sub_key, sub_value) = value.split(' ', 1)
+            team[sub_key] = sub_value
+
+        self.teams.append(team)
+
+    def parse(self):
+        """Parses the request body into instance variables."""
+        while len(self.q) > 0:
+            (key, value) = self.next_item()
+            if key is None and value is None:
+                continue
+            elif key == 'V':
+                self.version = value
+            elif key == 'R':
+                self.revision = value
+            elif key == 'G':
+                self.game_type_cd = value
+            elif key == 'O':
+                self.mod = value
+            elif key == 'M':
+                self.map_name = value
+            elif key == 'I':
+                self.match_id = value
+            elif key == 'S':
+                self.server_name = unicode(value, 'utf-8')
+            elif key == 'C':
+                self.impure_cvar_changes = int(value)
+            elif key == 'U':
+                self.port_number = int(value)
+            elif key == 'D':
+                self.duration = datetime.timedelta(seconds=int(round(float(value))))
+            elif key == 'L':
+                self.ladder = value
+            elif key == 'Q':
+                self.parse_team(key, value)
+            elif key == 'P':
+                self.parse_player(key, value)
+            else:
+                raise Exception("Invalid submission")
+
+        return self
+
+    def __repr__(self):
+        """Debugging representation of a submission."""
+        return "game_type_cd: {}, mod: {}, players: {}, humans: {}, bots: {}, weapons: {}".format(
+            self.game_type_cd, self.mod, len(self.players), len(self.humans), len(self.bots),
+            self.weapons)
+
+
+def elo_submission_category(submission):
+    """Determines the Elo category purely by what is in the submission data."""
+    mod = submission.mod
+
+    vanilla_allowed_weapons = {"shotgun", "devastator", "blaster", "mortar", "vortex", "electro",
+                               "arc", "hagar", "crylink", "machinegun"}
+    insta_allowed_weapons = {"vaporizer", "blaster"}
+    overkill_allowed_weapons = {"hmg", "vortex", "shotgun", "blaster", "machinegun", "rpc"}
+
+    if mod == "Xonotic":
+        if len(submission.weapons - vanilla_allowed_weapons) == 0:
+            return "vanilla"
+    elif mod == "InstaGib":
+        if len(submission.weapons - insta_allowed_weapons) == 0:
+            return "insta"
+    elif mod == "Overkill":
+        if len(submission.weapons - overkill_allowed_weapons) == 0:
+            return "overkill"
+    else:
+        return "general"
 
-    return (game_meta, players, teams)
+    return "general"
 
 
-def is_blank_game(gametype, players):
-    """Determine if this is a blank game or not. A blank game is either:
+def is_blank_game(submission):
+    """
+    Determine if this is a blank game or not. A blank game is either:
 
     1) a match that ended in the warmup stage, where accuracy events are not
     present (for non-CTS games)
@@ -105,40 +279,24 @@ def is_blank_game(gametype, players):
 
     1) a match in which no player made a positive or negative score
     """
-    r = re.compile(r'acc-.*-cnt-fired')
-    flg_nonzero_score = False
-    flg_acc_events = False
-    flg_fastest_lap = False
-
-    for events in players:
-        if is_real_player(events) and played_in_game(events):
-            for (key,value) in events.items():
-                if key == 'scoreboard-score' and value != 0:
-                    flg_nonzero_score = True
-                if r.search(key):
-                    flg_acc_events = True
-                if key == 'scoreboard-fastest':
-                    flg_fastest_lap = True
-
-    if gametype == 'cts':
-        return not flg_fastest_lap
-    elif gametype == 'nb':
-        return not flg_nonzero_score
+    if submission.game_type_cd == 'cts':
+        return not submission.human_fastest
+    elif submission.game_type_cd == 'nb':
+        return not submission.human_nonzero_score
     else:
-        return not (flg_nonzero_score and flg_acc_events)
+        return not (submission.human_nonzero_score and submission.human_fired_weapon)
 
 
-def get_remote_addr(request):
-    """Get the Xonotic server's IP address"""
-    if 'X-Forwarded-For' in request.headers:
-        return request.headers['X-Forwarded-For']
-    else:
-        return request.remote_addr
+def has_required_metadata(submission):
+    """Determines if a submission has all the required metadata fields."""
+    return (submission.game_type_cd is not None
+            and submission.map_name is not None
+            and submission.match_id is not None
+            and submission.server_name is not None)
 
 
-def is_supported_gametype(gametype, version):
-    """Whether a gametype is supported or not"""
-    is_supported = False
+def is_supported_gametype(submission):
+    """Determines if a submission is of a valid and supported game type."""
 
     # if the type can be supported, but with version constraints, uncomment
     # here and add the restriction for a specific version below
@@ -150,6 +308,7 @@ def is_supported_gametype(gametype, version):
             'cts',
             'dm',
             'dom',
+            'duel',
             'ft', 'freezetag',
             'ka', 'keepaway',
             'kh',
@@ -160,63 +319,77 @@ def is_supported_gametype(gametype, version):
             'tdm',
         )
 
-    if gametype in supported_game_types:
-        is_supported = True
-    else:
-        is_supported = False
+    is_supported = submission.game_type_cd in supported_game_types
 
     # some game types were buggy before revisions, thus this additional filter
-    if gametype == 'ca' and version <= 5:
+    if submission.game_type_cd == 'ca' and submission.version <= 5:
         is_supported = False
 
     return is_supported
 
 
-def do_precondition_checks(request, game_meta, raw_players):
-    """Precondition checks for ALL gametypes.
-       These do not require a database connection."""
-    if not has_required_metadata(game_meta):
-        log.debug("ERROR: Required game meta missing")
-        raise pyramid.httpexceptions.HTTPUnprocessableEntity("Missing game meta")
-
+def has_minimum_real_players(settings, submission):
+    """
+    Determines if the submission has enough human players to store in the database. The minimum
+    setting comes from the config file under the setting xonstat.minimum_real_players.
+    """
     try:
-        version = int(game_meta['V'])
+        minimum_required_players = int(settings.get("xonstat.minimum_required_players"))
     except:
-        log.debug("ERROR: Required game meta invalid")
-        raise pyramid.httpexceptions.HTTPUnprocessableEntity("Invalid game meta")
+        minimum_required_players = 2
 
-    if not is_supported_gametype(game_meta['G'], version):
-        log.debug("ERROR: Unsupported gametype")
-        raise pyramid.httpexceptions.HTTPOk("OK")
+    return len(submission.human_players) >= minimum_required_players
 
-    if not has_minimum_real_players(request.registry.settings, raw_players):
-        log.debug("ERROR: Not enough real players")
-        raise pyramid.httpexceptions.HTTPOk("OK")
 
-    if is_blank_game(game_meta['G'], raw_players):
-        log.debug("ERROR: Blank game")
-        raise pyramid.httpexceptions.HTTPOk("OK")
+def do_precondition_checks(settings, submission):
+    """Precondition checks for ALL gametypes. These do not require a database connection."""
+    if not has_required_metadata(submission):
+        msg = "Missing required game metadata"
+        log.debug(msg)
+        raise pyramid.httpexceptions.HTTPUnprocessableEntity(
+            body=msg,
+            content_type="text/plain"
+        )
 
+    if submission.version is None:
+        msg = "Invalid or incorrect game metadata provided"
+        log.debug(msg)
+        raise pyramid.httpexceptions.HTTPUnprocessableEntity(
+            body=msg,
+            content_type="text/plain"
+        )
 
-def is_real_player(events):
-    """
-    Determines if a given set of events correspond with a non-bot
-    """
-    if not events['P'].startswith('bot'):
-        return True
-    else:
-        return False
+    if not is_supported_gametype(submission):
+        msg = "Unsupported game type ({})".format(submission.game_type_cd)
+        log.debug(msg)
+        raise pyramid.httpexceptions.HTTPOk(
+            body=msg,
+            content_type="text/plain"
+        )
+
+    if not has_minimum_real_players(settings, submission):
+        msg = "Not enough real players"
+        log.debug(msg)
+        raise pyramid.httpexceptions.HTTPOk(
+            body=msg,
+            content_type="text/plain"
+        )
 
+    if is_blank_game(submission):
+        msg = "Blank game"
+        log.debug(msg)
+        raise pyramid.httpexceptions.HTTPOk(
+            body=msg,
+            content_type="text/plain"
+        )
 
-def played_in_game(events):
-    """
-    Determines if a given set of player events correspond with a player who
-    played in the game (matches 1 and scoreboardvalid 1)
-    """
-    if 'matches' in events and 'scoreboardvalid' in events:
-        return True
+
+def get_remote_addr(request):
+    """Get the Xonotic server's IP address"""
+    if 'X-Forwarded-For' in request.headers:
+        return request.headers['X-Forwarded-For']
     else:
-        return False
+        return request.remote_addr
 
 
 def num_real_players(player_events):
@@ -233,50 +406,9 @@ def num_real_players(player_events):
     return real_players
 
 
-def has_minimum_real_players(settings, player_events):
-    """
-    Determines if the collection of player events has enough "real" players
-    to store in the database. The minimum setting comes from the config file
-    under the setting xonstat.minimum_real_players.
-    """
-    flg_has_min_real_players = True
-
-    try:
-        minimum_required_players = int(
-                settings['xonstat.minimum_required_players'])
-    except:
-        minimum_required_players = 2
-
-    real_players = num_real_players(player_events)
-
-    if real_players < minimum_required_players:
-        flg_has_min_real_players = False
-
-    return flg_has_min_real_players
-
-
-def has_required_metadata(metadata):
-    """
-    Determines if a give set of metadata has enough data to create a game,
-    server, and map with.
-    """
-    flg_has_req_metadata = True
-
-    if 'G' not in metadata or\
-        'M' not in metadata or\
-        'I' not in metadata or\
-        'S' not in metadata:
-            flg_has_req_metadata = False
-
-    return flg_has_req_metadata
-
-
 def should_do_weapon_stats(game_type_cd):
     """True of the game type should record weapon stats. False otherwise."""
-    if game_type_cd in 'cts':
-        return False
-    else:
-        return True
+    return game_type_cd not in 'CTS'
 
 
 def gametype_elo_eligible(game_type_cd):