ogamejs 4.0.0 → 4.0.2
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
CHANGED
|
@@ -366,13 +366,20 @@ Ogame.Trader.sellCrystal(10000); // → { deut, metal }
|
|
|
366
366
|
|
|
367
367
|
#### `parseRate(rate = '2:1.5:1', type = 'deut')`
|
|
368
368
|
|
|
369
|
-
Normalizes a rate relative to a reference resource (`'metal'`, `'crystal'` or `'deut'`). Throws if the rate is malformed.
|
|
369
|
+
Normalizes a rate relative to a reference resource (`'metal'`, `'crystal'` or `'deut'`). Throws if the rate is malformed — that includes a term that is not a number, but also a `0`, an empty one (`'2::1'`) and a negative one, none of which describe a trade.
|
|
370
370
|
|
|
371
371
|
```javascript
|
|
372
372
|
Ogame.Trader.parseRate('3:2:1', 'deut');
|
|
373
373
|
// → { rateMetal: 3, rateCrystal: 2, rateDeut: 1 }
|
|
374
374
|
```
|
|
375
375
|
|
|
376
|
+
The reference term always comes back as `1`, so a rate and any multiple of it are the same rate, whichever resource you sell:
|
|
377
|
+
|
|
378
|
+
```javascript
|
|
379
|
+
Ogame.Trader.parseRate('4:3:2', 'deut');
|
|
380
|
+
// → { rateMetal: 2, rateCrystal: 1.5, rateDeut: 1 } — same as '2:1.5:1'
|
|
381
|
+
```
|
|
382
|
+
|
|
376
383
|
### `Ogame.i18n`
|
|
377
384
|
|
|
378
385
|
Every model entry carries `names: { en, fr }`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ogamejs",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.2",
|
|
4
4
|
"description": "OGame formulas as a dependency-free ESM library: building and research costs, production, build times, fleet debris and marketplace rates.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
package/src/trades/parseRate.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import isUsableRate from './utils.js';
|
|
2
2
|
|
|
3
3
|
function extract(rate, type) {
|
|
4
4
|
let res = {};
|
|
@@ -20,9 +20,14 @@ function extract(rate, type) {
|
|
|
20
20
|
rateDeut: deut / crystal,
|
|
21
21
|
};
|
|
22
22
|
} else if (type === 'deut') {
|
|
23
|
+
// Like the two branches above: express the other terms in units of the one
|
|
24
|
+
// being sold, so its own term becomes 1. Reading `metal` and `crystal` raw
|
|
25
|
+
// here only happened to work because rates are usually written against 1
|
|
26
|
+
// deuterium — `4:3:2` is the same rate as `2:1.5:1`, but used to pay twice
|
|
27
|
+
// as much for the very same deuterium.
|
|
23
28
|
res = {
|
|
24
|
-
rateMetal: metal,
|
|
25
|
-
rateCrystal: crystal,
|
|
29
|
+
rateMetal: metal / deut,
|
|
30
|
+
rateCrystal: crystal / deut,
|
|
26
31
|
rateDeut: 1,
|
|
27
32
|
};
|
|
28
33
|
} else {
|
|
@@ -41,7 +46,10 @@ export default function parseRate(rate = '2:1.5:1', type = 'deut') {
|
|
|
41
46
|
|
|
42
47
|
const res = extract(rate, type);
|
|
43
48
|
|
|
44
|
-
|
|
49
|
+
// Checked after normalizing rather than on the raw terms: whatever makes a
|
|
50
|
+
// rate unusable — a term that is not a number, `0`, empty, negative — shows
|
|
51
|
+
// up here as a NaN, an Infinity, a 0 or a negative ratio.
|
|
52
|
+
if (!isUsableRate(Object.values(res))) {
|
|
45
53
|
throw new Error('rate not parsed correctly');
|
|
46
54
|
}
|
|
47
55
|
|
package/src/trades/utils.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return response;
|
|
1
|
+
// A normalized rate is usable when every term is a real, strictly positive
|
|
2
|
+
// number. Checking only for `NaN` let two kinds of nonsense through as if they
|
|
3
|
+
// were answers: `Infinity`, produced by dividing into a `0` term, and `0`
|
|
4
|
+
// itself, which is what an empty term (`'2::1'`) parses to. A negative worth is
|
|
5
|
+
// not something the game can express either.
|
|
6
|
+
export default function isUsableRate(values) {
|
|
7
|
+
return values.every((value) => Number.isFinite(value) && value > 0);
|
|
9
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseRate.d.ts","sourceRoot":"","sources":["../../src/trades/parseRate.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parseRate.d.ts","sourceRoot":"","sources":["../../src/trades/parseRate.js"],"names":[],"mappings":"AAuCA,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAAI,SAAY,EAAE,IAAI,SAAS,MAiBhE"}
|
package/types/trades/utils.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export default function
|
|
1
|
+
export default function isUsableRate(values: any): any;
|
|
2
2
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/trades/utils.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/trades/utils.js"],"names":[],"mappings":"AAKA,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,MAAM,KAAA,OAE1C"}
|