Last active
September 6, 2016 08:39
-
-
Save featherbear/52296d3e2127456618918ddfb6bc6ff6 to your computer and use it in GitHub Desktop.
[Python 2.7] Counter-Strike 1.6 Server Log Kills & Deaths Extractor - For STHS SRC SCL Winter MCS 2016
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
if len(sys.argv) < 2: print("Please supply a log(s) as an argument!"); sys.exit() | |
import re | |
for fileLog in sys.argv[1:]: | |
if fileLog[-4:].lower() == ".dat": print("Skipping " + fileLog + " for protection..."); continue | |
logsArr = [] | |
lineParser = re.compile(r'L (\d{2}\/\d{2}\/\d{4}) - (\d{2}:\d{2}:\d{2}): (.*)') | |
nameParser = re.compile(r'"(?:(.+?)<\d+?><(.+?)><(.+?)>)"') | |
scoreParser = re.compile(r'\(CT "(\d+?)"\) \(T "(\d+?)"\)') | |
print("Opening log: " + fileLog) | |
with open(fileLog) as f: | |
for l in f: | |
l = l.strip() | |
reLine = lineParser.match(l) | |
if reLine: | |
logsArr.append(dict(zip(["ddmmyyyy", "hhmmss", "message"],reLine.groups()))) | |
print("Finished parsing log!") | |
print("Populating data...") | |
players = {} | |
finalScore = (0,0) | |
for l in logsArr: | |
matches = nameParser.findall(l['message']) | |
if matches: | |
for player in matches: | |
if player[1] not in players: | |
players[player[1]] = {'name': player[0], 'team': player[2], 'kills': 0, 'deaths': 0} | |
else: | |
scores = scoreParser.search(l['message']) | |
if scores: | |
finalScore = tuple(scores.groups()) | |
""" | |
Extract lines | |
""" | |
filtKills = [l for l in logsArr if " killed " in l['message']] # Get kills | |
#filtPlant = [l for l in logsArr if " triggered \"Planted_The_Bomb\"" in l['message']] # Get bomb plants | |
#filtDefuse = [l for l in logsArr if " triggered \"Defused_The_Bomb\"" in l['message']] # Get bomb defuses | |
for lineKill in filtKills: | |
killer, victim = nameParser.findall(lineKill['message']) | |
players[killer[1]]['kills'] += 1 | |
players[victim[1]]['deaths'] += 1 | |
#print("%s killed %s" % (killer[1], victim[1])) | |
filtCT = [p for p in players if players[p]['team'] == "CT"] | |
filtT = [p for p in players if players[p]['team'] == "TERRORIST"] | |
print("\n") | |
import os | |
with open(os.path.splitext(fileLog)[0]+".dat","w") as o: | |
def wprint(line): | |
print(line) | |
o.write(line+"\n") | |
wprint("===== Counter-Terrorists =====") | |
# ,key=lambda p:-p['kills'] | |
for player in map(players.get,sorted(filtCT)): | |
wprint("%s\t%s\t%s" % (player['name'],player['kills'],player['deaths'])) | |
wprint("\n========= Terrorists =========") | |
# ,key=lambda p:-p['kills'] | |
for player in map(players.get,sorted(filtT)): | |
wprint("%s\t%s\t%s" % (player['name'],player['kills'],player['deaths'])) | |
wprint("\nFinal Score - CT: %s T: %s" % (str(finalScore[0]), str(finalScore[1]))) | |
print("\n\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment