1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Initial support for saving games

This commit is contained in:
tobspr
2020-05-14 21:54:11 +02:00
parent 23874c43dc
commit b01d38e55d
44 changed files with 690 additions and 777 deletions

View File

@@ -67,6 +67,10 @@ export class InGameState extends GameState {
this.savegame;
this.boundInputFilter = this.filterInput.bind(this);
if (G_IS_DEV) {
window.performSave = this.doSave.bind(this);
}
}
/**
@@ -96,7 +100,7 @@ export class InGameState extends GameState {
onBeforeExit() {
logger.log("Saving before quitting");
return this.doSave(true, true).then(() => {
return this.doSave().then(() => {
logger.log(this, "Successfully saved");
// this.stageDestroyed();
});
@@ -105,7 +109,7 @@ export class InGameState extends GameState {
onAppPause() {
if (this.stage === stages.s10_gameRunning) {
logger.log("Saving because app got paused");
this.doSave(true, true);
this.doSave();
}
}
@@ -397,14 +401,9 @@ export class InGameState extends GameState {
/**
* Saves the game
* @param {boolean=} syncWithServer
* @param {boolean} force
*/
doSave(syncWithServer = true, force = false) {
// TODO
return;
doSave() {
if (!this.savegame || !this.savegame.isSaveable()) {
return Promise.resolve();
}
@@ -424,19 +423,9 @@ export class InGameState extends GameState {
}
// First update the game data
logger.log("Starting to save game ...");
this.savegame.updateData(this.core.root);
let savePromise = this.savegame.writeSavegameAndMetadata();
if (syncWithServer) {
// Sync in parallel
// @ts-ignore
savePromise = savePromise.then(() => this.syncer.sync(this.core, this.savegame, force));
}
return savePromise.catch(err => {
return this.savegame.writeSavegameAndMetadata().catch(err => {
logger.warn("Failed to save:", err);
});
}

View File

@@ -1,6 +1,7 @@
import { GameState } from "../core/game_state";
import { cachebust } from "../core/cachebust";
import { globalConfig } from "../core/config";
import { makeDiv, formatSecondsToTimeAgo } from "../core/utils";
export class MainMenuState extends GameState {
constructor() {
@@ -69,6 +70,45 @@ export class MainMenuState extends GameState {
}
});
}
this.renderSavegames();
}
renderSavegames() {
const games = this.app.savegameMgr.getSavegamesMetaData();
if (games.length > 0) {
const parent = makeDiv(this.htmlElement.querySelector(".mainContainer"), null, ["savegames"]);
for (let i = 0; i < games.length; ++i) {
const elem = makeDiv(parent, null, ["savegame"]);
makeDiv(elem, null, ["internalId"], games[i].internalId.substr(0, 15));
makeDiv(
elem,
null,
["updateTime"],
formatSecondsToTimeAgo((new Date().getTime() - games[i].lastUpdate) / 1000.0)
);
const resumeBtn = document.createElement("button");
resumeBtn.classList.add("styledButton", "resumeGame");
elem.appendChild(resumeBtn);
this.trackClicks(resumeBtn, () => this.resumeGame(games[i]));
}
}
}
/**
* @param {object} game
*/
resumeGame(game) {
const savegame = this.app.savegameMgr.getSavegameById(game.internalId);
savegame.readAsync().then(() => {
this.moveToState("InGameState", {
savegame,
});
});
}
onPlayButtonClicked() {