Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 22.63 KB | None | 0 0
  1. import minqlx
  2. import time
  3. import os
  4. import pysftp
  5. import re
  6. import codecs
  7.  
  8. RECORDS_KEY = "minqlx:uberstats_records:{}"
  9. WEAPON_RECORDS = {
  10.                     "kill_machine": ["KILL MACHINE", "{:0.2f} frags/min"],
  11.                     "counterstrike": ["BEST COUNTERSTRIKE PLAYER", "{:0.2f} K/D ratio"],
  12.                     "most_damage": ["DESTRUCTICATOR", "{:,} dmg given"],
  13.                     "longest_spree": ["RAMBO", "{} kill streak"],
  14.                     "best_rail_accuracy": ["LASER EYES", "{:0.2f} percent rail accuracy"],
  15.                     "most_nade_kills": ["PINEAPPLE POWER", "{} grenade frags"],
  16.                     "most_pummels": ["PUMMEL LORD", "{} pummels"],
  17.                     "most_dmg_taken": ["BIGGEST PINCUSHION", "{:,} dmg taken"],
  18.                     "most_world_deaths": ["CLUMSIEST FOOL", "{:,} deaths by world"],
  19.                     "most_dmg_per_kill": ["GOOD SAMARITAN", "{:0.2f} damage per frag"]
  20.                   }
  21. FILE_PATTERN = re.compile('[\W_]+')
  22.  
  23. class uberstats(minqlx.Plugin):
  24.  
  25.   def __init__(self):
  26.     self.set_cvar_once("qlx_uberstats_sftp_hostname", "")
  27.     self.set_cvar_once("qlx_uberstats_sftp_username", "")
  28.     self.set_cvar_once("qlx_uberstats_sftp_password", "")
  29.     self.set_cvar_once("qlx_uberstats_sftp_remote_path", "")
  30.  
  31.     self.sftp_hostname = self.get_cvar("qlx_uberstats_sftp_hostname")
  32.     self.sftp_username = self.get_cvar("qlx_uberstats_sftp_username")
  33.     self.sftp_password = self.get_cvar("qlx_uberstats_sftp_password")
  34.     self.sftp_remote_path = self.get_cvar("qlx_uberstats_sftp_remote_path")
  35.  
  36.     self.add_command("score", self.cmd_score)
  37.     self.add_command("highscores", self.cmd_highscores)
  38.  
  39.     self.add_hook("stats", self.handle_stats)
  40.     self.add_hook("map", self.handle_map)
  41.     self.add_hook("game_end", self.handle_game_end)
  42.  
  43.     self.weapons = ["PLASMA", "ROCKET", "PROXMINE", "RAILGUN", "CHAINGUN", "NAILGUN", "GRENADE", "LIGHTNING", "SHOTGUN", "MACHINEGUN", "HMG", "BFG", "GAUNTLET"]
  44.     self.weapon_sprees = ["PLASMORGASM", "ROCKET RENEGADE", "MINE MASTER", "RAIL RANGER", "CHAIN GANG", "GNARLY NAILER", "GRENADE GOON", "LIGHTNING LASHER", "SHOTGUN SAMURAI", "MACHINEGUN PECKER", "HMG HARASSER", "BFG BOSS", "GUANTLET GOD"]
  45.     self.kill_streak = {}
  46.     for weapon in self.weapons:
  47.       self.kill_streak[weapon] = {}
  48.  
  49.     self.outputted_accuracy_players = []
  50.     self.kamikaze_stats = {}
  51.  
  52.     self.best_kpm_names = []
  53.     self.best_kpm = 0
  54.  
  55.     self.best_kd_names = []
  56.     self.best_kd = 0
  57.  
  58.     self.most_damage_names = []
  59.     self.most_damage = 0
  60.  
  61.     self.longest_spree_names = []
  62.     self.longest_spree = 0
  63.  
  64.     self.best_rail_accuracy_names = []
  65.     self.best_rail_accuracy = 0
  66.     self.best_rail_hits = 0
  67.     self.best_rail_shots = 0
  68.  
  69.     self.most_nade_kills_names = []
  70.     self.most_nade_kills = 0
  71.  
  72.     self.most_pummels_names = []
  73.     self.most_pummels = 0
  74.  
  75.     self.most_dmg_taken_names = []
  76.     self.most_dmg_taken = 0
  77.  
  78.     self.world_death_types = ["UNKNOWN", "WATER", "SLIME", "LAVA", "CRUSH", "FALLING", "TRIGGER_HURT", "HURT"]
  79.     self.world_death_stats = {}
  80.     self.most_world_deaths_names = []
  81.     self.most_world_deaths = 0
  82.  
  83.     self.most_dmg_per_kill_names = []
  84.     self.most_dmg_per_kill = 0
  85.  
  86.   def cmd_score(self, player, msg, channel):
  87.     if player.team != "spectator":
  88.       sorted_players = sorted(self.players(), key = lambda p: p.stats.score, reverse=True)
  89.       player_index = sorted_players.index(player) + 1
  90.       player.tell("^2{} - ^3Score: ^7{} - ^3K/D: ^7{} - ^3DMG: ^7{} - ^3TIME: ^7{} - ^3PING: ^7{}".format(
  91.         self.ordinal(player_index),
  92.         player.stats.score,
  93.         str(player.stats.kills) + "/" + str(player.stats.deaths),
  94.         player.stats.damage_dealt,
  95.         int((player.stats.time/(1000*60))%60),
  96.         player.stats.ping
  97.       )
  98.       )
  99.  
  100.   def handle_stats(self, stats):
  101.     if self.game is not None:
  102.       if self.game.state == "in_progress":
  103.         if stats['TYPE'] == "PLAYER_DEATH":
  104.           victim_name = stats['DATA']['VICTIM']['NAME']
  105.  
  106.           #remove player from kill streak counters when they die
  107.           for weapon in self.weapons:
  108.             if self.kill_streak[weapon]:
  109.               if victim_name in self.kill_streak[weapon]:
  110.                 self.kill_streak[weapon][victim_name] = 0
  111.  
  112.           #count player world deaths
  113.           if stats['DATA']['MOD'] in self.world_death_types:
  114.             victim_name = stats['DATA']['VICTIM']['NAME']
  115.             if victim_name not in self.world_death_stats:
  116.               self.world_death_stats[victim_name] = 1
  117.             else:
  118.               self.world_death_stats[victim_name] += 1
  119.         elif stats['TYPE'] == "PLAYER_KILL" and stats['DATA']['MOD'] in self.weapons:
  120.           killer_name = stats['DATA']['KILLER']['NAME']
  121.           weapon = stats['DATA']['MOD']
  122.  
  123.           if killer_name != stats['DATA']['VICTIM']['NAME']:
  124.             if killer_name not in self.kill_streak[weapon]:
  125.               self.kill_streak[weapon][killer_name] = 1
  126.             else:
  127.               self.kill_streak[weapon][killer_name] += 1
  128.  
  129.             if self.kill_streak[weapon][killer_name] == 1:
  130.               self.handle_kill_streak(killer_name, weapon)
  131.         elif stats['TYPE'] == "PLAYER_KILL" and stats['DATA']['MOD'] == "KAMIKAZE":
  132.           killer_name = stats['DATA']['KILLER']['NAME']
  133.           if killer_name != stats['DATA']['VICTIM']['NAME']:
  134.             if killer_name not in self.kamikaze_stats:
  135.               self.kamikaze_stats[killer_name] = 1
  136.             else:
  137.               self.kamikaze_stats[killer_name] += 1
  138.  
  139.             if self.kamikaze_stats[killer_name] == 1:
  140.               self.handle_kamikaze_stats(killer_name)
  141.  
  142.     if stats['TYPE'] == "PLAYER_STATS":
  143.       #these stats come at end of game after MATCH_REPORT for each player
  144.       if stats['DATA']['QUIT'] == 0 and stats['DATA']['WARMUP'] == 0:
  145.         player_name = stats['DATA']['NAME']
  146.  
  147.         #player accuracies (sent to each player in tell)
  148.         if stats['DATA']['STEAM_ID'] != "0": #skip bots
  149.           player = self.player(int(stats['DATA']['STEAM_ID']))
  150.           #dont show if player is in spec, also handle multiple output bug as well
  151.           if player.team != "spectator" and player.steam_id not in self.outputted_accuracy_players:
  152.             accuracy_output = "^2YOUR ACCURACY:"
  153.             for weapon in self.weapons:
  154.               weapon_shots = stats['DATA']['WEAPONS'][weapon]["S"]
  155.               weapon_hits = stats['DATA']['WEAPONS'][weapon]["H"]
  156.               if weapon_shots > 0:
  157.                 if weapon_hits > 0:
  158.                   weapon_accuracy = 100 * (weapon_hits / weapon_shots)
  159.                 else:
  160.                   weapon_accuracy = 0.00
  161.                 accuracy_output += " - ^3{}: ^1{:0.2f}".format(weapon, weapon_accuracy)
  162.             player.tell(accuracy_output)
  163.             self.outputted_accuracy_players.append(player.steam_id)
  164.  
  165.         if stats['DATA']['PLAY_TIME'] > 0:
  166.           player_kpm = stats['DATA']['KILLS'] / (stats['DATA']['PLAY_TIME'] / 60)
  167.         else:
  168.           player_kpm = 0
  169.  
  170.         if stats['DATA']['DEATHS'] != 0: #we don't want to divide by 0!
  171.           player_kd = stats['DATA']['KILLS'] / stats['DATA']['DEATHS']
  172.         else:
  173.           player_kd = stats['DATA']['KILLS']
  174.  
  175.         player_dmg = stats['DATA']['DAMAGE']['DEALT']
  176.         player_longest_spree = stats['DATA']['MAX_STREAK']
  177.  
  178.         player_rail_hits = 0
  179.         player_rail_shots = 0
  180.  
  181.         if stats['DATA']['WEAPONS']['RAILGUN']['H'] >= 6:
  182.           player_rail_hits = stats['DATA']['WEAPONS']['RAILGUN']['H']
  183.           player_rail_shots = stats['DATA']['WEAPONS']['RAILGUN']['S']
  184.           player_rail_accuracy = 100 * (player_rail_hits / player_rail_shots)
  185.         else:
  186.           player_rail_accuracy = 0
  187.  
  188.         player_nade_kills = stats['DATA']['WEAPONS']['GRENADE']['K']
  189.         player_pummels = stats['DATA']['WEAPONS']['GAUNTLET']['K']
  190.         player_dmg_taken = stats['DATA']['DAMAGE']['TAKEN']
  191.  
  192.         player_dmg_per_kill = 0
  193.         if stats['DATA']['KILLS'] > 0: #we don't want to divide by 0!
  194.           player_dmg_per_kill = stats['DATA']['DAMAGE']['DEALT'] / stats['DATA']['KILLS']
  195.  
  196.         if not self.best_kpm_names:
  197.           self.best_kpm_names = [player_name]
  198.           self.best_kpm = player_kpm
  199.         elif player_kpm > self.best_kpm:
  200.           self.best_kpm_names = [player_name]
  201.           self.best_kpm = player_kpm
  202.         elif player_kpm == self.best_kpm:
  203.           self.best_kpm_names.append(player_name)
  204.  
  205.         if not self.best_kd_names:
  206.           self.best_kd_names = [player_name]
  207.           self.best_kd = player_kd
  208.         elif player_kd > self.best_kd:
  209.           self.best_kd_names = [player_name]
  210.           self.best_kd = player_kd
  211.         elif player_kd == self.best_kd:
  212.           self.best_kd_names.append(player_name)
  213.  
  214.         if not self.most_damage_names:
  215.           self.most_damage_names = [player_name]
  216.           self.most_damage = player_dmg
  217.         elif player_dmg > self.most_damage:
  218.           self.most_damage_names = [player_name]
  219.           self.most_damage = player_dmg
  220.         elif player_dmg == self.most_damage:
  221.           self.most_damage_names.append(player_name)
  222.  
  223.         if not self.longest_spree:
  224.           self.longest_spree_names = [player_name]
  225.           self.longest_spree = player_longest_spree
  226.         elif player_longest_spree > self.longest_spree:
  227.           self.longest_spree_names = [player_name]
  228.           self.longest_spree = player_longest_spree
  229.         elif player_longest_spree == self.longest_spree:
  230.           self.longest_spree_names.append(player_name)
  231.  
  232.         if not self.best_rail_accuracy_names:
  233.           self.best_rail_accuracy_names = [player_name]
  234.           self.best_rail_accuracy = player_rail_accuracy
  235.           self.best_rail_hits = player_rail_hits
  236.           self.best_rail_shots = player_rail_shots
  237.         elif player_rail_accuracy > self.best_rail_accuracy:
  238.           self.best_rail_accuracy_names = [player_name]
  239.           self.best_rail_accuracy = player_rail_accuracy
  240.           self.best_rail_hits = player_rail_hits
  241.           self.best_rail_shots = player_rail_shots
  242.         elif player_rail_accuracy == self.best_rail_accuracy:
  243.           self.best_rail_accuracy_names.append(player_name)
  244.  
  245.         if not self.most_nade_kills_names:
  246.           self.most_nade_kills_names = [player_name]
  247.           self.most_nade_kills = player_nade_kills
  248.         elif player_nade_kills > self.most_nade_kills:
  249.           self.most_nade_kills_names = [player_name]
  250.           self.most_nade_kills = player_nade_kills
  251.         elif player_nade_kills == self.most_nade_kills:
  252.           self.most_nade_kills_names.append(player_name)
  253.  
  254.         if not self.most_pummels_names:
  255.           self.most_pummels_names = [player_name]
  256.           self.most_pummels = player_pummels
  257.         elif player_pummels > self.most_pummels:
  258.           self.most_pummels_names = [player_name]
  259.           self.most_pummels = player_pummels
  260.         elif player_pummels == self.most_pummels:
  261.           self.most_pummels_names.append(player_name)
  262.  
  263.         if not self.most_dmg_taken_names:
  264.           self.most_dmg_taken_names = [player_name]
  265.           self.most_dmg_taken = player_dmg_taken
  266.         elif player_dmg_taken > self.most_dmg_taken:
  267.           self.most_dmg_taken_names = [player_name]
  268.           self.most_dmg_taken = player_dmg_taken
  269.         elif player_dmg_taken == self.most_dmg_taken:
  270.           self.most_dmg_taken_names.append(player_name)
  271.  
  272.         if not self.most_dmg_per_kill_names:
  273.           self.most_dmg_per_kill_names = [player_name]
  274.           self.most_dmg_per_kill = player_dmg_per_kill
  275.         elif player_dmg_per_kill > self.most_dmg_per_kill:
  276.           self.most_dmg_per_kill_names = [player_name]
  277.           self.most_dmg_per_kill = player_dmg_per_kill
  278.         elif player_dmg_per_kill == self.most_dmg_per_kill:
  279.           self.most_dmg_per_kill_names.append(player_name)
  280.  
  281.   @minqlx.delay(2)
  282.   @minqlx.thread
  283.   def handle_game_end(self, data):
  284.     if not data["ABORTED"]:
  285.       self.msg("^5***UBERSTATS***")
  286.       stats_output = "^1KILL MACHINE: "
  287.       record_response = ""
  288.       for i, player_name in enumerate(self.best_kpm_names):
  289.         record_response = self.check_record("kill_machine", float(self.best_kd), player_name)
  290.         stats_output += "^7" + player_name
  291.         if len(self.best_kpm_names) > 1 and len(self.best_kpm_names) - 1 != i:
  292.           stats_output += ", "
  293.       stats_output += "^2 - {:0.2f} frags/min".format(self.best_kpm)
  294.       self.msg(record_response + stats_output)
  295.  
  296.       stats_output = "^1BEST COUNTERSTRIKE PLAYER: "
  297.       record_response = ""
  298.       for i, player_name in enumerate(self.best_kd_names):
  299.         record_response = self.check_record("counterstrike", float(self.best_kd), player_name)
  300.         stats_output += "^7" + player_name
  301.         if len(self.best_kd_names) > 1 and len(self.best_kd_names) - 1 != i:
  302.           stats_output += ", "
  303.       stats_output += "^2 - {:0.2f} K/D ratio".format(self.best_kd)
  304.       self.msg(record_response + stats_output)
  305.  
  306.       stats_output = "^1DESTRUCTICATOR: "
  307.       record_response = ""
  308.       for i, player_name in enumerate(self.most_damage_names):
  309.         record_response = self.check_record("most_damage", float(self.most_damage), player_name)
  310.         stats_output += "^7" + player_name
  311.         if len(self.most_damage_names) > 1 and len(self.most_damage_names) - 1 != i:
  312.           stats_output += ", "
  313.       stats_output += "^2 - {:,} dmg given".format(self.most_damage)
  314.  
  315.       self.msg(record_response + stats_output)
  316.       time.sleep(3)
  317.  
  318.       if self.longest_spree > 1:
  319.         stats_output = "^1RAMBO: "
  320.         record_response = ""
  321.         for i, player_name in enumerate(self.longest_spree_names):
  322.           record_response = self.check_record("longest_spree", float(self.longest_spree), player_name)
  323.           stats_output += "^7" + player_name
  324.           if len(self.longest_spree_names) > 1 and len(self.longest_spree_names) - 1 != i:
  325.             stats_output += ", "
  326.         stats_output += "^2 - {} kill streak".format(self.longest_spree)
  327.         self.msg(record_response + stats_output)
  328.  
  329.       if self.best_rail_accuracy > 0:
  330.         stats_output = "^1LASER EYES: "
  331.         record_response = ""
  332.         for i, player_name in enumerate(self.best_rail_accuracy_names):
  333.           record_response = self.check_record("best_rail_accuracy", float(self.best_rail_accuracy), player_name)
  334.           stats_output += "^7" + player_name
  335.           if len(self.best_rail_accuracy_names) > 1 and len(self.best_rail_accuracy_names) - 1 != i:
  336.             stats_output += ", "
  337.         stats_output += "^2 - {:0.2f} percent rail accuracy ({} hits / {} shots)".format(self.best_rail_accuracy, self.best_rail_hits, self.best_rail_shots)
  338.         self.msg(record_response + stats_output)
  339.  
  340.       if self.most_nade_kills > 0:
  341.         stats_output = "^3PINEAPPLE POWER: "
  342.         record_response = ""
  343.         for i, player_name in enumerate(self.most_nade_kills_names):
  344.           record_response = self.check_record("most_nade_kills", float(self.most_nade_kills), player_name)
  345.           stats_output += "^7" + player_name
  346.           if len(self.most_nade_kills_names) > 1 and len(self.most_nade_kills_names) - 1 != i:
  347.             stats_output += ", "
  348.         stats_output += "^2 - {} grenade frags".format(self.most_nade_kills)
  349.         self.msg(record_response + stats_output)
  350.  
  351.       time.sleep(2)
  352.  
  353.       if self.most_pummels > 0:
  354.         stats_output = "^1PUMMEL LORD: "
  355.         record_response = ""
  356.         for i, player_name in enumerate(self.most_pummels_names):
  357.           record_response = self.check_record("most_pummels", float(self.most_pummels), player_name)
  358.           stats_output += "^7" + player_name
  359.           if len(self.most_pummels_names) > 1 and len(self.most_pummels_names) - 1 != i:
  360.             stats_output += ", "
  361.         stats_output += "^2 - {} pummels".format(self.most_pummels)
  362.         self.msg(record_response + stats_output)
  363.  
  364.       stats_output = "^6BIGGEST PINCUSHION: "
  365.       record_response = ""
  366.       for i, player_name in enumerate(self.most_dmg_taken_names):
  367.         record_response = self.check_record("most_dmg_taken", float(self.most_dmg_taken), player_name)
  368.         stats_output += "^7" + player_name
  369.         if len(self.most_dmg_taken_names) > 1 and len(self.most_dmg_taken_names) - 1 != i:
  370.           stats_output += ", "
  371.       stats_output += "^2 - {:,} ^6dmg taken".format(self.most_dmg_taken)
  372.       self.msg(record_response + stats_output)
  373.  
  374.       stats_output = "^6CLUMSIEST FOOL: "
  375.       record_response = ""
  376.       for name, world_deaths in self.world_death_stats.items():
  377.         if not self.most_world_deaths_names:
  378.           self.most_world_deaths_names = [name]
  379.           self.most_world_deaths = world_deaths
  380.         elif world_deaths > self.most_world_deaths:
  381.           self.most_world_deaths_names = [name]
  382.           self.most_world_deaths = world_deaths
  383.         elif world_deaths == self.most_world_deaths:
  384.           self.most_world_deaths_names.append(name)
  385.  
  386.       if self.most_world_deaths > 0:
  387.         for i, player_name in enumerate(self.most_world_deaths_names):
  388.           record_response = self.check_record("most_world_deaths", float(self.most_world_deaths), player_name)
  389.           stats_output += "^7" + player_name
  390.           if len(self.most_world_deaths_names) > 1 and len(self.most_world_deaths_names) - 1 != i:
  391.             stats_output += ", "
  392.         stats_output += "^2 - {:,} deaths by world".format(self.most_world_deaths)
  393.         self.msg(record_response + stats_output)
  394.  
  395.       time.sleep(2)
  396.  
  397.       if self.game.type not in ["Duel", "Instagib"]:
  398.         stats_output = "^6GOOD SAMARITAN: "
  399.         record_response = ""
  400.         for i, player_name in enumerate(self.most_dmg_per_kill_names):
  401.           record_response = self.check_record("most_dmg_per_kill", float(self.most_dmg_per_kill), player_name)
  402.           stats_output += "^7" + player_name
  403.           if len(self.most_dmg_per_kill_names) > 1 and len(self.most_dmg_per_kill_names) - 1 != i:
  404.             stats_output += ", "
  405.         stats_output += "^2 - {:0.2f} damage per frag".format(self.most_dmg_per_kill)
  406.         self.msg(record_response + stats_output)
  407.  
  408.       if self.sftp_hostname:
  409.         self.high_scores("endgame")
  410.  
  411.   @minqlx.delay(8)
  412.   def handle_kill_streak(self, player_name, weapon):
  413.     if int(self.kill_streak[weapon][player_name]) >= 4:
  414.       self.play_sound("sound/uberstats/{}.ogg".format(weapon.lower()))
  415.       self.center_print("{}^1 {}".format(player_name, self.weapon_sprees[self.weapons.index(weapon)]))
  416.       self.msg("{} ^1{}: ^2({} {} frags in 8s)".format(player_name, self.weapon_sprees[self.weapons.index(weapon)], self.kill_streak[weapon][player_name], weapon))
  417.       self.kill_streak[weapon][player_name] = 0
  418.  
  419.   @minqlx.delay(5)
  420.   def handle_kamikaze_stats(self, player_name):
  421.     kami_msg = "{}^7's ^3 KAMI: ^7{} ^1FRAGS".format(player_name, self.kamikaze_stats[player_name])
  422.     self.center_print(kami_msg)
  423.     self.msg(kami_msg)
  424.     self.kamikaze_stats[player_name] = 0
  425.  
  426.   @minqlx.delay(10)
  427.   def handle_map(self, mapname, factory):
  428.     self.best_kpm_names = []
  429.     self.best_kpm = 0
  430.  
  431.     self.best_kd_names = []
  432.     self.best_kd = 0
  433.  
  434.     self.most_damage_names = []
  435.     self.most_damage = 0
  436.  
  437.     self.longest_spree_names = []
  438.     self.longest_spree = 0
  439.  
  440.     self.best_rail_accuracy_names = []
  441.     self.best_rail_accuracy = 0
  442.     self.best_rail_hits = 0
  443.     self.best_rail_shots = 0
  444.  
  445.     self.most_nade_kills_names = []
  446.     self.most_nade_kills = 0
  447.  
  448.     self.most_pummels_names = []
  449.     self.most_pummels = 0
  450.  
  451.     self.most_dmg_taken_names = []
  452.     self.most_dmg_taken = 0
  453.  
  454.     self.world_death_stats = {}
  455.     self.most_world_deaths_names = []
  456.     self.most_world_deaths = 0
  457.  
  458.     self.most_dmg_per_kill_names = []
  459.     self.most_dmg_per_kill = 0
  460.  
  461.     self.kamikaze_stats = {}
  462.     for weapon in self.weapons:
  463.       self.kill_streak[weapon] = {}
  464.  
  465.     self.outputted_accuracy_players = []
  466.  
  467.     self.high_scores("triggered")
  468.  
  469.   def ordinal(self, value):
  470.     try:
  471.       value = int(value)
  472.     except ValueError:
  473.       return value
  474.  
  475.     if value % 100//10 != 1:
  476.       if value % 10 == 1:
  477.         ordval = u"%d%s" % (value, "st")
  478.       elif value % 10 == 2:
  479.         ordval = u"%d%s" % (value, "nd")
  480.       elif value % 10 == 3:
  481.         ordval = u"%d%s" % (value, "rd")
  482.       else:
  483.         ordval = u"%d%s" % (value, "th")
  484.     else:
  485.       ordval = u"%d%s" % (value, "th")
  486.  
  487.     return ordval
  488.  
  489.   def check_record(self, record_name, score, player_name):
  490.     current_record = self.db.get(RECORDS_KEY.format(record_name) + ":high_score")
  491.  
  492.     if current_record is None:
  493.       current_record = 0
  494.     else:
  495.       current_record = float(current_record)
  496.  
  497.     if score > current_record:
  498.       self.db.set(RECORDS_KEY.format(record_name) + ":high_score", score)
  499.       self.db.delete(RECORDS_KEY.format(record_name) + ":players")
  500.       self.db.sadd(RECORDS_KEY.format(record_name) + ":players", player_name)
  501.       return "^5NEW HIGH SCORE! - "
  502.     elif score == current_record:
  503.       self.db.sadd(RECORDS_KEY.format(record_name) + ":players", player_name)
  504.       return "^5TIED HIGH SCORE! - "
  505.     else:
  506.       return ""
  507.  
  508.   def cmd_highscores(self, player, msg, channel):
  509.     self.high_scores("triggered")
  510.  
  511.   @minqlx.thread
  512.   def high_scores(self, method):
  513.     if method == "triggered":
  514.       self.msg("^5***UBERSTATS HIGH SCORES***")
  515.     elif method == "endgame":
  516.       html = "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>\n" + \
  517.            "<script>\n" + \
  518.            "$(function(){\n"
  519.  
  520.     for key, val in WEAPON_RECORDS.items():
  521.       high_score = self.db.get(RECORDS_KEY.format(key) + ":high_score")
  522.       if high_score is not None:
  523.         players = ", ".join(self.db.smembers(RECORDS_KEY.format(key) + ":players"))
  524.         if method == "triggered":
  525.           self.msg("^1{} - ^7{} ^2- {}".format(val[0], players, val[1].format(float(high_score))))
  526.         elif method == "endgame":
  527.           html += u"$('.{}_record').text('{}');\n".format(key, val[1].format(float(high_score)))
  528.           html += u"$('.{}_players').text('{}');\n\n".format(key, players)
  529.  
  530.     if method == "endgame":
  531.       html += "});\n</script>"
  532.       #make nice filename from hostname
  533.       uberfilename = re.sub(' +', '_', (re.sub("[^a-zA-Z.\d\s]", "", self.game.hostname) + "-uberstats.html").lower())
  534.       f = open(uberfilename, "w+")
  535.       f.write(html)
  536.       f.close()
  537.       cnopts = pysftp.CnOpts()
  538.       cnopts.hostkeys = None
  539.       srv = pysftp.Connection(host = self.sftp_hostname, username = self.sftp_username, password = self.sftp_password, cnopts=cnopts)
  540.       srv.chdir(self.sftp_remote_path)
  541.       srv.put(uberfilename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement