Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add town events #670

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

### Added

### Changed
- Fixed a rather pesky and persistent bug where changing racial or religious weights would not be reflected in the town.

## 2.8.14

### Added
Expand Down
4 changes: 2 additions & 2 deletions lib/faction/createRivals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dice } from '../src/dice'
import { repeat, sumWeights } from '../src/utils'
import { validateWeight, weightRandom } from '../src/weightRandom'
import { repeat } from '../src/utils'
import { sumWeights, validateWeight, weightRandom } from '../src/weightRandom'
import { WeightRecord } from '../types'
import { factionData } from './factionData'
import { Faction } from './_common'
Expand Down
4 changes: 2 additions & 2 deletions lib/faction/setFactionJoinStats.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { assign, sumWeights } from '../src/utils'
import { assign } from '../src/utils'
import { random } from '../src/random'
import { Faction } from './_common'
import { factionData } from './factionData'
import { weightRandom } from '../src/weightRandom'
import { sumWeights, weightRandom } from '../src/weightRandom'

export function setFactionJoinStats (faction: Faction): void {
console.log('determining joining stats...')
Expand Down
4 changes: 2 additions & 2 deletions lib/faction/setFactionResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { defineRollDataGetter } from '../src/defineRollDataGetter'
import { dice, fm } from '../src/dice'
import { random } from '../src/random'
import { ThresholdTable } from '../src/rollFromTable'
import { repeat, sumWeights, keys, clamp } from '../src/utils'
import { weightRandom } from '../src/weightRandom'
import { repeat, keys, clamp } from '../src/utils'
import { weightRandom, sumWeights } from '../src/weightRandom'
import { WeightRecord } from '../types'
import { factionData, FactionResource } from './factionData'
import { Faction } from './_common'
Expand Down
5 changes: 5 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export * from './npc-generation/setAge'
export * from './npc-generation/setRace'
export * from './npc-generation/swapNPCs'
export * from './npc-generation/npcFinances'
export * from './npc-generation/randomiseNPC'

export * from './npc-generation/traits/createPersonality'
export * from './npc-generation/traits/getTraits'
Expand Down Expand Up @@ -170,6 +171,8 @@ export * from './town/getTownArcana'
export * from './town/getTownEconomics'
export * from './town/getTownLaw'
export * from './town/getTownMaterial'
export * from './town/getTownType'
export * from './town/getRaceReadout'
export * from '../src/Town/js/getTownMilitary'
export * from './town/getTownWelfare'
export * from './town/politicsWeightedRoll'
Expand All @@ -180,4 +183,6 @@ export * from './town/roads'
export * from './town/townRender'
export * from './town/updateTownSocioPolitics'
export * from './town/townDemographics'
export * from './town/getDemographicPercentile'
export * from './town/setBaseDemographics'
export * from './types'
14 changes: 14 additions & 0 deletions lib/npc-generation/randomiseNPC.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import { GenderName } from './genderData'
// import { AgeName, RaceName, raceTraits } from './raceTraits'
// import { randomiseNPC } from './randomiseNPC'

// test('Randomise NPC base attributes', () => {
// const genderArray: GenderName[] = ['man', 'woman']
// const raceArray = Object.keys(raceTraits) as RaceName[]
// const ageStageArray: AgeName[] = ['child', 'young adult', 'settled adult', 'elderly']
// const npc = randomiseNPC()
// expect(genderArray).toContain(npc.gender)
// expect(raceArray).toContain(npc.race)
// expect(ageStageArray).toContain(npc.ageStage)
// expect(Object.keys(npc)).toContain(['ageStage', 'gender', 'race'])
// })
15 changes: 15 additions & 0 deletions lib/npc-generation/randomiseNPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { GenderName } from './genderData'
import { AgeName, RaceName, raceTraits } from './raceTraits'
import { random } from '../src/random'

export const randomiseNPC = () => {
return {
gender: random(['man', 'woman']),
race: random(Object.keys(raceTraits)),
ageStage: random(['young adult', 'young adult', 'young adult', 'settled adult', 'settled adult', 'settled adult', 'elderly'])
} as {
gender: GenderName,
race: RaceName,
ageStage: AgeName
}
}
524 changes: 524 additions & 0 deletions lib/src/plague.ts

Large diffs are not rendered by default.

18 changes: 1 addition & 17 deletions lib/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DeepReadonly, WeightRecord } from '../types'
import { DeepReadonly } from '../types'
import { randomFloat } from './randomFloat'
import { validateWeight } from './weightRandom'

/**
* An alternative, stricter typed version of `Object.keys`.
Expand Down Expand Up @@ -147,18 +146,3 @@ export function repeat (fn: (index: number) => void, times: number) {
export function capitalizeFirstLetter (text: string) {
return text.charAt(0).toUpperCase() + text.slice(1)
}

export function sumWeights<T extends string> (
defaultWeights: WeightRecord<T>,
customWeights: WeightRecord<T>
) {
const finalWeights: WeightRecord<T> = {}

for (const name of keys(customWeights)) {
const weight = validateWeight(customWeights[name])
const defaultWeight = defaultWeights[name] ?? 0
finalWeights[name] = defaultWeight + weight
}

return finalWeights
}
15 changes: 15 additions & 0 deletions lib/src/weightRandom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ export function validateWeight (weight?: number) {
}
throw new TypeError(`Weight "${weight}" is not a number.`)
}

export function sumWeights<T extends string> (
defaultWeights: WeightRecord<T>,
customWeights: WeightRecord<T>
) {
const finalWeights: WeightRecord<T> = {}

for (const name of keys(customWeights)) {
const weight = validateWeight(customWeights[name])
const defaultWeight = defaultWeights[name] ?? 0
finalWeights[name] = defaultWeight + weight
}

return finalWeights
}
Loading