Skip to content

Commit

Permalink
Merge pull request #7 from DavidLlanio/Davr
Browse files Browse the repository at this point in the history
Davr
  • Loading branch information
DavidLlanio authored Jul 27, 2024
2 parents e76bdd6 + 292f29f commit 117bec5
Show file tree
Hide file tree
Showing 10 changed files with 600 additions and 118 deletions.
134 changes: 74 additions & 60 deletions cogs/character.py → cogs/character_manage.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import json
import os
from pathlib import Path

import discord
from discord import app_commands
from discord.ext import commands

from helpers.character import Character
from helpers.character_database import CharacterDatabase

characters = CharacterDatabase()
# Get the data folder path
repo_path = Path(__file__).parent.parent
data_fp = os.path.join(repo_path, "data")

# Initialize Database
character_db = CharacterDatabase(data_path=data_fp)


class Dropdown(discord.ui.Select):
Expand All @@ -23,54 +30,6 @@ async def callback(self, interaction: discord.Interaction):
await interaction.response.defer()


class SubmitButton(discord.ui.Button):
def __init__(self):
super().__init__(
label="Submit", style=discord.ButtonStyle.green, custom_id="submit"
)

async def callback(self, interaction: discord.Interaction):
sex = None
race = None
class_ = None

for child in self.view.children: # type: ignore
if isinstance(child, Dropdown):
if child.custom_id == "sex":
sex = child.values[0] if child.values else None
elif child.custom_id == "race":
race = child.values[0] if child.values else None
elif child.custom_id == "class":
class_ = child.values[0] if child.values else None

if sex and race and class_:
user_id = interaction.user.id

char_info = characters.get_character_info(user_id)
if char_info != -1:
await interaction.response.send_message(
"You already have a character created.", ephemeral=True
)
else:
characters.add_character(
{
"uid": user_id,
"info": {"sex": sex, "class": class_, "race": race},
"inventory": {},
}
)

await interaction.response.send_message(
"Congratulations! You have created a character!",
ephemeral=True,
delete_after=10,
)
else:
await interaction.response.send_message(
"Please select all options before submitting.", ephemeral=True
)


class DropdownView(discord.ui.View):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -156,7 +115,62 @@ def __init__(self):
self.add_item(SubmitButton())


class Characters(commands.Cog):
class SubmitButton(discord.ui.Button):
def __init__(self):
super().__init__(
label="Submit", style=discord.ButtonStyle.green, custom_id="submit"
)

async def callback(self, interaction: discord.Interaction):
sex = None
race = None
clss = None

for child in self.view.children: # type: ignore
if isinstance(child, Dropdown):
if child.custom_id == "sex":
sex = child.values[0] if child.values else None
elif child.custom_id == "race":
race = child.values[0] if child.values else None
elif child.custom_id == "class":
clss = child.values[0] if child.values else None

if sex and race and clss:
user_id = interaction.user.id

char_info = character_db.get_character_info(user_id)
if char_info != -1:
await interaction.response.send_message(
"You already have a character created.", ephemeral=True
)
else:
new_character = Character()
new_character.name = interaction.user.name
new_character.sex = sex
new_character.class_ = clss
new_character.race = race

res = character_db.add_character(user_id, new_character)

if res < 0:
await interaction.response.send_message(
"You already have a character!",
ephemeral=True,
delete_after=10,
)
else:
await interaction.response.send_message(
"Congratulations! You have created a character!",
ephemeral=True,
delete_after=10,
)
else:
await interaction.response.send_message(
"Please select all options before submitting.", ephemeral=True
)


