SyncTk/classes/frames/saves.py

149 lines
5.7 KiB
Python
Raw Normal View History

2023-01-25 17:23:18 +02:00
from os import path, walk
2023-01-26 12:11:04 +02:00
from tkinter import E, NSEW, W, ttk
2023-01-26 17:27:32 +02:00
from traceback import print_exc
2023-01-25 17:23:18 +02:00
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-26 17:27:32 +02:00
from classes.enums import SaveState, SaveType
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 = []
2023-01-26 12:11:04 +02:00
# self["borderwidth"] = 1
# self["relief"] = "solid"
2023-01-26 17:27:32 +02:00
for index, entry in enumerate(self.saves):
self.saves[index]["state"] = SaveState.RECENT
self.saves[index]["type"] = SaveType.REMOTE
2023-01-25 17:23:18 +02:00
for subdir, dirs, files in walk(configGet("saves_location")):
2023-01-26 17:27:32 +02:00
2023-01-25 17:23:18 +02:00
try:
2023-01-26 17:27:32 +02:00
2023-01-25 17:23:18 +02:00
for dir in dirs:
2023-01-26 17:27:32 +02:00
print("Processing", dir)
with open(path.join(configGet("saves_location"), dir, "SaveGameInfo"), "r", encoding="utf-8") as file:
save_info_dict = xmltodict.parse(file.read())
with open(path.join(configGet("saves_location"), dir, dir), "r", encoding="utf-8") as file:
2023-01-25 17:23:18 +02:00
save_dict = xmltodict.parse(file.read())
2023-01-26 17:27:32 +02:00
save_date = None
save_type = SaveType.LOCAL
save_state = SaveState.RECENT
use_remote = False
for index, entry in enumerate(self.saves):
if entry["id"] == int(save_dict["SaveGame"]["uniqueIDForThisGame"]):
print(f'Remote: {entry["data"]["save_time"]}; Local: {int(save_info_dict["Farmer"]["saveTime"])}')
if int(save_info_dict["Farmer"]["saveTime"]) > entry["data"]["save_time"]:
self.saves[index]["type"] = SaveType.BOTH
save_type = SaveType.BOTH
save_state = SaveState.RECENT
save_date = entry["date"]
elif int(save_info_dict["Farmer"]["saveTime"]) < entry["data"]["save_time"]:
self.saves[index]["type"] = SaveType.BOTH
use_remote = True
else:
self.saves[index]["type"] = SaveType.BOTH
self.saves[index]["state"] = SaveState.CURRENT
use_remote = True
if use_remote is True:
continue
if save_state == SaveState.RECENT:
for index, entry in enumerate(self.saves):
if entry["id"] == int(save_dict["SaveGame"]["uniqueIDForThisGame"]):
del self.saves[index]
break
self.saves.append(
2023-01-25 17:23:18 +02:00
{
"id": int(save_dict["SaveGame"]["uniqueIDForThisGame"]),
"user": None,
"device": configGet("name"),
2023-01-26 17:27:32 +02:00
"date": save_date,
"type": save_type,
"state": SaveState.RECENT,
2023-01-25 17:23:18 +02:00
"data": {
2023-01-26 17:27:32 +02:00
"farmer": save_info_dict["Farmer"]["name"],
"money": int(save_info_dict["Farmer"]["money"]),
"played": int(save_info_dict["Farmer"]["millisecondsPlayed"]),
"save_time": int(save_info_dict["Farmer"]["saveTime"]),
"year": int(save_info_dict["Farmer"]["yearForSaveGame"]),
"season": int(save_info_dict["Farmer"]["seasonForSaveGame"]),
"day": int(save_info_dict["Farmer"]["dayOfMonthForSaveGame"]),
"game_version": save_info_dict["Farmer"]["gameVersion"]
2023-01-25 17:23:18 +02:00
},
"file": {
"name": None,
"uuid": None,
"path": None
}
}
2023-01-26 17:27:32 +02:00
)
2023-01-25 17:23:18 +02:00
except:
2023-01-26 17:27:32 +02:00
print_exc()
2023-01-25 17:23:18 +02:00
# 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:
2023-01-26 17:27:32 +02:00
print(save)
2023-01-25 17:23:18 +02:00
save_frame = FrameSave(self, save_dict=save)
2023-01-26 12:11:04 +02:00
save_frame.grid(column=0, row=i, pady=9, padx=9, sticky=W+E)
2023-01-25 17:23:18 +02:00
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)