refac: rsc -> rc

This commit is contained in:
2024-03-15 16:13:27 +00:00
parent 6a6bdba08b
commit 78bcc95e2e
10 changed files with 46 additions and 46 deletions
+6 -6
View File
@@ -6,20 +6,20 @@ import { NotYetImplemented } from "@sammo/util";
export type QueueType = 'server' | 'api'; export type QueueType = 'server' | 'api';
//type QueueType = 'server' | 'npc' | 'api'; //type QueueType = 'server' | 'npc' | 'api';
export function setDefenceTrained(invoker: QueueType, arg: { generalID: GeneralID, defenceTrained: number }, rsc: IResourceController): void{ export function setDefenceTrained(invoker: QueueType, arg: { generalID: GeneralID, defenceTrained: number }, rc: IResourceController): void{
throw new NotYetImplemented(); throw new NotYetImplemented();
} }
export function kickSquadMember(invoker: QueueType, arg: { nationID: NationID, squadID: SquadID, memberID: GeneralID }, rsc: IResourceController): void { export function kickSquadMember(invoker: QueueType, arg: { nationID: NationID, squadID: SquadID, memberID: GeneralID }, rc: IResourceController): void {
throw new NotYetImplemented(); throw new NotYetImplemented();
} }
export function blockAttackCommand(invoker: QueueType, arg: { nationID: NationID, block: boolean }, rsc: IResourceController): void { export function blockAttackCommand(invoker: QueueType, arg: { nationID: NationID, block: boolean }, rc: IResourceController): void {
throw new NotYetImplemented(); throw new NotYetImplemented();
} }
export function blockJoinNation(invoker: QueueType, arg: { nationID: NationID, block: boolean }, rsc: IResourceController): void { export function blockJoinNation(invoker: QueueType, arg: { nationID: NationID, block: boolean }, rc: IResourceController): void {
throw new NotYetImplemented(); throw new NotYetImplemented();
} }
@@ -40,11 +40,11 @@ export function createGeneral(invoker: QueueType, arg: {
bonusStat?: [number, number, number], bonusStat?: [number, number, number],
//penalty: Record<GeneralPenalty, number>, //penalty: Record<GeneralPenalty, number>,
}, rsc: IResourceController):void{ }, rc: IResourceController):void{
throw new NotYetImplemented(); throw new NotYetImplemented();
} }
type ActionFunc<Arg extends Record<string, any>, Res> = (invoker: QueueType, arg: Arg, rsc: IResourceController)=>Res; type ActionFunc<Arg extends Record<string, any>, Res> = (invoker: QueueType, arg: Arg, rc: IResourceController)=>Res;
export type ActionPackDef = { export type ActionPackDef = {
[key: string]: ActionFunc<any, any> [key: string]: ActionFunc<any, any>
} }
+3 -3
View File
@@ -15,7 +15,7 @@ export class City extends LazyEntityUpdater<ICityEntity>{
constructor( constructor(
raw: ICityEntity, raw: ICityEntity,
protected readonly rsc: IResourceController, protected readonly rc: IResourceController,
bootstrapInfo: { bootstrapInfo: {
general: GeneralID[]; general: GeneralID[];
} }
@@ -27,7 +27,7 @@ export class City extends LazyEntityUpdater<ICityEntity>{
private _bootstrapInfo?: ConstructorParameters<typeof City>[2]; private _bootstrapInfo?: ConstructorParameters<typeof City>[2];
public bootstrap(){ public bootstrap(){
const rsc = this.rsc; const rc = this.rc;
const bootstrapInfo = must(this._bootstrapInfo); const bootstrapInfo = must(this._bootstrapInfo);
delete this._bootstrapInfo; delete this._bootstrapInfo;
@@ -35,7 +35,7 @@ export class City extends LazyEntityUpdater<ICityEntity>{
const nationID = this._raw.nationID; const nationID = this._raw.nationID;
for(const id of bootstrapInfo.general){ for(const id of bootstrapInfo.general){
const general = must(rsc.general(id)); const general = must(rc.general(id));
this.notifyJoinGeneral(general); this.notifyJoinGeneral(general);
} }
} }
+1 -1
View File
@@ -11,7 +11,7 @@ export class GameEnv extends LazyEntityUpdater<IGameEnvEntity>{
constructor( constructor(
raw: IGameEnvEntity, raw: IGameEnvEntity,
protected readonly rsc: IResourceController protected readonly rc: IResourceController
){ ){
super(); super();
this._raw = raw; this._raw = raw;
+4 -4
View File
@@ -25,15 +25,15 @@ export class GameLoggerEngine {
private static instance: GameLoggerEngine; private static instance: GameLoggerEngine;
private constructor( private constructor(
private rsc: IResourceController private rc: IResourceController
) { ) {
} }
public static initInstance(rsc: IResourceController): GameLoggerEngine{ public static initInstance(rc: IResourceController): GameLoggerEngine{
if(GameLoggerEngine.instance){ if(GameLoggerEngine.instance){
throw new Error("GameLogger is already initialized"); throw new Error("GameLogger is already initialized");
} }
GameLoggerEngine.instance = new GameLoggerEngine(rsc); GameLoggerEngine.instance = new GameLoggerEngine(rc);
return GameLoggerEngine.instance; return GameLoggerEngine.instance;
} }
@@ -68,7 +68,7 @@ export class GameLoggerEngine {
if(this.logStack.size === 0){ if(this.logStack.size === 0){
return; return;
} }
//rsc? //rc?
throw new Error("Not yet implemented"); throw new Error("Not yet implemented");
this.logStack.clear(); this.logStack.clear();
} }
+7 -7
View File
@@ -25,20 +25,20 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
constructor( constructor(
raw: IGeneralEntity, raw: IGeneralEntity,
protected readonly rsc: IResourceController protected readonly rc: IResourceController
){ ){
super(); super();
this._raw = raw; this._raw = raw;
} }
public bootstrap(){ public bootstrap(){
const rsc = this.rsc; const rc = this.rc;
const raw = this._raw; const raw = this._raw;
this._city = new WeakRef(must(rsc.city(raw.cityID))); this._city = new WeakRef(must(rc.city(raw.cityID)));
this._nation = new WeakRef(must(rsc.nation(raw.nationID))); this._nation = new WeakRef(must(rc.nation(raw.nationID)));
if(raw.squadID){ if(raw.squadID){
this._squad = new WeakRef(must(rsc.squad(raw.squadID, raw.nationID))); this._squad = new WeakRef(must(rc.squad(raw.squadID, raw.nationID)));
} }
this.reconfigureIActionHandlers(); this.reconfigureIActionHandlers();
@@ -141,7 +141,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
else{ else{
raw.officerLevel = OfficerLevel.normal; raw.officerLevel = OfficerLevel.normal;
} }
raw.nationJoinedYearMonth = this.rsc.gameEnv.raw.yearMonth; raw.nationJoinedYearMonth = this.rc.gameEnv.raw.yearMonth;
}); });
nation.notifyJoinGeneral(this); nation.notifyJoinGeneral(this);
@@ -153,7 +153,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
} }
public updateTurnTime(){ public updateTurnTime(){
const secondsPerTurn = this.rsc.gameEnv.raw.secondsPerTurn; const secondsPerTurn = this.rc.gameEnv.raw.secondsPerTurn;
this.forceUpdate((raw) => { this.forceUpdate((raw) => {
raw.turnTime = addSeconds(raw.turnTime, secondsPerTurn); raw.turnTime = addSeconds(raw.turnTime, secondsPerTurn);
+2 -2
View File
@@ -60,8 +60,8 @@ export interface IResourceController {
let _resourceController: IResourceController | undefined = undefined; let _resourceController: IResourceController | undefined = undefined;
export const setResourceController = (rsc: IResourceController) => { export const setResourceController = (rc: IResourceController) => {
_resourceController = rsc; _resourceController = rc;
} }
export const getResourceController = () => { export const getResourceController = () => {
+3 -3
View File
@@ -7,7 +7,7 @@ export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
protected abstract readonly _raw: Entity; protected abstract readonly _raw: Entity;
protected abstract readonly rsc: IResourceController; protected abstract readonly rc: IResourceController;
public abstract bootstrap(): void; public abstract bootstrap(): void;
@@ -21,7 +21,7 @@ export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
protected forceUpdate(callback: (raw: Entity) => void) { protected forceUpdate(callback: (raw: Entity) => void) {
callback(this._raw); callback(this._raw);
this.rsc.reserveUpdate(this); this.rc.reserveUpdate(this);
} }
public update(callback: (raw: Entity) => void) { public update(callback: (raw: Entity) => void) {
@@ -34,6 +34,6 @@ export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
throw new Error(`${String(indirectName)} change by update() is not allowed`); throw new Error(`${String(indirectName)} change by update() is not allowed`);
} }
} }
this.rsc.reserveUpdate(this); this.rc.reserveUpdate(this);
} }
} }
+5 -5
View File
@@ -41,7 +41,7 @@ export class Nation extends LazyEntityUpdater<INationEntity> {
constructor( constructor(
raw: INationEntity, raw: INationEntity,
protected readonly rsc: IResourceController, protected readonly rc: IResourceController,
bootstrapInfo: { bootstrapInfo: {
general: GeneralID[]; general: GeneralID[];
squad: SquadID[]; squad: SquadID[];
@@ -55,23 +55,23 @@ export class Nation extends LazyEntityUpdater<INationEntity> {
private _bootstrapInfo?: ConstructorParameters<typeof Nation>[2]; private _bootstrapInfo?: ConstructorParameters<typeof Nation>[2];
bootstrap() { bootstrap() {
const rsc = this.rsc; const rc = this.rc;
const bootstrapInfo = must(this._bootstrapInfo); const bootstrapInfo = must(this._bootstrapInfo);
delete this._bootstrapInfo; delete this._bootstrapInfo;
const bootstrapCityID = bootstrapInfo.city; const bootstrapCityID = bootstrapInfo.city;
for (const city of bootstrapCityID.map((id) => must(rsc.cityList.get(id)))) { for (const city of bootstrapCityID.map((id) => must(rc.cityList.get(id)))) {
this.notifyJoinCity(city); this.notifyJoinCity(city);
} }
const bootstrapSquadID = bootstrapInfo.squad; const bootstrapSquadID = bootstrapInfo.squad;
for(const squad of bootstrapSquadID.map((id) => must(rsc.squadList.get(id)))){ for(const squad of bootstrapSquadID.map((id) => must(rc.squadList.get(id)))){
this.notifyAddSquad(squad); this.notifyAddSquad(squad);
} }
const bootstrapGeneralID = bootstrapInfo.general; const bootstrapGeneralID = bootstrapInfo.general;
for (const general of bootstrapGeneralID.map((id) => must(rsc.generalList.get(id)))) { for (const general of bootstrapGeneralID.map((id) => must(rc.generalList.get(id)))) {
this.notifyJoinGeneral(general); this.notifyJoinGeneral(general);
} }
} }
+4 -4
View File
@@ -14,7 +14,7 @@ export class Squad extends LazyEntityUpdater<ISquadEntity> {
constructor( constructor(
raw: ISquadEntity, raw: ISquadEntity,
protected rsc: IResourceController, protected rc: IResourceController,
bootstrapInfo: { bootstrapInfo: {
general: GeneralID[] general: GeneralID[]
} }
@@ -26,13 +26,13 @@ export class Squad extends LazyEntityUpdater<ISquadEntity> {
private _bootstrapInfo?: ConstructorParameters<typeof Squad>[2]; private _bootstrapInfo?: ConstructorParameters<typeof Squad>[2];
public bootstrap() { public bootstrap() {
const rsc = this.rsc; const rc = this.rc;
const bootstrapInfo = must(this._bootstrapInfo); const bootstrapInfo = must(this._bootstrapInfo);
delete this._bootstrapInfo; delete this._bootstrapInfo;
this._members = new Map(bootstrapInfo.general.map((id) => { this._members = new Map(bootstrapInfo.general.map((id) => {
return [id, new WeakRef(must(rsc.general(id)))]; return [id, new WeakRef(must(rc.general(id)))];
})); }));
const leaderID = this._raw.id as GeneralID; const leaderID = this._raw.id as GeneralID;
@@ -64,7 +64,7 @@ export class Squad extends LazyEntityUpdater<ISquadEntity> {
this.leader.nation.notifyDestroySquad(this); this.leader.nation.notifyDestroySquad(this);
this.rsc.reserveDelete(this); this.rc.reserveDelete(this);
} }
public changeName(name: string) { public changeName(name: string) {
+11 -11
View File
@@ -44,15 +44,15 @@ export class GameEngine {
api: new Denque<ActionRequestContainer>(), api: new Denque<ActionRequestContainer>(),
} as const satisfies Record<QueueType, Denque<ActionRequestContainer>>; } as const satisfies Record<QueueType, Denque<ActionRequestContainer>>;
private rsc: ResourceController private rc: ResourceController
private loggerEngine: GameLoggerEngine; private loggerEngine: GameLoggerEngine;
private constructor( private constructor(
db: Mongoose, db: Mongoose,
private postStatus: (msg: GameEngineMsg) => void, private postStatus: (msg: GameEngineMsg) => void,
) { ) {
this.rsc = new ResourceController(db); this.rc = new ResourceController(db);
this.loggerEngine = GameLoggerEngine.initInstance(this.rsc); this.loggerEngine = GameLoggerEngine.initInstance(this.rc);
} }
private continueRun = true; private continueRun = true;
@@ -67,7 +67,7 @@ export class GameEngine {
while (true) { while (true) {
//장수턴 부터 처리 //장수턴 부터 처리
const upcomingGeneral = this.rsc.popGeneralTurnTimeQueue(); const upcomingGeneral = this.rc.popGeneralTurnTimeQueue();
if (!upcomingGeneral) { if (!upcomingGeneral) {
break; break;
} }
@@ -80,8 +80,8 @@ export class GameEngine {
//TODO: 턴 수행해야지! //TODO: 턴 수행해야지!
upcomingGeneral.updateTurnTime(); upcomingGeneral.updateTurnTime();
this.rsc.insertGeneralTurnTimeQueue(upcomingGeneral, true); this.rc.insertGeneralTurnTimeQueue(upcomingGeneral, true);
this.rsc.gameEnv.update((env) => { this.rc.gameEnv.update((env) => {
env.lastExecuted = generalTurnTime; env.lastExecuted = generalTurnTime;
}) })
processedGeneralCount++; processedGeneralCount++;
@@ -89,9 +89,9 @@ export class GameEngine {
if (processedGeneralCount > 0) { if (processedGeneralCount > 0) {
await this.prevSaveWaiter; await this.prevSaveWaiter;
const lastExecuted = this.rsc.gameEnv.raw.lastExecuted; const lastExecuted = this.rc.gameEnv.raw.lastExecuted;
this.loggerEngine.flushAll(); this.loggerEngine.flushAll();
this.prevSaveWaiter = this.rsc.saveAll().then(() => { this.prevSaveWaiter = this.rc.saveAll().then(() => {
this.postStatus({ this.postStatus({
type: 'update', type: 'update',
lastExecuted: lastExecuted.toISOString() lastExecuted: lastExecuted.toISOString()
@@ -136,7 +136,7 @@ export class GameEngine {
await this.prevSaveWaiter; await this.prevSaveWaiter;
this.prevSaveWaiter = null; this.prevSaveWaiter = null;
this.loggerEngine.flushAll(); this.loggerEngine.flushAll();
await this.rsc.saveAll(); await this.rc.saveAll();
for (const response of responseQueue) { for (const response of responseQueue) {
response(); response();
} }
@@ -151,7 +151,7 @@ export class GameEngine {
//가장 빠른 장수 //가장 빠른 장수
do { do {
const now = new Date(); const now = new Date();
const upcomingGeneral = this.rsc.peekGeneralTurnTimeQueue(); const upcomingGeneral = this.rc.peekGeneralTurnTimeQueue();
if (!upcomingGeneral) { if (!upcomingGeneral) {
break; break;
} }
@@ -244,7 +244,7 @@ export class GameEngine {
const actionArgs = action.args; const actionArgs = action.args;
try{ try{
const result = ActionRequestList[actionName](invoker, actionArgs as any, this.rsc); const result = ActionRequestList[actionName](invoker, actionArgs as any, this.rc);
return { return {
success: true, success: true,
value: result value: result