refac: shorthand name. ResourceController > rsc
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import type { ICityEntity } from "./Entity/CityEntity.js";
|
import type { ICityEntity } from "./Entity/CityEntity.js";
|
||||||
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
||||||
import type { IResourceController } from "./ResourceController.js";
|
import type { IResourceController } from "./IResourceController.js";
|
||||||
import type { CityID } from "./defs.js";
|
import type { CityID } from "./defs.js";
|
||||||
|
|
||||||
export class City extends LazyEntityUpdater<ICityEntity>{
|
export class City extends LazyEntityUpdater<ICityEntity>{
|
||||||
@@ -10,7 +10,7 @@ export class City extends LazyEntityUpdater<ICityEntity>{
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
raw: ICityEntity,
|
raw: ICityEntity,
|
||||||
protected readonly resourceController: IResourceController
|
protected readonly rsc: IResourceController
|
||||||
){
|
){
|
||||||
super();
|
super();
|
||||||
this._raw = raw;
|
this._raw = raw;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { IGameEnvEntity } from "./Entity/GameEnvEntity.js";
|
import type { IGameEnvEntity } from "./Entity/GameEnvEntity.js";
|
||||||
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
||||||
import type { IResourceController } from "./ResourceController.js";
|
import type { IResourceController } from "./IResourceController.js";
|
||||||
|
|
||||||
export class GameEnv extends LazyEntityUpdater<IGameEnvEntity>{
|
export class GameEnv extends LazyEntityUpdater<IGameEnvEntity>{
|
||||||
protected readonly _indirectNames: readonly (keyof IGameEnvEntity)[] = [
|
protected readonly _indirectNames: readonly (keyof IGameEnvEntity)[] = [
|
||||||
@@ -11,7 +11,7 @@ export class GameEnv extends LazyEntityUpdater<IGameEnvEntity>{
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
raw: IGameEnvEntity,
|
raw: IGameEnvEntity,
|
||||||
protected readonly resourceController: IResourceController
|
protected readonly rsc: IResourceController
|
||||||
){
|
){
|
||||||
super();
|
super();
|
||||||
this._raw = raw;
|
this._raw = raw;
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
import { InvalidArgument } from "@sammo/util";
|
import { InvalidArgument } from "@sammo/util";
|
||||||
import { City } from "./City.js";
|
import { City } from "./City.js";
|
||||||
import type { IGeneralEntity } from "./Entity/GeneralEntity.js";
|
import type { IGeneralEntity } from "./Entity/GeneralEntity.js";
|
||||||
import type { IResourceController } from "./ResourceController.js";
|
import type { IResourceController } from "./IResourceController.js";
|
||||||
import { DiplomaticPermission, type CityID, type GeneralID, type GeneralName, type NationID, OfficerLevel, type TroopID } from "./defs.js";
|
import { DiplomaticPermission, type CityID, type GeneralID, type GeneralName, type NationID, OfficerLevel, type TroopID } from "./defs.js";
|
||||||
import { must } from "@sammo/util";
|
import { must } from "@sammo/util";
|
||||||
import { Nation } from "./Nation.js";
|
import { Nation } from "./Nation.js";
|
||||||
import { Troop } from "./Troop.js";
|
import { Troop } from "./Troop.js";
|
||||||
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
||||||
import type { IAction, IActionKey } from "./IAction.js";
|
import type { IAction, IActionKey } from "./IAction.js";
|
||||||
|
import { addSeconds } from "date-fns";
|
||||||
|
|
||||||
export class General extends LazyEntityUpdater<IGeneralEntity> {
|
export class General extends LazyEntityUpdater<IGeneralEntity> {
|
||||||
protected readonly _indirectNames: (keyof IGeneralEntity)[] = ["id", "nationID", "troopID", "nationID"];
|
protected readonly _indirectNames: (keyof IGeneralEntity)[] = [
|
||||||
|
"id", "nationID", "troopID", "nationID", "turntime"
|
||||||
|
];
|
||||||
protected readonly _entityType = "General";
|
protected readonly _entityType = "General";
|
||||||
protected readonly _raw: IGeneralEntity;
|
protected readonly _raw: IGeneralEntity;
|
||||||
|
|
||||||
@@ -22,15 +25,15 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
raw: IGeneralEntity,
|
raw: IGeneralEntity,
|
||||||
protected readonly resourceController: IResourceController
|
protected readonly rsc: IResourceController
|
||||||
){
|
){
|
||||||
super();
|
super();
|
||||||
this._raw = raw;
|
this._raw = raw;
|
||||||
this._city = new WeakRef(must(resourceController.city(raw.cityID)));
|
this._city = new WeakRef(must(rsc.city(raw.cityID)));
|
||||||
this._nation = new WeakRef(must(resourceController.nation(raw.nationID)));
|
this._nation = new WeakRef(must(rsc.nation(raw.nationID)));
|
||||||
|
|
||||||
if(raw.troopID){
|
if(raw.troopID){
|
||||||
this._troop = new WeakRef(must(resourceController.troop(raw.troopID, raw.nationID)));
|
this._troop = new WeakRef(must(rsc.troop(raw.troopID, raw.nationID)));
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: iAction 처리해야함
|
//TODO: iAction 처리해야함
|
||||||
@@ -62,7 +65,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
|||||||
if(city instanceof City){
|
if(city instanceof City){
|
||||||
return city;
|
return city;
|
||||||
}
|
}
|
||||||
const cityObj = this.resourceController.city(city);
|
const cityObj = this.rsc.city(city);
|
||||||
if(!cityObj){
|
if(!cityObj){
|
||||||
throw new InvalidArgument(`City not found: ${city}`);
|
throw new InvalidArgument(`City not found: ${city}`);
|
||||||
}
|
}
|
||||||
@@ -71,9 +74,9 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
|||||||
this._city = new WeakRef(cityObj);
|
this._city = new WeakRef(cityObj);
|
||||||
|
|
||||||
//TODO: ResourceController에게 알려야 하는가?
|
//TODO: ResourceController에게 알려야 하는가?
|
||||||
this.update((raw) => {
|
this.forceUpdate((raw) => {
|
||||||
raw.cityID = cityObj.id;
|
raw.cityID = cityObj.id;
|
||||||
}, true);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public quitTroop(){
|
public quitTroop(){
|
||||||
@@ -90,9 +93,9 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._troop = undefined;
|
this._troop = undefined;
|
||||||
this.update((raw) => {
|
this.forceUpdate((raw) => {
|
||||||
raw.troopID = undefined;
|
raw.troopID = undefined;
|
||||||
}, true);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public changeTroop(troop: TroopID | Troop){
|
public changeTroop(troop: TroopID | Troop){
|
||||||
@@ -102,7 +105,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
|||||||
if(troop instanceof Troop){
|
if(troop instanceof Troop){
|
||||||
return troop;
|
return troop;
|
||||||
}
|
}
|
||||||
const troopObj = this.resourceController.troop(troop, this._raw.nationID);
|
const troopObj = this.rsc.troop(troop, this._raw.nationID);
|
||||||
if(!troopObj){
|
if(!troopObj){
|
||||||
throw new InvalidArgument(`Troop not found: ${troop}`);
|
throw new InvalidArgument(`Troop not found: ${troop}`);
|
||||||
}
|
}
|
||||||
@@ -111,9 +114,9 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
|||||||
|
|
||||||
troopObj.members.set(this._raw.id, new WeakRef(this));
|
troopObj.members.set(this._raw.id, new WeakRef(this));
|
||||||
this._troop = new WeakRef(troopObj);
|
this._troop = new WeakRef(troopObj);
|
||||||
this.update((raw) => {
|
this.forceUpdate((raw) => {
|
||||||
raw.troopID = troopObj.id;
|
raw.troopID = troopObj.id;
|
||||||
}, true);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public changeNation(nation: NationID | Nation){
|
public changeNation(nation: NationID | Nation){
|
||||||
@@ -121,7 +124,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
|||||||
if(nation instanceof Nation){
|
if(nation instanceof Nation){
|
||||||
return nation;
|
return nation;
|
||||||
}
|
}
|
||||||
const nationObj = this.resourceController.nation(nation);
|
const nationObj = this.rsc.nation(nation);
|
||||||
if(!nationObj){
|
if(!nationObj){
|
||||||
throw new InvalidArgument(`Nation not found: ${nation}`);
|
throw new InvalidArgument(`Nation not found: ${nation}`);
|
||||||
}
|
}
|
||||||
@@ -130,7 +133,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
|||||||
this._nation = new WeakRef(nationObj);
|
this._nation = new WeakRef(nationObj);
|
||||||
this.quitTroop();
|
this.quitTroop();
|
||||||
|
|
||||||
this.update((raw) => {
|
this.forceUpdate((raw) => {
|
||||||
raw.nationID = nationObj.id;
|
raw.nationID = nationObj.id;
|
||||||
raw.diplomaticPermission = DiplomaticPermission.none;
|
raw.diplomaticPermission = DiplomaticPermission.none;
|
||||||
if(nationObj.id == 0){
|
if(nationObj.id == 0){
|
||||||
@@ -140,7 +143,16 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
|||||||
raw.officerLevel = OfficerLevel.normal;
|
raw.officerLevel = OfficerLevel.normal;
|
||||||
}
|
}
|
||||||
raw.nationBelong = 0;
|
raw.nationBelong = 0;
|
||||||
}, true);
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateTurntime(){
|
||||||
|
const turntermSec = this.rsc.gameEnv().raw.turntermSec;
|
||||||
|
|
||||||
|
this.forceUpdate((raw) => {
|
||||||
|
raw.turntime = addSeconds(raw.turntime, turntermSec);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { ValidEntity, EntityType } from "./Entity/index.js";
|
import type { ValidEntity, EntityType } from "./Entity/index.js";
|
||||||
import type { IResourceController } from "./ResourceController.js";
|
import type { IResourceController } from "./IResourceController.js";
|
||||||
|
|
||||||
export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
|
export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
|
||||||
protected abstract readonly _indirectNames: ReadonlyArray<keyof Entity>;
|
protected abstract readonly _indirectNames: ReadonlyArray<keyof Entity>;
|
||||||
@@ -7,7 +7,7 @@ export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
|
|||||||
|
|
||||||
protected abstract readonly _raw: Entity;
|
protected abstract readonly _raw: Entity;
|
||||||
|
|
||||||
protected abstract readonly resourceController: IResourceController;
|
protected abstract readonly rsc: IResourceController;
|
||||||
|
|
||||||
public get raw(): Readonly<Entity> {
|
public get raw(): Readonly<Entity> {
|
||||||
return this._raw;
|
return this._raw;
|
||||||
@@ -17,16 +17,21 @@ export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
|
|||||||
return this._entityType;
|
return this._entityType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public update(callback: (oldRaw: Entity) => void, force = false) {
|
protected forceUpdate(callback: (raw: Entity) => void) {
|
||||||
|
callback(this._raw);
|
||||||
|
this.rsc.reserveUpdate(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public update(callback: (raw: Entity) => void) {
|
||||||
const oldRaw = { ...this._raw };
|
const oldRaw = { ...this._raw };
|
||||||
callback(this._raw);
|
callback(this._raw);
|
||||||
if (!force) {
|
|
||||||
for (const indirectName of this._indirectNames) {
|
//for구문이 빠른가, Proxy setter로 제어하는게 빠른가
|
||||||
if (oldRaw[indirectName] !== this._raw[indirectName]) {
|
for (const indirectName of this._indirectNames) {
|
||||||
throw new Error(`${String(indirectName)} change by update() is not allowed`);
|
if (oldRaw[indirectName] !== this._raw[indirectName]) {
|
||||||
}
|
throw new Error(`${String(indirectName)} change by update() is not allowed`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.resourceController.reserveUpdate(this);
|
this.rsc.reserveUpdate(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
||||||
import type { INationEntity } from "./Entity/NationEntity.js";
|
import type { INationEntity } from "./Entity/NationEntity.js";
|
||||||
import type { IResourceController } from "./ResourceController.js";
|
import type { IResourceController } from "./IResourceController.js";
|
||||||
import type { NationID, NationName } from "./defs.js";
|
import type { NationID, NationName } from "./defs.js";
|
||||||
|
|
||||||
export class Nation extends LazyEntityUpdater<INationEntity> {
|
export class Nation extends LazyEntityUpdater<INationEntity> {
|
||||||
@@ -10,7 +10,7 @@ export class Nation extends LazyEntityUpdater<INationEntity> {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
raw: INationEntity,
|
raw: INationEntity,
|
||||||
protected readonly resourceController: IResourceController
|
protected readonly rsc: IResourceController
|
||||||
){
|
){
|
||||||
super();
|
super();
|
||||||
this._raw = raw;
|
this._raw = raw;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { must } from "@sammo/util";
|
import { must } from "@sammo/util";
|
||||||
import type { General } from "./General.js";
|
import type { General } from "./General.js";
|
||||||
import type { IResourceController } from "./ResourceController.js";
|
import type { IResourceController } from "./IResourceController.js";
|
||||||
import type { ITroopEntity } from "./Entity/TroopEntity.js";
|
import type { ITroopEntity } from "./Entity/TroopEntity.js";
|
||||||
import type { GeneralID } from "./defs.js";
|
import type { GeneralID } from "./defs.js";
|
||||||
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
||||||
@@ -14,12 +14,12 @@ export class Troop extends LazyEntityUpdater<ITroopEntity> {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
raw: ITroopEntity,
|
raw: ITroopEntity,
|
||||||
protected resourceController: IResourceController
|
protected rsc: IResourceController
|
||||||
){
|
){
|
||||||
super();
|
super();
|
||||||
this._raw = raw;
|
this._raw = raw;
|
||||||
|
|
||||||
const members = resourceController.generalByTroop(raw.id);
|
const members = rsc.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);
|
||||||
@@ -47,7 +47,15 @@ export class Troop extends LazyEntityUpdater<ITroopEntity> {
|
|||||||
this.leader.quitTroop();
|
this.leader.quitTroop();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resourceController.reserveDelete(this);
|
this.rsc.reserveDelete(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public changeName(name: string){
|
||||||
|
this.update((raw)=>{
|
||||||
|
raw.name = name;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//joinTroop은 General에서 처리
|
||||||
|
//quitTroop은 General에서 처리
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user