mirror of
https://github.com/Jylpah/blitz-tools.git
synced 2026-10-26 11:39:52 +01:00
New categorization: battle, refactor print_headers()
This commit is contained in:
1 parent
5c19f4f5a6
commit
058d3ad88a
1 file changed
+81
-28
+81
-28
@@ -144,10 +144,11 @@ class BattleCategorizationList():
|
|||||||
'enemies_spotted' : [ 'Player Spots', 'number'],
|
'enemies_spotted' : [ 'Player Spots', 'number'],
|
||||||
'hit_rate' : [ 'Player Hit Rate', 'bucket', [ 0, .25, .5, .6, .7, .8, .9, .95 ], '%' ],
|
'hit_rate' : [ 'Player Hit Rate', 'bucket', [ 0, .25, .5, .6, .7, .8, .9, .95 ], '%' ],
|
||||||
'pen_rate' : [ 'Player Pen Rate', 'bucket', [ 0, .25, .5, .6, .7, .8, .9, .95 ], '%' ],
|
'pen_rate' : [ 'Player Pen Rate', 'bucket', [ 0, .25, .5, .6, .7, .8, .9, .95 ], '%' ],
|
||||||
'player_name' : [ 'Player', 'string' ],
|
'player_name' : [ 'Player', 'string', 25 ],
|
||||||
'protagonist' : [ 'account_id', 'number' ],
|
'protagonist' : [ 'account_id', 'number' ],
|
||||||
'tank_name' : [ 'Tank', 'string' ],
|
'tank_name' : [ 'Tank', 'string', 25],
|
||||||
'map_name' : [ 'Map', 'string' ],
|
'map_name' : [ 'Map', 'string', 20],
|
||||||
|
'battle' : [ 'Battle', 'string', 40],
|
||||||
'battle_i' : [ 'Battle #', 'number' ]
|
'battle_i' : [ 'Battle #', 'number' ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +230,7 @@ class BattleCategorizationList():
|
|||||||
if ctype == 'number':
|
if ctype == 'number':
|
||||||
cat_tmp = BattleNumberCategorization(cat, ctitle)
|
cat_tmp = BattleNumberCategorization(cat, ctitle)
|
||||||
elif ctype == 'string':
|
elif ctype == 'string':
|
||||||
cat_tmp = BattleStringCategorization(cat, ctitle)
|
cat_tmp = BattleStringCategorization(cat, ctitle, cparams)
|
||||||
elif ctype == 'category':
|
elif ctype == 'category':
|
||||||
cat_tmp = BattleClassCategorization(cat, ctitle, cparams[0])
|
cat_tmp = BattleClassCategorization(cat, ctitle, cparams[0])
|
||||||
elif ctype == 'bucket':
|
elif ctype == 'bucket':
|
||||||
@@ -281,7 +282,7 @@ class BattleCategorizationList():
|
|||||||
elif ctype == 'number':
|
elif ctype == 'number':
|
||||||
self.categorizations[cat] = BattleNumberCategorization(cat, ctitle)
|
self.categorizations[cat] = BattleNumberCategorization(cat, ctitle)
|
||||||
elif ctype == 'string':
|
elif ctype == 'string':
|
||||||
self.categorizations[cat] = BattleStringCategorization(cat, ctitle)
|
self.categorizations[cat] = BattleStringCategorization(cat, ctitle, cparams)
|
||||||
elif ctype == 'category':
|
elif ctype == 'category':
|
||||||
self.categorizations[cat] = BattleClassCategorization(cat, ctitle, cparams[0])
|
self.categorizations[cat] = BattleClassCategorization(cat, ctitle, cparams[0])
|
||||||
elif ctype == 'bucket':
|
elif ctype == 'bucket':
|
||||||
@@ -382,7 +383,9 @@ class BattleCategorizationList():
|
|||||||
|
|
||||||
class BattleCategorization():
|
class BattleCategorization():
|
||||||
|
|
||||||
RESULT_CAT_FRMT = '{:>20s}'
|
RESULT_CAT_HEADER_LEN = 15
|
||||||
|
RESULT_CAT_HEADER_FRMT = '{:_<' + str(RESULT_CAT_HEADER_LEN) + 's}'
|
||||||
|
RESULT_CAT_FRMT = '{:>' + str(RESULT_CAT_HEADER_LEN) + 's}'
|
||||||
|
|
||||||
def __init__(self, cat_key : str, title: str):
|
def __init__(self, cat_key : str, title: str):
|
||||||
self.total_battles = 0
|
self.total_battles = 0
|
||||||
@@ -391,6 +394,15 @@ class BattleCategorization():
|
|||||||
self.categories = collections.defaultdict(def_value_BattleCategory)
|
self.categories = collections.defaultdict(def_value_BattleCategory)
|
||||||
|
|
||||||
|
|
||||||
|
def set_cat_header_format(self, header_len: int, left_aligned: bool = False):
|
||||||
|
self.RESULT_CAT_HEADER_LEN = header_len
|
||||||
|
self.RESULT_CAT_HEADER_FRMT = '{:_<' + str(header_len) + 's}'
|
||||||
|
if left_aligned:
|
||||||
|
self.RESULT_CAT_FRMT = '{:<' + str(header_len) + 's}'
|
||||||
|
else:
|
||||||
|
self.RESULT_CAT_FRMT = '{:>' + str(header_len) + 's}'
|
||||||
|
|
||||||
|
|
||||||
def get_categories(self):
|
def get_categories(self):
|
||||||
"""Get category keys sorted"""
|
"""Get category keys sorted"""
|
||||||
return sorted(self.categories.keys() , key=str.casefold)
|
return sorted(self.categories.keys() , key=str.casefold)
|
||||||
@@ -435,12 +447,26 @@ class BattleCategorization():
|
|||||||
bu.error(exception = err)
|
bu.error(exception = err)
|
||||||
|
|
||||||
|
|
||||||
|
def print_headers(self):
|
||||||
|
if len(self.title) > self.RESULT_CAT_HEADER_LEN:
|
||||||
|
cat_name = self.title[:self.RESULT_CAT_HEADER_LEN]
|
||||||
|
else:
|
||||||
|
cat_name = self.title
|
||||||
|
header = self.RESULT_CAT_HEADER_FRMT.format(cat_name)
|
||||||
|
BattleCategory.print_headers(header)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def print_results(self):
|
def print_results(self):
|
||||||
try:
|
try:
|
||||||
bu.print_new_line()
|
bu.print_new_line()
|
||||||
BattleCategory.print_headers(self.title)
|
self.print_headers()
|
||||||
for cat in self.get_categories():
|
for cat in self.get_categories():
|
||||||
print(self.RESULT_CAT_FRMT.format(cat), end=' ')
|
if len(cat) > self.RESULT_CAT_HEADER_LEN:
|
||||||
|
cat_name = cat[:self.RESULT_CAT_HEADER_LEN]
|
||||||
|
else:
|
||||||
|
cat_name = cat
|
||||||
|
print(self.RESULT_CAT_FRMT.format(cat_name), end=' ')
|
||||||
self.categories[cat].print_results()
|
self.categories[cat].print_results()
|
||||||
except KeyError as err:
|
except KeyError as err:
|
||||||
bu.error('Key not found', err)
|
bu.error('Key not found', err)
|
||||||
@@ -547,6 +573,17 @@ class BattleClassCategorization(BattleCategorization):
|
|||||||
class BattleStringCategorization(BattleCategorization):
|
class BattleStringCategorization(BattleCategorization):
|
||||||
"""Class for categorization by string"""
|
"""Class for categorization by string"""
|
||||||
|
|
||||||
|
def __init__(self, cat_key : str, title: str, params: list):
|
||||||
|
super().__init__(cat_key, title)
|
||||||
|
if len(params) > 0:
|
||||||
|
width = params[0]
|
||||||
|
left_align = True # align long category names left by default
|
||||||
|
if len(params) > 1:
|
||||||
|
left_align = params[1]
|
||||||
|
|
||||||
|
self.set_cat_header_format(width, left_align)
|
||||||
|
|
||||||
|
|
||||||
def get_category(self, result: dict) -> str:
|
def get_category(self, result: dict) -> str:
|
||||||
"""Get category"""
|
"""Get category"""
|
||||||
try:
|
try:
|
||||||
@@ -556,7 +593,22 @@ class BattleStringCategorization(BattleCategorization):
|
|||||||
except Exception as err:
|
except Exception as err:
|
||||||
bu.error('Category: ' + self.category_key, exception=err)
|
bu.error('Category: ' + self.category_key, exception=err)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_categories(self):
|
||||||
|
"""Get category keys sorted"""
|
||||||
|
try:
|
||||||
|
p_number_sort = re.compile(r'^(\d+): .+')
|
||||||
|
cats = list(self.categories.keys())
|
||||||
|
if (p_number_sort.match(cats[0]) != None):
|
||||||
|
ndxs = sorted(range(len(cats)), key=lambda k: int(cats[k][:cats[k].find(':')]) )
|
||||||
|
return [ cats[i] for i in ndxs ]
|
||||||
|
else:
|
||||||
|
return sorted(cats , key=str.casefold)
|
||||||
|
|
||||||
|
except Exception as err:
|
||||||
|
bu.error('Category: ' + self.category_key, exception=err)
|
||||||
|
return None
|
||||||
|
|
||||||
def get_filter_categories(self, filters: list) -> list:
|
def get_filter_categories(self, filters: list) -> list:
|
||||||
try:
|
try:
|
||||||
@@ -793,7 +845,6 @@ class BattleCategory():
|
|||||||
avg_fields = set()
|
avg_fields = set()
|
||||||
ratio_fields = set()
|
ratio_fields = set()
|
||||||
|
|
||||||
RESULT_CAT_HEADER_FRMT = '{:_<20s}'
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_modes(cls) -> list:
|
def get_modes(cls) -> list:
|
||||||
@@ -890,9 +941,9 @@ class BattleCategory():
|
|||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def print_headers(cls, cat_name: str):
|
def print_headers(cls, header: str):
|
||||||
try:
|
try:
|
||||||
headers = [ cls.RESULT_CAT_HEADER_FRMT.format(cat_name) ]
|
headers = [ header ]
|
||||||
for field in cls.result_fields:
|
for field in cls.result_fields:
|
||||||
print_format = '{:^' + str(cls.get_field_width(field)) + 's}'
|
print_format = '{:^' + str(cls.get_field_width(field)) + 's}'
|
||||||
headers.append(print_format.format(cls.get_field_name(field)))
|
headers.append(print_format.format(cls.get_field_name(field)))
|
||||||
@@ -2071,9 +2122,10 @@ async def read_replay_JSON(replay_json: dict, args : argparse.Namespace) -> dict
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
result['_id'] = replay_json['_id']
|
result['_id'] = replay_json['_id']
|
||||||
result['battle_start_timestamp'] = int(replay_json['data']['summary']['battle_start_timestamp'])
|
replay_summary = replay_json['data']['summary']
|
||||||
|
result['battle_start_timestamp'] = int(replay_summary['battle_start_timestamp'])
|
||||||
# TBD...
|
# TBD...
|
||||||
protagonist = int(replay_json['data']['summary']['protagonist'])
|
protagonist = int(replay_summary['protagonist'])
|
||||||
|
|
||||||
# For filtering replays per submitter
|
# For filtering replays per submitter
|
||||||
result['protagonist'] = protagonist
|
result['protagonist'] = protagonist
|
||||||
@@ -2081,17 +2133,17 @@ async def read_replay_JSON(replay_json: dict, args : argparse.Namespace) -> dict
|
|||||||
if account_id == None:
|
if account_id == None:
|
||||||
account_id = protagonist
|
account_id = protagonist
|
||||||
elif protagonist != account_id:
|
elif protagonist != account_id:
|
||||||
if account_id in replay_json['data']['summary']['enemies']:
|
if account_id in replay_summary['enemies']:
|
||||||
# switch the teams...
|
# switch the teams...
|
||||||
if replay_json['data']['summary']['battle_result'] != 2:
|
if replay_summary['battle_result'] != 2:
|
||||||
if replay_json['data']['summary']['battle_result'] == 0:
|
if replay_summary['battle_result'] == 0:
|
||||||
replay_json['data']['summary']['battle_result'] = 1
|
replay_summary['battle_result'] = 1
|
||||||
else:
|
else:
|
||||||
replay_json['data']['summary']['battle_result'] = 0
|
replay_summary['battle_result'] = 0
|
||||||
tmp = replay_json['data']['summary']['enemies']
|
tmp = replay_summary['enemies']
|
||||||
replay_json['data']['summary']['enemies'] = replay_json['data']['summary']['allies']
|
replay_summary['enemies'] = replay_summary['allies']
|
||||||
replay_json['data']['summary']['allies'] = tmp
|
replay_summary['allies'] = tmp
|
||||||
elif account_id in replay_json['data']['summary']['allies']:
|
elif account_id in replay_summary['allies']:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
# looking for an account_id, but account_id not found in the teams => skipping the replay
|
# looking for an account_id, but account_id not found in the teams => skipping the replay
|
||||||
@@ -2100,7 +2152,7 @@ async def read_replay_JSON(replay_json: dict, args : argparse.Namespace) -> dict
|
|||||||
result['url'] = replay_json['data']['view_url']
|
result['url'] = replay_json['data']['view_url']
|
||||||
|
|
||||||
for key in replay_summary_flds:
|
for key in replay_summary_flds:
|
||||||
result[key] = replay_json['data']['summary'][key]
|
result[key] = replay_summary[key]
|
||||||
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
bu.error(exception=err)
|
bu.error(exception=err)
|
||||||
@@ -2114,7 +2166,7 @@ async def read_replay_JSON(replay_json: dict, args : argparse.Namespace) -> dict
|
|||||||
btl_duration = 0
|
btl_duration = 0
|
||||||
btl_tier = 0
|
btl_tier = 0
|
||||||
protagonist_tank = None
|
protagonist_tank = None
|
||||||
for player in replay_json['data']['summary']['details']:
|
for player in replay_summary['details']:
|
||||||
btl_duration = max(btl_duration, player['time_alive'])
|
btl_duration = max(btl_duration, player['time_alive'])
|
||||||
player_tank_tier = wg.get_tank_data(player['vehicle_descr'], 'tier')
|
player_tank_tier = wg.get_tank_data(player['vehicle_descr'], 'tier')
|
||||||
btl_tier = max(btl_tier, player_tank_tier)
|
btl_tier = max(btl_tier, player_tank_tier)
|
||||||
@@ -2154,7 +2206,7 @@ async def read_replay_JSON(replay_json: dict, args : argparse.Namespace) -> dict
|
|||||||
else:
|
else:
|
||||||
survived = False
|
survived = False
|
||||||
|
|
||||||
if player['dbid'] in replay_json['data']['summary']['allies']:
|
if player['dbid'] in replay_summary['allies']:
|
||||||
result['allies'].add(get_stat_id(tmp_account_id, tmp_tank_id, tmp_battletime))
|
result['allies'].add(get_stat_id(tmp_account_id, tmp_tank_id, tmp_battletime))
|
||||||
if survived:
|
if survived:
|
||||||
result['allies_survived'] += 1
|
result['allies_survived'] += 1
|
||||||
@@ -2168,9 +2220,9 @@ async def read_replay_JSON(replay_json: dict, args : argparse.Namespace) -> dict
|
|||||||
bu.debug('Player stats_id: ' + result['player'])
|
bu.debug('Player stats_id: ' + result['player'])
|
||||||
# remove platoon buddy from stats
|
# remove platoon buddy from stats
|
||||||
if result['squad_index'] != None:
|
if result['squad_index'] != None:
|
||||||
for player in replay_json['data']['summary']['details']:
|
for player in replay_summary['details']:
|
||||||
bu.debug(str(player))
|
bu.debug(str(player))
|
||||||
if (player['squad_index'] == result['squad_index']) and (player['dbid'] in replay_json['data']['summary']['allies']) and (player['dbid'] != account_id):
|
if (player['squad_index'] == result['squad_index']) and (player['dbid'] in replay_summary['allies']) and (player['dbid'] != account_id):
|
||||||
# platoon buddy found
|
# platoon buddy found
|
||||||
tmp_account_id = player['dbid']
|
tmp_account_id = player['dbid']
|
||||||
tmp_tank_id = player['vehicle_descr']
|
tmp_tank_id = player['vehicle_descr']
|
||||||
@@ -2187,7 +2239,8 @@ async def read_replay_JSON(replay_json: dict, args : argparse.Namespace) -> dict
|
|||||||
result['top_tier'] = 1 if (result['tank_tier'] == btl_tier) else 0
|
result['top_tier'] = 1 if (result['tank_tier'] == btl_tier) else 0
|
||||||
result['win'] = 1 if result['battle_result'] == 1 else 0
|
result['win'] = 1 if result['battle_result'] == 1 else 0
|
||||||
REPLAY_I += 1
|
REPLAY_I += 1
|
||||||
result['battle_i'] = REPLAY_I
|
result['battle_i'] = REPLAY_I
|
||||||
|
result['battle'] = str(REPLAY_I) + ': ' + replay_summary['vehicle'] + ' @' + replay_summary['map_name']
|
||||||
bu.debug(str(result))
|
bu.debug(str(result))
|
||||||
return result
|
return result
|
||||||
except KeyError as err:
|
except KeyError as err:
|
||||||
|
|||||||
Reference in new issue
Block a user