get save workgin on load

This commit is contained in:
Georges-Antoine Assi
2024-01-19 18:09:51 -05:00
parent 951f7df375
commit 9fba1d1a64
7 changed files with 34 additions and 19 deletions

View File

@@ -1,3 +1,4 @@
from datetime import datetime
from typing import Optional
from pydantic import BaseModel
@@ -13,6 +14,8 @@ class BaseAsset(BaseModel):
file_size_bytes: int
full_path: str
download_path: str
created_at: datetime
updated_at: datetime
class Config:
from_attributes = True

View File

@@ -43,6 +43,7 @@ def add_saves(
scanned_save.rom_id = rom.id
db_save_handler.add_save(scanned_save)
rom = db_rom_handler.get_roms(rom_id)
return {"uploaded": len(saves), "saves": rom.saves}
@@ -73,6 +74,7 @@ async def update_save(request: Request, id: int) -> SaveSchema:
)
db_save_handler.update_save(db_save.id, {"file_size_bytes": file.size})
db_save = db_save_handler.get_save(id)
return db_save

View File

@@ -74,6 +74,7 @@ async def update_state(request: Request, id: int) -> StateSchema:
)
db_state_handler.update_state(db_state.id, {"file_size_bytes": file.size})
db_state = db_state_handler.get_state(id)
return db_state

View File

@@ -12,6 +12,8 @@ export type SaveSchema = {
file_size_bytes: number;
full_path: string;
download_path: string;
created_at: string;
updated_at: string;
rom_id: number;
emulator: (string | null);
};

View File

@@ -12,6 +12,8 @@ export type ScreenshotSchema = {
file_size_bytes: number;
full_path: string;
download_path: string;
created_at: string;
updated_at: string;
rom_id: number;
};

View File

@@ -12,6 +12,8 @@ export type StateSchema = {
file_size_bytes: number;
full_path: string;
download_path: string;
created_at: string;
updated_at: string;
rom_id: number;
emulator: (string | null);
};

View File

@@ -45,7 +45,6 @@ declare global {
}
}
const script = document.createElement("script");
script.src = "/assets/emulatorjs/loader.js";
script.async = true;
@@ -128,22 +127,25 @@ window.EJS_onSaveState = function ({
],
})
.then(({ data }) => {
if (rom.value) rom.value.states = data.states;
stateRef.value = data.states.pop() ?? null;
const allStates = data.states.sort((a: StateSchema, b: StateSchema) => a.id - b.id);
if (rom.value) rom.value.states = allStates;
stateRef.value = allStates.pop() ?? null;
});
}
};
async function getSave(): Promise<Uint8Array> {
if (saveRef.value) {
const { data } = await api.get(saveRef.value.download_path.replace("/api", ""));
const { data } = await api.get(
saveRef.value.download_path.replace("/api", "")
);
var enc = new TextEncoder();
return enc.encode(data);
} else {
const file = await window.EJS_emulator.selectFile();
return new Uint8Array(await file.arrayBuffer());
}
};
}
window.EJS_onLoadSave = async function () {
const sav = await getSave();
@@ -151,22 +153,17 @@ window.EJS_onLoadSave = async function () {
const path = window.EJS_emulator.gameManager.getSaveFilePath();
const paths = path.split("/");
let cp = "";
for (let i=0; i<paths.length-1; i++) {
if (paths[i] === "") continue;
cp += "/"+paths[i];
if (!FS.analyzePath(cp).exists) FS.mkdir(cp);
for (let i = 0; i < paths.length - 1; i++) {
if (paths[i] === "") continue;
cp += "/" + paths[i];
if (!FS.analyzePath(cp).exists) FS.mkdir(cp);
}
if (FS.analyzePath(path).exists) FS.unlink(path);
FS.writeFile(path, sav);
window.EJS_emulator.gameManager.loadSaveFiles();
};
window.EJS_onSaveSave = function ({
save,
}: {
screenshot: File;
save: File;
}) {
window.EJS_onSaveSave = function ({ save }: { screenshot: File; save: File }) {
if (saveRef.value) {
saveApi
.updateSave({
@@ -189,14 +186,20 @@ window.EJS_onSaveSave = function ({
],
})
.then(({ data }) => {
if (rom.value) rom.value.saves = data.saves;
saveRef.value = data.saves.pop() ?? null;
const allSaves = data.saves.sort((a: SaveSchema, b: SaveSchema) => a.id - b.id);
if (rom.value) rom.value.saves = allSaves;
saveRef.value = allSaves.pop() ?? null;
});
}
};
window.EJS_onGameStart = () => {
if (saveRef.value) window.EJS_onLoadSave();
window.EJS_onGameStart = async () => {
setTimeout(() => {
if (stateRef.value) {
window.EJS_onLoadSave();
window.EJS_emulator.gameManager.restart();
}
}, 1000);
gameRunning.value = true;
};