powergrid-viewer 2.0.4 → 2.0.6

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.
@@ -1,6 +1,6 @@
1
1
  import { isEqual } from 'lodash';
2
2
  import type { GameState, LogItem, LogMove, Move } from 'powergrid-engine';
3
- import { move as engineMove } from 'powergrid-engine';
3
+ import { move as engineMove, MoveName } from 'powergrid-engine';
4
4
 
5
5
  /**
6
6
  * Pure helpers for the viewer's tentative-turn buffer.
@@ -147,3 +147,68 @@ export function replayTurnBuffer(
147
147
 
148
148
  return { state, applied };
149
149
  }
150
+
151
+ /**
152
+ * Where a resource purchase came from. The same resource bought from the north or the
153
+ * south table, or out of South Africa's storage pool, is a different source with its
154
+ * own cubes — so a decrement must give back a cube from the source that was clicked.
155
+ */
156
+ export interface BuySource {
157
+ resource: string;
158
+ side?: 'north' | 'south';
159
+ fromStorage?: boolean;
160
+ }
161
+
162
+ /** Stable key for a buy source, so counts can be looked up per market row or track. */
163
+ export function buySourceKey(source: BuySource): string {
164
+ return `${source.resource}|${source.side || ''}|${source.fromStorage ? 'storage' : ''}`;
165
+ }
166
+
167
+ function isBuyFrom(move: Move, source: BuySource): boolean {
168
+ const data = move.data as BuySource | undefined;
169
+
170
+ return (
171
+ move.name === MoveName.BuyResource &&
172
+ !!data &&
173
+ data.resource === source.resource &&
174
+ (data.side || '') === (source.side || '') &&
175
+ !!data.fromStorage === !!source.fromStorage
176
+ );
177
+ }
178
+
179
+ /**
180
+ * How many cubes of each source the buffer would give back, keyed by `buySourceKey`.
181
+ * The market draws one ghost per cube taken off it, so this is also how many of those
182
+ * ghosts can be clicked to take a purchase back.
183
+ */
184
+ export function bufferedBuyCounts(turnMoves: Move[]): Record<string, number> {
185
+ const counts: Record<string, number> = {};
186
+
187
+ for (const move of turnMoves) {
188
+ if (move.name !== MoveName.BuyResource || !move.data) {
189
+ continue;
190
+ }
191
+
192
+ const key = buySourceKey(move.data as BuySource);
193
+ counts[key] = (counts[key] || 0) + 1;
194
+ }
195
+
196
+ return counts;
197
+ }
198
+
199
+ /**
200
+ * Index of the purchase a decrement click should remove: the LAST buffered buy from
201
+ * that source. Cubes leave a track cheapest-first, so the last buy is the one the
202
+ * market hands back — and removing a purchase only ever frees money and plant capacity,
203
+ * so the rest of the buffer always replays (verified over 653 mid-buffer removals).
204
+ * Returns -1 when the buffer holds no purchase from that source.
205
+ */
206
+ export function lastBuyIndex(turnMoves: Move[], source: BuySource): number {
207
+ for (let i = turnMoves.length - 1; i >= 0; i--) {
208
+ if (isBuyFrom(turnMoves[i], source)) {
209
+ return i;
210
+ }
211
+ }
212
+
213
+ return -1;
214
+ }
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M14.601 21.5c0 1.38-1.116 2.5-2.499 2.5-1.378 0-2.499-1.12-2.499-2.5s1.121-2.5 2.499-2.5c1.383 0 2.499 1.119 2.499 2.5zm-2.42-21.5c-4.029 0-7.06 2.693-7.06 8h3.955c0-2.304.906-4.189 3.024-4.189 1.247 0 2.57.828 2.684 2.411.123 1.666-.767 2.511-1.892 3.582-2.924 2.78-2.816 4.049-2.816 7.196h3.943c0-1.452-.157-2.508 1.838-4.659 1.331-1.436 2.986-3.222 3.021-5.943.047-3.963-2.751-6.398-6.697-6.398z"/></svg>
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M24 12c0 6.627-5.373 12-12 12s-12-5.373-12-12h2c0 5.514 4.486 10 10 10s10-4.486 10-10-4.486-10-10-10c-2.777 0-5.287 1.141-7.099 2.977l2.061 2.061-6.962 1.354 1.305-7.013 2.179 2.18c2.172-2.196 5.182-3.559 8.516-3.559 6.627 0 12 5.373 12 12zm-13-6v8h7v-2h-5v-6h-2z"/></svg>
@@ -1,3 +0,0 @@
1
- <svg width="40" height="40" xmlns="http://www.w3.org/2000/svg">
2
- <path d="M0 0 L20 20 L0 40 Z M20 0 L40 20 L20 40 Z" />
3
- </svg>
@@ -1,28 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="475.452px" height="475.451px" viewBox="0 0 475.452 475.451">
2
- <g>
3
- <path d="M468.083,118.385c-3.99-5.33-9.61-9.419-16.854-12.275c0.387,6.665-0.086,12.09-1.42,16.281l-85.65,281.789
4
- c-1.526,4.948-4.859,8.897-9.992,11.848c-5.141,2.953-10.469,4.428-15.989,4.428H74.66c-22.84,0-36.542-6.652-41.112-19.985
5
- c-1.903-5.14-1.807-9.229,0.288-12.275c2.092-2.857,5.708-4.288,10.85-4.288h248.102c17.702,0,29.93-3.285,36.688-9.852
6
- c6.763-6.567,13.565-21.177,20.413-43.824l78.228-258.669c4.186-14.084,2.474-26.457-5.141-37.113s-18.462-15.987-32.548-15.987
7
- H173.163c-2.474,0-7.329,0.854-14.562,2.568l0.284-0.859c-5.33-1.14-9.851-1.662-13.562-1.571
8
- c-3.71,0.099-7.137,1.192-10.277,3.289c-3.14,2.094-5.664,4.328-7.566,6.706c-1.903,2.38-3.761,5.426-5.568,9.136
9
- c-1.805,3.715-3.33,7.142-4.567,10.282c-1.237,3.14-2.666,6.473-4.281,9.998c-1.62,3.521-3.186,6.423-4.71,8.706
10
- c-1.143,1.523-2.758,3.521-4.854,5.996c-2.091,2.474-3.805,4.664-5.137,6.567c-1.331,1.903-2.19,3.616-2.568,5.14
11
- c-0.378,1.711-0.19,4.233,0.571,7.566c0.76,3.328,1.047,5.753,0.854,7.277c-0.76,7.232-3.378,16.414-7.849,27.552
12
- c-4.471,11.136-8.52,19.18-12.135,24.126c-0.761,0.95-2.853,3.092-6.28,6.424c-3.427,3.33-5.52,6.23-6.279,8.704
13
- c-0.762,0.951-0.81,3.617-0.144,7.994c0.666,4.38,0.907,7.423,0.715,9.136c-0.765,6.473-3.14,15.037-7.139,25.697
14
- c-3.999,10.657-7.994,19.414-11.993,26.265c-0.569,1.141-2.185,3.328-4.853,6.567c-2.662,3.237-4.283,5.902-4.853,7.99
15
- c-0.38,1.523-0.33,4.188,0.144,7.994c0.473,3.806,0.426,6.66-0.144,8.562c-1.521,7.228-4.377,15.94-8.565,26.125
16
- c-4.187,10.178-8.47,18.896-12.851,26.121c-1.138,1.906-2.712,4.145-4.708,6.711c-1.999,2.566-3.568,4.805-4.711,6.707
17
- c-1.141,1.903-1.903,3.901-2.284,5.996c-0.19,1.143,0.098,2.998,0.859,5.571c0.76,2.566,1.047,4.612,0.854,6.14
18
- c-0.192,2.662-0.57,6.187-1.141,10.567c-0.572,4.373-0.859,6.939-0.859,7.699c-4.187,11.424-3.999,23.511,0.572,36.269
19
- c5.33,14.838,14.797,27.36,28.406,37.541c13.61,10.185,27.74,15.27,42.398,15.27h263.521c12.367,0,24.026-4.141,34.971-12.416
20
- c10.944-8.281,18.227-18.507,21.837-30.696l78.511-258.662C477.412,141.51,475.701,129.234,468.083,118.385z M164.31,118.956
21
- l5.997-18.274c0.76-2.474,2.329-4.615,4.709-6.423c2.38-1.805,4.808-2.712,7.282-2.712h173.589c2.663,0,4.565,0.903,5.708,2.712
22
- c1.14,1.809,1.335,3.949,0.575,6.423l-6.002,18.274c-0.764,2.475-2.327,4.611-4.713,6.424c-2.382,1.805-4.805,2.708-7.278,2.708
23
- H170.593c-2.666,0-4.568-0.9-5.711-2.708C163.74,123.567,163.55,121.431,164.31,118.956z M140.615,192.045l5.996-18.271
24
- c0.76-2.474,2.331-4.615,4.709-6.423c2.38-1.809,4.805-2.712,7.282-2.712h173.583c2.666,0,4.572,0.9,5.712,2.712
25
- c1.14,1.809,1.331,3.949,0.568,6.423l-5.996,18.271c-0.759,2.474-2.33,4.617-4.708,6.423c-2.383,1.809-4.805,2.712-7.283,2.712
26
- H146.895c-2.664,0-4.567-0.9-5.708-2.712C140.043,196.662,139.854,194.519,140.615,192.045z"/>
27
- </g>
28
- </svg>
@@ -1,3 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
2
- <path d="M5 17h-5v-10h5v10zm2-10v10l9 5v-20l-9 5zm15.324 4.993l1.646-1.659-1.324-1.324-1.651 1.67-1.665-1.648-1.316 1.318 1.67 1.657-1.65 1.669 1.318 1.317 1.658-1.672 1.666 1.653 1.324-1.325-1.676-1.656z"/>
3
- </svg>
@@ -1,3 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
2
- <path d="M5 17h-5v-10h5v10zm2-10v10l9 5v-20l-9 5zm17 4h-5v2h5v-2zm-1.584-6.232l-4.332 2.5 1 1.732 4.332-2.5-1-1.732zm1 12.732l-4.332-2.5-1 1.732 4.332 2.5 1-1.732z"/>
3
- </svg>
@@ -1,3 +0,0 @@
1
- <svg width="497.25px" height="497.25px" viewBox="0 0 497.25 497.25" xmlns="http://www.w3.org/2000/svg">
2
- <path fill="black" d="M248.625,89.25 V0 l-127.5,127.5 l127.5,127.5 V140.25 c84.15,0,153,68.85,153,153 c0,84.15-68.85,153-153,153 c-84.15,0-153-68.85-153-153 h-51 c0,112.2,91.8,204,204,204 s204-91.8,204-204 S360.825,89.25,248.625,89.25z"/>
3
- </svg>
Binary file