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

106 lines
3.0 KiB
TypeScript
Raw Normal View History

/// <reference path="./Resource.ts" />
class Money extends Resource {
public readonly resourceType = ResourceType.purchasable;
public readonly resourceKey = ResourceKey.money;
2021-08-20 17:12:32 -05:00
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
public constructor(initialValue: number) {
super();
this.rawValue = initialValue;
}
public isUnlocked = (_: GameState): boolean => true;
public max = (state: GameState): number => {
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
};
public inc = (state: GameState): number => {
2021-09-04 22:21:14 -05:00
let inc = 0;
2021-08-22 11:07:49 -05:00
2021-09-11 22:45:52 -05:00
// tithings
inc +=
((state.resource.pastors?.value ?? 0) *
(state.resource.followers?.value ?? 0) *
2021-09-11 22:45:52 -05:00
(state.config.cfgTitheAmount ?? 0) *
Credibility.ratio(state)) /
state.config.cfgTimeBetweenTithes;
2021-09-05 19:20:24 -05:00
2021-09-11 22:45:52 -05:00
// salaries
inc -=
(state.resource.compoundManagers?.value ?? 0) *
(state.config.cfgSalary.compoundManagers ?? 0);
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;
2021-09-11 22:45:52 -05:00
// can't tithe your pastors
const followers =
(state.resource.followers?.value ?? 0) -
(state.resource.pastors?.value ?? 0);
2021-09-06 09:51:23 -05:00
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
2021-09-06 12:26:43 -05:00
protected _purchaseLog(amount: number, state: GameState): void {
const followers = state.resource.followers;
if (followers !== undefined) {
2021-09-06 12:26:43 -05:00
state.log(
`You collected $${formatNumber(amount)} from ${formatNumber(
followers.value
2021-09-06 12:26:43 -05:00
)} ${
followers.value > 1 ? followers.pluralName : followers.singularName
2021-09-06 12:26:43 -05:00
}.`
);
} else {
state.log(`You collected $${formatNumber(amount)} in tithings.`);
}
2021-08-21 21:06:29 -05:00
}
2021-08-20 17:12:32 -05:00
}