Skip to content

Commit

Permalink
Repaired PyCharm inspections
Browse files Browse the repository at this point in the history
Replaced type hints with typing module hints. Also fixed some imports and clarified some type hints.
  • Loading branch information
plaaosert committed Jul 27, 2021
1 parent 3e21d40 commit ecdbdea
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 42 deletions.
6 changes: 3 additions & 3 deletions resources/embeds/credits.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Main Man

===

Special Helpy Boyes
Special Helpy People
**@Good RumbleFish#1650** - was one of the first people to gain sooch and the founder of s!build coremine max.

**@xavrr#9975** - also a first, and was very helpful with testing max and min amounts of Sooch. Had the ideas for numerous commands and features.
Expand All @@ -12,7 +12,7 @@ Special Helpy Boyes

===

Special Helpy Boyes part 2: the second one
Special Helpy People part 2: the second one
**@ziul123#4197** - s!eat max.

**@CharlieSad#0400** - basically forced me to redesign the user creation system from the ground up. thanks >:]
Expand All @@ -25,7 +25,7 @@ Special Helpy Boyes part 2: the second one

===

Special Helpy Boyes: The Reboot 20 Years Afterwards
Special Helpy People: The Reboot 20 Years Afterwards
**@Boggy#7863** - made some really good and nice gif images for all the commands. really good guy. he makes games.

===
Expand Down
6 changes: 4 additions & 2 deletions sooch/buildings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
from BaseBuilding.
"""
import math
from typing import Optional
from typing import Optional, List, Dict


class BaseBuilding:
"""Contain methods applicable to every building in the game"""
base_cost_amp = 0 # 0.0x increase is equivalent to "no increase"

def __init__(self, name, cost, cost_amp=None):
# BaseBuilding objects don't have IDs because they are abstract.
self.id = -1
self.name = name
self.cost = int(cost)
# Some buildings may have steeper cost scaling than others of its type
Expand Down Expand Up @@ -115,7 +117,7 @@ def __init__(self,
self.build_limit = build_limit


def populate_lookup(original: list[BaseBuilding]) -> dict[str, BaseBuilding]:
def populate_lookup(original: List[BaseBuilding]) -> Dict[str, BaseBuilding]:
"""
Set up the lookup table for a list of buildings.
Keys in the lookup table include:
Expand Down
4 changes: 2 additions & 2 deletions sooch/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
import math
import time
from typing import Optional
from typing import Optional, List

import discord

Expand All @@ -29,7 +29,7 @@ async def setup_default_player(discord_id: int, discord_name: str) -> Player:

async def claim(client: discord.Client,
message: discord.Message,
content: list[str]) -> Optional[discord.Embed]:
content: List[str]) -> Optional[discord.Embed]:
"""Handle the s!claim command."""
del client, content
player_id = message.author.id
Expand Down
8 changes: 4 additions & 4 deletions sooch/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Module that contains command that provide information on how to progress to
the player.
"""
from typing import Optional
from typing import Optional, List, Dict

import discord

# Preload all help embeds
help_embeds: dict[str, discord.Embed] = {}
help_embeds: Dict[str, discord.Embed] = {}


def populate_help_embeds(commands: list[any]):
def populate_help_embeds(commands: List[any]):
"""Populate the help embed cache with the provided list of commands."""
default_text = ""

Expand Down Expand Up @@ -41,7 +41,7 @@ def populate_help_embeds(commands: list[any]):

async def help_command(client: discord.Client,
message: discord.Message,
content: list[str]) -> Optional[discord.Embed]:
content: List[str]) -> Optional[discord.Embed]:
"""Handle the s!help command."""
del client, message

Expand Down
8 changes: 4 additions & 4 deletions sooch/commands/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module that contains miscellaneous command irrelevant to the progression of
the player.
"""
from typing import Optional
from typing import Optional, List

import discord

Expand All @@ -11,7 +11,7 @@

