damage_data = {} # Don't touch the above line """ mystats module for BombSquad version 1.5.29 Provides functionality for dumping player stats to disk between rounds. """ ranks = [] top3Name = [] import threading, json, os, urllib.request, ba, _ba, setting from ba._activity import Activity from ba._music import setmusic, MusicType # False-positive from pylint due to our class-generics-filter. from ba._player import EmptyPlayer # pylint: disable=W0611 from ba._team import EmptyTeam # pylint: disable=W0611 from typing import Any, Dict, Optional from ba._lobby import JoinInfo from ba import _activitytypes as ba_actypes from ba._activitytypes import * import urllib.request import custom_hooks import datetime # variables our_settings = setting.get_settings_data() # where our stats file and pretty html output will go base_path = os.path.join(_ba.env()['python_directory_user'], "stats" + os.sep) statsfile = base_path + 'stats.json' htmlfile = base_path + 'stats_page.html' table_style = "{width:100%;border: 3px solid black;border-spacing: 5px;border-collapse:collapse;text-align:center;background-color:#fff}" heading_style = "{border: 3px solid black;text-align:center;}" html_start = f'''
| Rank | Name | Score | Kills | Deaths | Games Played | Total Damage | #removed this line as it isn't crt data # useful functions seasonStartDate = None import shutil, os def get_all_stats(): global seasonStartDate if os.path.exists(statsfile): renameFile = False with open(statsfile, 'r', encoding='utf8') as f: try: jsonData = json.loads(f.read()) except: f=open(statsfile+".backup",encoding='utf-8') jsonData=json.load(f) try: stats = jsonData["stats"] seasonStartDate = datetime.datetime.strptime(jsonData["startDate"], "%d-%m-%Y") if (datetime.datetime.now() - seasonStartDate).days >= our_settings["statsResetAfterDays"]: backupStatsFile() seasonStartDate = datetime.datetime.now() return statsDefault return stats except OSError as e: print(e) return jsonData else: return {} def backupStatsFile(): shutil.copy(statsfile, statsfile.replace(".json", "") + str(seasonStartDate) + ".json") def dump_stats(s: dict): global seasonStartDate if seasonStartDate == None: seasonStartDate = datetime.datetime.now() s = {"startDate": seasonStartDate.strftime("%d-%m-%Y"), "stats": s} if os.path.exists(statsfile): shutil.copyfile(statsfile,statsfile+".backup") with open(statsfile, 'w', encoding='utf8') as f: f.write(json.dumps(s, indent=4, ensure_ascii=False)) f.close() else: print('Stats file not found!') def get_stats_by_id(ID: str): a = get_all_stats() if ID in a: return a[ID] else: return None def refreshStats(): # lastly, write a pretty html version. # our stats url could point at something like this... pStats = get_all_stats() # f=open(htmlfile, 'w') # f.write(html_start) entries = [(a['scores'], a['kills'], a['deaths'], a['games'], a['name'], a['aid']) for a in pStats.values()] # this gives us a list of kills/names sorted high-to-low entries.sort(key=lambda x: x[1] or 0, reverse=True) rank = 0 toppers = {} toppersIDs = [] _ranks = [] for entry in entries: if True: rank += 1 scores = str(entry[0]) kills = str(entry[1]) deaths = str(entry[2]) games = str(entry[3]) name = str(entry[4]) aid = str(entry[5]) if rank < 6: toppersIDs.append(aid) # The below kd and avg_score will not be added to website's html document, it will be only added in stats.json try: kd = str(float(kills) / float(deaths)) kd_int = kd.split('.')[0] kd_dec = kd.split('.')[1] p_kd = kd_int + '.' + kd_dec[:3] except Exception: p_kd = "0" try: avg_score = str(float(scores) / float(games)) avg_score_int = avg_score.split('.')[0] avg_score_dec = avg_score.split('.')[1] p_avg_score = avg_score_int + '.' + avg_score_dec[:3] except Exception: p_avg_score = "0" if damage_data and aid in damage_data: dmg = damage_data[aid] dmg = str(str(dmg).split('.')[0] + '.' + str(dmg).split('.')[1][:3]) else: dmg = 0 _ranks.append(aid) pStats[str(aid)]["rank"] = int(rank) pStats[str(aid)]["scores"] = int(scores) pStats[str(aid)]["total_damage"] += float(dmg) # not working properly pStats[str(aid)]["games"] = int(games) pStats[str(aid)]["kills"] = int(kills) pStats[str(aid)]["deaths"] = int(deaths) pStats[str(aid)]["kd"] = float(p_kd) pStats[str(aid)]["avg_score"] = float(p_avg_score) # if rank < 201: # #{str(dmg)} | #removed this line as it isn't crt data # f.write(f''' #
|---|---|---|---|---|---|
| {str(rank)} | #{str(name)} | #{str(scores)} | #{str(kills)} | #{str(deaths)} | #{str(games)} | #