Skip to content

Commit

Permalink
performance + QOL update
Browse files Browse the repository at this point in the history
- made generation much faster
- added ability in code to designate file to write to (currently just in the "save_data" folder
- fixed var names to make them easier to understand
- fixed issue where types and region lists were stored as strings
  • Loading branch information
Samalmeida1028 committed Dec 11, 2022
1 parent 0238916 commit 456c696
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 3,680 deletions.
20 changes: 4 additions & 16 deletions Objects/Item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
from dataclasses import dataclass
from pathlib import Path


# TODO: Fix serialization of Item and Shops, change all item and shop keys to match within the file,
# TODO cont.. update keys on main terminal, refactor terminal into different programs to support adding new programs
# TODO: add chest generation, add support for custom files to load shops and items, make it easier to update items
# TODO: Make shop regions and biome easier to interpret, as well as shop types and item types
# TODO: Refactor code for ease of use and remove dependencies on hard coded values to support tweaking of parameters
# TODO: Load those parameters into another json editable file so users can tweak program values without tweaking code
@dataclass
class Item:
name: str
Expand All @@ -34,11 +27,6 @@ def __init__(self, filename: str = "items.txt"): # inits by trying to load an i
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 addItem(self, item_list): # add new item by list
temp = Item("", "", "", "", "", item_list)
self.item_list[item_list[0]] = temp.Serialize()

def getItemRegions(self) -> list: # returns a list of all the regions in the item list
for key in self.item_list:
if self.item_list[key]["base_region"] not in self.itemRegions:
Expand All @@ -53,13 +41,13 @@ def getItemTypes(self) -> list: # gets all the item types 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.item_list)[int(index)]
item = Item(item_name, self.item_list[item_name]["base_region"], self.item_list[item_name]["item_type"],
self.item_list[item_name]["base_value"], self.item_list[item_name]["rarity"])
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: str) -> Item: # gets an item if given a name
item = Item(name, self.item_list[name]["base_region"], self.item_list[name]["item_type"],
self.item_list[name]["base_value"], self.item_list[name]["rarity"])
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
Expand Down
8 changes: 3 additions & 5 deletions Objects/Shop.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ def getShopRegions(self) -> list: # returns all the regions that shops have
return regions

def getShopTypes(self) -> dict: # returns all the different types of shops
types = {}
temp = []
types = []
for key in self.shop_list:
if self.shop_list[key]["shop_type"] not in temp:
temp.append(self.shop_list[key]["shop_type"])
types.update({'Shop Types': temp})
if self.shop_list[key]["shop_type"] not in types:
types.append(self.shop_list[key]["shop_type"])
return types

def getShopByRegion(self, region: str) -> dict:
Expand Down
112 changes: 59 additions & 53 deletions Objects/TypeAndRegionManager.py
Original file line number Diff line number Diff line change
@@ -1,114 +1,120 @@
from Objects.Item import *
from Objects.Shop import ShopManager
from Objects.Item import ItemManager


class TypeManager:

def __init__(self, shop, item):
def __init__(self, shop: ShopManager, item: ItemManager, filename="types.txt"):

self.itemTypes = item.getItemTypes() # gets all the item Types
self.shopTypes = shop.getShopTypes() # gets all the shop Types
self.typeManager = {} # this is to manage the save file for the supported item Types for each shop
self.item_types = item.getItemTypes() # gets all the item Types
self.shop_types = shop.getShopTypes() # gets all the shop Types
self.filename = filename
self.type_list = {}
try:
with open("../Resources/save_data/types.txt", "x") as file:
with open("../Resources/save_data/"+self.filename, "x") as file:
file.write("")
file.close()
self.typeList = {}
except FileExistsError:
try:
with open("../Resources/save_data/types.txt") as file:
self.typeList = json.load(file)
except json.decoder.JSONDecodeError:
self.typeList = {}
for key in self.typeList:
self.typeManager.update({key: self.typeList[key]}) # updates all the keys with the types in the save file
with open("../Resources/save_data/"+self.filename) as file:
self.type_list = json.load(file)
except json.decoder.JSONDecodeError as e:
self.type_list = {}

for key in self.type_list:
self.type_list.update({key: self.type_list[key]})
# updates all the keys with the types in the save file
for key in self.shop_types:
if key not in self.type_list:
self.type_list.update({key: []}) # updates all the keys with the types in the save file

def addItemType(self, key, value): # adds an item shop_type to a given shop shop_type
temp = []
if self.typeManager[key] != '':
temp += self.typeManager[key]
if self.type_list[key]:
temp += self.type_list[key]
if temp.count(value) == 0:
temp += [value]
if len(temp) != 0:
self.typeManager.update({key: temp})
self.type_list.update({key: temp})

def addNewShopType(self, key):
if key not in self.typeManager:
self.typeManager.update({key: ""})
if key not in self.type_list:
self.type_list.update({key: []})

