Fix a string formatting bug.

This commit is contained in:
Jylpah committed 2021-02-06 15:29:17 +02:00
1 parent 970c24072d
commit 257ba190c2
1 file changed
+32 -14
+32 -14
View File
@@ -131,7 +131,7 @@ replay_details_flds = [
## Syntax: key == stat field in https://api.wotblitz.eu/wotb/tanks/stats/ (all.KEY) ## Syntax: key == stat field in https://api.wotblitz.eu/wotb/tanks/stats/ (all.KEY)
## Value array [ 'Stat Title', [ 0, data buckets ....], scaling_factor_for_bucket_values, 'print_format' ] ## Value array [ 'Stat Title', [ 0, data buckets ....], scaling_factor_for_bucket_values, 'print_format' ]
histogram_fields = { histogram_fields = {
'wins' : [ 'Win rate', [0, .35, .40, .45, .48, .5, .52, .55, .60, .65, .70, 1], 100, '{:2.0f}%' ], 'wins' : [ 'Win rate', [0, .35, .40, .45, .48, .5, .52, .55, .60, .65, .70, 1], 100, '{:2.0%}' ],
'damage_dealt' : [ 'Avg. Dmg.', [0, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1750, 2000, 2250, 2500, 2750, 3000, 100e3], 1, '{:3.0f}' ], 'damage_dealt' : [ 'Avg. Dmg.', [0, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1750, 2000, 2250, 2500, 2750, 3000, 100e3], 1, '{:3.0f}' ],
'battles' : [ 'Battles', [0, 1000, 2500, 5000, 7000, 10e3, 15e3, 25e3, 50e3, 5e7], .001, '{:.0f}k'] # battles is a mandatory stat to include 'battles' : [ 'Battles', [0, 1000, 2500, 5000, 7000, 10e3, 15e3, 25e3, 50e3, 5e7], .001, '{:.0f}k'] # battles is a mandatory stat to include
} }
@@ -196,7 +196,7 @@ 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 ], '%' ],
'time_alive%' : [ 'Time alive %', 'bucket', [ x/100 for x in range(0,100,10) ], '%' ], 'alive' : [ 'Time alive (pcs)', 'bucket', [ x/100 for x in range(0,100,10) ], '%' ],
'time_alive' : [ 'Time alive (s)', 'bucket', [ x*60 for x in range(0,8) ], 'int' ], 'time_alive' : [ 'Time alive (s)', 'bucket', [ x*60 for x in range(0,8) ], 'int' ],
'battle_duration' : [ 'Battle Duration', 'bucket', [ x*60 for x in range(0,8) ], 'int' ], 'battle_duration' : [ 'Battle Duration', 'bucket', [ x*60 for x in range(0,8) ], 'int' ],
'player_name' : [ 'Player', 'string', 25 ], 'player_name' : [ 'Player', 'string', 25 ],
@@ -897,7 +897,7 @@ class BattleCategory():
'pen_rate' : [ 'Pen rate', 'Shots pen / shots hit', 8, '{:8.1%}' ], 'pen_rate' : [ 'Pen rate', 'Shots pen / shots hit', 8, '{:8.1%}' ],
'survived' : [ 'Surv%', 'Survival rate', 6, '{:6.1%}' ], 'survived' : [ 'Surv%', 'Survival rate', 6, '{:6.1%}' ],
'time_alive' : [ 'T alive', 'Time being alive in a battle in secs', 7, '{:7.0f}' ], 'time_alive' : [ 'T alive', 'Time being alive in a battle in secs', 7, '{:7.0f}' ],
'time_alive%' : [ 'T alive%', 'Percentage of time being alive in a battle', 8, '{:8.0%}' ], 'alive' : [ 'Share live', 'Percentage of time being alive in a battle', 8, '{:8.0%}' ],
'battle_duration' : [ 'Duration', 'Battle duration', 8, '{:8.0f}' ], 'battle_duration' : [ 'Duration', 'Battle duration', 8, '{:8.0f}' ],
'top_tier' : [ 'Top tier', 'Share of games as top tier', 8, '{:8.0%}' ], 'top_tier' : [ 'Top tier', 'Share of games as top tier', 8, '{:8.0%}' ],
'player_wins' : [ 'Player WR', 'Average WR of the player', 9, '{:9.2%}' ], 'player_wins' : [ 'Player WR', 'Average WR of the player', 9, '{:9.2%}' ],
@@ -915,10 +915,16 @@ class BattleCategory():
# fields to display in results # fields to display in results
_result_fields_modes = { _result_fields_modes = {
'default' : [ 'battles', 'win','damage_made', 'enemies_destroyed', 'enemies_spotted', 'top_tier', 'DR', 'survived', 'allies_wins', 'enemies_wins'], 'default' : [ 'battles', 'win','damage_made', 'enemies_destroyed', 'enemies_spotted',
'top_tier', 'DR', 'survived', 'allies_wins', 'enemies_wins'],
'team' : [ 'battles', 'win','player_wins','allies_wins','enemies_wins'], 'team' : [ 'battles', 'win','player_wins','allies_wins','enemies_wins'],
'team_extended' : [ 'battles', 'battles%', 'win','player_wins','allies_wins','enemies_wins', 'player_damage_dealt', 'allies_damage_dealt', 'enemies_damage_dealt', 'player_battles','allies_battles','enemies_battles' ], 'team_extended' : [ 'battles', 'battles%', 'win','player_wins','allies_wins','enemies_wins',
'extended' : [ 'battles', 'battles%', 'win','damage_made', 'enemies_destroyed', 'enemies_spotted', 'top_tier', 'DR', 'KDR', 'hit_rate', 'pen_rate', 'survived', 'battle_duration', 'time_alive%', 'player_wins', 'allies_wins', 'enemies_wins', MISSING_STATS ] 'player_damage_dealt', 'allies_damage_dealt', 'enemies_damage_dealt',
'player_battles','allies_battles','enemies_battles' ],
'extended' : [ 'battles', 'battles%', 'win','damage_made', 'enemies_destroyed', 'enemies_spotted',
'top_tier', 'DR', 'KDR', 'hit_rate', 'pen_rate', 'survived', 'battle_duration',
'alive', 'player_wins', 'allies_wins', 'enemies_wins', MISSING_STATS ],
'all' : [ cat for cat in _result_fields.keys() ]
} }
_team_fields = [ 'wins', 'battles', 'damage_dealt' ] _team_fields = [ 'wins', 'battles', 'damage_dealt' ]
@@ -981,6 +987,10 @@ class BattleCategory():
return cls.result_fields return cls.result_fields
@classmethod
def get_result_fields_all(cls) -> list:
return cls._result_fields.keys()
@classmethod @classmethod
def get_result_fields_ratio(cls, all: bool = False) -> list: def get_result_fields_ratio(cls, all: bool = False) -> list:
if all: if all:
@@ -1280,7 +1290,7 @@ class PlayerHistogram():
print("\n{:12s} | {:13s} | {:13s} | {:13s}".format(self.name, "Allies", "Enemies", "TOTAL")) print("\n{:12s} | {:13s} | {:13s} | {:13s}".format(self.name, "Allies", "Enemies", "TOTAL"))
for cat in self.results: for cat in self.results:
stat = self.results[cat] stat = self.results[cat]
print("{:12s} | {:5d} ({:4.1f}%) | {:5d} ({:4.1f}%) | {:5d} ({:4.1f}%)".format(cat, stat['allies'], stat['allies%']*100, stat['enemies'], stat['enemies%']*100, stat['total'], stat['total%']*100 )) print("{:12s} | {:5d} ({:4.1%}) | {:5d} ({:4.1%}) | {:5d} ({:4.1%})".format(cat, stat['allies'], stat['allies%']*100, stat['enemies'], stat['enemies%']*100, stat['total'], stat['total%']*100 ))
else: else:
bu.error('Results have not been calculated yet.') bu.error('Results have not been calculated yet.')
except Exception as err: except Exception as err:
@@ -1300,10 +1310,6 @@ class ErrorCatchingArgumentParser(argparse.ArgumentParser):
## main() ------------------------------------------------------------- ## main() -------------------------------------------------------------
OPT_MODE_DEFAULT = 'default'
OPT_MODE_HELP = 'help'
OPT_MODES = BattleCategory.get_modes() + [OPT_MODE_HELP]
async def main(argv): async def main(argv):
global wg, wi, WG_APP_ID, OPT_MODE_DEFAULT global wg, wi, WG_APP_ID, OPT_MODE_DEFAULT
# set the directory for the script # set the directory for the script
@@ -1311,6 +1317,9 @@ async def main(argv):
os.chdir(os.path.dirname(sys.argv[0])) os.chdir(os.path.dirname(sys.argv[0]))
## Default options: ## Default options:
OPT_MODE_DEFAULT = 'default'
OPT_MODE_HELP = 'help'
OPT_MODES = BattleCategory.get_modes() + [OPT_MODE_HELP]
OPT_DB = False OPT_DB = False
OPT_HIST = False OPT_HIST = False
OPT_STAT_FUNC = StatFunc.get_default() OPT_STAT_FUNC = StatFunc.get_default()
@@ -1422,6 +1431,7 @@ async def main(argv):
try: try:
args = parser.parse_args() args = parser.parse_args()
except Exception as err: except Exception as err:
bu.error('Invalid arguments', exception=err)
sys.exit(0) sys.exit(0)
# res_categories = BattleCategorization.get_categorizations(OPT_CATEGORIZATIONS, args) # res_categories = BattleCategorization.get_categorizations(OPT_CATEGORIZATIONS, args)
@@ -1606,6 +1616,14 @@ async def help_extended(db : motor.motor_asyncio.AsyncIOMotorDatabase = None, pa
print("\tExample: --filter '{ \"room_type\" :1 }'") print("\tExample: --filter '{ \"room_type\" :1 }'")
print('') print('')
BattleCategorizationList.help() BattleCategorizationList.help()
print('\n-------------------------------------------------------------------')
print('| Rounding errors |')
print('-------------------------------------------------------------------')
print('')
print('Yes, the numbers are rounded UP because reasons.')
print('If interested why please read this: ')
print('\thttps://realpython.com/python-rounding/#pythons-built-in-round-function')
print('No, I am not planning to fix these for now.')
## move the class PlayerHistogram? ## move the class PlayerHistogram?
@@ -2416,11 +2434,11 @@ async def read_replay_JSON(replay_json: dict, args : argparse.Namespace) -> dict
result['enemies'] = set() result['enemies'] = set()
result['allies_survived'] = 0 # for steamroller stats result['allies_survived'] = 0 # for steamroller stats
result['enemies_survived'] = 0 # for steamroller stats result['enemies_survived'] = 0 # for steamroller stats
btl_duration = 0 #btl_duration = result['battle_duration']
btl_tier = 0 btl_tier = 0
protagonist_tank = None protagonist_tank = None
for player in replay_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)
@@ -2487,7 +2505,7 @@ async def read_replay_JSON(replay_json: dict, args : argparse.Namespace) -> dict
for ratio in BattleCategory.get_result_fields_ratio(all=True): for ratio in BattleCategory.get_result_fields_ratio(all=True):
result[ratio] = BattleCategory.calc_ratio(ratio, result) result[ratio] = BattleCategory.calc_ratio(ratio, result)
result['time_alive%'] = result['time_alive'] / btl_duration result['alive'] = min(result['time_alive'] / result['battle_duration'], 1)
result['battle_tier'] = btl_tier result['battle_tier'] = btl_tier
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