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/PlayerOrg.ts

149 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-08-21 19:08:49 -05:00
/// <reference path="./IResource.ts" />
2021-08-21 17:06:51 -05:00
class PlayerOrg implements IResource {
2021-09-05 14:45:37 -05:00
public readonly resourceType: ResourceType = ResourceType.religion;
2021-08-21 17:06:51 -05:00
public readonly name: string = 'Player';
public readonly description: string = 'In you they trust.';
2021-08-22 11:07:49 -05:00
public readonly valueInWholeNumbers: boolean = true;
public readonly clickText: string = 'Recruit';
public readonly clickDescription: string = 'Gather new followers.';
2021-09-04 22:21:14 -05:00
public value = 0;
2021-08-22 13:08:41 -05:00
public readonly cost: null = null;
2021-08-21 17:06:51 -05:00
2021-09-04 22:21:14 -05:00
private _timeSinceLastLost = 0;
private _lastRecruitmentLog = 0;
2021-08-22 13:08:41 -05:00
private _followerSources: { [key: string]: number } = { };
private _followerDests: { [key: string]: number } = { };
2021-08-21 17:06:51 -05:00
2021-08-21 19:02:57 -05:00
public max (state: GameState): number {
2021-09-05 14:45:37 -05:00
let max: number = state.config.cfgStartingPlayerMax;
2021-08-21 19:53:43 -05:00
max += state.getResource('tents').value * 2;
max += state.getResource('house').value * 10;
return max;
2021-08-21 19:02:57 -05:00
}
2021-08-22 13:08:41 -05:00
public inc (state: GameState): number {
2021-09-04 22:21:14 -05:00
let inc = 0;
2021-08-22 13:08:41 -05:00
// pastor recruiting
2021-09-05 14:45:37 -05:00
const pastors = state.getResource('pstor').value;
2021-08-22 13:08:41 -05:00
inc += pastors * state.config.cfgPastorRecruitRate;
// credibility adjustment
2021-09-05 14:45:37 -05:00
const creds = state.getResource('creds');
if (creds.max !== null) inc *= creds.value / creds.max(state);
2021-08-22 13:08:41 -05:00
return inc;
}
2021-08-21 17:06:51 -05:00
public clickAction (state: GameState): void {
2021-08-21 19:02:57 -05:00
// don't exceed max
if (this.value >= this.max(state)) {
state.log('You have no room for more followers.');
return;
}
// chance to fail increases as credibility decreases
const creds: IResource = state.getResource('creds');
2021-09-05 14:45:37 -05:00
if (creds.max !== null) {
const ratio: number = Math.ceil(creds.value) / creds.max(state);
if (Math.random() > ratio) {
state.log('Your recruitment efforts failed.');
return;
}
2021-08-21 19:02:57 -05:00
}
2021-08-22 13:48:48 -05:00
this._lastRecruitmentLog = 0; // always log on click
2021-08-22 13:08:41 -05:00
this.addValue(1, state);
2021-08-21 17:06:51 -05:00
}
public addValue (amount: number, state: GameState): void {
2021-09-05 14:45:37 -05:00
const oldValue = this.value;
this.value += amount;
2021-09-05 14:45:37 -05:00
const diff = Math.floor(this.value) - Math.floor(oldValue);
2021-08-22 13:08:41 -05:00
if (diff > 0) {
// gained followers must come from other faiths
2021-09-04 22:21:14 -05:00
for (let i = 0; i < diff; i++) {
2021-09-05 14:45:37 -05:00
const source = this._getRandomReligion(state);
2021-08-22 13:08:41 -05:00
source[1].addValue(-1, state);
2021-09-05 14:45:37 -05:00
const curFollowers = this._followerSources[source[0]];
this._followerSources[source[0]] = !isNaN(curFollowers)
2021-08-22 13:08:41 -05:00
? curFollowers + 1
: 1;
}
} else {
// lost followers must return to other faiths
2021-09-04 22:21:14 -05:00
for (let i = 0; i < diff * -1; i++) {
2021-08-22 13:08:41 -05:00
const dest: [string, IResource] = this._getRandomReligion(state);
dest[1].addValue(1, state);
const curFollowers: number = this._followerDests[dest[0]];
2021-09-05 14:45:37 -05:00
this._followerDests[dest[0]] = !isNaN(curFollowers)
2021-08-22 13:08:41 -05:00
? curFollowers + 1
: 1;
}
}
}
2021-08-22 11:07:49 -05:00
2021-09-05 14:45:37 -05:00
public isUnlocked (_state: GameState): boolean {
return true;
2021-08-22 11:07:49 -05:00
}
2021-08-21 17:06:51 -05:00
public advanceAction (time: number, state: GameState): void {
2021-08-21 19:02:57 -05:00
// chance to lose some followers every 10s if credibility < 100%
2021-08-22 13:08:41 -05:00
this._timeSinceLastLost += time;
if (this._timeSinceLastLost > 10000) {
2021-08-21 19:02:57 -05:00
if (this.value > 0) {
2021-09-05 14:45:37 -05:00
const creds = state.getResource('creds');
if (creds.max !== null) {
const ratio: number = Math.ceil(creds.value) / creds.max(state);
if (Math.random() > ratio) {
const lost: number = Math.ceil(this.value / 25 * (1 - ratio));
this.addValue(lost * -1, state);
}
2021-08-21 19:02:57 -05:00
}
2021-08-21 17:06:51 -05:00
}
2021-08-22 13:08:41 -05:00
this._timeSinceLastLost = 0;
}
// log lost and gained followers every 10s
if (state.now - this._lastRecruitmentLog > 10000
&& (Object.keys(this._followerSources).length > 0
|| Object.keys(this._followerDests).length > 0)) {
if (Object.keys(this._followerDests).length > 0) {
2021-09-04 22:21:14 -05:00
let msg = '';
let total = 0;
2021-08-22 13:08:41 -05:00
for (const rkey of Object.keys(this._followerDests)) {
2021-08-22 14:29:49 -05:00
if (msg !== '') msg += ', ';
2021-08-22 13:08:41 -05:00
const religion: IResource = state.getResource(rkey);
2021-09-05 14:45:37 -05:00
msg += `${state.formatNumber(this._followerDests[rkey])} to ${religion.name}`;
2021-08-22 14:29:49 -05:00
total += this._followerDests[rkey];
2021-08-22 13:08:41 -05:00
delete this._followerDests[rkey];
}
2021-08-22 14:29:49 -05:00
state.log(`You lost ${state.formatNumber(total)} followers: ${msg}`);
2021-08-22 13:08:41 -05:00
}
if (Object.keys(this._followerSources).length > 0) {
2021-09-04 22:21:14 -05:00
let msg = '';
let total = 0;
2021-08-22 13:08:41 -05:00
for (const rkey of Object.keys(this._followerSources)) {
2021-08-22 14:29:49 -05:00
if (msg !== '') msg += ', ';
2021-08-22 13:08:41 -05:00
const religion: IResource = state.getResource(rkey);
msg +=
2021-08-22 14:29:49 -05:00
`${state.formatNumber(this._followerSources[rkey])} from ${religion.name}`;
total += this._followerSources[rkey];
2021-08-22 13:08:41 -05:00
delete this._followerSources[rkey];
}
2021-08-22 14:29:49 -05:00
state.log(`You gained ${state.formatNumber(total)} followers: ${msg}`);
2021-08-22 13:08:41 -05:00
}
this._lastRecruitmentLog = state.now;
2021-08-21 17:06:51 -05:00
}
}
private _getRandomReligion (state: GameState): [string, IResource] {
const religs: string[] = ['xtian', 'islam', 'hindu',
'buddh', 'sikhi', 'judah', 'other', 'agnos'];
const source: string = religs[Math.floor(Math.random() * 8)];
return [source, state.getResource(source)];
}
}