This repository has been archived on 2022-01-16. You can view files and clone it, but cannot push or open issues or pull requests.
irreligious/src/model/resource/Money.ts

95 lines
2.7 KiB
TypeScript
Raw Normal View History

/// <reference path="./Purchasable.ts" />
2021-08-20 17:12:32 -05:00
2021-09-06 09:51:23 -05:00
class Money implements IResource {
public readonly resourceType = ResourceType.consumable;
2021-09-06 09:51:23 -05:00
public readonly label = 'Money';
public readonly singularName = '${}';
public readonly pluralName = '${}';
public readonly description = 'Used to purchase goods and services.';
public readonly valueInWholeNumbers = false;
public userActions: ResourceAction[] = [
{
name: 'Collect Tithes',
description: 'Voluntary contributions from followers.',
isEnabled: (state: GameState): boolean =>
this.value < this.max(state) &&
(state.resource.followers?.value ?? 0) >= 1,
2021-09-06 09:51:23 -05:00
performAction: (state: GameState): void => {
this._collectTithes(state);
},
},
];
2021-08-21 17:06:51 -05:00
2021-09-04 22:21:14 -05:00
private _lastCollectionTime = 0;
2021-08-21 17:06:51 -05:00
constructor(public value: number) {}
2021-09-06 09:51:23 -05:00
public isUnlocked = (_state: GameState): boolean => true;
public addValue(amount: number, _state: GameState): void {
2021-09-06 09:51:23 -05:00
this.value += amount;
}
2021-09-05 14:45:37 -05:00
public max: (state: GameState) => number = (state: GameState) => {
2021-09-05 20:43:11 -05:00
let max = state.config.cfgInitialMax.money ?? 0;
max +=
(state.resource.compounds?.value ?? 0) *
(state.config.cfgCapacity.compounds?.money ?? 0);
return max;
2021-09-05 14:45:37 -05:00
};
2021-09-05 14:45:37 -05:00
public inc: (state: GameState) => number = (state) => {
2021-09-04 22:21:14 -05:00
let inc = 0;
2021-08-22 11:07:49 -05:00
2021-08-21 19:02:57 -05:00
// crypto currency
inc +=
(state.resource.cryptoCurrency?.value ?? 0) *
state.config.cfgCryptoReturnAmount;
2021-08-22 11:07:49 -05:00
2021-09-05 19:20:24 -05:00
// salaries
inc -=
(state.resource.pastors?.value ?? 0) *
(state.config.cfgSalary.pastors ?? 0);
2021-09-05 19:20:24 -05:00
2021-08-22 11:07:49 -05:00
return inc;
2021-09-05 14:45:37 -05:00
};
2021-08-21 19:02:57 -05:00
protected _collectTithes(state: GameState): void {
2021-09-06 09:51:23 -05:00
if (this.value >= this.max(state)) return;
const followers = state.resource.followers?.value ?? 0;
if (followers <= 0) return;
// collecting too frequently hurts credibility
const diff = state.now - this._lastCollectionTime;
2021-08-22 11:07:49 -05:00
if (diff < state.config.cfgTimeBetweenTithes) {
const lost =
state.config.cfgTimeBetweenTithes /
diff /
state.config.cfgTitheCredibilityHitFactor;
state.resource.credibility?.addValue(lost * -1, state);
2021-08-21 17:06:51 -05:00
}
2021-09-06 09:51:23 -05:00
const tithings = followers * state.config.cfgTitheAmount;
2021-08-21 17:06:51 -05:00
this._lastCollectionTime = state.now;
2021-09-06 09:51:23 -05:00
if (tithings > 0) {
this.addValue(tithings, state);
this._purchaseLog(tithings, state);
}
}
2021-08-21 21:06:29 -05:00
protected _purchaseLog(amount: number, state: GameState): string {
const followers = state.resource.followers;
if (followers !== undefined) {
return `You collected $${formatNumber(amount)} from ${formatNumber(
followers.value
)} ${
followers.value > 1 ? followers.pluralName : followers.singularName
}.`;
}
return `You collected $${formatNumber(amount)} in tithings.`;
2021-08-21 21:06:29 -05:00
}
2021-08-20 17:12:32 -05:00
}