async def invalid(client: discord.Client,
message: discord.Message,
content: list[str]) -> Optional[discord.Embed]:
content: List[str]) -> Optional[discord.Embed]:
"""Handle all invalid command with Sooch prefixes."""
del client, message
command = content[0]
Expand Down Expand Up @@ -40,8 +40,8 @@ async def invalid(client: discord.Client,


async def credits_command(client: discord.Client,
message: discord.Message,
content: list[str]) -> Optional[discord.Embed]:
message: discord.Message,
content: List[str]) -> Optional[discord.Embed]:
"""Handle the s!credit command."""
del client, message, content
return credits_embed
2 changes: 1 addition & 1 deletion sooch/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ def get_reg_building_query() -> str:
"""Generate the layout of the regular building query."""
return ("CREATE TABLE IF NOT EXISTS `reg_buildings`("
+ " `discord_id` BIGINT PRIMARY KEY,"
+ ", ".join([f"`b{id}` INT NOT NULL DEFAULT 0" for id in range(1, 51+1)])
+ ", ".join([f"`b{building_id}` INT NOT NULL DEFAULT 0" for building_id in range(1, 51+1)])
+ ");")
10 changes: 5 additions & 5 deletions sooch/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Handles incoming messages and dispatches them to the appropriate handler.
"""
from dataclasses import dataclass, field
from typing import Callable, Optional
from typing import Callable, Optional, List, Dict, Coroutine

import discord
from sooch.commands import base, misc, help
Expand All @@ -11,12 +11,12 @@
@dataclass
class Command:
"""Class representing a command that can be executed."""
handler: Callable[[discord.Client, str, list[str]],
Optional[discord.Embed]]
handler: Callable[[discord.Client, discord.Message, List[str]],
Coroutine[Optional[discord.Embed]]]
name: str = "s!invalid"
description: str = "No description provided"
syntax: Optional[str] = None
aliases: list[str] = field(default_factory=list[str])
aliases: List[str] = field(default_factory=List[str])


valid_prefix = {
Expand Down Expand Up @@ -53,7 +53,7 @@ class Command:
invalid_command = Command(handler=misc.invalid)
help.populate_help_embeds(commands)

command_lookup: dict[str, Command] = {}
command_lookup: Dict[str, Command] = {}
for cmd in commands:
command_lookup[cmd.name] = cmd
for alias in cmd.aliases:
Expand Down
25 changes: 19 additions & 6 deletions sooch/player.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Contain class and method that allows for manipulation of player data."""
import time
from typing import Tuple


