mirror of
https://github.com/wlinator/luminara.git
synced 2024-10-02 20:23:12 +00:00
Stats command in beta mode
For now this command will be pushed with the "beta" check, so I can test the stats command on the main bot's database.
This commit is contained in:
parent
15d7e7e8ae
commit
c8e3708b7e
10 changed files with 119 additions and 6 deletions
8
config/json_loader.py
Normal file
8
config/json_loader.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def load_strings(path="config/strings.en-US.json"):
|
||||||
|
with open(path, 'r') as file:
|
||||||
|
data = json.load(file)
|
||||||
|
|
||||||
|
return data
|
|
@ -30,5 +30,10 @@
|
||||||
"duel_challenge": "**{0}** challenges {1} to a duel ({2}{3}{4})\nUse the buttons to accept/deny (challenge expires after 60s)",
|
"duel_challenge": "**{0}** challenges {1} to a duel ({2}{3}{4})\nUse the buttons to accept/deny (challenge expires after 60s)",
|
||||||
"duel_results": "{1} wins **{2}{3}**\n{4} loses this bet.",
|
"duel_results": "{1} wins **{2}{3}**\n{4} loses this bet.",
|
||||||
"duel_cancel": "**{0}** canceled the duel.",
|
"duel_cancel": "**{0}** canceled the duel.",
|
||||||
"duel_time": "Time ran out."
|
"duel_time": "Time ran out.",
|
||||||
|
"stats_all_title": "All Racu Stats",
|
||||||
|
"stats_all_blackjack": "BlackJack Stats",
|
||||||
|
"stats_all_gambling_value": "**{0}** - # of games\n**{1}** - wins (#)\n**{2}** - losses (#)\n**{3}%** - ROI ($)",
|
||||||
|
"stats_all_gambling_value2": "Racu has logged **{0}** games.\n**{1}** of those were won by players!\nThe dealer beat you in **{2}** games.\nReturn of investment: **{3}%**",
|
||||||
|
"stats_all_gambling_value3": "Games played: **{0}**\nGames won: **{1}**\nGames lost: **{2}**\nProfit margin: **{3}**%"
|
||||||
}
|
}
|
|
@ -22,6 +22,35 @@ class BlackJackStats:
|
||||||
|
|
||||||
database.execute_query(query, values)
|
database.execute_query(query, values)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def count_games(user_id=None):
|
||||||
|
if not user_id:
|
||||||
|
# count ALL blackjack games
|
||||||
|
query = "SELECT COUNT(*) FROM stats_bj"
|
||||||
|
amount = database.select_query_one(query)
|
||||||
|
return amount
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_investment_and_payout(user_id=None):
|
||||||
|
if not user_id:
|
||||||
|
# return from ALL blackjack games
|
||||||
|
query = "SELECT SUM(bet), SUM(payout) FROM stats_bj"
|
||||||
|
(investment, payout) = database.select_query(query)[0]
|
||||||
|
return investment, payout
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_winning_and_losing_amount(user_id=None):
|
||||||
|
if not user_id:
|
||||||
|
# return from ALL blackjack games
|
||||||
|
query = """
|
||||||
|
SELECT
|
||||||
|
SUM(CASE WHEN is_won = 1 THEN 1 ELSE 0 END) AS winning,
|
||||||
|
SUM(CASE WHEN is_won = 0 THEN 1 ELSE 0 END) AS losing
|
||||||
|
FROM stats_bj;
|
||||||
|
"""
|
||||||
|
(winning, losing) = database.select_query(query)[0]
|
||||||
|
return winning, losing
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fetch_all():
|
def fetch_all():
|
||||||
query = "SELECT * FROM stats_bj"
|
query = "SELECT * FROM stats_bj"
|
||||||
|
|
|
@ -6,7 +6,7 @@ import pytz
|
||||||
from data.Currency import Currency
|
from data.Currency import Currency
|
||||||
from db import database
|
from db import database
|
||||||
|
|
||||||
with open("json/economy.json") as file:
|
with open("config/economy.json") as file:
|
||||||
json_data = json.load(file)
|
json_data = json.load(file)
|
||||||
|
|
||||||
|
|
||||||
|
|
5
main.py
5
main.py
|
@ -11,15 +11,18 @@ import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
import db.tables
|
import db.tables
|
||||||
import sb_tools.resources
|
import sb_tools.resources
|
||||||
|
from config import json_loader
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
load_dotenv('.env')
|
load_dotenv('.env')
|
||||||
|
|
||||||
|
# load all strings.en-US.json strings
|
||||||
|
strings = json_loader.load_strings()
|
||||||
|
|
||||||
sbbot = discord.Bot(
|
sbbot = discord.Bot(
|
||||||
owner_id=os.getenv('OWNER_ID'),
|
owner_id=os.getenv('OWNER_ID'),
|
||||||
intents=discord.Intents.all(),
|
intents=discord.Intents.all(),
|
||||||
|
|
|
@ -15,7 +15,7 @@ active_blackjack_games = {}
|
||||||
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
|
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
|
||||||
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
|
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
|
||||||
|
|
||||||
with open("json/economy.json") as file:
|
with open("config/economy.json") as file:
|
||||||
json_data = json.load(file)
|
json_data = json.load(file)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ active_blackjack_games = {}
|
||||||
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
|
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
|
||||||
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
|
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
|
||||||
|
|
||||||
with open("json/economy.json") as file:
|
with open("config/economy.json") as file:
|
||||||
json_data = json.load(file)
|
json_data = json.load(file)
|
||||||
|
|
||||||
|
|
||||||
|
|
68
modules/stats.py
Normal file
68
modules/stats.py
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
from data.BlackJackStats import BlackJackStats
|
||||||
|
from main import strings
|
||||||
|
from sb_tools import universal
|
||||||
|
|
||||||
|
load_dotenv('.env')
|
||||||
|
|
||||||
|
active_blackjack_games = {}
|
||||||
|
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
|
||||||
|
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
|
||||||
|
|
||||||
|
with open("config/economy.json") as file:
|
||||||
|
json_data = json.load(file)
|
||||||
|
|
||||||
|
|
||||||
|
class Stats(commands.Cog):
|
||||||
|
def __init__(self, sbbot):
|
||||||
|
self.bot = sbbot
|
||||||
|
|
||||||
|
stats = discord.SlashCommandGroup("stats", "Racu stats.")
|
||||||
|
|
||||||
|
@stats.command(
|
||||||
|
name="all",
|
||||||
|
description="Show the stats for all Racu users."
|
||||||
|
)
|
||||||
|
# @commands.check(universal.channel_check)
|
||||||
|
@commands.check(universal.beta_check)
|
||||||
|
async def all(self, ctx):
|
||||||
|
# collect data
|
||||||
|
bj_games_amount = BlackJackStats.count_games()
|
||||||
|
(bj_winning_games_amount, bj_losing_games_amount) = BlackJackStats.get_winning_and_losing_amount()
|
||||||
|
(bj_total_investment, bj_total_payout) = BlackJackStats.get_investment_and_payout()
|
||||||
|
|
||||||
|
# calculate data
|
||||||
|
ROI = ((bj_total_payout - bj_total_investment) / bj_total_investment) * 100
|
||||||
|
|
||||||
|
# output
|
||||||
|
embed = discord.Embed(
|
||||||
|
title=strings["stats_all_title"]
|
||||||
|
)
|
||||||
|
embed.add_field(name=strings["stats_all_blackjack"],
|
||||||
|
value=strings["stats_all_gambling_value3"].format(
|
||||||
|
bj_games_amount,
|
||||||
|
bj_winning_games_amount,
|
||||||
|
bj_losing_games_amount,
|
||||||
|
ROI
|
||||||
|
))
|
||||||
|
|
||||||
|
await ctx.respond(embed=embed)
|
||||||
|
|
||||||
|
@stats.command(
|
||||||
|
name="me",
|
||||||
|
description="Show your personal Racu stats."
|
||||||
|
)
|
||||||
|
# @commands.check(universal.channel_check)
|
||||||
|
@commands.check(universal.beta_check)
|
||||||
|
async def me(self, ctx):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def setup(sbbot):
|
||||||
|
sbbot.add_cog(Stats(sbbot))
|
|
@ -12,7 +12,7 @@ cash_balance_name = os.getenv("CASH_BALANCE_NAME")
|
||||||
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
|
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
|
||||||
est = pytz.timezone('US/Eastern')
|
est = pytz.timezone('US/Eastern')
|
||||||
|
|
||||||
with open("json/economy.json") as file:
|
with open("config/economy.json") as file:
|
||||||
json_data = json.load(file)
|
json_data = json.load(file)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue