SHARE
TWEET

CSGO JSON Fantasy Data Parser

a guest Apr 1st, 2016 4,005 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # CSGO JSON Data Parser
  2. # Version 0.3
  3. # Calculates optimal picks for fantasy teams for specified day
  4. # Requires properly formatted JSON data
  5.  
  6. # Shitty code by /u/fknsonikk
  7.  
  8. import json
  9. import itertools
  10. import pprint
  11. import collections
  12. from operator import itemgetter
  13. from prettytable import PrettyTable
  14.  
  15. commando = [];
  16. clutch_king = [];
  17. eco_warrior = [];
  18. entry_fragger = [];
  19. sniper = [];
  20. optimal_candidates = [];
  21. optimal_combinations = [];
  22.  
  23. stageToParse = "stage2";
  24. input_file = "day3.json";       # Download: http://pastebin.com/Hew6k6Ku
  25. output_file = "day3points.txt";
  26.  
  27. def calculateCommando (name, kills, deaths):
  28.     global commando;
  29.     points = 2*kills;
  30.     points -= deaths;
  31.     plusminus = kills-deaths;
  32.     if plusminus > 0:
  33.         points += plusminus;
  34.     commando.append([name, points, plusminus]);
  35.  
  36. def calculateClutchKing (name, kills, deaths, clutch_kills):
  37.     global clutch_king;
  38.     points = 2*kills;
  39.     points -= deaths;
  40.     points += 4*clutch_kills;
  41.     clutch_king.append([name, points, clutch_kills]);
  42.  
  43. def calculateEcoWarrior (name, kills, deaths, pistol_kills):
  44.     global eco_warrior;
  45.     points = 2*kills;
  46.     points -= deaths;
  47.     points += 2*pistol_kills;
  48.     eco_warrior.append([name, points, pistol_kills]);
  49.  
  50. def calculateEntryFragger (name, kills, deaths, opening_kills):
  51.     global entry_fragger;
  52.     points = 2*kills;
  53.     points -= deaths;
  54.     points += 2*opening_kills;
  55.     entry_fragger.append([name, points, opening_kills]);
  56.  
  57. def calculateSniper (name, kills, deaths, sniper_kills):
  58.     global sniper;
  59.     points = 2*kills;
  60.     points -= deaths;
  61.     points += 2*sniper_kills;
  62.     sniper.append([name, points, sniper_kills]);
  63.  
  64. def findOptimal():
  65.     global s_commando, s_clutch_king, s_eco_warrior, s_entry_fragger, s_sniper, optimal_candidates, optimal_combinations;
  66.     for a in range (0,4):
  67.         for b in range (0,4):
  68.             for c in range (0,4):
  69.                 for d in range (0,4):
  70.                     for e in range(0,4):
  71.                         optimal_candidates.append(
  72.                             [s_commando[a][0], s_clutch_king[b][0], s_eco_warrior[c][0], s_entry_fragger[d][0], s_sniper[e][0],
  73.                             s_commando[a][1] + s_clutch_king[b][1] + s_eco_warrior[c][1] + s_entry_fragger[d][1] + s_sniper[e][1]]
  74.                             );
  75.  
  76.  
  77.     for candidate in optimal_candidates:
  78.         unique_values = set(candidate);
  79.         if len(unique_values) == 6:
  80.             optimal_combinations.append(candidate);
  81.  
  82. def printOptimal (sortedList, outFile):
  83.     outFile.write('Best lineups today:\n')
  84.     optimal = PrettyTable(["Commando", "Clutch King", "Eco Warrior", "Entry Fragger", "Sniper", "Total Points"]);
  85.     optimal.align = "l";
  86.     optimal.align["Total Points"] = "r";
  87.     for entry in sortedList:
  88.         optimal.add_row(entry);
  89.     str_optimal = optimal.get_string();
  90.     outFile.write(str_optimal);
  91.  
  92. def printCommando (sortedList, outFile):
  93.     outFile.write('\n\nCommando: \n');
  94.     commando = PrettyTable(["Name", "Points", "Kills - Deaths"]);
  95.     commando.align["Name"] = "l";
  96.     commando.align["Points"] = "r";
  97.     commando.align["Kills - Deaths"] = "r";
  98.     for entry in sortedList:
  99.         commando.add_row(entry);
  100.     str_commando = commando.get_string();
  101.     outFile.write(str_commando);
  102.  
  103. def printClutchKing (sortedList, outFile):
  104.     outFile.write('\n\nClutch King: \n');
  105.     clutch_king = PrettyTable(["Name", "Points", "Clutch Kills"]);
  106.     clutch_king.align["Name"] = "l";
  107.     clutch_king.align["Points"] = "r";
  108.     clutch_king.align["Clutch Kills"] = "r";
  109.     for entry in sortedList:
  110.         clutch_king.add_row(entry);
  111.     str_clutch_king = clutch_king.get_string();
  112.     outFile.write(str_clutch_king);
  113.  
  114. def printEcoWarrior (sortedList, outFile):
  115.     outFile.write('\n\nEco Warrior: \n');
  116.     eco_warrior = PrettyTable(["Name", "Points", "Pistol Kills"]);
  117.     eco_warrior.align["Name"] = "l";
  118.     eco_warrior.align["Points"] = "r";
  119.     eco_warrior.align["Pistol Kills"] = "r";
  120.     for entry in sortedList:
  121.         eco_warrior.add_row(entry);
  122.     str_eco_warrior = eco_warrior.get_string();
  123.     outFile.write(str_eco_warrior);
  124.  
  125. def printEntryFragger (sortedList, outFile):
  126.     outFile.write('\n\nEntry Fragger: \n');
  127.     entry_fragger = PrettyTable(["Name", "Points", "Opening Kills"]);
  128.     entry_fragger.align["Name"] = "l";
  129.     entry_fragger.align["Points"] = "r";
  130.     entry_fragger.align["Opening Kills"] = "r";
  131.     for entry in sortedList:
  132.         entry_fragger.add_row(entry);
  133.     str_entry_fragger = entry_fragger.get_string();
  134.     outFile.write(str_entry_fragger);
  135.  
  136. def printSniper (sortedList, outFile):
  137.     outFile.write('\n\nSniper: \n');
  138.     sniper = PrettyTable(["Name", "Points", "Sniper Kills"]);
  139.     sniper.align["Name"] = "l";
  140.     sniper.align["Points"] = "r";
  141.     sniper.align["Sniper Kills"] = "r";
  142.     for entry in sortedList:
  143.         sniper.add_row(entry);
  144.     str_sniper = sniper.get_string();
  145.     outFile.write(str_sniper);
  146.  
  147. with open(input_file) as inputfile:
  148.     data = json.load(inputfile);
  149.     for player in data['items_game_live']['pro_players'].values():
  150.         name = player['name'];
  151.  
  152.         if stageToParse in player['events']['9']:
  153.             thisStage = player['events']['9'][stageToParse];
  154.  
  155.             kills = int(thisStage['enemy_kills']);
  156.             deaths = int(thisStage['deaths']);
  157.             clutch_kills = int(thisStage['clutch_kills']);
  158.             opening_kills = int(thisStage['opening_kills']);
  159.             pistol_kills = int(thisStage['pistol_kills']);
  160.             sniper_kills = int(thisStage['sniper_kills']);
  161.  
  162.             calculateCommando(name, kills, deaths);
  163.             calculateClutchKing(name, kills, deaths, clutch_kills);
  164.             calculateEcoWarrior(name, kills, deaths, pistol_kills);
  165.             calculateEntryFragger(name, kills, deaths, opening_kills);
  166.             calculateSniper(name, kills, deaths, sniper_kills);
  167.  
  168. s_commando = sorted(commando, key=itemgetter(1, 2), reverse=True);
  169. s_clutch_king = sorted(clutch_king, key=itemgetter(1, 2), reverse=True);
  170. s_eco_warrior = sorted(eco_warrior, key=itemgetter(1, 2), reverse=True);
  171. s_entry_fragger = sorted(entry_fragger, key=itemgetter(1, 2), reverse=True);
  172. s_sniper = sorted(sniper, key=itemgetter(1, 2), reverse=True);
  173.  
  174. findOptimal();
  175. s_optimal_combinations = sorted(optimal_combinations, key=itemgetter(5), reverse=True);
  176.  
  177. with open(output_file, 'w') as outFile:
  178.     printOptimal(s_optimal_combinations, outFile);
  179.     printCommando(s_commando, outFile);
  180.     printClutchKing(s_clutch_king, outFile);
  181.     printEcoWarrior(s_eco_warrior, outFile);
  182.     printEntryFragger(s_entry_fragger, outFile);
  183.     printSniper(s_sniper, outFile);
RAW Paste Data
Top