@evefrontier/dapp-kit 0.1.8 → 0.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -25
- package/dist/config/dapp-kit.d.ts +4 -0
- package/dist/config/dapp-kit.d.ts.map +1 -0
- package/dist/config/dapp-kit.js +14 -0
- package/dist/config/index.d.ts +2 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +1 -0
- package/dist/graphql/client.d.ts +277 -0
- package/dist/graphql/client.d.ts.map +1 -0
- package/dist/graphql/client.js +346 -0
- package/dist/graphql/index.d.ts +4 -0
- package/dist/graphql/index.d.ts.map +1 -0
- package/dist/graphql/index.js +16 -0
- package/dist/graphql/queries.d.ts +103 -0
- package/dist/graphql/queries.d.ts.map +1 -0
- package/{graphql/queries.ts → dist/graphql/queries.js} +113 -223
- package/dist/graphql/types.d.ts +396 -0
- package/dist/graphql/types.d.ts.map +1 -0
- package/dist/graphql/types.js +4 -0
- package/dist/hooks/index.d.ts +10 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useCharacterDirectory.d.ts +16 -0
- package/dist/hooks/useCharacterDirectory.d.ts.map +1 -0
- package/dist/hooks/useCharacterDirectory.js +23 -0
- package/dist/hooks/useCharacterSearch.d.ts +24 -0
- package/dist/hooks/useCharacterSearch.d.ts.map +1 -0
- package/dist/hooks/useCharacterSearch.js +24 -0
- package/dist/hooks/useConnection.d.ts +51 -0
- package/dist/hooks/useConnection.d.ts.map +1 -0
- package/{hooks/useConnection.ts → dist/hooks/useConnection.js} +8 -10
- package/dist/hooks/useEntityRole.d.ts +26 -0
- package/dist/hooks/useEntityRole.d.ts.map +1 -0
- package/dist/hooks/useEntityRole.js +91 -0
- package/dist/hooks/useNotification.d.ts +72 -0
- package/dist/hooks/useNotification.d.ts.map +1 -0
- package/{hooks/useNotification.ts → dist/hooks/useNotification.js} +8 -12
- package/dist/hooks/useSmartObject.d.ts +44 -0
- package/dist/hooks/useSmartObject.d.ts.map +1 -0
- package/dist/hooks/useSmartObject.js +50 -0
- package/dist/hooks/useSponsoredTransaction.d.ts +147 -0
- package/dist/hooks/useSponsoredTransaction.d.ts.map +1 -0
- package/dist/hooks/useSponsoredTransaction.js +240 -0
- package/dist/hooks/useTypeNames.d.ts +11 -0
- package/dist/hooks/useTypeNames.d.ts.map +1 -0
- package/dist/hooks/useTypeNames.js +24 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/providers/EveFrontierProvider.d.ts +23 -0
- package/dist/providers/EveFrontierProvider.d.ts.map +1 -0
- package/dist/providers/EveFrontierProvider.js +23 -0
- package/dist/providers/NotificationProvider.d.ts +14 -0
- package/dist/providers/NotificationProvider.d.ts.map +1 -0
- package/dist/providers/NotificationProvider.js +66 -0
- package/dist/providers/SmartObjectProvider.d.ts +31 -0
- package/dist/providers/SmartObjectProvider.d.ts.map +1 -0
- package/dist/providers/SmartObjectProvider.js +80 -0
- package/dist/providers/VaultProvider.d.ts +10 -0
- package/dist/providers/VaultProvider.d.ts.map +1 -0
- package/dist/providers/VaultProvider.js +79 -0
- package/dist/providers/index.d.ts +5 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +4 -0
- package/dist/providers/smartObject/useEntityData.d.ts +29 -0
- package/dist/providers/smartObject/useEntityData.d.ts.map +1 -0
- package/dist/providers/smartObject/useEntityData.js +135 -0
- package/dist/providers/smartObject/useEntitySubscription.d.ts +14 -0
- package/dist/providers/smartObject/useEntitySubscription.d.ts.map +1 -0
- package/dist/providers/smartObject/useEntitySubscription.js +58 -0
- package/dist/providers/smartObject/useObjectSelection.d.ts +11 -0
- package/dist/providers/smartObject/useObjectSelection.d.ts.map +1 -0
- package/dist/providers/smartObject/useObjectSelection.js +27 -0
- package/dist/providers/smartObject/usePollingBackstop.d.ts +18 -0
- package/dist/providers/smartObject/usePollingBackstop.d.ts.map +1 -0
- package/dist/providers/smartObject/usePollingBackstop.js +38 -0
- package/dist/types/character.d.ts +21 -0
- package/dist/types/character.d.ts.map +1 -0
- package/dist/types/character.js +1 -0
- package/dist/types/contexts.d.ts +61 -0
- package/dist/types/contexts.d.ts.map +1 -0
- package/dist/types/contexts.js +6 -0
- package/dist/types/entity.d.ts +245 -0
- package/dist/types/entity.d.ts.map +1 -0
- package/dist/types/entity.js +6 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +5 -0
- package/dist/types/sponsoredTransaction.d.ts +26 -0
- package/dist/types/sponsoredTransaction.d.ts.map +1 -0
- package/dist/types/sponsoredTransaction.js +10 -0
- package/dist/types/types.d.ts +110 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/types.js +41 -0
- package/dist/types/worldApiReturnTypes.d.ts +68 -0
- package/dist/types/worldApiReturnTypes.d.ts.map +1 -0
- package/dist/types/worldApiReturnTypes.js +1 -0
- package/{utils/burnRate.ts → dist/utils/burnRate.d.ts} +4 -29
- package/dist/utils/burnRate.d.ts.map +1 -0
- package/dist/utils/burnRate.js +25 -0
- package/dist/utils/characters.d.ts +26 -0
- package/dist/utils/characters.d.ts.map +1 -0
- package/dist/utils/characters.js +84 -0
- package/dist/utils/constants.d.ts +153 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +190 -0
- package/dist/utils/datahub.d.ts +40 -0
- package/dist/utils/datahub.d.ts.map +1 -0
- package/{utils/datahub.ts → dist/utils/datahub.js} +32 -25
- package/dist/utils/entity.d.ts +58 -0
- package/dist/utils/entity.d.ts.map +1 -0
- package/dist/utils/entity.js +386 -0
- package/dist/utils/errors.d.ts +18 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +128 -0
- package/dist/utils/events/entityEvents.d.ts +44 -0
- package/dist/utils/events/entityEvents.d.ts.map +1 -0
- package/dist/utils/events/entityEvents.js +95 -0
- package/dist/utils/events/entitySubscription.d.ts +30 -0
- package/dist/utils/events/entitySubscription.d.ts.map +1 -0
- package/dist/utils/events/entitySubscription.js +84 -0
- package/dist/utils/events/graphqlEventStream.d.ts +90 -0
- package/dist/utils/events/graphqlEventStream.d.ts.map +1 -0
- package/dist/utils/events/graphqlEventStream.js +198 -0
- package/dist/utils/events/refetchScheduler.d.ts +30 -0
- package/dist/utils/events/refetchScheduler.d.ts.map +1 -0
- package/dist/utils/events/refetchScheduler.js +57 -0
- package/dist/utils/events/sseClient.d.ts +42 -0
- package/dist/utils/events/sseClient.d.ts.map +1 -0
- package/dist/utils/events/sseClient.js +97 -0
- package/dist/utils/fuel.d.ts +231 -0
- package/dist/utils/fuel.d.ts.map +1 -0
- package/dist/utils/fuel.js +652 -0
- package/dist/utils/grid.d.ts +115 -0
- package/dist/utils/grid.d.ts.map +1 -0
- package/dist/utils/grid.js +115 -0
- package/dist/utils/index.d.ts +15 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +22 -0
- package/dist/utils/inventory.d.ts +3 -0
- package/dist/utils/inventory.d.ts.map +1 -0
- package/dist/utils/inventory.js +12 -0
- package/dist/utils/logger.d.ts +25 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +228 -0
- package/dist/utils/mapping.d.ts +30 -0
- package/dist/utils/mapping.d.ts.map +1 -0
- package/dist/utils/mapping.js +73 -0
- package/dist/utils/mvr/worldTypes.d.ts +35 -0
- package/dist/utils/mvr/worldTypes.d.ts.map +1 -0
- package/dist/utils/mvr/worldTypes.js +54 -0
- package/dist/utils/role.d.ts +116 -0
- package/dist/utils/role.d.ts.map +1 -0
- package/dist/utils/role.js +120 -0
- package/dist/utils/smartObject.d.ts +24 -0
- package/dist/utils/smartObject.d.ts.map +1 -0
- package/dist/utils/smartObject.js +24 -0
- package/dist/utils/utils.d.ts +133 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +213 -0
- package/dist/wallet/features.d.ts +28 -0
- package/dist/wallet/features.d.ts.map +1 -0
- package/dist/wallet/features.js +22 -0
- package/dist/wallet/index.d.ts +9 -0
- package/dist/wallet/index.d.ts.map +1 -0
- package/dist/wallet/index.js +9 -0
- package/dist/wallet/utils.d.ts +71 -0
- package/dist/wallet/utils.d.ts.map +1 -0
- package/dist/wallet/utils.js +79 -0
- package/package.json +69 -35
- package/config/dapp-kit.ts +0 -20
- package/config/index.ts +0 -1
- package/graphql/client.ts +0 -578
- package/graphql/index.ts +0 -39
- package/graphql/types.ts +0 -468
- package/hooks/index.ts +0 -15
- package/hooks/useSmartObject.ts +0 -66
- package/hooks/useSponsoredTransaction.ts +0 -442
- package/index.ts +0 -88
- package/providers/EveFrontierProvider.tsx +0 -41
- package/providers/NotificationProvider.tsx +0 -108
- package/providers/SmartObjectProvider.tsx +0 -296
- package/providers/VaultProvider.tsx +0 -108
- package/providers/index.ts +0 -10
- package/types/contexts.ts +0 -67
- package/types/global.d.ts +0 -7
- package/types/index.ts +0 -4
- package/types/sponsoredTransaction.ts +0 -70
- package/types/types.ts +0 -198
- package/types/worldApiReturnTypes.ts +0 -144
- package/utils/__tests__/burnRate.test.ts +0 -121
- package/utils/__tests__/character.test.ts +0 -112
- package/utils/__tests__/characterOwnedObjects.test.ts +0 -225
- package/utils/__tests__/config.test.ts +0 -424
- package/utils/__tests__/datahub.test.ts +0 -58
- package/utils/__tests__/logger.test.ts +0 -145
- package/utils/__tests__/mapping.test.ts +0 -439
- package/utils/__tests__/utils.test.ts +0 -299
- package/utils/character.ts +0 -46
- package/utils/characterOwnedObjects.ts +0 -53
- package/utils/config.ts +0 -151
- package/utils/constants.ts +0 -254
- package/utils/errors.ts +0 -171
- package/utils/index.ts +0 -47
- package/utils/logger.ts +0 -300
- package/utils/mapping.ts +0 -118
- package/utils/transforms.ts +0 -263
- package/utils/utils.ts +0 -270
- package/wallet/features.ts +0 -136
- package/wallet/index.ts +0 -26
- package/wallet/utils.ts +0 -117
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port of world-contracts `power::fuel_math` and the fuel parts of
|
|
3
|
+
* `power::power_grid`, for both grid models (see {@link PowerGridModel}).
|
|
4
|
+
* Integer maths in bigint, with the contract's rounding, so results match the
|
|
5
|
+
* chain to the unit.
|
|
6
|
+
*
|
|
7
|
+
* - `fuelFactor` (up to dev-v1.1.11): `fuel_burn`, `fuel_left`, `used_mw`,
|
|
8
|
+
* `effective_capacity_mw`. Power, fuel and fuel stats are fixed point at
|
|
9
|
+
* {@link FUEL_SCALE} (4 decimals): 150000 is 15 MW.
|
|
10
|
+
* - `pools` (dev-v1.1.16 onward): `project`, `step`, `pool_rates`,
|
|
11
|
+
* `capacity_of` and friends. Power is plain MW; fuel and fuel stats are fixed
|
|
12
|
+
* point at {@link POOLS_FUEL_SCALE} (1e-9 kg).
|
|
13
|
+
*/
|
|
14
|
+
/** Fixed-point scale of power, fuel and fuel stats on a `fuelFactor` grid (`fuel_math::SCALE` up to dev-v1.1.11). @category Utilities - Power */
|
|
15
|
+
export const FUEL_SCALE = 10000n;
|
|
16
|
+
/** Fixed-point scale of fuel and fuel stats on a `pools` grid (`fuel_math::SCALE` from dev-v1.1.16): 1e-9 kg. @category Utilities - Power */
|
|
17
|
+
export const POOLS_FUEL_SCALE = 1000000000n;
|
|
18
|
+
const MS_PER_SECOND = 1000n;
|
|
19
|
+
const MS_PER_HOUR = 3600000n;
|
|
20
|
+
const MIN_FUEL_FACTOR = 10000n;
|
|
21
|
+
const MAX_FUEL_FACTOR = 1000000n;
|
|
22
|
+
const U64_MAX = (1n << 64n) - 1n;
|
|
23
|
+
const max = (a, b) => (a > b ? a : b);
|
|
24
|
+
const min = (a, b) => (a < b ? a : b);
|
|
25
|
+
const divideRoundUp = (numerator, denominator) => (numerator + denominator - 1n) / denominator;
|
|
26
|
+
const modelOf = (grid) => grid.model ?? 'fuelFactor';
|
|
27
|
+
/**
|
|
28
|
+
* Fixed-point scale of the grid's power values: {@link FUEL_SCALE} on a
|
|
29
|
+
* `fuelFactor` grid, 1 (plain MW) on a `pools` grid.
|
|
30
|
+
*
|
|
31
|
+
* @category Utilities - Power
|
|
32
|
+
*/
|
|
33
|
+
export function powerScale(grid) {
|
|
34
|
+
return modelOf(grid) === 'pools' ? 1n : FUEL_SCALE;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Fixed-point scale of the grid's fuel values: {@link FUEL_SCALE} on a
|
|
38
|
+
* `fuelFactor` grid, {@link POOLS_FUEL_SCALE} on a `pools` grid.
|
|
39
|
+
*
|
|
40
|
+
* @category Utilities - Power
|
|
41
|
+
*/
|
|
42
|
+
export function fuelScale(grid) {
|
|
43
|
+
return modelOf(grid) === 'pools' ? POOLS_FUEL_SCALE : FUEL_SCALE;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* What a whole unit of the grid's fuel is: abstract `units` on a `fuelFactor`
|
|
47
|
+
* grid, `kg` on a `pools` grid.
|
|
48
|
+
*
|
|
49
|
+
* @category Utilities - Power
|
|
50
|
+
*/
|
|
51
|
+
export function fuelUnit(grid) {
|
|
52
|
+
return modelOf(grid) === 'pools' ? 'kg' : 'units';
|
|
53
|
+
}
|
|
54
|
+
const rescale = (value, from) => from === FUEL_SCALE ? value : (value * FUEL_SCALE) / from;
|
|
55
|
+
/**
|
|
56
|
+
* A power value of this grid at {@link FUEL_SCALE}, whatever the grid's model:
|
|
57
|
+
* plain MW on a `pools` grid becomes MW × 10^4. For display with
|
|
58
|
+
* {@link fromFixed}.
|
|
59
|
+
*
|
|
60
|
+
* @category Utilities - Power
|
|
61
|
+
*/
|
|
62
|
+
export function normalizeMw(grid, value) {
|
|
63
|
+
return rescale(BigInt(value), powerScale(grid));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A fuel value of this grid at {@link FUEL_SCALE}, whatever the grid's model:
|
|
67
|
+
* 1e-9 kg on a `pools` grid becomes kg × 10^4, rounded down. For display with
|
|
68
|
+
* {@link fromFixed}; see {@link fuelUnit} for what one unit is.
|
|
69
|
+
*
|
|
70
|
+
* @category Utilities - Power
|
|
71
|
+
*/
|
|
72
|
+
export function normalizeFuel(grid, value) {
|
|
73
|
+
return rescale(BigInt(value), fuelScale(grid));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Fuel burned per second of load: `impulse / max(1, burden / containment_reduction)`,
|
|
77
|
+
* clamped to [1, 100]. All values at {@link FUEL_SCALE}. Port of `fuel_math::fuel_factor`.
|
|
78
|
+
*
|
|
79
|
+
* @category Utilities - Power
|
|
80
|
+
*/
|
|
81
|
+
export function fuelFactor(impulse, containmentBurden, containmentReduction) {
|
|
82
|
+
const reduction = max(containmentReduction, FUEL_SCALE);
|
|
83
|
+
const burdenRatio = max((containmentBurden * FUEL_SCALE) / reduction, FUEL_SCALE);
|
|
84
|
+
const factor = (impulse * FUEL_SCALE) / burdenRatio;
|
|
85
|
+
return max(min(factor, MAX_FUEL_FACTOR), MIN_FUEL_FACTOR);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* One online generator's burn over `elapsedMs`, rounded up. With no fuel
|
|
89
|
+
* impulse it burns its base rate instead. Port of `fuel_math::generator_burn`.
|
|
90
|
+
*
|
|
91
|
+
* @category Utilities - Power
|
|
92
|
+
*/
|
|
93
|
+
export function generatorBurn(input) {
|
|
94
|
+
if (input.impulse === 0n) {
|
|
95
|
+
return divideRoundUp(input.baseFuelRate * input.elapsedMs, MS_PER_SECOND);
|
|
96
|
+
}
|
|
97
|
+
const factor = fuelFactor(input.impulse, input.containmentBurden, input.containmentReduction);
|
|
98
|
+
const numerator = input.load * input.maxOutputMw * FUEL_SCALE * input.elapsedMs;
|
|
99
|
+
const denominator = input.capacityMw * factor * MS_PER_SECOND;
|
|
100
|
+
return divideRoundUp(numerator, denominator);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* MW in use at the last settle: the sum of nonzero module draws. Port of
|
|
104
|
+
* `power_grid::used_mw`.
|
|
105
|
+
*
|
|
106
|
+
* @category Utilities - Power
|
|
107
|
+
*/
|
|
108
|
+
export function usedMw(grid) {
|
|
109
|
+
let total = 0n;
|
|
110
|
+
for (const module of Object.values(grid.modules)) {
|
|
111
|
+
total += BigInt(module.activeDraw);
|
|
112
|
+
}
|
|
113
|
+
return total;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Burn over `elapsedMs` at the load stored at the last settle, not capped by fuel.
|
|
117
|
+
* Ignores `grid.on`: `set_power_grid` settles and regrants at zero capacity when
|
|
118
|
+
* switched off, so an off grid's stored draws are all zero.
|
|
119
|
+
*/
|
|
120
|
+
function uncappedBurn(grid, elapsedMs) {
|
|
121
|
+
const capacityMw = BigInt(grid.capacityMw);
|
|
122
|
+
const load = min(usedMw(grid), capacityMw);
|
|
123
|
+
if (load === 0n || BigInt(grid.fuelQuantity) === 0n || elapsedMs <= 0n) {
|
|
124
|
+
return 0n;
|
|
125
|
+
}
|
|
126
|
+
let burn = 0n;
|
|
127
|
+
for (const generator of Object.values(grid.generators)) {
|
|
128
|
+
if (!generator.online)
|
|
129
|
+
continue;
|
|
130
|
+
burn += generatorBurn({
|
|
131
|
+
impulse: BigInt(grid.fuelImpulse),
|
|
132
|
+
containmentBurden: BigInt(grid.fuelContainmentBurden),
|
|
133
|
+
capacityMw,
|
|
134
|
+
maxOutputMw: BigInt(generator.maxOutputMw),
|
|
135
|
+
containmentReduction: BigInt(generator.containmentReduction),
|
|
136
|
+
baseFuelRate: BigInt(generator.baseFuelRate),
|
|
137
|
+
load,
|
|
138
|
+
elapsedMs,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return burn;
|
|
142
|
+
}
|
|
143
|
+
const nowOf = (nowMs) => BigInt(Math.floor(nowMs));
|
|
144
|
+
/**
|
|
145
|
+
* Fuel burned between the last settle and `nowMs`, capped at what is left.
|
|
146
|
+
* Port of `power_grid::fuel_burn` (`fuelFactor`), or the settled fuel in
|
|
147
|
+
* online bays less `projected_fuel` (`pools`). No load means no burn.
|
|
148
|
+
*
|
|
149
|
+
* @category Utilities - Power
|
|
150
|
+
* @param grid - Parsed power grid
|
|
151
|
+
* @param nowMs - Current time in ms since the epoch
|
|
152
|
+
*/
|
|
153
|
+
export function fuelBurn(grid, nowMs) {
|
|
154
|
+
if (modelOf(grid) === 'pools') {
|
|
155
|
+
return BigInt(grid.fuelQuantity) - projectedFuel(grid, nowMs);
|
|
156
|
+
}
|
|
157
|
+
const elapsedMs = nowOf(nowMs) - BigInt(grid.lastSettledMs);
|
|
158
|
+
return min(uncappedBurn(grid, elapsedMs), BigInt(grid.fuelQuantity));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Fuel left now (in online bays, on a `pools` grid), with the burn since the
|
|
162
|
+
* last settle applied. The same number `power_grid::projected_fuel` returns.
|
|
163
|
+
*
|
|
164
|
+
* @category Utilities - Power
|
|
165
|
+
*/
|
|
166
|
+
export function projectedFuel(grid, nowMs) {
|
|
167
|
+
if (modelOf(grid) === 'pools') {
|
|
168
|
+
return onlineQuantity(projectPools(grid, nowOf(nowMs)).bays);
|
|
169
|
+
}
|
|
170
|
+
return BigInt(grid.fuelQuantity) - fuelBurn(grid, nowMs);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Fuel the grid burns per hour, or 0 when nothing draws. Rounded up per
|
|
174
|
+
* generator, as the contract rounds a single settle. On a `fuelFactor` grid
|
|
175
|
+
* at the load stored at the last settle; on a `pools` grid at the load now
|
|
176
|
+
* (`nowMs`, default the last settle), as `projected_pools`' burn rates.
|
|
177
|
+
*
|
|
178
|
+
* @category Utilities - Power
|
|
179
|
+
*/
|
|
180
|
+
export function fuelBurnPerHour(grid, nowMs) {
|
|
181
|
+
if (modelOf(grid) === 'pools') {
|
|
182
|
+
const at = nowMs === undefined ? BigInt(grid.lastSettledMs) : nowOf(nowMs);
|
|
183
|
+
const { bays, capacity, draws } = projectPools(grid, at);
|
|
184
|
+
const load = loadOf(draws, capacity);
|
|
185
|
+
if (load === 0n)
|
|
186
|
+
return 0n;
|
|
187
|
+
return poolRates(grid, poolsOf(bays)).reduce((sum, rate) => sum + rateBurn(rate, capacity, load, MS_PER_HOUR), 0n);
|
|
188
|
+
}
|
|
189
|
+
return uncappedBurn(grid, MS_PER_HOUR);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Time from `nowMs` until the grid stops burning at its current grants: the
|
|
193
|
+
* fuel runs out (`fuelFactor`), or every pool in use runs dry or a run-out
|
|
194
|
+
* trips the grid (`pools`, stepping through each pool's run-out as `project`
|
|
195
|
+
* does). Null when nothing burns.
|
|
196
|
+
*
|
|
197
|
+
* @category Utilities - Power
|
|
198
|
+
*/
|
|
199
|
+
export function msUntilEmpty(grid, nowMs) {
|
|
200
|
+
if (modelOf(grid) === 'pools') {
|
|
201
|
+
const now = nowOf(nowMs);
|
|
202
|
+
const { bays, on, capacity, draws } = projectPools(grid, now);
|
|
203
|
+
const { stoppedAt } = step(grid, bays, on, capacity, draws, now, U64_MAX);
|
|
204
|
+
return stoppedAt === null || stoppedAt === now
|
|
205
|
+
? null
|
|
206
|
+
: Number(stoppedAt - now);
|
|
207
|
+
}
|
|
208
|
+
const perHour = fuelBurnPerHour(grid);
|
|
209
|
+
if (perHour === 0n)
|
|
210
|
+
return null;
|
|
211
|
+
const left = projectedFuel(grid, nowMs);
|
|
212
|
+
return Number((left * MS_PER_HOUR) / perHour);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* A fixed-point value as a number of whole units: `150000` → `15`.
|
|
216
|
+
*
|
|
217
|
+
* @category Utilities - Power
|
|
218
|
+
* @param value - Fixed-point value
|
|
219
|
+
* @param scale - Its scale; {@link FUEL_SCALE} by default. Pass the result of
|
|
220
|
+
* {@link normalizeMw} or {@link normalizeFuel} to display any grid's values.
|
|
221
|
+
*/
|
|
222
|
+
export function fromFixed(value, scale = FUEL_SCALE) {
|
|
223
|
+
const raw = typeof value === 'string' ? BigInt(value) : value;
|
|
224
|
+
return Number(raw) / Number(scale);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Where the grid stands at `nowMs`. On a `fuelFactor` grid: the stored state,
|
|
228
|
+
* with draws and output zeroed once the fuel has run out. On a `pools` grid:
|
|
229
|
+
* `power_grid::project`, stepping through each pool's run-out, re-granting and
|
|
230
|
+
* tripping as a settle would.
|
|
231
|
+
*
|
|
232
|
+
* @category Utilities - Power
|
|
233
|
+
*/
|
|
234
|
+
export function projectGrid(grid, nowMs) {
|
|
235
|
+
const draws = {};
|
|
236
|
+
const outputs = {};
|
|
237
|
+
if (modelOf(grid) === 'pools') {
|
|
238
|
+
const projected = projectPools(grid, nowOf(nowMs));
|
|
239
|
+
Object.keys(grid.modules).forEach((id, i) => {
|
|
240
|
+
draws[id] = projected.draws[i] ?? 0n;
|
|
241
|
+
});
|
|
242
|
+
const pools = poolsOf(projected.bays);
|
|
243
|
+
for (const [id, generator] of Object.entries(grid.generators)) {
|
|
244
|
+
outputs[id] = outputFrom(pools, generator);
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
on: projected.on,
|
|
248
|
+
capacityMw: capacityOf(grid, projected.bays),
|
|
249
|
+
draws,
|
|
250
|
+
outputs,
|
|
251
|
+
fuelNow: onlineQuantity(projected.bays),
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
const fuelNow = projectedFuel(grid, nowMs);
|
|
255
|
+
for (const [id, module] of Object.entries(grid.modules)) {
|
|
256
|
+
draws[id] = fuelNow === 0n ? 0n : BigInt(module.activeDraw);
|
|
257
|
+
}
|
|
258
|
+
for (const [id, generator] of Object.entries(grid.generators)) {
|
|
259
|
+
outputs[id] =
|
|
260
|
+
generator.online && fuelNow > 0n ? BigInt(generator.maxOutputMw) : 0n;
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
on: grid.on,
|
|
264
|
+
capacityMw: BigInt(grid.capacityMw),
|
|
265
|
+
draws,
|
|
266
|
+
outputs,
|
|
267
|
+
fuelNow,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Split the grid's burn per hour by generator, fuel source and module.
|
|
272
|
+
*
|
|
273
|
+
* On a `pools` grid each generator burns `generator_burn` over an hour at its
|
|
274
|
+
* own output and usable impulse; each online bay feeds its pool's burn in
|
|
275
|
+
* proportion to what it holds, as `take_from_online_bays` takes it. On a
|
|
276
|
+
* `fuelFactor` grid each online generator burns `generator_burn` at the one
|
|
277
|
+
* pool, and the sources, which only record a capacity, share it by capacity.
|
|
278
|
+
*
|
|
279
|
+
* @category Utilities - Power
|
|
280
|
+
*/
|
|
281
|
+
export function burnBreakdown(grid, nowMs) {
|
|
282
|
+
const generators = {};
|
|
283
|
+
const impulses = {};
|
|
284
|
+
const sources = {};
|
|
285
|
+
const moduleIds = Object.keys(grid.modules);
|
|
286
|
+
let draws;
|
|
287
|
+
if (modelOf(grid) === 'pools') {
|
|
288
|
+
const projected = projectPools(grid, nowOf(nowMs));
|
|
289
|
+
draws = projected.draws;
|
|
290
|
+
const load = loadOf(draws, projected.capacity);
|
|
291
|
+
const pools = poolsOf(projected.bays);
|
|
292
|
+
const poolBurns = new Map();
|
|
293
|
+
for (const [id, generator] of Object.entries(grid.generators)) {
|
|
294
|
+
const output = outputFrom(pools, generator);
|
|
295
|
+
const pool = generator.fuelType === undefined
|
|
296
|
+
? undefined
|
|
297
|
+
: pools.get(generator.fuelType);
|
|
298
|
+
// `fuel_math::usable_impulse`
|
|
299
|
+
const impulse = pool
|
|
300
|
+
? min(pool.impulse, BigInt(generator.impulseLimit ?? '0'))
|
|
301
|
+
: 0n;
|
|
302
|
+
const burn = load > 0n && output > 0n && pool && generator.fuelType !== undefined
|
|
303
|
+
? poolGeneratorBurn(impulse, projected.capacity, output, load, MS_PER_HOUR)
|
|
304
|
+
: 0n;
|
|
305
|
+
generators[id] = burn;
|
|
306
|
+
if (burn > 0n)
|
|
307
|
+
impulses[id] = impulse;
|
|
308
|
+
if (generator.fuelType !== undefined && burn > 0n) {
|
|
309
|
+
poolBurns.set(generator.fuelType, (poolBurns.get(generator.fuelType) ?? 0n) + burn);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
Object.keys(grid.bays ?? {}).forEach((id, i) => {
|
|
313
|
+
const bay = projected.bays[i];
|
|
314
|
+
if (!bay)
|
|
315
|
+
return;
|
|
316
|
+
const pool = bay.fuelType === null ? undefined : pools.get(bay.fuelType);
|
|
317
|
+
const poolBurn = bay.fuelType === null ? 0n : (poolBurns.get(bay.fuelType) ?? 0n);
|
|
318
|
+
sources[id] = {
|
|
319
|
+
perHour: bay.online && pool && pool.quantity > 0n
|
|
320
|
+
? (poolBurn * bay.quantity) / pool.quantity
|
|
321
|
+
: 0n,
|
|
322
|
+
fuelNow: bay.quantity,
|
|
323
|
+
};
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
const fuelNow = projectedFuel(grid, nowMs);
|
|
328
|
+
// `set_power_grid` regrants at zero capacity when switched off, so an off
|
|
329
|
+
// grid burns nothing whatever draws it still stores
|
|
330
|
+
const burning = grid.on && fuelNow > 0n;
|
|
331
|
+
draws = moduleIds.map((id) => burning ? BigInt(grid.modules[id]?.activeDraw ?? '0') : 0n);
|
|
332
|
+
const capacityMw = BigInt(grid.capacityMw);
|
|
333
|
+
const load = min(usedMw(grid), capacityMw);
|
|
334
|
+
for (const [id, generator] of Object.entries(grid.generators)) {
|
|
335
|
+
generators[id] =
|
|
336
|
+
generator.online && load > 0n && burning
|
|
337
|
+
? generatorBurn({
|
|
338
|
+
impulse: BigInt(grid.fuelImpulse),
|
|
339
|
+
containmentBurden: BigInt(grid.fuelContainmentBurden),
|
|
340
|
+
capacityMw,
|
|
341
|
+
maxOutputMw: BigInt(generator.maxOutputMw),
|
|
342
|
+
containmentReduction: BigInt(generator.containmentReduction),
|
|
343
|
+
baseFuelRate: BigInt(generator.baseFuelRate),
|
|
344
|
+
load,
|
|
345
|
+
elapsedMs: MS_PER_HOUR,
|
|
346
|
+
})
|
|
347
|
+
: 0n;
|
|
348
|
+
}
|
|
349
|
+
const total = Object.values(generators).reduce((a, b) => a + b, 0n);
|
|
350
|
+
const totalCapacity = Object.values(grid.fuelSources).reduce((a, c) => a + BigInt(c), 0n);
|
|
351
|
+
for (const [id, capacity] of Object.entries(grid.fuelSources)) {
|
|
352
|
+
const share = (v) => totalCapacity > 0n ? (v * BigInt(capacity)) / totalCapacity : 0n;
|
|
353
|
+
sources[id] = { perHour: share(total), fuelNow: share(fuelNow) };
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const perHour = Object.values(generators).reduce((a, b) => a + b, 0n);
|
|
357
|
+
const drawn = draws.reduce((a, b) => a + b, 0n);
|
|
358
|
+
const modules = {};
|
|
359
|
+
moduleIds.forEach((id, i) => {
|
|
360
|
+
const draw = draws[i] ?? 0n;
|
|
361
|
+
modules[id] = drawn > 0n ? (perHour * draw) / drawn : 0n;
|
|
362
|
+
});
|
|
363
|
+
return { perHour, generators, sources, modules, impulses };
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* The usable impulse every burning generator shares, at the grid's fuel scale;
|
|
367
|
+
* null when nothing burns, when burning generators use different impulses, or
|
|
368
|
+
* on a `fuelFactor` grid.
|
|
369
|
+
*
|
|
370
|
+
* @category Utilities - Power
|
|
371
|
+
*/
|
|
372
|
+
export function sharedBurnImpulse(burn) {
|
|
373
|
+
const distinct = new Set(Object.values(burn.impulses));
|
|
374
|
+
if (distinct.size !== 1)
|
|
375
|
+
return null;
|
|
376
|
+
const [impulse] = distinct;
|
|
377
|
+
return impulse ?? null;
|
|
378
|
+
}
|
|
379
|
+
/** The grid's bays as stored at the last settle, in install order. */
|
|
380
|
+
function baysOf(grid) {
|
|
381
|
+
return Object.values(grid.bays ?? {}).map((bay) => ({
|
|
382
|
+
online: bay.online,
|
|
383
|
+
fuelType: bay.fuel.fuelType,
|
|
384
|
+
quantity: BigInt(bay.fuel.quantity),
|
|
385
|
+
impulse: BigInt(bay.fuel.impulse),
|
|
386
|
+
volatility: BigInt(bay.fuel.volatility),
|
|
387
|
+
}));
|
|
388
|
+
}
|
|
389
|
+
const onlineQuantity = (bays) => bays.reduce((sum, bay) => (bay.online ? sum + bay.quantity : sum), 0n);
|
|
390
|
+
/**
|
|
391
|
+
* Every pool by fuel type: the quantity-weighted blend of the online bays
|
|
392
|
+
* holding it. A type with no fuel in an online bay is left out. Port of
|
|
393
|
+
* `power_grid::pools_of`.
|
|
394
|
+
*/
|
|
395
|
+
function poolsOf(bays) {
|
|
396
|
+
const sums = new Map();
|
|
397
|
+
for (const bay of bays) {
|
|
398
|
+
if (!bay.online || bay.quantity === 0n || bay.fuelType === null)
|
|
399
|
+
continue;
|
|
400
|
+
const sum = sums.get(bay.fuelType) ?? {
|
|
401
|
+
quantity: 0n,
|
|
402
|
+
impulse: 0n,
|
|
403
|
+
volatility: 0n,
|
|
404
|
+
};
|
|
405
|
+
sum.quantity += bay.quantity;
|
|
406
|
+
sum.impulse += bay.quantity * bay.impulse;
|
|
407
|
+
sum.volatility += bay.quantity * bay.volatility;
|
|
408
|
+
sums.set(bay.fuelType, sum);
|
|
409
|
+
}
|
|
410
|
+
const pools = new Map();
|
|
411
|
+
for (const [fuelType, sum] of sums) {
|
|
412
|
+
pools.set(fuelType, {
|
|
413
|
+
quantity: sum.quantity,
|
|
414
|
+
impulse: sum.impulse / sum.quantity,
|
|
415
|
+
volatility: sum.volatility / sum.quantity,
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
return pools;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Share of rated output a pool's volatility allows, at {@link POOLS_FUEL_SCALE}:
|
|
422
|
+
* `min(volatility × SCALE ÷ volatility_limit, SCALE)`, at least 1 while the
|
|
423
|
+
* volatility is above zero. Port of `fuel_math::volatility_share`.
|
|
424
|
+
*
|
|
425
|
+
* @category Utilities - Power
|
|
426
|
+
*/
|
|
427
|
+
export function volatilityShare(poolVolatility, volatilityLimit) {
|
|
428
|
+
// The contract aborts on a zero limit; treat it as no limit
|
|
429
|
+
const share = volatilityLimit === 0n
|
|
430
|
+
? POOLS_FUEL_SCALE
|
|
431
|
+
: min((poolVolatility * POOLS_FUEL_SCALE) / volatilityLimit, POOLS_FUEL_SCALE);
|
|
432
|
+
return poolVolatility > 0n ? max(share, 1n) : share;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Plain MW a generator sends out at `share`: `max_output_mw × share ÷ SCALE`,
|
|
436
|
+
* floored, but at least 1 MW while both are above zero. Port of
|
|
437
|
+
* `fuel_math::generator_output`.
|
|
438
|
+
*
|
|
439
|
+
* @category Utilities - Power
|
|
440
|
+
*/
|
|
441
|
+
export function generatorOutput(maxOutputMw, share) {
|
|
442
|
+
const output = (maxOutputMw * share) / POOLS_FUEL_SCALE;
|
|
443
|
+
return maxOutputMw > 0n && share > 0n ? max(output, 1n) : output;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* One online generator's burn over `elapsedMs`, at {@link POOLS_FUEL_SCALE},
|
|
447
|
+
* rounded up: its load (`load × output ÷ capacity`) over `usableImpulse`.
|
|
448
|
+
* Capped at u64 max. Port of the dev-v1.1.16+ `fuel_math::generator_burn`.
|
|
449
|
+
*
|
|
450
|
+
* @category Utilities - Power
|
|
451
|
+
*/
|
|
452
|
+
export function poolGeneratorBurn(usableImpulse, capacityMw, outputMw, load, elapsedMs) {
|
|
453
|
+
const denominator = capacityMw * usableImpulse * MS_PER_SECOND;
|
|
454
|
+
// The contract aborts on a zero denominator; nothing burns
|
|
455
|
+
if (denominator === 0n)
|
|
456
|
+
return 0n;
|
|
457
|
+
const numerator = load * outputMw * POOLS_FUEL_SCALE * POOLS_FUEL_SCALE * elapsedMs;
|
|
458
|
+
return min(divideRoundUp(numerator, denominator), U64_MAX);
|
|
459
|
+
}
|
|
460
|
+
/** A generator's output with `pools`, plain MW: 0 while offline or its pool is empty. Port of `output_from`. */
|
|
461
|
+
function outputFrom(pools, generator) {
|
|
462
|
+
if (!generator.online || generator.fuelType === undefined)
|
|
463
|
+
return 0n;
|
|
464
|
+
const pool = pools.get(generator.fuelType);
|
|
465
|
+
if (!pool)
|
|
466
|
+
return 0n;
|
|
467
|
+
const share = volatilityShare(pool.volatility, BigInt(generator.volatilityLimit ?? '0'));
|
|
468
|
+
return generatorOutput(BigInt(generator.maxOutputMw), share);
|
|
469
|
+
}
|
|
470
|
+
/** The sum of every generator's output with `bays`. Port of `capacity_of`. */
|
|
471
|
+
function capacityOf(grid, bays) {
|
|
472
|
+
const pools = poolsOf(bays);
|
|
473
|
+
return Object.values(grid.generators).reduce((sum, generator) => min(sum + outputFrom(pools, generator), U64_MAX), 0n);
|
|
474
|
+
}
|
|
475
|
+
/** MW the switched-on Firm modules need. Port of `desired_firm_mw`. */
|
|
476
|
+
function desiredFirmMw(grid) {
|
|
477
|
+
let total = 0n;
|
|
478
|
+
for (const module of Object.values(grid.modules)) {
|
|
479
|
+
if (module.desiredOnline && module.drawKind === 'Firm') {
|
|
480
|
+
total = min(total + BigInt(module.powerReq), U64_MAX);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return total;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Each module's grant at `capacity`, in connect order: Firm modules all get
|
|
487
|
+
* their requirement or none do; Elastic ones take the leftover in order. Port
|
|
488
|
+
* of `draws_at`.
|
|
489
|
+
*/
|
|
490
|
+
function drawsAt(grid, capacity) {
|
|
491
|
+
const firmCost = desiredFirmMw(grid);
|
|
492
|
+
const firmFits = firmCost <= capacity;
|
|
493
|
+
let leftover = firmFits ? capacity - firmCost : 0n;
|
|
494
|
+
return Object.values(grid.modules).map((module) => {
|
|
495
|
+
if (!module.desiredOnline)
|
|
496
|
+
return 0n;
|
|
497
|
+
if (module.drawKind === 'Firm') {
|
|
498
|
+
return firmFits ? BigInt(module.powerReq) : 0n;
|
|
499
|
+
}
|
|
500
|
+
const grant = min(BigInt(module.powerReq), leftover);
|
|
501
|
+
leftover -= grant;
|
|
502
|
+
return grant;
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
/** MW actually burned: the sum of `draws`, capped at `capacity`. Port of `load_of`. */
|
|
506
|
+
function loadOf(draws, capacity) {
|
|
507
|
+
return min(draws.reduce((sum, draw) => sum + draw, 0n), capacity);
|
|
508
|
+
}
|
|
509
|
+
/** Burn inputs of each pool that has a supplying generator, in generator order. Port of `pool_rates`. */
|
|
510
|
+
function poolRates(grid, pools) {
|
|
511
|
+
const rates = [];
|
|
512
|
+
for (const generator of Object.values(grid.generators)) {
|
|
513
|
+
const output = outputFrom(pools, generator);
|
|
514
|
+
if (output === 0n || generator.fuelType === undefined)
|
|
515
|
+
continue;
|
|
516
|
+
const pool = pools.get(generator.fuelType);
|
|
517
|
+
if (!pool)
|
|
518
|
+
continue;
|
|
519
|
+
let rate = rates.find((r) => r.fuelType === generator.fuelType);
|
|
520
|
+
if (!rate) {
|
|
521
|
+
rate = {
|
|
522
|
+
fuelType: generator.fuelType,
|
|
523
|
+
quantity: pool.quantity,
|
|
524
|
+
impulses: [],
|
|
525
|
+
outputs: [],
|
|
526
|
+
};
|
|
527
|
+
rates.push(rate);
|
|
528
|
+
}
|
|
529
|
+
// `fuel_math::usable_impulse`: the pool's impulse, capped at the generator's limit
|
|
530
|
+
rate.impulses.push(min(pool.impulse, BigInt(generator.impulseLimit ?? '0')));
|
|
531
|
+
rate.outputs.push(output);
|
|
532
|
+
}
|
|
533
|
+
return rates;
|
|
534
|
+
}
|
|
535
|
+
/** A pool's burn over `elapsedMs` at `load` of `capacity`, uncapped. Port of `rate_burn`. */
|
|
536
|
+
function rateBurn(rate, capacity, load, elapsedMs) {
|
|
537
|
+
return rate.impulses.reduce((sum, impulse, i) => sum +
|
|
538
|
+
poolGeneratorBurn(impulse, capacity, rate.outputs[i] ?? 0n, load, elapsedMs), 0n);
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Milliseconds until the first pool runs dry, or `remaining` if none does
|
|
542
|
+
* within it. Binary search, as rounding up makes a closed form inexact. Port
|
|
543
|
+
* of `first_run_out`.
|
|
544
|
+
*/
|
|
545
|
+
function firstRunOut(rates, capacity, load, remaining) {
|
|
546
|
+
let first = remaining;
|
|
547
|
+
for (const rate of rates) {
|
|
548
|
+
if (rateBurn(rate, capacity, load, first) >= rate.quantity) {
|
|
549
|
+
let low = 1n;
|
|
550
|
+
let high = first;
|
|
551
|
+
while (low < high) {
|
|
552
|
+
const mid = low + (high - low) / 2n;
|
|
553
|
+
if (rateBurn(rate, capacity, load, mid) >= rate.quantity)
|
|
554
|
+
high = mid;
|
|
555
|
+
else
|
|
556
|
+
low = mid + 1n;
|
|
557
|
+
}
|
|
558
|
+
first = low;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return first;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Take `amount` from the online bays of `fuelType`, each in proportion to what
|
|
565
|
+
* it holds; the rounding remainder comes off the largest, then the others in
|
|
566
|
+
* install order. A bay that runs dry goes back to empty. Port of
|
|
567
|
+
* `take_from_online_bays`. Returns new bays.
|
|
568
|
+
*/
|
|
569
|
+
function takeFromOnlineBays(bays, fuelType, amount) {
|
|
570
|
+
if (amount === 0n)
|
|
571
|
+
return bays;
|
|
572
|
+
const next = bays.map((bay) => ({ ...bay }));
|
|
573
|
+
const feeds = (bay) => bay.online && bay.fuelType === fuelType;
|
|
574
|
+
const total = next.reduce((sum, bay) => (feeds(bay) ? sum + bay.quantity : sum), 0n);
|
|
575
|
+
let taken = 0n;
|
|
576
|
+
let largestIdx = 0;
|
|
577
|
+
let largestAmount = 0n;
|
|
578
|
+
next.forEach((bay, i) => {
|
|
579
|
+
if (!feeds(bay) || bay.quantity === 0n)
|
|
580
|
+
return;
|
|
581
|
+
if (bay.quantity > largestAmount) {
|
|
582
|
+
largestAmount = bay.quantity;
|
|
583
|
+
largestIdx = i;
|
|
584
|
+
}
|
|
585
|
+
const share = (bay.quantity * amount) / total;
|
|
586
|
+
bay.quantity -= share;
|
|
587
|
+
taken += share;
|
|
588
|
+
});
|
|
589
|
+
let remainder = amount - taken;
|
|
590
|
+
const takeUpTo = (i) => {
|
|
591
|
+
const bay = next[i];
|
|
592
|
+
if (remainder === 0n || !bay || !feeds(bay))
|
|
593
|
+
return;
|
|
594
|
+
const take = min(remainder, bay.quantity);
|
|
595
|
+
bay.quantity -= take;
|
|
596
|
+
remainder -= take;
|
|
597
|
+
};
|
|
598
|
+
takeUpTo(largestIdx);
|
|
599
|
+
for (let i = 0; i < next.length; i++)
|
|
600
|
+
takeUpTo(i);
|
|
601
|
+
for (const bay of next) {
|
|
602
|
+
if (bay.quantity === 0n)
|
|
603
|
+
bay.fuelType = null;
|
|
604
|
+
}
|
|
605
|
+
return next;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Step `bays` from `fromMs` to `untilMs` at `on`, `capacity` and `draws`:
|
|
609
|
+
* burn every pool up to the earliest run-out, recompute capacity, trip if the
|
|
610
|
+
* switched-on Firm need no longer fits, re-grant, and repeat. Port of `step`.
|
|
611
|
+
* Also returns when burning stopped (nothing left to burn, or tripped), or
|
|
612
|
+
* null if it was still burning at `untilMs`.
|
|
613
|
+
*/
|
|
614
|
+
function step(grid, startBays, startOn, startCapacity, startDraws, fromMs, untilMs) {
|
|
615
|
+
let bays = startBays;
|
|
616
|
+
let on = startOn;
|
|
617
|
+
let capacity = startCapacity;
|
|
618
|
+
let draws = startDraws;
|
|
619
|
+
let at = fromMs;
|
|
620
|
+
while (untilMs > at) {
|
|
621
|
+
const load = loadOf(draws, capacity);
|
|
622
|
+
if (load === 0n) {
|
|
623
|
+
return { bays, on, capacity, draws, stoppedAt: at };
|
|
624
|
+
}
|
|
625
|
+
const rates = poolRates(grid, poolsOf(bays));
|
|
626
|
+
const elapsed = firstRunOut(rates, capacity, load, untilMs - at);
|
|
627
|
+
at += elapsed;
|
|
628
|
+
for (const rate of rates) {
|
|
629
|
+
const burn = min(rateBurn(rate, capacity, load, elapsed), rate.quantity);
|
|
630
|
+
bays = takeFromOnlineBays(bays, rate.fuelType, burn);
|
|
631
|
+
}
|
|
632
|
+
capacity = capacityOf(grid, bays);
|
|
633
|
+
if (desiredFirmMw(grid) > capacity) {
|
|
634
|
+
on = false;
|
|
635
|
+
capacity = 0n;
|
|
636
|
+
}
|
|
637
|
+
draws = drawsAt(grid, capacity);
|
|
638
|
+
}
|
|
639
|
+
const stopped = loadOf(draws, capacity) === 0n;
|
|
640
|
+
return { bays, on, capacity, draws, stoppedAt: stopped ? at : null };
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* The grid at `nowMs` from its last settle: bays, whether it is on, effective
|
|
644
|
+
* capacity, and each module's draw in connect order. Port of `project`.
|
|
645
|
+
*/
|
|
646
|
+
function projectPools(grid, nowMs) {
|
|
647
|
+
const bays = baysOf(grid);
|
|
648
|
+
const draws = Object.values(grid.modules).map((m) => BigInt(m.activeDraw));
|
|
649
|
+
const capacity = grid.on ? capacityOf(grid, bays) : 0n;
|
|
650
|
+
const { stoppedAt: _, ...projected } = step(grid, bays, grid.on, capacity, draws, BigInt(grid.lastSettledMs), nowMs);
|
|
651
|
+
return projected;
|
|
652
|
+
}
|