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';
//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();
}
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();
}
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();
}
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();
}
@@ -40,11 +40,11 @@ export function createGeneral(invoker: QueueType, arg: {
bonusStat?: [number, number, number],
//penalty: Record<GeneralPenalty, number>,
}, rsc: IResourceController):void{
}, rc: IResourceController):void{
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 = {
[key: string]: ActionFunc<any, any>
}
+3 -3
View File
@@ -15,7 +15,7 @@ export class City extends LazyEntityUpdater<ICityEntity>{
constructor(
raw: ICityEntity,
protected readonly rsc: IResourceController,
protected readonly rc: IResourceController,
bootstrapInfo: {
general: GeneralID[];
}
@@ -27,7 +27,7 @@ export class City extends LazyEntityUpdater<ICityEntity>{
private _bootstrapInfo?: ConstructorParameters<typeof City>[2];
public bootstrap(){
const rsc = this.rsc;
const rc = this.rc;
const bootstrapInfo = must(this._bootstrapInfo);
delete this._bootstrapInfo;
@@ -35,7 +35,7 @@ export class City extends LazyEntityUpdater<ICityEntity>{
const nationID = this._raw.nationID;
for(const id of bootstrapInfo.general){
const general = must(rsc.general(id));
const general = must(rc.general(id));
this.notifyJoinGeneral(general);
}
}
+1 -1
View File
@@ -11,7 +11,7 @@ export class GameEnv extends LazyEntityUpdater<IGameEnvEntity>{
constructor(
raw: IGameEnvEntity,
protected readonly rsc: IResourceController
protected readonly rc: IResourceController
){
super();
this._raw = raw;
+4 -4
View File
@@ -25,15 +25,15 @@ export class GameLoggerEngine {
private static instance: GameLoggerEngine;
private constructor(
private rsc: IResourceController
private rc: IResourceController
) {
}
public static initInstance(rsc: IResourceController): GameLoggerEngine{
public static initInstance(rc: IResourceController): GameLoggerEngine{
if(GameLoggerEngine.instance){
throw new Error("GameLogger is already initialized");
}
GameLoggerEngine.instance = new GameLoggerEngine(rsc);
GameLoggerEngine.instance = new GameLoggerEngine(rc);
return GameLoggerEngine.instance;
}
@@ -68,7 +68,7 @@ export class GameLoggerEngine {
if(this.logStack.size === 0){
return;
}
//rsc?
//rc?
throw new Error("Not yet implemented");
this.logStack.clear();
}
+7 -7
View File
@@ -25,20 +25,20 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
constructor(
raw: IGeneralEntity,
protected readonly rsc: IResourceController
protected readonly rc: IResourceController
){
super();
this._raw = raw;
}
public bootstrap(){
const rsc = this.rsc;
const rc = this.rc;
const raw = this._raw;
this._city = new WeakRef(must(rsc.city(raw.cityID)));
this._nation = new WeakRef(must(rsc.nation(raw.nationID)));
this._city = new WeakRef(must(rc.city(raw.cityID)));
this._nation = new WeakRef(must(rc.nation(raw.nationID)));
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();
@@ -141,7 +141,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
else{
raw.officerLevel = OfficerLevel.normal;
}
raw.nationJoinedYearMonth = this.rsc.gameEnv.raw.yearMonth;
raw.nationJoinedYearMonth = this.rc.gameEnv.raw.yearMonth;
});
nation.notifyJoinGeneral(this);
@@ -153,7 +153,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
}
public updateTurnTime(){
const secondsPerTurn = this.rsc.gameEnv.raw.secondsPerTurn;
const secondsPerTurn = this.rc.gameEnv.raw.secondsPerTurn;
this.forceUpdate((raw) => {
raw.turnTime = addSeconds(raw.turnTime, secondsPerTurn);
+2 -2
View File
@@ -60,8 +60,8 @@ export interface IResourceController {
let _resourceController: IResourceController | undefined = undefined;
export const setResourceController = (rsc: IResourceController) => {
_resourceController = rsc;
export const setResourceController = (rc: IResourceController) => {
_resourceController = rc;
}
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 rsc: IResourceController;
protected abstract readonly rc: IResourceController;
public abstract bootstrap(): void;
@@ -21,7 +21,7 @@ export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
protected forceUpdate(callback: (raw: Entity) => void) {
callback(this._raw);
this.rsc.reserveUpdate(this);
this.rc.reserveUpdate(this);
}
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`);
}
}
this.rsc.reserveUpdate(this);
this.rc.reserveUpdate(this);
}
}
+5 -5
View File
@@ -41,7 +41,7 @@ export class Nation extends LazyEntityUpdater<INationEntity> {
constructor(
raw: INationEntity,
protected readonly rsc: IResourceController,
protected readonly rc: IResourceController,
bootstrapInfo: {
general: GeneralID[];
squad: SquadID[];
@@ -55,23 +55,23 @@ export class Nation extends LazyEntityUpdater<INationEntity> {
private _bootstrapInfo?: ConstructorParameters<typeof Nation>[2];
bootstrap() {
const rsc = this.rsc;
const rc = this.rc;
const bootstrapInfo = must(this._bootstrapInfo);
delete this._bootstrapInfo;
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);
}
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);
}
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);
}
}
+4 -4
View File
@@ -14,7 +14,7 @@ export class Squad extends LazyEntityUpdater<ISquadEntity> {
constructor(
raw: ISquadEntity,
protected rsc: IResourceController,
protected rc: IResourceController,
bootstrapInfo: {
general: GeneralID[]
}
@@ -26,13 +26,13 @@ export class Squad extends LazyEntityUpdater<ISquadEntity> {
private _bootstrapInfo?: ConstructorParameters<typeof Squad>[2];
public bootstrap() {
const rsc = this.rsc;
const rc = this.rc;
const bootstrapInfo = must(this._bootstrapInfo);
delete this._bootstrapInfo;
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;
@@ -64,7 +64,7 @@ export class Squad extends LazyEntityUpdater<ISquadEntity> {
this.leader.nation.notifyDestroySquad(this);
this.rsc.reserveDelete(this);
this.rc.reserveDelete(this);
}
public changeName(name: string) {
+11 -11
View File
@@ -44,15 +44,15 @@ export class GameEngine {
api: new Denque<ActionRequestContainer>(),
} as const satisfies Record<QueueType, Denque<ActionRequestContainer>>;
private rsc: ResourceController
private rc: ResourceController
private loggerEngine: GameLoggerEngine;
private constructor(
db: Mongoose,
private postStatus: (msg: GameEngineMsg) => void,
) {
this.rsc = new ResourceController(db);
this.loggerEngine = GameLoggerEngine.initInstance(this.rsc);
this.rc = new ResourceController(db);
this.loggerEngine = GameLoggerEngine.initInstance(this.rc);
}
private continueRun = true;
@@ -67,7 +67,7 @@ export class GameEngine {
while (true) {
//장수턴 부터 처리
const upcomingGeneral = this.rsc.popGeneralTurnTimeQueue();
const upcomingGeneral = this.rc.popGeneralTurnTimeQueue();
if (!upcomingGeneral) {
break;
}
@@ -80,8 +80,8 @@ export class GameEngine {
//TODO: 턴 수행해야지!
upcomingGeneral.updateTurnTime();
this.rsc.insertGeneralTurnTimeQueue(upcomingGeneral, true);
this.rsc.gameEnv.update((env) => {
this.rc.insertGeneralTurnTimeQueue(upcomingGeneral, true);
this.rc.gameEnv.update((env) => {
env.lastExecuted = generalTurnTime;
})
processedGeneralCount++;
@@ -89,9 +89,9 @@ export class GameEngine {
if (processedGeneralCount > 0) {
await this.prevSaveWaiter;
const lastExecuted = this.rsc.gameEnv.raw.lastExecuted;
const lastExecuted = this.rc.gameEnv.raw.lastExecuted;
this.loggerEngine.flushAll();
this.prevSaveWaiter = this.rsc.saveAll().then(() => {
this.prevSaveWaiter = this.rc.saveAll().then(() => {
this.postStatus({
type: 'update',
lastExecuted: lastExecuted.toISOString()
@@ -136,7 +136,7 @@ export class GameEngine {
await this.prevSaveWaiter;
this.prevSaveWaiter = null;
this.loggerEngine.flushAll();
await this.rsc.saveAll();
await this.rc.saveAll();
for (const response of responseQueue) {
response();
}
@@ -151,7 +151,7 @@ export class GameEngine {
//가장 빠른 장수
do {
const now = new Date();
const upcomingGeneral = this.rsc.peekGeneralTurnTimeQueue();
const upcomingGeneral = this.rc.peekGeneralTurnTimeQueue();
if (!upcomingGeneral) {
break;
}
@@ -244,7 +244,7 @@ export class GameEngine {
const actionArgs = action.args;
try{
const result = ActionRequestList[actionName](invoker, actionArgs as any, this.rsc);
const result = ActionRequestList[actionName](invoker, actionArgs as any, this.rc);
return {
success: true,
value: result