Skip to content

Commit

Permalink
feat: implement actual counting of game statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
d-Rickyy-b committed Sep 13, 2020
1 parent f18dcc7 commit c22f270
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
12 changes: 8 additions & 4 deletions blackjack/game/blackjackgame.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def __init__(self, gametype=None, game_id=None, lang_id="en"):
self.logger = logging.getLogger(__name__)
self.__on_start_handlers = []
self.__on_stop_handlers = []
self.list_won = []
self.list_tie = []
self.list_lost = []
self.bets_active = True
self._current_player = 0
self.players = []
self.running = False
Expand Down Expand Up @@ -209,11 +213,11 @@ def evaluation(self):

list_lost.extend(list_busted)

list_won = sorted(list_won, key=lambda player: player.cardvalue, reverse=True)
list_tie = sorted(list_tie, key=lambda player: player.cardvalue, reverse=True)
list_lost = sorted(list_lost, key=lambda player: player.cardvalue, reverse=True)
self.list_won = sorted(list_won, key=lambda player: player.cardvalue, reverse=True)
self.list_tie = sorted(list_tie, key=lambda player: player.cardvalue, reverse=True)
self.list_lost = sorted(list_lost, key=lambda player: player.cardvalue, reverse=True)

return list_won, list_tie, list_lost
return self.list_won, self.list_tie, self.list_lost

def get_player_list(self):
return "\n".join(["👤{}".format(p.first_name) for p in self.players])
2 changes: 1 addition & 1 deletion blackjackbot/commands/util/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def stats_cmd(update, context):
update.message.reply_text(get_user_stats(update.effective_user.id))
update.message.reply_text(get_user_stats(update.effective_user.id), parse_mode="HTML")


def comment_cmd(update, context):
Expand Down
8 changes: 5 additions & 3 deletions blackjackbot/gamestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from random import randint

from .errors.noactivegameexception import NoActiveGameException
import database.statistics


class GameStore(object):
Expand Down Expand Up @@ -74,9 +75,10 @@ def _game_stopped_callback(self, game):
:param game:
:return:
"""
# TODO Game statistics
# for player in game.players:
# player set game won
for player in game.players:
database.statistics.add_game_played(player.user_id)
if player in game.list_won:
database.statistics.set_game_won(player.user_id)
self.remove_game(self._game_dict[game.id])

self.logger.debug("Current games: {}".format(len(self._chat_dict)))
9 changes: 5 additions & 4 deletions database/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from time import time

from database import Database
from blackjackbot.lang import translate

__author__ = 'Rico'
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -44,14 +45,14 @@ def get_user_stats(user_id):
:return:
"""
user = Database().get_user(user_id)
lang_id = Database().get_lang_id(user_id)

played_games = int(user[5]) or 1
won_games = user[6]
last_played = int(user[8])
last_played_formatted = datetime.utcfromtimestamp(last_played).strftime('%d.%m.%y %H:%M')
win_percentage = round(float(won_games) / float(played_games), 4) * 100
bar = generate_bar_chart(win_percentage)

template = "Here are your statistics 📊:\n\nPlayed Games: {}\nWon Games: {}\nLast Played: {} UTC\n\n{}\n\nWinning rate: {:.2%}"
win_percentage = round(float(won_games) / float(played_games), 4)
bar = generate_bar_chart(win_percentage * 100)
template = translate("statistic_template", lang_id)
statistics_string = template.format(played_games, won_games, last_played_formatted, bar, win_percentage)
return statistics_string

0 comments on commit c22f270

Please sign in to comment.