Expand All @@ -17,8 +18,11 @@ class Player:
updated income amounts and updating the leaderboard.
"""

def __init__(self, data: Tuple[int, str, ...], initialise=False):
self.pid = data[0]
# 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):
self.discord_id = data[0]
self.name = data[1]

# Further initialise all variables here as they are needed
Expand All @@ -28,13 +32,22 @@ def __init__(self, data: Tuple[int, str, ...], initialise=False):
# be initialised using constants.
if initialise:
# Initialise the data.
pass
self.sooch_skin = ""
self.embed_color = None
self.sooch = 10
self.tsooch = 0
self.csooch = 0
self.last_claim = time.time()
else:
# Read from the provided data.

# We need to always read buildings and skills (because we need to calculate income)
# Probably just use a left join in the original query when we get round to that.
pass
self.sooch_skin = data[2]
self.embed_color = data[3]
self.sooch = data[4]
self.tsooch = data[5]
self.csooch = data[6]
self.last_claim = data[7]

@classmethod
def from_loaded_data(cls, data):
Expand All @@ -56,7 +69,7 @@ def calculate_all_stats(self):
# Mostly concerned with temp buffs and bonuses from t/cprops.
# Nothing to do here... yet.

def calclate_income(self):
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
7 changes: 4 additions & 3 deletions sooch/services/claiming.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import time
from dataclasses import dataclass
import sooch.player


@dataclass
Expand All @@ -23,7 +24,7 @@ class ClaimResult:
event_currency = None


def determine_times(player):
def determine_times(player: sooch.player.Player):
# Get base difference (in seconds) from the last stored claim time. Multiply by time acceleration.
base_diff = time.time() - player.last_claim_time
diff = base_diff * player.time_mult
Expand Down Expand Up @@ -88,7 +89,7 @@ def claim_items(result, player, hours_real, minutes_real):
pass


def claim_all(player):
def claim_all(player: sooch.player.Player):
hours, hours_real, minutes, minutes_real = determine_times(player)

result = ClaimResult()
Expand All @@ -111,7 +112,7 @@ def claim_all(player):
# ???????????????????

# Claiming can increase XP which can give a bonus to income, so we need to recalculate income here.
player.calclate_income()
player.calculate_income()

# Return everything.
return result
13 changes: 6 additions & 7 deletions sooch/services/players_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def get_player(self, discord_id: int) -> Optional["Player"]:
cursor = self.database.connection.cursor()
cursor.execute(
("SELECT"
"`sooch_skin`, `embed_color`,"
"`discord_id`, `name`,`sooch_skin`, `embed_color`,"
"`sooch`, `tsooch`, `csooch`,"
"`last_claim`"
"FROM `player` WHERE `discord_id` = ?"),
Expand All @@ -29,26 +29,25 @@ async def get_player(self, discord_id: int) -> Optional["Player"]:
return None

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

async def add_player(self, player: "Player"):
"""Add the provided player into the database"""
cursor = self.database.connection.cursor()
cursor.execute(
("INSERT INTO `player`"
"(`discord_id`, `sooch_skin`, `embed_color`,"
"(`name`, `discord_id`, `sooch_skin`, `embed_color`,"
"`sooch`, `tsooch`, `csooch`, `last_claim`)"
"VALUES(?, ?, ?, ?, ?, ?, ?)"),
(player.discord_id, player.sooch_skin, player.embed_color,
(player.name, player.discord_id, player.sooch_skin, player.embed_color,
player.sooch, player.tsooch, player.csooch, player.last_claim)
)
self.database.connection.commit()

# We can't really use these. Doing multiple commits like this is very expensive.
# We can't really use these. Doing multiple calls to the DB like this is very expensive.
# I would prefer functions inside Player to mutate attribute values and a final save function here that commits
# everything in a single bulk commit.
# everything in a single bulk save.
# -plaao

#async def set_sooch_skin(self, discord_id: int, sooch_skin: str):
Expand Down
6 changes: 3 additions & 3 deletions sooch/services/reg_buildings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Contain models for the amount of regular buildings players own."""
from typing import List, Tuple


set_query: list[str] = []
set_query: List[str] = []
for set_building_id in range(1, 51+1):
set_query.append(
f"INSERT INTO `reg_buildings`(`discord_id`, `b{set_building_id}`) "
Expand All @@ -16,7 +16,7 @@ class RegBuildings:
def __init__(self, database):
self.database = database

async def get_building_count(self, discord_id: int) -> tuple[int, ...]:
async def get_building_count(self, discord_id: int) -> Tuple[int, ...]:
"""Return the amount of buildings the player with the ID has."""
cursor = self.database.connection.cursor()
cursor.execute(
Expand Down
3 changes: 2 additions & 1 deletion sooch/services/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
server boosts.
"""
from dataclasses import dataclass
from typing import Optional


class Servers:
Expand All @@ -11,7 +12,7 @@ class Servers:
def __init__(self, database):
self.database = database

async def get_server(self, discord_id: int) -> "Server":
async def get_server(self, discord_id: int) -> Optional["Server"]:
"""Return the server info associated with the ID requested"""
cursor = self.database.connection.cursor()
cursor.execute(
Expand Down
7 changes: 6 additions & 1 deletion sooch/utilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
A class to store all utilities which may be reused several times within the code.
"""
from typing import Tuple

from sooch import buildings


Expand All @@ -13,13 +15,16 @@ def format_balance(bal: float) -> str:
:param bal: the decimal itself.
:return: the formatted decimal as a string.
"""

# This should support arbitrary usage of different function code -
# like how sooch original had 12 different formatting methods
if bal > 1.00e+10:
return '{:.2e}'.format(bal)
else:
return '{:,}'.format(bal)


def determine_income(count: tuple[int]) -> float:
def determine_income(count: Tuple[int]) -> float:
"""
Used to determine a user's income from their building counts.
:param count: The tuple of building counts.
Expand Down

0 comments on commit ecdbdea

Please sign in to comment.