Skip to content

Commit

Permalink
Fixed scaling stats carrying over between runs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Math-Man committed Aug 10, 2024
1 parent 6e07b73 commit 6e3e101
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
54 changes: 31 additions & 23 deletions src/data/saveFile/SaveDataHandler.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import { flog } from "../../helpers/DebugHelper";
import { mod } from "../../Mod";
import {flog} from "../../helpers/DebugHelper";
import {mod} from "../../Mod";
import {BladeScalingUpgradeType} from "../../items/samuraiBlade/scaling/BladeScalingUpgradeType";
import {CachePosition} from "../../items/samuraiBlade/scaling/CachePosition";


const saveDataObject = {
persistent: {},
run: {
loaded: false,
damageDealt: new Map<number, number>(), // Controller Index, damage dealt pairs.
},
room: {},
level: {},
persistent: {},
run: {
loaded: false,
damageDealt: new Map<number, number>(), // Controller Index, damage dealt pairs.
scalingStateCache: new Map<number, Map<BladeScalingUpgradeType, CachePosition>>()
},
room: {},
level: {},
};

export function getScalingStateCache(): Map<number, Map<BladeScalingUpgradeType, CachePosition>> {
return saveDataObject.run.scalingStateCache;
}

export function registerSaveData(): void {
if (!saveDataObject.run.loaded) {
saveDataObject.run.loaded = true;
mod.saveDataManager("bladeProgressionData", saveDataObject);
}
flog(`Save data registered: ${saveDataObject.run.damageDealt}`);
if (!saveDataObject.run.loaded) {
saveDataObject.run.loaded = true;
mod.saveDataManager("bladeProgressionData", saveDataObject);
}
flog(`Save data registered: ${saveDataObject.run.damageDealt}`);
}

export function getTotalDamageDealt(controllerIndex: number): number {
const value = saveDataObject.run.damageDealt.get(controllerIndex);
return value ? value : 0;
const value = saveDataObject.run.damageDealt.get(controllerIndex);
return value ? value : 0;
}

export function increaseDamageDealt(controllerIndex: number, amount: number): void {
const value = saveDataObject.run.damageDealt.get(controllerIndex);
if (value) {
saveDataObject.run.damageDealt.set(controllerIndex, value + amount);
} else {
saveDataObject.run.damageDealt.set(controllerIndex, amount);
}
flog(`DAMAGE DEALT: ${saveDataObject.run.damageDealt.get(controllerIndex)}, LOADED: ${saveDataObject.run.loaded}`, `[PERSISTENT BLADE DATA]`);
const value = saveDataObject.run.damageDealt.get(controllerIndex);
if (value) {
saveDataObject.run.damageDealt.set(controllerIndex, value + amount);
} else {
saveDataObject.run.damageDealt.set(controllerIndex, amount);
}
flog(`DAMAGE DEALT: ${saveDataObject.run.damageDealt.get(controllerIndex)}, LOADED: ${saveDataObject.run.loaded}`, `[PERSISTENT BLADE DATA]`);
}
22 changes: 5 additions & 17 deletions src/items/samuraiBlade/scaling/BladeScalingManager.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
import { getTotalDamageDealt } from "../../../data/saveFile/SaveDataHandler";
import {getScalingStateCache, getTotalDamageDealt} from "../../../data/saveFile/SaveDataHandler";
import { BladeScalingMap, DUMMY_UPGRADE } from "./BladeScalingMap";
import { BladeScalingUpgrade } from "./BladeScalingUpgrade";
import { BladeScalingUpgradeType } from "./BladeScalingUpgradeType";

// O P T I M I Z E D
const scalingStateCache = new Map<number, Map<BladeScalingUpgradeType, CachePosition>>();

class CachePosition {
public index: number;
public value: number;

constructor(index: number, value: number) {
this.index = index;
this.value = value;
}
}
import {CachePosition} from "./CachePosition";

function getFromCache(controllerIndex: number, upgradeType: BladeScalingUpgradeType): CachePosition | undefined {
let playerMap = scalingStateCache.get(controllerIndex);
let playerMap = getScalingStateCache().get(controllerIndex);
if (playerMap) {
return playerMap.get(upgradeType);
}
return undefined;
}

function setCache(controllerIndex: number, upgradeType: BladeScalingUpgradeType, currentIndex: number, currentValue: number): CachePosition {
let playerMap = scalingStateCache.get(controllerIndex);
let playerMap = getScalingStateCache().get(controllerIndex);
if (!playerMap) {
playerMap = new Map<BladeScalingUpgradeType, CachePosition>();
scalingStateCache.set(controllerIndex, playerMap);
getScalingStateCache().set(controllerIndex, playerMap);
}
const cachePosition = new CachePosition(currentIndex, currentValue);
playerMap.set(upgradeType, cachePosition);
Expand Down
9 changes: 9 additions & 0 deletions src/items/samuraiBlade/scaling/CachePosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class CachePosition {
public index: number;
public value: number;

constructor(index: number, value: number) {
this.index = index;
this.value = value;
}
}

0 comments on commit 6e3e101

Please sign in to comment.