Adding location_stats dict to hold stats for each run (#297)

* Adding location_stats dict to hold stats for each run

Logs Items,Deaths,Chickens,Merc Deaths, and Fails for each run
Discord message prints stats by run in table
stats.log lists run table and items by run

* clean up

Co-authored-by: aeon0 <aeon4@pm.me>
This commit is contained in:
egut125
2021-12-17 13:57:12 -05:00
committed by GitHub
parent db63ce66b6
commit bfcd2babaa

View File

@@ -5,7 +5,7 @@ from config import Config
from messenger import Messenger
from utils.misc import hms
import inspect
from beautifultable import BeautifulTable
from version import __version__
@@ -25,10 +25,12 @@ class GameStats:
self._runs_failed = 0
self._failed_game_time = 0
self._location = None
self._location_stats = {}
def update_location(self, loc: str):
if self._location != loc:
self._location = str(loc)
self.populate_location_stat()
def get_location_msg(self):
if self._location is not None:
@@ -45,32 +47,45 @@ class GameStats:
send_message_thread.daemon = True
send_message_thread.start()
def populate_location_stat(self):
if self._location not in self._location_stats:
self._location_stats[self._location] = { "items": [], "deaths": 0, "chickens": 0, "merc_deaths": 0, "failed_runs": 0 }
def log_item_pickup(self, item_name: str, send_message: bool):
self._picked_up_items.append(item_name)
if self._location is not None:
self._location_stats[self._location]["items"].append(item_name)
if send_message:
msg = f"{self._config.general['name']}: Found {item_name}{self.get_location_msg()}"
self._send_message_thread(msg)
def log_death(self):
self._death_counter += 1
if self._location is not None:
self._location_stats[self._location]["deaths"] += 1
msg = f"{self._config.general['name']}: You have died{self.get_location_msg()}"
self._send_message_thread(msg)
def log_chicken(self):
self._chicken_counter += 1
if self._location is not None:
self._location_stats[self._location]["chickens"] += 1
msg = f"{self._config.general['name']}: You have chickened{self.get_location_msg()}"
self._send_message_thread(msg)
def log_merc_death(self):
self._merc_death_counter += 1
# TODO: That message comes up a bit often, either make a param for it or remove it completely
# self._send_message_thread(f"{self._config.general['name']}: Merc has died{self.get_location_msg()}")
self._merc_death_counter += 1
# TODO: That message comes up a bit often, either make a param for it or remove it completely
# self._send_message_thread(f"{self._config.general['name']}: Merc has died{self.get_location_msg()}")
if self._location is not None:
self._location_stats[self._location]["merc_deaths"] += 1
def log_start_game(self):
if self._game_counter > 0:
self._save_stats_to_file()
if self._config.general["discord_status_count"] and self._game_counter % self._config.general["discord_status_count"] == 0:
# every 20th game send a message update about current status
# every discord_status_count game send a message update about current status
self._send_status_update()
self._game_counter += 1
self._timer = time.time()
@@ -83,6 +98,8 @@ class GameStats:
self._timer = None
if failed:
self._runs_failed += 1
if self._location is not None:
self._location_stats[self._location]["failed_runs"] += 1
self._failed_game_time += elapsed_time
Logger.warning(f"End failed game: Elpased time: {elapsed_time:.2f}s")
else:
@@ -122,11 +139,19 @@ class GameStats:
Session length: {elapsed_time_str}
Games: {self._game_counter}
Avg Game Length: {avg_length_str}
Chickens: {self._chicken_counter}
Deaths: {self._death_counter}
Merc deaths: {self._merc_death_counter}
Failed runs: {self._runs_failed}
''')
totals = { "items": 0, "chickens": 0, "deaths": 0, "merc_deaths": 0, "failed_runs": 0 }
table = BeautifulTable()
for location in self._location_stats:
stats = self._location_stats[location]
totals["items"] += len(stats["items"])
totals["chickens"] += stats["chickens"]
totals["deaths"] += stats["deaths"]
totals["merc_deaths"] += stats["merc_deaths"]
table.rows.append([location, len(stats["items"]), stats["chickens"], stats["deaths"], stats["merc_deaths"], stats["failed_runs"]])
table.rows.append(["Total", totals["items"], totals["chickens"], totals["deaths"], totals["merc_deaths"], totals["failed_runs"]])
table.columns.header = ["Run", "I", "C", "D", "MD", "F"]
msg += f"\n{str(table)}\n"
return msg
def _send_status_update(self):
@@ -135,9 +160,13 @@ class GameStats:
def _save_stats_to_file(self):
msg = self._create_msg()
msg += "\nItems:"
for item_name in self._picked_up_items:
msg += f"\n {item_name}"
msg += "\nItems:"
for location in self._location_stats:
stats = self._location_stats[location]
msg += f"\n {location}:"
for item_name in stats["items"]:
msg += f"\n {item_name}"
with open("stats.log", "w+") as f:
f.write(msg)