-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.ts
182 lines (163 loc) · 6.32 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { get, writable, derived } from "svelte/store"
import { GameSettings, UserState, GameState, GameSnapshot } from "./models/Game"
import { Farm } from "./models/Farm"
export const gameSettings = writable<GameSettings>(new GameSettings())
export const userState = writable<UserState>(new UserState())
export const gameState = writable<GameState>(new GameState())
export const gameHistory = writable<GameSnapshot[]>([] as GameSnapshot[])
export const farm = writable<Farm>(new Farm(get(gameSettings)))
export const successMetrics = derived(
[farm, gameState, gameSettings],
([$farm, $gameState, $gameSettings]) => {
const { year, coefficients, nutritionalRequirements } = $gameState
const nutrientConversion = coefficients.yieldMultiplier * coefficients.lossRatio
const $gameHistory = get(gameHistory)
const calorieProductionChange =
($farm.calories.total - $farm.initialState.calories.total) / $farm.initialState.calories.total
const currentPopulationFed = (1 + calorieProductionChange) * $gameSettings.populationStart
const hectaresPerPerson = +(($farm.rows * $farm.cols) / currentPopulationFed).toFixed(2)
// Protein
const proteinProductionChange =
($farm.protein.total - $farm.initialState.protein.total) / $farm.initialState.protein.total
const proteinPerPersonPerDayLimit = nutritionalRequirements.protein
const proteinPerPersonPerDayValue = +(
($farm.protein.total *
coefficients.proteinMultiplier *
nutrientConversion *
coefficients.landRatio) /
(currentPopulationFed * 365)
).toFixed(0)
// Emissions
const emissionsChangeLimit = 0.1
const emissionsChangeValue =
($farm.emissions.total - $farm.initialState.emissions.total) /
$farm.initialState.emissions.total
// Water use
const waterUseChangeLimit = 0.1
const waterUseChangeValue =
($farm.waterUse.total - $farm.initialState.waterUse.total) / $farm.initialState.waterUse.total
// Eutrophy
const eutrophyChangeLimit = 0.1
const eutrophyChangeValue =
($farm.eutrophy.total - $farm.initialState.eutrophy.total) / $farm.initialState.eutrophy.total
// Check for success or failure
const hasSucceeded = calorieProductionChange >= $gameSettings.gap
const hasFailed =
year.current >= year.end ||
proteinPerPersonPerDayValue < proteinPerPersonPerDayLimit ||
emissionsChangeValue > emissionsChangeLimit ||
waterUseChangeValue > waterUseChangeLimit ||
eutrophyChangeValue > eutrophyChangeLimit
// Update snapshot history
gameHistory.update(($gameHistory) => {
const snapshot = new GameSnapshot(
year.current,
calorieProductionChange,
proteinPerPersonPerDayValue,
emissionsChangeValue,
waterUseChangeValue,
eutrophyChangeValue,
hasSucceeded,
hasFailed
)
// If year already exists in history, replace it with the new snapshot. This is necessary because all values are tweened.
const index = $gameHistory.findIndex((item) => item.year === year.current)
if (index === -1) $gameHistory.push(snapshot)
return $gameHistory
})
// Metric monitors
const proteinPerPersonPerDay: FailureMetric = {
value: proteinPerPersonPerDayValue,
key: "proteinPerPersonPerDay",
label: "Protein per capita",
suffix: "g",
limit: proteinPerPersonPerDayLimit,
objective: `Keep above ${proteinPerPersonPerDayLimit}g`,
warn: proteinPerPersonPerDayValue < proteinPerPersonPerDayLimit + 5,
fail: proteinPerPersonPerDayValue < proteinPerPersonPerDayLimit,
history: $gameHistory.map((o) => o.proteinPerPersonPerDay),
farmMetricKey: "protein",
foodMetricKey: "proteinRatio",
chartSettings: {
yLimit: proteinPerPersonPerDayLimit,
yMin: proteinPerPersonPerDayLimit,
yMax: proteinPerPersonPerDayLimit + 15
}
}
const emissionsChange: FailureMetric = {
value: emissionsChangeValue,
key: "emissionsChange",
label: "GHG Emissions",
suffix: "%",
limit: emissionsChangeLimit,
objective: `Keep below +${100 * emissionsChangeLimit}%`,
warn: emissionsChangeValue > emissionsChangeLimit - 0.05,
fail: emissionsChangeValue > emissionsChangeLimit,
history: $gameHistory.map((o) => o.emissionsChange),
farmMetricKey: "emissions",
foodMetricKey: "ghgPerKg",
chartSettings: {
yDatum: 0,
yLimit: emissionsChangeLimit,
yMax: emissionsChangeLimit,
yMin: -0.5 * emissionsChangeLimit
}
}
const waterUseChange: FailureMetric = {
value: waterUseChangeValue,
key: "waterUseChange",
label: "Freshwater use",
suffix: "%",
limit: waterUseChangeLimit,
objective: `Keep below +${100 * waterUseChangeLimit}%`,
warn: waterUseChangeValue > waterUseChangeLimit - 0.05,
fail: waterUseChangeValue > waterUseChangeLimit,
history: $gameHistory.map((o) => o.waterUseChange),
farmMetricKey: "waterUse",
foodMetricKey: "waterPerKg",
chartSettings: {
yDatum: 0,
yLimit: waterUseChangeLimit,
yMax: waterUseChangeLimit,
yMin: -0.5 * waterUseChangeLimit
}
}
const eutrophyChange: FailureMetric = {
value: eutrophyChangeValue,
key: "eutrophyChange",
label: "Water pollution",
suffix: "%",
limit: eutrophyChangeLimit,
objective: `Keep below +${100 * eutrophyChangeLimit}%`,
warn: eutrophyChangeValue > eutrophyChangeLimit - 0.05,
fail: eutrophyChangeValue > eutrophyChangeLimit,
history: $gameHistory.map((o) => o.eutrophyChange),
farmMetricKey: "eutrophy",
foodMetricKey: "eutrophyPerKg",
chartSettings: {
yDatum: 0,
yLimit: eutrophyChangeLimit,
yMax: eutrophyChangeLimit,
yMin: -0.5 * eutrophyChangeLimit
}
}
return {
hectaresPerPerson,
currentPopulationFed,
calorieProductionChange,
proteinProductionChange,
proteinPerPersonPerDay,
emissionsChange,
waterUseChange,
eutrophyChange,
hasSucceeded,
hasFailed
}
}
)
export const sparklineData = derived(successMetrics, ($successMetrics) => [
$successMetrics.proteinPerPersonPerDay,
$successMetrics.emissionsChange,
$successMetrics.waterUseChange,
$successMetrics.eutrophyChange
])