Last active
February 20, 2022 23:21
-
-
Save jmbeach/9079932727e4118dce770ea3ce4558d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
CountryResources, | |
ResourceDefinitions, | |
} from './models/country-resources.interface'; | |
import { PEOPLE_PER_HOUSE } from './transformation-templates/transformation-templates'; | |
export const stateQuality = ( | |
country: CountryResources, | |
resourceDefinitions: ResourceDefinitions | |
) => { | |
let result = 0; | |
for (const r of Object.keys(country)) { | |
const resourceName = r as keyof CountryResources; | |
const amount = country[resourceName]; | |
if (resourceName === 'R23_Housing') { | |
const housesPerPerson = | |
country.R1_Population === 0 ? 0 : amount / country.R1_Population; | |
// Reward = number of houses per person | |
let housingReward = | |
country.R1_Population * | |
// Don't reward extra if there's more than 1 house per person (too many houses) | |
Math.min(housesPerPerson, 1) * | |
resourceDefinitions.R23_Housing.weight; | |
// Shrink reward if housesPerPerson is very small | |
if (housesPerPerson < 1 / PEOPLE_PER_HOUSE) { | |
housingReward *= housesPerPerson * PEOPLE_PER_HOUSE; | |
} | |
result += housingReward; | |
} else { | |
// Every other resource is just a weighted sum | |
result += resourceDefinitions[resourceName].weight * amount; | |
} | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment