From 4c07c6d5cbbf8cb8f4acf0b4e5f363a274bf8e8c Mon Sep 17 00:00:00 2001
From: Flashfyre <flashfireex@gmail.com>
Date: Thu, 11 Jan 2024 20:27:50 -0500
Subject: [PATCH] Changes for how play time is handled

---
 src/battle-phases.ts            |  5 ++---
 src/battle-scene.ts             | 32 +++++++++++++++++++++++---------
 src/system/game-data.ts         |  4 ++++
 src/ui/game-stats-ui-handler.ts | 14 +-------------
 src/utils.ts                    | 11 +++++++++++
 5 files changed, 41 insertions(+), 25 deletions(-)

diff --git a/src/battle-phases.ts b/src/battle-phases.ts
index 646fddd7b04..51019759dca 100644
--- a/src/battle-phases.ts
+++ b/src/battle-phases.ts
@@ -363,9 +363,8 @@ export class EncounterPhase extends BattlePhase {
   start() {
     super.start();
 
-    this.scene.updateWaveCountText();
-    this.scene.updateMoneyText();
-
+    this.scene.initSession();
+    
     const loadEnemyAssets = [];
 
     const battle = this.scene.currentBattle;
diff --git a/src/battle-scene.ts b/src/battle-scene.ts
index 5e1d656213c..c27f89bb393 100644
--- a/src/battle-scene.ts
+++ b/src/battle-scene.ts
@@ -84,6 +84,7 @@ export default class BattleScene extends Phaser.Scene {
 	public rexUI: UIPlugin;
 
 	public auto: boolean;
+	public sessionPlayTime: integer = null;
 	public masterVolume: number = 0.5;
 	public bgmVolume: number = 1;
 	public seVolume: number = 1;
@@ -141,6 +142,7 @@ export default class BattleScene extends Phaser.Scene {
 	private bgm: AnySound;
 	private bgmResumeTimer: Phaser.Time.TimerEvent;
 	private bgmCache: Set<string> = new Set();
+	private playTimeTimer: Phaser.Time.TimerEvent;
 	
 	private buttonKeys: Phaser.Input.Keyboard.Key[][];
 
@@ -402,15 +404,6 @@ export default class BattleScene extends Phaser.Scene {
 
 		this.gameData = new GameData(this);
 
-		this.time.addEvent({
-			delay: Utils.fixedInt(1000),
-			repeat: -1,
-    	callback: () => {
-				if (this.gameData)
-					this.gameData.gameStats.playTime++;
-			}
-		})
-
 		this.setupControls();
 
 		this.load.setBaseURL();
@@ -578,6 +571,27 @@ export default class BattleScene extends Phaser.Scene {
 		});
 	}
 
+	initSession(): void {
+		this.sessionPlayTime = 0;
+
+		if (this.playTimeTimer)
+			this.playTimeTimer.destroy();
+
+		this.playTimeTimer = this.time.addEvent({
+			delay: Utils.fixedInt(1000),
+			repeat: -1,
+    	callback: () => {
+				if (this.gameData)
+					this.gameData.gameStats.playTime++;
+				if (this.sessionPlayTime !== null)
+					this.sessionPlayTime++;
+			}
+		});
+
+		this.updateWaveCountText();
+		this.updateMoneyText();
+	}
+
 	setupControls() {
 		const keyCodes = Phaser.Input.Keyboard.KeyCodes;
 		const keyConfig = {
diff --git a/src/system/game-data.ts b/src/system/game-data.ts
index f12c1d57e42..cacbcd23f2e 100644
--- a/src/system/game-data.ts
+++ b/src/system/game-data.ts
@@ -58,6 +58,7 @@ interface SystemSaveData {
 
 interface SessionSaveData {
   seed: string;
+  playTime: integer;
   gameMode: GameMode;
   party: PokemonData[];
   enemyParty: PokemonData[];
@@ -360,6 +361,7 @@ export class GameData {
 
         const sessionData = {
           seed: scene.seed,
+          playTime: scene.sessionPlayTime,
           gameMode: scene.gameMode,
           party: scene.getParty().map(p => new PokemonData(p)),
           enemyParty: scene.getEnemyParty().map(p => new PokemonData(p)),
@@ -408,6 +410,8 @@ export class GameData {
           scene.seed = sessionData.seed || scene.game.config.seed[0];
           scene.resetSeed();
 
+          scene.sessionPlayTime = sessionData.playTime || 0;
+
           scene.gameMode = sessionData.gameMode || GameMode.CLASSIC;
 
           const loadPokemonAssets: Promise<void>[] = [];
diff --git a/src/ui/game-stats-ui-handler.ts b/src/ui/game-stats-ui-handler.ts
index 81a70bf2160..3f233c8c958 100644
--- a/src/ui/game-stats-ui-handler.ts
+++ b/src/ui/game-stats-ui-handler.ts
@@ -1,5 +1,4 @@
 import BattleScene, { Button } from "../battle-scene";
-import { GameStats } from "../system/game-stats";
 import { TextStyle, addTextObject, getTextColor } from "./text";
 import { Mode } from "./ui";
 import UiHandler from "./ui-handler";
@@ -18,20 +17,9 @@ interface DisplayStats {
   [key: string]: DisplayStat | string
 }
 
-const secondsInHour = 3600;
-
 const displayStats: DisplayStats = {
   playTime: {
-    sourceFunc: gameData => {
-      const totalSeconds = gameData.gameStats.playTime;
-
-      const days = `${Math.floor(totalSeconds / (secondsInHour * 24))}`;
-      const hours = `${Math.floor(totalSeconds % (secondsInHour * 24) / secondsInHour)}`;
-      const minutes = `${Math.floor(totalSeconds % secondsInHour / 60)}`;
-      const seconds = `${Math.floor(totalSeconds % 60)}`;
-    
-      return `${days.padStart(2, '0')}:${hours.padStart(2, '0')}:${minutes.padStart(2, '0')}:${seconds.padStart(2, '0')}`;
-    }
+    sourceFunc: gameData => Utils.getPlayTimeString(gameData.gameStats.playTime)
   },
   battles: 'Total Battles',
   startersUnlocked: {
diff --git a/src/utils.ts b/src/utils.ts
index c4612798cb4..495d5d207ac 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -88,6 +88,17 @@ export function getCurrentTime(): number {
   return (((date.getHours() * 60 + date.getMinutes()) / 1440) + 0.675) % 1;
 }
 
+const secondsInHour = 3600;
+
+export function getPlayTimeString(totalSeconds: integer): string {
+  const days = `${Math.floor(totalSeconds / (secondsInHour * 24))}`;
+  const hours = `${Math.floor(totalSeconds % (secondsInHour * 24) / secondsInHour)}`;
+  const minutes = `${Math.floor(totalSeconds % secondsInHour / 60)}`;
+  const seconds = `${Math.floor(totalSeconds % 60)}`;
+
+  return `${days.padStart(2, '0')}:${hours.padStart(2, '0')}:${minutes.padStart(2, '0')}:${seconds.padStart(2, '0')}`;
+}
+
 export function binToDec(input: string): integer {
   let place: integer[] = []; 
   let binary: string[] = [];