refac: RSC

This commit is contained in:
2024-03-09 05:26:13 +00:00
parent c9afc111cc
commit 3ba5793755
4 changed files with 28 additions and 30 deletions
+6 -6
View File
@@ -23,11 +23,11 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
){ ){
super(); super();
this._raw = raw; this._raw = raw;
this._city = new WeakRef(must(resourceController.getCity(raw.cityID))); this._city = new WeakRef(must(resourceController.city(raw.cityID)));
this._nation = new WeakRef(must(resourceController.getNation(raw.nationID))); this._nation = new WeakRef(must(resourceController.nation(raw.nationID)));
if(raw.troopID){ if(raw.troopID){
this._troop = new WeakRef(must(resourceController.getTroop(raw.troopID, raw.nationID))); this._troop = new WeakRef(must(resourceController.troop(raw.troopID, raw.nationID)));
} }
} }
@@ -56,7 +56,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
if(city instanceof City){ if(city instanceof City){
return city; return city;
} }
const cityObj = this.resourceController.getCity(city); const cityObj = this.resourceController.city(city);
if(!cityObj){ if(!cityObj){
throw new InvalidArgument(`City not found: ${city}`); throw new InvalidArgument(`City not found: ${city}`);
} }
@@ -96,7 +96,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
if(troop instanceof Troop){ if(troop instanceof Troop){
return troop; return troop;
} }
const troopObj = this.resourceController.getTroop(troop, this._raw.nationID); const troopObj = this.resourceController.troop(troop, this._raw.nationID);
if(!troopObj){ if(!troopObj){
throw new InvalidArgument(`Troop not found: ${troop}`); throw new InvalidArgument(`Troop not found: ${troop}`);
} }
@@ -115,7 +115,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
if(nation instanceof Nation){ if(nation instanceof Nation){
return nation; return nation;
} }
const nationObj = this.resourceController.getNation(nation); const nationObj = this.resourceController.nation(nation);
if(!nationObj){ if(!nationObj){
throw new InvalidArgument(`Nation not found: ${nation}`); throw new InvalidArgument(`Nation not found: ${nation}`);
} }
+1 -1
View File
@@ -27,6 +27,6 @@ export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
} }
} }
} }
this.resourceController.setDirty(this); this.resourceController.reserveUpdate(this);
} }
} }
+20 -22
View File
@@ -12,47 +12,45 @@ import type { INationEntity } from "./Entity/NationEntity.js";
import type { EntityType, ValidEntityType } from "./Entity/index.js"; import type { EntityType, ValidEntityType } from "./Entity/index.js";
import type { IReservedTurn } from "./Entity/ReservedTurn.js"; import type { IReservedTurn } from "./Entity/ReservedTurn.js";
export interface IResourceController{ export interface IResourceController {
addCity(city: City): boolean; city(id: CityID): City | undefined;
addNation(nation: Nation): boolean; nation(id: NationID): Nation | undefined;
addGeneral(general: General): boolean; troop(id: TroopID, nation: NationID): Troop | undefined;
general(id: GeneralID): General | undefined;
getCity(id: CityID): City | undefined;
getNation(id: NationID): Nation | undefined;
getGeneral(id: GeneralID): General | undefined;
getGameEnv(): GameEnv; getGameEnv(): GameEnv;
findAllNations(): Map<NationID, Nation>; allNations(): Map<NationID, Nation>;
findAllCities(): Map<CityID, City>; allCities(): Map<CityID, City>;
findAllGenerals(): Map<GeneralID, General>; allGenerals(): Map<GeneralID, General>;
findGeneralByTroop(id: TroopID): Map<GeneralID, General>; generalByTroop(id: TroopID): Map<GeneralID, General>;
findCityByNation(id: NationID): Map<CityID, City>; cityByNation(id: NationID): Map<CityID, City>;
findGeneralByNation(id: NationID): Map<GeneralID, General>; generalByNation(id: NationID): Map<GeneralID, General>;
findGeneralByCity(id: CityID, nationID?: NationID): Map<GeneralID, General>; generalByCity(id: CityID, nationID?: NationID): Map<GeneralID, General>;
getTroop(id: TroopID, nation: NationID): Troop | undefined; reserveUpdate(entity: LazyEntityUpdater<any>): void;
setDirty(entity: LazyEntityUpdater<any>): void;
reserveDelete(entity: LazyEntityUpdater<any>): void; reserveDelete(entity: LazyEntityUpdater<any>): void;
//TODO: create* 방식과 구현 방식 비교 //TODO: create* 방식과 구현 방식 비교
reserveNew(entity: LazyEntityUpdater<any>): void; reserveInsert(entity: LazyEntityUpdater<any>): void;
//TODO: reserveNew와 구현 방식 비교 //TODO: reserveInsert와 구현 방식 비교
createGeneral(raw: IGeneralEntity): General; createGeneral(raw: IGeneralEntity): General;
createNation(raw: INationEntity): Nation; createNation(raw: INationEntity): Nation;
createTroop(raw: ITroopEntity): Troop; createTroop(raw: ITroopEntity): Troop;
_getAllChanges(): Record<ValidEntityType, Map<number, ['new'|'update'|'delete', LazyEntityUpdater<any>]>>; _getAllChanges(): Record<
ValidEntityType,
Map<number, [op: 'insert' | 'update' | 'delete', value: LazyEntityUpdater<any>]>
>;
//---- DB 접근 --- //---- DB 접근 ---
//예턴은 호출마다 DB에서 조회 //예턴은 호출마다 DB에서 조회
getReservedTurn(general: General): Promise<IReservedTurn>; getReservedTurn(general: General): Promise<IReservedTurn>;
getNationReservedTurn(general: General): Promise<IReservedTurn | undefined>; nationReservedTurn(general: General): Promise<IReservedTurn | undefined>;
resetAndFill(): Promise<void>; resetAndFill(): Promise<void>;
saveAll(): Promise<void>; saveAll(): Promise<void>;
+1 -1
View File
@@ -19,7 +19,7 @@ export class Troop extends LazyEntityUpdater<ITroopEntity> {
super(); super();
this._raw = raw; this._raw = raw;
const members = resourceController.findGeneralByTroop(raw.id); const members = resourceController.generalByTroop(raw.id);
const leaderID: GeneralID = raw.id as GeneralID; const leaderID: GeneralID = raw.id as GeneralID;
this._leader = new WeakRef(must(members.get(leaderID))); this._leader = new WeakRef(must(members.get(leaderID)));
members.delete(leaderID); members.delete(leaderID);