This repository has been archived on 2022-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
texty/lib/save.py

44 lines
1.7 KiB
Python
Raw Normal View History

2022-05-04 10:16:30 +02:00
from time import sleep
from os import path, system
2022-02-04 19:35:38 +01:00
import yaml
2022-05-04 10:16:30 +02:00
from lib.game import Item
2022-05-04 14:26:32 +02:00
class SaveManager: # Spravuje ukládání
2022-05-04 10:16:30 +02:00
def __init__(self,gid:str,lang):
2022-05-04 14:26:32 +02:00
self.id = gid # ID hry
self.currentPrompt = "" # Aktuální node
self.inventory = [] # Předměty v inventáři
2022-05-04 10:16:30 +02:00
self.version = 1
self.lang = lang
2022-02-04 19:35:38 +01:00
def load(self):
if(path.exists(f"./saves/{self.id}.yml")):
with open(f"./saves/{self.id}.yml",encoding="utf-8") as f:
2022-05-04 14:26:32 +02:00
data = yaml.load(f,Loader=yaml.SafeLoader) # Načteme z YAMLu
2022-02-04 19:35:38 +01:00
self.currentPrompt = data["currentPrompt"]
2022-05-04 14:26:32 +02:00
if(data["version"] < self.version): # V případě nekompatibility zobrazit varování
2022-05-04 10:16:30 +02:00
system("cls||clear")
print(self.lang["no_comp"])
sleep(5)
inv = []
2022-05-04 14:26:32 +02:00
for item in data["inventory"]: # Zpracovat inventář (zvlášť pouze text a zvlášť vybavitelné)
2022-05-04 10:16:30 +02:00
if type(item) is str:
inv.append(item)
else:
inv.append(Item(item["name"],item["atk"],item["def"]))
2022-02-04 19:35:38 +01:00
return True
return False
def save(self):
2022-05-04 10:16:30 +02:00
inv = []
for item in self.inventory:
if type(item) is str:
inv.append(item)
else:
2022-05-04 14:26:32 +02:00
# Pro vybavitelné předměty
2022-05-04 10:16:30 +02:00
inv.append({"name":item.name,"atk":item.attack,"def":item.defense})
data = {"id":self.id,"currentPrompt":self.currentPrompt,"inventory":self.inventory,"version":1}
2022-02-04 19:35:38 +01:00
with open(f"./saves/{self.id}.yml",mode="w",encoding="utf-8") as f:
2022-04-19 08:26:58 +02:00
yaml.dump(data,f)