def removeItemType(self, key, value): # removes an item shop_type from a given shop shop_type
temp = []
if self.typeManager[key] != '':
temp += self.typeManager[key]
if self.type_list[key]:
temp += self.type_list[key]
if temp.count(value) != 0:
temp.pop(value)
if len(temp) != 0:
self.typeManager.update({key: temp})
self.type_list.update({key: temp})

def addNewShopItemType(self, key, value): # adds a new shop shop_type and a new item shop_type to the shop shop_type
if key not in self.typeManager:
if key not in self.type_list:
print(key)
self.typeManager.update({key: ""})
self.type_list.update({key: []})
self.addItemType(key, value)

def saveTypes(self): # saves the types for each shop to the save text file
file = open("../Resources/save_data/types.txt", "r+")
json.dump(self.typeManager, file, indent=4)
file = open("../Resources/save_data/"+self.filename, "r+")
json.dump(self.type_list, file, indent=2)
file.close()


class RegionManager:
def __init__(self, shop, item):
self.itemRegions = item.getItemRegions() # gets all the item regions
self.shopRegions = shop.getShopRegions() # gets all the shop regions
self.regionManager = {} # this is to manage the save file for the supported item regions for each shop
def __init__(self, shop: ShopManager, item: ItemManager,filename: str = "regions.txt"):
self.item_regions = item.getItemRegions() # gets all the item regions
self.shop_regions = shop.getShopRegions() # gets all the shop regions
self.filename = filename
self.region_list = {}
try:
with open("../Resources/save_data/regions.txt", "x") as file:
with open("../Resources/save_data/" + self.filename, "x") as file:
file.write("")
file.close()
self.regionList = {}
except FileExistsError:
try:
with open("../Resources/save_data/regions.txt") as file:
self.regionList = json.load(file)
except json.decoder.JSONDecodeError:
self.regionList = {}
for key in self.regionList:
self.regionManager.update({key: self.regionList[key]})
with open("../Resources/save_data/" + self.filename) as file:
self.region_list = json.load(file)
except json.decoder.JSONDecodeError as e:
self.region_list = {}

for key in self.region_list:
self.region_list.update({key: self.region_list[key]})
# updates all the keys with the regions in the save file
count = 0
for key in self.shopRegions:
if key not in self.regionManager:
self.regionManager.update({key: '[]'}) # updates all the keys with the regions in the save file
count += 1
for key in self.shop_regions:
if key not in self.region_list:
self.region_list.update({key: []}) # updates all the keys with the regions in the save file

def addItemRegion(self, key, value): # adds an item region to a given shop region
temp = []
if self.regionManager[key] != '':
temp += self.regionManager[key]
if self.region_list[key]:
temp += self.region_list[key]
if temp.count(value) == 0:
temp += [value]
if len(temp) != 0:
self.regionManager.update({key: temp})
self.region_list.update({key: temp})

def addNewShopRegion(self, key):
if key not in self.regionManager:
self.regionManager.update({key: "[]"})
if key not in self.region_list:
self.region_list.update({key: []})

def removeItemRegion(self, key, value): # removes an item region from a given shop region
temp = []
if self.regionManager[key] != '':
temp += self.regionManager[key]
if self.region_list[key]:
temp += self.region_list[key]
if temp.count(value) != 0:
temp.pop(value)
if len(temp) != 0:
self.regionManager.update({key: temp})
self.region_list.update({key: temp})

def addNewShopItemRegion(self, key, value): # adds a new shop region and a new item region to the shop region
if key not in self.regionManager:
self.regionManager.update({key: ""})
if key not in self.region_list:
self.region_list.update({key: []})
self.addItemRegion(key, value)

def saveRegions(self): # saves the regions for each shop to the save text file
file = open("../Resources/save_data/regions.txt", "r+")
json.dump(self.regionManager, file, indent=4)
file = open("../Resources/save_data/" + self.filename, "r+")
json.dump(self.region_list, file, indent=2)
file.close()
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__/TypeAndRegionManager.cpython-310.pyc
Binary file not shown.
40 changes: 30 additions & 10 deletions Resources/save_data/regions.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
{
"East Ozoa": "['Mountains','Forest','Developed Area']",
"Unknown Lands": "[]",
"Magius": "['Magic Lands']",
"South East Ozoa": "['Plains','Mines]",
"West Ozoa": "['Plains']",
"Fechura": "['Jungle','Plains']",
"Polar": "[]",
"Oksandarr": "['Mines','Magic Lands']",
"Crestus": "['Mines','Plains']",
"Pitt": "[]"
"East Ozoa": [
"Mountains",
"Forest",
"Developed Area"
],
"Unknown Lands": [],
"Magius": [
"Magic Lands"
],
"South East Ozoa": [
"Plains",
"Mines"
],
"West Ozoa": [
"Plains"
],
"Fechura": [
"Jungle",
"Plains"
],
"Polar": [],
"Oksandarr": [
"Mines",
"Magic Lands"
],
"Crestus": [
"Mines",
"Plains"
],
"Pitt": []
}
Loading

0 comments on commit 456c696

Please sign in to comment.