diff --git a/src/components/GameSaves.svelte b/src/components/GameSaves.svelte new file mode 100644 index 0000000000000000000000000000000000000000..0825777b5f5299127d48fcc755f4300bf0c6ba16 --- /dev/null +++ b/src/components/GameSaves.svelte @@ -0,0 +1,210 @@ + + +
+ + diff --git a/src/components/GameWindow.svelte b/src/components/GameWindow.svelte index f90ab1b6a85da5751dd1ca60e90f073f29f89597..97034acff65c1004b0f4a2a94201da0b2d640a09 100644 --- a/src/components/GameWindow.svelte +++ b/src/components/GameWindow.svelte @@ -3,21 +3,21 @@ import type { IGame, IGameSave } from "../types"; import { alert, post, promptLogin } from "../libgalaxy"; import Chat from "./Chat.svelte"; + import GameSaves from "./GameSaves.svelte"; export let game: IGame; export let userId: number | null; let loggedIn = userId !== null; + let showSaves = false; + let saves: GameSaves; + let chatEverOpened = false; let height = "75vh"; let width = "100%"; let frame: HTMLIFrameElement; let chatChannel = `game-${game.id}`; - let showSaves = false; - let savesLoaded = false; - let showCreateSave = false; - let loadedSaves: IGameSave[] = []; let lastInteraction = Date.now(); // ms * sc * mn * hrs const TWELVE_HOURS = 1000 * 60 * 60 * 12; @@ -43,7 +43,7 @@ function toggleSaves() { showSaves = !showSaves; - if (showSaves) refreshSaves(); + if (showSaves) saves.refreshSaves(); } function toggleChat() { @@ -335,110 +335,6 @@ Data: ${e.data}` }; const NO_ACCOUNT_LIST = { ...LIST_ERROR, message: "no_account" }; const SERVER_ERROR_LIST = { ...LIST_ERROR, message: "server_error" }; - - function toggleCreateSave() { - showCreateSave = !showCreateSave; - } - - async function refreshSaves(skipLoad = false) { - if (skipLoad) savesLoaded = false; - let { saves } = await post( - "/api/gapi/saves/list", - { - game: game.id, - }, - { errorOnFailure: false } - ); - if (saves) loadedSaves = saves; - savesLoaded = true; - } - - function copySave(slot: number) { - let save = loadedSaves.find(s => s.slot === slot); - try { - navigator.clipboard.writeText(save.data); - alert.success("Copied save!"); - } catch { - prompt( - "We could not copy your save automatically. You can find it in this box below:", - save.data - ); - } - } - - // Wrapper func for repetitive /saves/set usage - async function modifySave(slot: number, data: string, label: string) { - let res = await post("/api/gapi/saves/set", { - slot, - data, - label, - game: game.id, - }); - refreshSaves(true); - return res; - } - - let creatingSlot = ""; - let creatingData = ""; - let creatingLabel = ""; - async function createSave() { - let res = await modifySave( - parseInt(creatingSlot), - creatingData, - creatingLabel - ); - - if (res._resCode === 200) { - alert.success("Save created!"); - creatingSlot = ""; - creatingData = ""; - creatingLabel = ""; - showCreateSave = false; - } - } - - function validateSlot() { - setTimeout(() => { - if (creatingSlot === "") return; - let num = parseFloat(creatingSlot); - if (isNaN(num)) creatingSlot = ""; - else if (num < 0) creatingSlot = "0"; - else if (num > 10) creatingSlot = "10"; - else creatingSlot = num.toString(); - }, 0); - } - - // TODO merge these 2 functions? - async function editSave(slot: number) { - let newContent = prompt("Enter the new content:"); - if (newContent === null) return; - let res = await modifySave( - slot, - newContent, - loadedSaves.find(s => s.slot === slot).label - ); - if (res._resCode === 200) alert.success("Save edited!"); - } - - async function renameSave(slot: number) { - let newLabel = prompt("Enter the new content:"); - if (newLabel === null) return; - let res = await modifySave( - slot, - loadedSaves.find(s => s.slot === slot).data, - newLabel - ); - if (res._resCode === 200) alert.success("Save renamed!"); - } - - async function deleteSave(slot: number) { - let res = await post("/api/gapi/saves/delete", { - slot, - game: game.id, - }); - if (res._resCode === 200) alert.success("Save deleted!"); - refreshSaves(true); - }