From 8e8c71224a35c79d7413ffd73b434687af50e1fd Mon Sep 17 00:00:00 2001
From: thepaperpilot <thepaperpilot@gmail.com>
Date: Wed, 7 Dec 2022 23:00:23 -0600
Subject: [PATCH] Fix total resources increasing on load

---
 src/features/resources/resource.ts | 9 +++++++++
 src/game/gameLoop.ts               | 3 +++
 src/game/persistence.ts            | 1 -
 src/util/save.ts                   | 4 ++++
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/features/resources/resource.ts b/src/features/resources/resource.ts
index 2b47cde..be42e7e 100644
--- a/src/features/resources/resource.ts
+++ b/src/features/resources/resource.ts
@@ -1,9 +1,12 @@
 import { globalBus } from "game/events";
 import { NonPersistent, Persistent, State } from "game/persistence";
 import { persistent } from "game/persistence";
+import player from "game/player";
+import settings from "game/settings";
 import type { DecimalSource } from "util/bignum";
 import Decimal, { format, formatWhole } from "util/bignum";
 import type { ProcessedComputable } from "util/computed";
+import { loadingSave } from "util/save";
 import type { ComputedRef, Ref } from "vue";
 import { computed, isRef, ref, unref, watch } from "vue";
 
@@ -51,6 +54,9 @@ export function createResource<T extends State>(
 export function trackBest(resource: Resource): Ref<DecimalSource> {
     const best = persistent(resource.value);
     watch(resource, amount => {
+        if (loadingSave.value) {
+            return;
+        }
         if (Decimal.gt(amount, best.value)) {
             best.value = amount;
         }
@@ -61,6 +67,9 @@ export function trackBest(resource: Resource): Ref<DecimalSource> {
 export function trackTotal(resource: Resource): Ref<DecimalSource> {
     const total = persistent(resource.value);
     watch(resource, (amount, prevAmount) => {
+        if (loadingSave.value) {
+            return;
+        }
         if (Decimal.gt(amount, prevAmount)) {
             total.value = Decimal.add(total.value, Decimal.sub(amount, prevAmount));
         }
diff --git a/src/game/gameLoop.ts b/src/game/gameLoop.ts
index dc3ca87..b001dd6 100644
--- a/src/game/gameLoop.ts
+++ b/src/game/gameLoop.ts
@@ -2,6 +2,7 @@ import projInfo from "data/projInfo.json";
 import { globalBus } from "game/events";
 import settings from "game/settings";
 import Decimal from "util/bignum";
+import { loadingSave } from "util/save";
 import type { Ref } from "vue";
 import { watch } from "vue";
 import player from "./player";
@@ -39,6 +40,8 @@ function update() {
         return;
     }
 
+    loadingSave.value = false;
+
     // Add offline time if any
     if (player.offlineTime != undefined) {
         if (Decimal.gt(player.offlineTime, projInfo.offlineLimit * 3600)) {
diff --git a/src/game/persistence.ts b/src/game/persistence.ts
index ace98eb..03f5dad 100644
--- a/src/game/persistence.ts
+++ b/src/game/persistence.ts
@@ -1,4 +1,3 @@
-import { identifier } from "@babel/types";
 import { isArray } from "@vue/shared";
 import { globalBus } from "game/events";
 import type { GenericLayer } from "game/layers";
diff --git a/src/util/save.ts b/src/util/save.ts
index 005e4e4..4d89c12 100644
--- a/src/util/save.ts
+++ b/src/util/save.ts
@@ -5,6 +5,7 @@ import player, { stringifySave } from "game/player";
 import settings, { loadSettings } from "game/settings";
 import LZString from "lz-string";
 import { ProxyState } from "util/proxies";
+import { ref } from "vue";
 
 export function setupInitialStore(player: Partial<PlayerData> = {}): Player {
     return Object.assign(
@@ -88,8 +89,11 @@ export function getUniqueID(): string {
     return id;
 }
 
+export const loadingSave = ref(false);
+
 export async function loadSave(playerObj: Partial<PlayerData>): Promise<void> {
     console.info("Loading save", playerObj);
+    loadingSave.value = true;
     const { layers, removeLayer, addLayer } = await import("game/layers");
     const { fixOldSave, getInitialLayers } = await import("data/projEntry");