SyncTk/classes/frames/saves.py

92 lines
3.3 KiB
Python
Raw Normal View History

2023-01-25 17:23:18 +02:00
from os import path, walk
from tkinter import NSEW, ttk
import xmltodict
2023-01-22 18:53:55 +02:00
from ttkthemes import ThemedTk
2023-01-25 17:23:18 +02:00
from classes.custom.scrollable_frame import ScrollableFrame
2023-01-22 18:53:55 +02:00
from classes.custom.themed_frame import ThemedFrame
2023-01-25 17:23:18 +02:00
from classes.frames.save import FrameSave
from modules.utils import configGet
2023-01-22 18:53:55 +02:00
2023-01-25 17:23:18 +02:00
class FrameSaves(ScrollableFrame):
2023-01-22 18:53:55 +02:00
2023-01-25 17:23:18 +02:00
def __init__(self, master: ThemedTk, saves: list, **kwargs) -> None:
2023-01-22 18:53:55 +02:00
super().__init__(master, **kwargs)
master.title("Saves - Stardew Sync")
2023-01-25 17:23:18 +02:00
self.saves = saves
self.saves_local = []
for subdir, dirs, files in walk(configGet("saves_location")):
try:
for dir in dirs:
with open(path.join(dir, "SaveGameInfo"), "r", encoding="utf-8") as file:
save_dict = xmltodict.parse(file.read())
self.saves_local.append(
{
"id": int(save_dict["SaveGame"]["uniqueIDForThisGame"]),
"user": None,
"device": configGet("name"),
"date": None,
"data": {
"farmer": save_dict["Farmer"]["name"],
"money": int(save_dict["Farmer"]["money"]),
"played": int(save_dict["Farmer"]["millisecondsPlayed"]),
"save_time": int(save_dict["Farmer"]["saveTime"]),
"year": int(save_dict["Farmer"]["yearForSaveGame"]),
"season": int(save_dict["Farmer"]["seasonForSaveGame"]),
"day": int(save_dict["Farmer"]["dayOfMonthForSaveGame"]),
"game_version": save_dict["Farmer"]["gameVersion"]
},
"file": {
"name": None,
"uuid": None,
"path": None
}
}
)
except:
pass
# Merge local and remote saves.
# Maybe add something that indicates availability of an
# remote update to pull or local new version to push.
master.columnconfigure(1, weight=1)
self.render_saves()
def render_saves(self):
i = 0
for save in self.saves:
save_frame = FrameSave(self, save_dict=save)
save_frame.grid(column=0, row=i, pady=9, padx=9, sticky=NSEW)
i += 1
if i+1 != len(self.saves):
divider = ttk.Separator(self, orient="horizontal")
divider.grid(column=0, row=i+1, pady=9)
i += 1
class FrameSavesEmpty(ThemedFrame):
def __init__(self, master: ThemedTk, **kwargs) -> None:
super().__init__(master, **kwargs)
master.title("Saves - Stardew Sync")
2023-01-22 18:53:55 +02:00
2023-01-25 17:23:18 +02:00
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=2)
2023-01-22 18:53:55 +02:00
2023-01-25 17:23:18 +02:00
master.columnconfigure(1, weight=1)
2023-01-22 18:53:55 +02:00
2023-01-25 17:23:18 +02:00
self.label = ttk.Label(self, text="No saves found")
self.label.grid(column=0, row=0, padx=9, pady=9)