]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/submission.py
Add a function to determine the Elo category.
[xonotic/xonstat.git] / xonstat / views / submission.py
index 52d609199cb6286e890fc9619731baf559333e72..b942f09821675bf437ff466744f826a8199b3025 100644 (file)
@@ -16,6 +16,27 @@ from xonstat.util import strip_colors, qfont_decode, verify_request, weapon_map
 log = logging.getLogger(__name__)
 
 
+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
+
+
+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
+    else:
+        return False
+
+
 class Submission(object):
     """Parses an incoming POST request for stats submissions."""
 
@@ -38,6 +59,9 @@ class Submission(object):
         # distinct weapons that we have seen fired
         self.weapons = set()
 
+        # number of real players in the match
+        self.real_players = 0
+
         # the parsing deque (we use this to allow peeking)
         self.q = collections.deque(self.body.split("\n"))
 
@@ -46,12 +70,21 @@ class Submission(object):
         try:
             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:
             return None, None
 
+    def check_for_new_weapon_fired(self, sub_key):
+        """Checks if a given player key (subkey, actually) is a new weapon fired in the match."""
+        if sub_key.endswith("cnt-fired"):
+            weapon = sub_key.split("-")[1]
+            if weapon not in self.weapons:
+                self.weapons.add(weapon)
+
     def parse_player(self, key, pid):
         """Construct a player events listing from the submission."""
 
@@ -68,6 +101,9 @@ class Submission(object):
             elif key == 'e':
                 (sub_key, sub_value) = value.split(' ', 1)
                 player[sub_key] = sub_value
+
+                # keep track of the distinct weapons fired during the match
+                self.check_for_new_weapon_fired(sub_key)
             elif key == 'n':
                 player[key] = unicode(value, 'utf-8')
             elif key in player_keys:
@@ -77,6 +113,9 @@ class Submission(object):
                 self.q.appendleft("{} {}".format(key, value))
                 break
 
+        if is_real_player(player) and played_in_game(player):
+            self.real_players += 1
+
         self.players.append(player)
 
     def parse_team(self, key, tid):
@@ -109,6 +148,30 @@ class Submission(object):
         return self
 
 
+def elo_submission_category(submission):
+    """Determines the Elo category purely by what is in the submission data."""
+    mod = submission.meta.get("O", "None")
+
+    vanilla_allowed_weapons = {"shotgun", "devastatorblaster", "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 "general"
+
+
 def parse_stats_submission(body):
     """
     Parses the POST request body for a stats submission
@@ -312,27 +375,6 @@ def do_precondition_checks(request, game_meta, raw_players):
         )
 
 
-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
-
-
-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
-    else:
-        return False
-
-
 def num_real_players(player_events):
     """
     Returns the number of real players (those who played