Skip to content

Commit

Permalink
Merge branch 'experimental'
Browse files Browse the repository at this point in the history
  • Loading branch information
Samalmeida1028 committed Dec 11, 2022
2 parents 44791e9 + c4e47f8 commit b9ab7c5
Show file tree
Hide file tree
Showing 15 changed files with 956 additions and 899 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.

107 changes: 49 additions & 58 deletions Objects/Item.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,72 @@
import json
from dataclasses import dataclass
from pathlib import Path


@dataclass
class Item:
name: str
item_type: str
base_value: str
rarity: float | str
base_region: str

def __init__(self, name, baseRegion, item_type="misc", baseValue="1", rarity="1", arg=[]):
if len(arg) == 0:
self.name = name
self.item_type = item_type
self.baseValue = baseValue
self.rarity = rarity
self.baseRegion = baseRegion
else:
self.name = arg[0]
self.item_type = arg[1]
self.baseValue = arg[2]
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:

def __init__(self): # inits by trying to load an items.txt json for the save data, else it creates a new one
temp = ""
def __init__(self, filename: str = "items.txt"): # inits by trying to load an items.txt json for the save data,
# else it creates a new one
self.item_types = []
self.itemRegions = []
self.itemList = {}
try:
with open("../Resources/save_data/items.txt", "x") as file:
file.write(temp)
file.close()
except FileExistsError:
try:
with open("../Resources/save_data/items.txt") as file: # loads the json file into a dictionary
self.itemList = json.load(file)
except json.decoder.JSONDecodeError:
self.itemList = {}

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())

def addItem(self, item_list): # add new item by list
temp = Item("", "", "", "", "", item_list)
self.itemList[item_list[0]] = json.loads(temp.str())
self.item_list = {}
self.file = "../Resources/save_data/" + filename
self.Deserialize()

def addItem(self, name, item_type, base_value, rarity, base_region): # add item by property
temp = Item(name, item_type, base_value, rarity, base_region)
self.item_list[name] = temp.Serialize()
def getItemRegions(self) -> list: # returns a list of all the regions in the item list
for key in self.itemList:
if self.itemList[key]["Base Region"] not in self.itemRegions:
self.itemRegions.append(self.itemList[key]["Base Region"])
for key in self.item_list:
if self.item_list[key]["base_region"] not in self.itemRegions:
self.itemRegions.append(self.item_list[key]["base_region"])
return self.itemRegions

def getItemTypes(self) -> list: # gets all the item types in the item list
for key in self.itemList:
if self.itemList[key]["Type"] not in self.item_types:
self.item_types.append(self.itemList[key]["Type"])
for key in self.item_list:
if self.item_list[key]["item_type"] not in self.item_types:
self.item_types.append(self.item_list[key]["item_type"])
return self.item_types

def getItemIndex(self, index) -> 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"])
def getItemIndex(self, index: int | str) -> Item: # gets the index of an item in the item list
item_name = list(self.item_list)[int(index)]
item = Item(item_name, self.item_list[item_name]["item_type"], self.item_list[item_name]["base_value"],
self.item_list[item_name]["rarity"], self.item_list[item_name]["base_region"])
return item

def getItemName(self, name) -> 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"])
def getItemName(self, name: str) -> Item: # gets an item if given a name
item = Item(name, self.item_list[name]["item_type"], self.item_list[name]["base_value"],
self.item_list[name]["rarity"], self.item_list[name]["base_region"])
return item

def saveItems(self): # saves the items to the txt file
open('../Resources/save_data/items.txt', 'w').close()
file = open("../Resources/save_data/items.txt", "r+")
json.dump(self.itemList, file, indent=4)
open(Path(self.file), 'w').close()
file = open(Path(self.file), "r+")
json.dump(self.item_list, file, indent=4)
file.close()

def Deserialize(self):
temp = ""
try:
with open(Path(self.file), "x") as file:
file.write(temp)
file.close()
except FileExistsError:
try:
with open(Path(self.file)) as file: # loads the json file into a dictionary
self.item_list = json.load(file)
except json.decoder.JSONDecodeError:
self.item_list = {}


Loading

0 comments on commit b9ab7c5

Please sign in to comment.