Skip to content

Commit

Permalink
Added in ability to get player scores and reward/punish appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Luckie committed Mar 30, 2024
1 parent be048a9 commit d7d2015
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
2 changes: 2 additions & 0 deletions disc_bot/cog_helpers/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class DiscordPlayer:

def __post_init__(self):
if self.score is None:
# this must be done in post init since the definition of _G will be
# defined on import for all instances of this class
self.score = _G.DEFAULT_SCORE
15 changes: 13 additions & 2 deletions disc_bot/cog_helpers/server_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
from collections import UserDict
from .player import DiscordPlayer
from ..config.globals import _G
from ..exceptions.ranks import PlayerNotFound
from dataclasses import dataclass as dc
import warnings

class ServerContainer(UserDict):

def get_player(self, server_id, user_id):
return self.get_server(server_id).get(user_id, None)
player = self.get_server(server_id).get(user_id, None)
if player is None:
if _G.AUTOCREATE:
player = self.add_player(server_id, user_id)
warnings.warn(
"Player does not exist, creating new player. If you would like to not see this message, please disable auto-creation, or add player and save"
)
else:
raise PlayerNotFound(f"Player {user_id} not found in server {server_id}")
return player

def get_server(self, server_id):
server = self.data.get(server_id, None)
Expand All @@ -23,7 +33,8 @@ def get_server(self, server_id):
return server

def add_server(self, server_id):
self.data[server_id] = {}
if server_id not in self.data:
self.data[server_id] = {}
return self.data[server_id]

def add_player(self, server_id, user_id):
Expand Down
54 changes: 52 additions & 2 deletions disc_bot/cogs/RankCog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
from discord.ext import commands
from ..cog_helpers.server_containers import ServerContainer
from ..config.globals import _G
from pprint import pprint

class Rank_Commands(commands.Cog):

def __init__(self, bot, **extras):
self.bot = bot
for key, value in extras.items():
setattr(self, key, value)
if not hasattr(self, "servers"):
self.servers = ServerContainer()


@commands.command()
async def pprint_servers(self, ctx):
pprint(self.servers)

@commands.command()
async def add_server(self, ctx):
self.servers.add_server(ctx.guild.id)

@commands.command()
async def add_player(self, ctx):
for member in ctx.message.mentions:
self.servers.add_player(ctx.guild.id, member.id)
pprint(self.servers)

@commands.command()
async def flush(self, ctx):
self.servers.remove_server(ctx.guild.id)
pprint(self.servers)

@commands.command()
async def get_player(self, ctx):
pprint(self.servers.get_player(ctx.guild.id, ctx.author.id))


@commands.command()
async def add_one(self, ctx):
self.servers.get_player(ctx.guild.id, ctx.author.id).score += 1
pprint(self.servers)


@commands.command()
async def remove_global(self, ctx):
_G.AUTOCREATE = False

@commands.command()
async def reward_player(self, ctx, score):
for member in ctx.message.mentions:
self.servers.get_player(ctx.guild.id, member.id).score += int(score)
pprint(self.servers)

@commands.command()
async def ranks(self, ctx):
await ctx.channel.send(ranks)
async def punish_player(self, ctx, score):
for member in ctx.message.mentions:
self.servers.get_player(ctx.guild.id, member.id).score -= int(score)
pprint(self.servers)
4 changes: 3 additions & 1 deletion disc_bot/cogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from pathlib import Path
from .TranslatorCog import Translation_Commands
from .BlankCog import BlankCog
from .RankCog import Rank_Commands
cogs_list = [
Translation_Commands,
BlankCog
BlankCog,
Rank_Commands
]
1 change: 1 addition & 0 deletions disc_bot/exceptions/ranks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class PlayerNotFound(Exception): pass

0 comments on commit d7d2015

Please sign in to comment.