class CharacterHandle(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot

Expand All @@ -167,7 +181,7 @@ def __init__(self, bot: commands.Bot) -> None:
async def create(self, interaction: discord.Interaction):
user_id = interaction.user.id

char_info = characters.get_character_info(user_id)
char_info = character_db.get_character_info(user_id)
if char_info != -1:
await interaction.response.send_message(
"You already have a character created.", ephemeral=True
Expand All @@ -187,13 +201,13 @@ async def create(self, interaction: discord.Interaction):
async def view(self, interaction: discord.Interaction):
user_id = interaction.user.id

character = characters.get_character_info(user_id)
character = character_db.get_character_info(user_id)

if character != -1:
sex, race, class_ = (
character["sex"],
character["race"],
character["class"],
character.sex,
character.race,
character.class_,
)
embed = discord.Embed(
title="Your Character",
Expand All @@ -210,13 +224,13 @@ async def view(self, interaction: discord.Interaction):

@app_commands.command(name="balance", description="Check your balance.")
async def balance(self, interaction: discord.Interaction):
with open("data/coins.json", "r") as f:
coins = json.load(f).get(str(interaction.user.id), 0)
user_id = interaction.user.id
character = character_db.get_character_info(user_id)

await interaction.response.send_message(
f"Your balance is `{coins}` coins."
f"Your balance is `{character.coins}` coins."
)


async def setup(bot: commands.Bot) -> None:
await bot.add_cog(Characters(bot))
await bot.add_cog(CharacterHandle(bot))
29 changes: 29 additions & 0 deletions data/armor_front.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Spiked Mail of
Dreadnought Armor of
Enchanted Plate of
Infernal Breastplate of
Blessed Chestguard of
Shadowed Vest of
Glorious Cuirass of
Mystic Tunic of
Ancient Body Armor of
Vengeful Hauberk of
Fiery Brigandine of
Radiant Battle Plate of
Savage Chestplate of
Haunted Mail of
Noble Surcoat of
Demonic Jerkin of
Celestial Armor of
Ferocious Scale Mail of
Arcane Bodyguard of
Malevolent Harness of
Gleaming Armor of
Divine Chestpiece of
Spectral Vestment of
Barbaric Breastplate of
Eternal Plate Mail of
Wicked Hauberk of
Primal War Armor of
Shimmering Bodyguard of
Infernal Cuirass of
29 changes: 29 additions & 0 deletions data/boots_front.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Windstriders of
Obsidian Greaves of
Shadowstep Boots of
Enchanted Footwear of
Ironclad Boots of
Blessed Sandals of
Infernal Stompers of
Swiftwalker Boots of
Cursed Sole of
Glorious High Boots of
Ethereal Slippers of
Demonic Footguards of
Fiery Booties of
Radiant Travelers of
Phantom Stalkers of
Noble Treads of
Rugged Hiking Boots of
Arcane Walkers of
Vengeful Kicks of
Mystic Sole of
Fierce War Boots of
Celestial Footpads of
Savage Stepper of
Dreaded Ankle Boots of
Eternal Wanderers of
Shimmering Loafers of
Wicked Greaves of
Primal Striders of
Frostforged Boots of
30 changes: 30 additions & 0 deletions data/helmet_front.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Cursed Visor of
Holy Guardian Helmet of
Enchanted Helm of
Infernal Mask of
Blessed Crown of
Shadowed Cap of
Glorious Protector Helmet of
Mystic Hood of
Ancient Headgear of
Vengeful Helm of
Fiery Visor of
Radiant Guardian Helmet of
Savage Helm of
Haunted Mask of
Noble Crown of
Demonic Cap of
Celestial Protector Helmet of
Ferocious Hood of
Arcane Headgear of
Malevolent Helm of
Gleaming Visor of
Divine Guardian Helmet of
Spectral Helm of
Barbaric Mask of
Eternal Crown of
Wicked Cap of
Primal Protector Helmet of
Shimmering Hood of
Infernal Headgear of
Hidden Veil of
28 changes: 28 additions & 0 deletions data/necklace_front.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Cursed Eye of
Holy Pendant of
Enchanted Amulet of
Infernal Necklace of
Blessed Charm of
Shadowed Locket of
Glorious Medallion of
Mystic Talisman of
Ancient Brooch of
Vengeful Ring of
Fiery Bracelet of
Radiant Band of
Savage Chain of
Haunted Trinket of
Noble Circlet of
Demonic Sigil of
Celestial Ornament of
Ferocious Token of
Arcane Relic of
Malevolent Jewel of
Gleaming Bauble of
Divine Emblem of
Spectral Crest of
Barbaric Pin of
Eternal Seal of
Wicked Gem of
Primal Adornment of
Shimmering Jewel of
56 changes: 56 additions & 0 deletions helpers/character.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from .generator import BufferItem
from .item_generator import Item


class Character:
"""Class for keeping track of character information"""

def __init__(self):
self.name: str = ""
self.sex: str = ""
self.race: str = ""
self.class_: str = ""
self.coins: int = 0
self.equipped_helmet: Item = None
self.equipped_armor: Item = None
self.equipped_boots: Item = None
self.equipped_necklace: Item = None
self.equipped_weapon: Item = None
self.inventory: list[Item] = []
self.buffer: list[BufferItem] = []
self.quest_log: list[str] = []
self.ranking_points: int = 0

def equip_best(self):
for item in self.inventory:
item_t = item.type_
if (
item_t == "Weapon"
and item.enumerate_rarity()
> self.equipped_weapon.enumerate_rarity()
):
self.equipped_weapon = item
elif (
item_t == "Helmet"
and item.enumerate_rarity()
> self.equipped_helmet.enumerate_rarity()
):
self.equipped_helmet = item
elif (
item_t == "Armor"
and item.enumerate_rarity()
> self.equipped_armor.enumerate_rarity()
):
self.equipped_armor = item
elif (
item_t == "Boots"
and item.enumerate_rarity()
> self.equipped_boots.enumerate_rarity()
):
self.equipped_boots = item
elif (
item_t == "Necklace"
and item.enumerate_rarity()
> self.equipped_necklace.enumerate_rarity()
):
self.equipped_necklace = item
Loading

0 comments on commit 117bec5

Please sign in to comment.