Skip to content

Commit

Permalink
changing to 3.10, changing serialization of objects to support adding…
Browse files Browse the repository at this point in the history
… more attributes more easily
  • Loading branch information
Samalmeida1028 committed Dec 9, 2022
1 parent a943ab5 commit 6c40389
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 50 deletions.
6 changes: 4 additions & 2 deletions .idea/Dnd_Shop_Manager.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions Objects/Item.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import json
from dataclasses import dataclass


@dataclass
class Item:

name: str
item_type: str
baseValue: str
rarity: float
baseRegion: str
def __init__(self, name, baseRegion, item_type="misc", baseValue="1", rarity="1", arg=[]):
if len(arg) == 0:
self.name = name
Expand All @@ -17,12 +22,8 @@ def __init__(self, name, baseRegion, item_type="misc", baseValue="1", rarity="1"
self.rarity = arg[3]
self.baseRegion = arg[4]

def __str__(self):
return '{"Name":"' + self.name + '","Type":"' + self.item_type + '","Value":"' + self.baseValue + '","Rarity":"'\
+ self.rarity + '","Base Region":"' + self.baseRegion + '"}'

def str(self):
return self.__str__()
def Serialize(self):
return vars(self)


class ItemManager:
Expand All @@ -45,11 +46,11 @@ def __init__(self): # inits by trying to load an items.txt json for the save da

def addItem(self, name, item_type, baseValue, rarity, baseRegion): # add item by property
temp = Item(name, item_type, baseValue, rarity, baseRegion)
self.itemList[name] = json.loads(temp.str())
self.itemList[name] = temp.Serialize()

def addItem(self, item_list): # add new item by list
temp = Item("", "", "", "", "", item_list)
self.itemList[item_list[0]] = json.loads(temp.str())
self.itemList[item_list[0]] = temp.Serialize()

def getItemRegions(self) -> list: # returns a list of all the regions in the item list
for key in self.itemList:
Expand All @@ -63,13 +64,13 @@ def getItemTypes(self) -> list: # gets all the item types in the item list
self.item_types.append(self.itemList[key]["Type"])
return self.item_types

def getItemIndex(self, index) -> Item: # gets the index of an item in the item list
def getItemIndex(self, index: int | str) -> Item: # gets the index of an item in the item list
item_name = list(self.itemList)[int(index)]
item = Item(item_name, self.itemList[item_name]["Base Region"], self.itemList[item_name]["Type"],
self.itemList[item_name]["Value"], self.itemList[item_name]["Rarity"])
return item

def getItemName(self, name) -> Item: # gets an item if given a name
def getItemName(self, name: str) -> Item: # gets an item if given a name
item = Item(name, self.itemList[name]["Base Region"], self.itemList[name]["Type"], self.itemList[name]["Value"],
self.itemList[name]["Rarity"])
return item
Expand Down
80 changes: 45 additions & 35 deletions Objects/Shop.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
import json
from typing import Iterable

from Objects.Item import *
import random
from dataclasses import dataclass


#
# Class: Shop
# Takes in the name, shop_type, city, region, wealth, gold amount, and sell multiplier of a shop
@dataclass
class Shop:
def __init__(self, name="", owner="", notes="", shop_type="", city="", region="", wealth="", items={}, gold_amount="",
sell_mult=""):
self.name = name
self.owner = owner
self.shop_type = shop_type
self.notes = notes
self.city = city
self.region = region
self.wealth = wealth
self.items = {}
if type(items) != str:
self.addItems(items)
self.gold_amount = gold_amount
self.sell_mult = sell_mult
name: str = ""
owner: str = ""
notes: str = ""
shop_type: str = ""
city: str = ""
region: str = ""
wealth: str = ""
gold_amount: str = ""
sell_mult: str = ""
items: dict = None

def __str__(self): # this puts the shop items in JSON format for saving
temp = json.dumps(self.items, indent=4)
return '{"Name":"' + self.name + '","Owner":"' + self.owner + '","Notes":"' + self.notes + '","Type":"' \
+ self.shop_type + '","City":"' + self.city + '","Region":"' + self.region + '","Wealth":"' + self.wealth \
+ '","Items":' + temp + ',"GoldAmount":"' + self.gold_amount + '","SellMult":"' + self.sell_mult + '"}'
+ self.shop_type + '","City":"' + self.city + '","Region":"' + self.region + '","Wealth":"' + self.wealth \
+ '","Items":' + temp + ',"GoldAmount":"' + self.gold_amount + '","SellMult":"' + self.sell_mult + '"}'

def dir(self):
return self.__dir__

def str(self):
return self.__str__()
def __dir__(self):
return

def Serialize(self):
return vars(self)

def addItems(self, items: dict): # adds a list of items, given an item list, to the shop
for key in items:
Expand All @@ -55,23 +61,11 @@ def updateItemPrice(self, item_name: str, cost: float): # updates the cost of a
# Class: ShopManager
# Manages the save data of the list of shops, and loads shops from save data
class ShopManager:
def __init__(self):
def __init__(self, filename='shops.txt'):
self.items = ItemManager()
temp = ""
self.shopList = {}
try:
with open("../Resources/save_data/shops.txt",
"x") as file: # sees if the file already exists, if it doesn't create the file
file.write(temp)
file.close()
except FileExistsError:
try:
with open( # if the file does exist then load the file contents to the shopList
"../Resources/save_data/shops.txt") as file:
self.shopList = json.load(file)
except json.decoder.JSONDecodeError as e: # if the file is in incorrect json, then don't load it
print(e)
self.shopList = {}
self.Deserialize(filename)

def addShop(self, name, owner, type, city, region, wealth, items, gold_amount,
sell_multiplier): # adds a shop by the input values
Expand All @@ -80,7 +74,7 @@ def addShop(self, name, owner, type, city, region, wealth, items, gold_amount,

def addShop(self, shop): # adds a shop if you made a shop already
name = shop.name
self.shopList[name] = json.loads(shop.str()) # loads the shop to the json string
self.shopList[name] = shop.Serialize() # loads the shop to the json string

def saveShops(self): # saves all the shops into a json file
open('../Resources/save_data/shops.txt', 'w').close()
Expand Down Expand Up @@ -138,7 +132,7 @@ def getShopByTypeinCity(self, city: str, shop_type) -> dict:
def getItemAmount(self, shop_name: str, item: str) -> int:
return int(self.shopList[shop_name]["Items"][item]["Amount"])

def removeItem(self, shop_name:str, item: str):
def removeItem(self, shop_name: str, item: str):
self.shopList[shop_name]["Items"].pop(item)

def addNewItem(self, shop_name: str, item_name: str):
Expand Down Expand Up @@ -179,7 +173,7 @@ def increaseShopGoldAmount(self, shop_name: str, gold):
def getShopByName(self, name: str) -> dict:
return self.shopList[name]

def printShopItems(self, shop: str ):
def printShopItems(self, shop: str):
printed_shop = self.shopList[shop]
for k in printed_shop["Items"]:
cost = float(self.shopList[shop]["Items"][k]["Cost"]) / 100
Expand All @@ -206,3 +200,19 @@ def printShop(self, shop_name: str):
print("||||||||||||||||||||||||||||||||||||||||||||||||")
print("||||||||||||||||||||ITEMS|||||||||||||||||||||||")
self.printShopItems(shop["Name"])

def Deserialize(self, filename):
temp = ""
try:
with open("../Resources/save_data/shops.txt",
"x") as file: # sees if the file already exists, if it doesn't create the file
file.write(temp)
file.close()
except FileExistsError:
try:
with open( # if the file does exist then load the file contents to the shopList
"../Resources/save_data/shops.txt") as file:
self.shopList = json.load(file)
except json.decoder.JSONDecodeError as e: # if the file is in incorrect json, then don't load it
print(e)
self.shopList = {}
Binary file modified Objects/__pycache__/Item.cpython-310.pyc
Binary file not shown.
Binary file modified Objects/__pycache__/Shop.cpython-310.pyc
Binary file not shown.
Binary file modified Objects/__pycache__/Shop.cpython-39.pyc
Binary file not shown.
Binary file modified Objects/__pycache__/TypeAndRegionManager.cpython-310.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion shop-generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from Objects.Shop import *
from Objects.TypeAndRegionManager import *
# ---GOLBALS---
# ---GLOBALS---
REGION_ITEM_VALUE = 1.2 # used to determine how an item's value fluctuates based on region
TYPE_ITEM_VALUE = 2 # used to determine item's price when sold if it is in the shop that sells it
ELITE_RARITY_MULT = 10 # this number dictates the rarity of item found in elite shops, higher means more rare items
Expand Down
12 changes: 12 additions & 0 deletions test/testShop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from Objects.Shop import *
items = ItemManager()

shop = Shop()

manager = ShopManager()

shop.items = {}
shop.addNewItem(items.getItemName('Potion of Healing'), 5, 5)
print(vars(shop))
manager.addShop(shop)
manager.saveShops()

0 comments on commit 6c40389

Please sign in to comment.