GameLogger

This commit is contained in:
2024-03-12 16:53:31 +00:00
parent e7b751c3f1
commit cdfaac773a
4 changed files with 213 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
import type { LogType } from "@/defs.js";
export interface IGeneralRecord {
_id: string;
generalID: string;
logType: LogType;
year: number;
month: number;
text: string;
}
+7
View File
@@ -0,0 +1,7 @@
export interface IGlobalRecord {
_id: string;
type: 'history' | 'action';
year: number;
month: number;
text: string;
}
+9
View File
@@ -0,0 +1,9 @@
import type { NationID } from "@/defs.js";
export interface INationRecord {
_id: string;
nationID: NationID;
year: number;
month: number;
text: string;
}
+182
View File
@@ -0,0 +1,182 @@
import type { IResourceController } from "./IResourceController.js";
import type { GeneralID, IntMonth, IntYear, LogType, NationID } from "./defs.js";
type LoggerStack = {
symbol: Symbol;
year: IntYear;
month: IntMonth;
global: {
action: string[];
history: string[];
}
nation: {
nationID: NationID;
history: string[];
}
general: {
[key in LogType]: string[];
} & {
generalID: GeneralID;
}
}
export class GameLoggerEngine {
private static instance: GameLoggerEngine;
private constructor(
private rsc: IResourceController
) {
}
public static initInstance(rsc: IResourceController) {
GameLoggerEngine.instance = new GameLoggerEngine(rsc);
}
public static getInstance(): GameLoggerEngine {
if (!GameLoggerEngine.instance) {
throw new Error("GameLogger is not initialized");
}
return GameLoggerEngine.instance;
}
private logStack = new Map<Symbol, LoggerStack>();
public addStack(stack: LoggerStack) {
this.logStack.set(stack.symbol, stack);
return stack;
}
public getGameLogger(year: IntYear, month: IntMonth): GameLogger {
return new GameLogger(year, month, this);
}
public getNationLogger(year: IntYear, month: IntMonth, nationID: NationID): NationLogger {
return new NationLogger(year, month, nationID, this);
}
public getGeneralLogger(year: IntYear, month: IntMonth, nationID: NationID, generalID: GeneralID): GeneralLogger {
return new GeneralLogger(year, month, nationID, generalID, this);
}
flushAll() {
//rsc?
throw new Error("Not yet implemented");
}
}
export class GameLogger {
protected stack: LoggerStack;
public constructor(
year: IntYear,
month: IntMonth,
protected readonly engine: GameLoggerEngine,
) {
this.stack = {
symbol: Symbol(),
year,
month,
global: {
action: [],
history: []
},
nation: {
nationID: 0,
history: []
},
general: {
action: [],
battle: [],
battleBrief: [],
history: [],
generalID: 0
}
};
}
pushGlobalActionLog(text: string) {
this.stack.global.action.push(text);
}
pushGlobalHistoryLog(text: string) {
this.stack.global.history.push(text);
}
reset() {
const { year, month } = this.stack;
const nationID = this.stack.nation.nationID;
const generalID = this.stack.general.generalID;
this.stack = {
symbol: Symbol(),
year,
month,
global: {
action: [],
history: []
},
nation: {
nationID,
history: []
},
general: {
action: [],
battle: [],
battleBrief: [],
history: [],
generalID
}
};
}
commit() {
this.engine.addStack(this.stack);
this.reset();
}
}
export class NationLogger extends GameLogger {
public constructor(
year: IntYear,
month: IntMonth,
private readonly nationID: NationID,
engine: GameLoggerEngine
) {
super(year, month, engine);
this.stack.nation.nationID = nationID;
}
pushNationLog(text: string) {
this.stack.nation.history.push(text);
}
}
export class GeneralLogger extends NationLogger {
public constructor(
year: IntYear,
month: IntMonth,
nationID: NationID,
private readonly generalID: GeneralID,
engine: GameLoggerEngine
) {
super(year, month, nationID, engine);
this.stack.general.generalID = generalID;
}
pushGeneralActionLog(text: string) {
this.stack.general.action.push(text);
}
pushGeneralBattleLog(text: string) {
this.stack.general.battle.push(text);
}
pushGeneralBattleBriefLog(text: string) {
this.stack.general.battleBrief.push(text);
}
pushGeneralHistoryLog(text: string) {
this.stack.general.history.push(text);
}
}