2018-12-07 11:54:26 +01:00
|
|
|
/**
|
|
|
|
* Initialization and manipulation of the Factions object, which stores data
|
|
|
|
* about all Factions in the game
|
|
|
|
*/
|
|
|
|
import { Faction } from "./Faction";
|
|
|
|
import { FactionInfos } from "./FactionInfo";
|
|
|
|
|
|
|
|
import { IMap } from "../types";
|
|
|
|
|
|
|
|
import { Reviver } from "../../utils/JSONReviver";
|
|
|
|
|
|
|
|
|
|
|
|
export let Factions: IMap<Faction> = {};
|
|
|
|
|
|
|
|
export function loadFactions(saveString: string): void {
|
|
|
|
Factions = JSON.parse(saveString, Reviver);
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function AddToFactions(faction: Faction): void {
|
2018-12-07 11:54:26 +01:00
|
|
|
const name: string = faction.name;
|
|
|
|
Factions[name] = faction;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function factionExists(name: string): boolean {
|
|
|
|
return Factions.hasOwnProperty(name);
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function initFactions(): void {
|
2018-12-07 11:54:26 +01:00
|
|
|
for (const name in FactionInfos) {
|
|
|
|
resetFaction(new Faction(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Resets a faction during (re-)initialization. Saves the favor in the new
|
|
|
|
//Faction object and deletes the old Faction Object from "Factions". Then
|
|
|
|
//reinserts the new Faction object
|
2021-05-01 09:17:31 +02:00
|
|
|
export function resetFaction(newFactionObject: Faction): void {
|
2018-12-07 11:54:26 +01:00
|
|
|
if (!(newFactionObject instanceof Faction)) {
|
|
|
|
throw new Error("Invalid argument 'newFactionObject' passed into resetFaction()");
|
|
|
|
}
|
|
|
|
const factionName: string = newFactionObject.name;
|
|
|
|
if (factionExists(factionName)) {
|
|
|
|
newFactionObject.favor = Factions[factionName].favor;
|
|
|
|
delete Factions[factionName];
|
|
|
|
}
|
|
|
|
AddToFactions(newFactionObject);
|
|
|
|
}
|