Skip to content

Commit

Permalink
Edited commands, added a command to set building amounts.
Browse files Browse the repository at this point in the history
  • Loading branch information
plaaosert committed Aug 4, 2021
1 parent d3cd711 commit 31e0801
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 24 deletions.
23 changes: 23 additions & 0 deletions sooch/commands/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Module that contains admin commands.
An admin command is a command that is only accessible to admins and should never be granted to a normal player
(as they contain direct SQL evals, unlimited data manipulation, etc.)
"""
from typing import List, Optional

import discord

from sooch.commands.shared import services
from sooch.services.players_load import Players
from sooch.services.reg_buildings import RegBuildings


async def set_building(client: discord.Client,
message: discord.Message,
content: List[str]) -> Optional[discord.Embed]:
result = discord.Embed()

await services.reg_buildings_svc.set_building_count(message.author.id, int(content[1]), int(content[2]))

result.add_field(name="Set building", value="{} amount = {}".format(content[1], content[2]))
return result
26 changes: 15 additions & 11 deletions sooch/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,28 @@

from sooch import buildings
from sooch.player import Player
from sooch.services.players_load import Players
from sooch.services.reg_buildings import RegBuildings
from sooch.commands.shared import services
from sooch.services.claiming import claim_all

player_svc: Optional[Players] = None
reg_buildings_svc: Optional[RegBuildings] = None


async def setup_default_player(discord_id: int, discord_name: str) -> Player:
async def setup_default_player(client, discord_id: int, discord_name: str) -> Player:
"""Setup default player info in the database."""
# Not in database so setup default.
player = Player.from_new_player(discord_id, discord_name)
await player_svc.add_player(player)
player = Player.from_new_player(client, discord_id, discord_name)
await services.player_svc.add_player(player)
return player


async def claim(client: discord.Client,
message: discord.Message,
content: List[str]) -> Optional[discord.Embed]:
"""Handle the s!claim command."""
del client, content
player_id = message.author.id
player_name = message.author.name

player = await player_svc.get_player(player_id)
player = await services.player_svc.get_player(player_id)
if player is None:
player = await setup_default_player(player_id, player_name)
player = await setup_default_player(client, player_id, player_name)

claim_result = claim_all(player)

Expand All @@ -56,8 +51,17 @@ async def claim(client: discord.Client,
"event_currency"
)

player_debug_data = (
"base_income",
"income"
)

result.add_field(name="Result test", value="```\n" + "\n".join("{:20} {}".format(
d + ":", getattr(claim_result, d)
) for d in debug_data) + "\n```")

result.add_field(name="Player attributes", value="```\n" + "\n".join("{:20} {}".format(
d + ":", getattr(player, d)
) for d in player_debug_data) + "\n```", inline=False)

return result
10 changes: 10 additions & 0 deletions sooch/commands/shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Optional


class Services:
def __init__(self):
self.player_svc: Optional['Players'] = None
self.reg_buildings_svc: Optional['RegBuildings'] = None


services = Services()
9 changes: 8 additions & 1 deletion sooch/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Callable, Optional, List, Dict, Coroutine, Any

import discord
from sooch.commands import base, misc, help
from sooch.commands import base, misc, help, admin


@dataclass
Expand Down Expand Up @@ -49,6 +49,13 @@ class Command:
syntax="s!help <command>",
handler=help.help_command
),
Command(
name="s$setbuilding",
description="Set a building ID to amount given",
aliases=["s$sb"],
syntax="s$setbuilding <buildingID> <amount>",
handler=admin.set_building
)
]
invalid_command = Command(handler=misc.invalid)
help.populate_help_embeds(commands)
Expand Down
30 changes: 23 additions & 7 deletions sooch/player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Contain class and method that allows for manipulation of player data."""
import time
from typing import Tuple
from sooch.commands.shared import services
from sooch.buildings import reg_buildings, trans_buildings


class Player:
Expand All @@ -21,7 +23,9 @@ class Player:
# Need to have some sort of type hint for "Tuple[int, str, ...]" - but I don't know how to make that type hint,
# like, work.
# - plaao
def __init__(self, data: Tuple, initialise=False):
def __init__(self, bot, data: Tuple, initialise=False):
self.bot = bot

self.discord_id = data[0]
self.name = data[1]

Expand All @@ -38,6 +42,9 @@ def __init__(self, data: Tuple, initialise=False):
self.tsooch = 0
self.csooch = 0
self.last_claim = time.time()

self.base_income = 0
self.income = 0
else:
# Read from the provided data.
# We need to always read buildings and skills (because we need to calculate income)
Expand All @@ -49,27 +56,36 @@ def __init__(self, data: Tuple, initialise=False):
self.csooch = data[6]
self.last_claim = data[7]

self.base_income = 0
self.income = 0

@classmethod
def from_loaded_data(cls, data):
def from_loaded_data(cls, bot, data):
"""
Take in raw data loaded from the database, applies preprocessing as
necessary and passes it into the constructor.
"""
# -- preprocessing goes here --
return cls(data)
return cls(bot, data)

@classmethod
def from_new_player(cls, pid: int, name: str) -> "Player":
def from_new_player(cls, bot, pid: int, name: str) -> "Player":
"""Creates a new player with only ID and username from the bot."""
# Should not have to apply preprocessing since both are strings.
return cls((pid, name), True)
return cls(bot, (pid, name), True)

def calculate_all_stats(self):
async def calculate_all_stats(self):
"""Update all player's stat such as income and other bonuses."""
# Mostly concerned with temp buffs and bonuses from t/cprops.
# Nothing to do here... yet.
await self.get_base_sooch_income()

async def get_base_sooch_income(self):
# Get buildings from buildings service.
building_counts = await services.reg_buildings_svc.get_building_count(self.discord_id)
self.base_income = sum(reg_buildings[i].income * building_counts[i] for i in range(len(building_counts)))

def calculate_income(self):
async def calculate_income(self):
"""
Update player currency income values based on their income bonus. Call this after calculate_all_stats
and after doing any command that modifies player.
Expand Down
3 changes: 3 additions & 0 deletions sooch/services/claiming.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def determine_times(player: sooch.player.Player):


def claim_basic(result, player, hours, minutes):
# Need buildings.


pass


Expand Down
File renamed without changes.
8 changes: 6 additions & 2 deletions sooch/services/players_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ async def get_player(self, discord_id: int) -> Optional["Player"]:
if row is None:
return None

return Player(
(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
player_load = Player(
self, (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
)

await player_load.calculate_all_stats()

return player_load

async def add_player(self, player: "Player"):
"""Add the provided player into the database"""
cursor = self.database.connection.cursor()
Expand Down
6 changes: 3 additions & 3 deletions sooch/sooch_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import discord

from sooch import listeners, message, path
from sooch.commands import base
from sooch.commands.shared import services
from sooch.database import Database
from sooch.services.players_load import Players
from sooch.services.reg_buildings import RegBuildings
Expand Down Expand Up @@ -52,8 +52,8 @@ def __init__(self):

# Initialize all the other modules.
listeners.servers = Servers(self.database)
base.player_svc = Players(self.database)
base.reg_buildings_svc = RegBuildings(self.database)
services.player_svc = Players(self.database)
services.reg_buildings_svc = RegBuildings(self.database)

super().__init__()

Expand Down

0 comments on commit 31e0801

Please sign in to comment.