@civ-clone/core-treasury 0.1.6 → 0.1.8
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.
|
@@ -6,9 +6,34 @@ jobs:
|
|
|
6
6
|
build:
|
|
7
7
|
runs-on: ubuntu-latest
|
|
8
8
|
steps:
|
|
9
|
-
- uses: actions/checkout@
|
|
10
|
-
- uses:
|
|
9
|
+
- uses: actions/checkout@v4
|
|
10
|
+
- uses: pnpm/action-setup@v4
|
|
11
11
|
with:
|
|
12
|
-
|
|
13
|
-
-
|
|
14
|
-
|
|
12
|
+
version: 11
|
|
13
|
+
- uses: actions/setup-node@v4
|
|
14
|
+
with:
|
|
15
|
+
node-version: 22
|
|
16
|
+
# No lock file, so this tests what a consumer resolves. The settings match
|
|
17
|
+
# web-renderer's pnpm-workspace.yaml: several @civ-clone packages depend on
|
|
18
|
+
# each other as `github:` URLs, this project's own releases are often only
|
|
19
|
+
# minutes old, and nothing here needs a dependency's build script.
|
|
20
|
+
- run: >-
|
|
21
|
+
pnpm install --no-frozen-lockfile
|
|
22
|
+
--config.block-exotic-subdeps=false
|
|
23
|
+
--config.minimum-release-age=0
|
|
24
|
+
--config.strict-dep-builds=false
|
|
25
|
+
- run: npm run --if-present prettier:check
|
|
26
|
+
# The tests load the committed `.js`, so compile to prove the source
|
|
27
|
+
# type-checks, and fail if the committed output is stale. `--force`
|
|
28
|
+
# because a fresh checkout's timestamps can make `tsc --build` treat the
|
|
29
|
+
# committed output as up to date and skip the compile entirely.
|
|
30
|
+
- run: npm run --if-present ts:compile -- --force
|
|
31
|
+
- run: npm run --if-present prettier:format
|
|
32
|
+
# Stale *or never committed* output fails: `git diff` alone misses a new
|
|
33
|
+
# module whose `.js` was never added, which the published package would
|
|
34
|
+
# then omit. Only the compiled extensions, so the lock file the install
|
|
35
|
+
# writes cannot fail it; `node_modules` is ignored, so it stays out.
|
|
36
|
+
- run: |
|
|
37
|
+
git status --short --untracked-files=all -- '*.js' '*.d.ts' '*.js.map'
|
|
38
|
+
test -z "$(git status --porcelain --untracked-files=all -- '*.js' '*.d.ts' '*.js.map')"
|
|
39
|
+
- run: npm run --if-present test
|
package/PlayerTreasury.js
CHANGED
|
@@ -25,8 +25,11 @@ class PlayerTreasury extends DataObject_1.default {
|
|
|
25
25
|
this._value += value;
|
|
26
26
|
}
|
|
27
27
|
buy(city) {
|
|
28
|
-
const cityBuild = this._cityBuildRegistry.getByCity(city), [spendCost] = this.cost(city).filter((spendCost) => spendCost.resource() === this._yield), cost = spendCost.value();
|
|
29
|
-
|
|
28
|
+
const cityBuild = this._cityBuildRegistry.getByCity(city), [spendCost] = this.cost(city).filter((spendCost) => spendCost.resource() === this._yield), cost = spendCost === null || spendCost === void 0 ? void 0 : spendCost.value();
|
|
29
|
+
// Nothing to buy when the rules give no price in this treasury's yield.
|
|
30
|
+
if (cost === undefined ||
|
|
31
|
+
city.player() !== this._player ||
|
|
32
|
+
this.value() < cost) {
|
|
30
33
|
return;
|
|
31
34
|
}
|
|
32
35
|
this._ruleRegistry.process(Rush_1.default, cityBuild, spendCost);
|
|
@@ -47,7 +50,7 @@ class PlayerTreasury extends DataObject_1.default {
|
|
|
47
50
|
}
|
|
48
51
|
subtract(value) {
|
|
49
52
|
if (value instanceof Yield_1.default) {
|
|
50
|
-
this.
|
|
53
|
+
this.subtract(value.value());
|
|
51
54
|
return;
|
|
52
55
|
}
|
|
53
56
|
this._value -= value;
|
package/PlayerTreasury.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlayerTreasury.js","sourceRoot":"","sources":["PlayerTreasury.ts"],"names":[],"mappings":";;;AAAA,oFAGsD;AACtD,oEAG2C;AAE3C,uEAAgE;AAEhE,yCAAkC;AAElC,uDAAgD;AAChD,uCAAgC;AAYhC,MAAa,cAAe,SAAQ,oBAAU;IAQ5C,YACE,MAAc,EACd,SAAuB,EACvB,oBAAuC,4BAAyB,EAChE,eAA6B,uBAAoB;QAEjD,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AAO,CAAC,eAAK,EAAE,SAAS,CAAC,CAAC;IACtD,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,GAAG,CAAC,KAAqB;QACvB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAEhB,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClB,CAAC;IAED,QAAQ,CAAC,KAAqB;QAC5B,IAAI,KAAK,YAAY,eAAK,EAAE;YAC1B,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;YAE7B,OAAO;SACR;QAED,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC;IACvB,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;;AAzFH,wCA0FC;AAzFiB,wBAAS,GAAG,CAAC,oBAAoB,EAAE,eAAe,CAAC,CAAC;AA2FtE,kBAAe,cAAc,CAAC"}
|
package/PlayerTreasury.ts
CHANGED
|
@@ -63,9 +63,14 @@ export class PlayerTreasury extends DataObject implements IPlayerTreasury {
|
|
|
63
63
|
[spendCost] = this.cost(city).filter(
|
|
64
64
|
(spendCost) => spendCost.resource() === this._yield
|
|
65
65
|
),
|
|
66
|
-
cost = spendCost
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
cost = spendCost?.value();
|
|
67
|
+
|
|
68
|
+
// Nothing to buy when the rules give no price in this treasury's yield.
|
|
69
|
+
if (
|
|
70
|
+
cost === undefined ||
|
|
71
|
+
city.player() !== this._player ||
|
|
72
|
+
this.value() < cost
|
|
73
|
+
) {
|
|
69
74
|
return;
|
|
70
75
|
}
|
|
71
76
|
|
|
@@ -94,7 +99,7 @@ export class PlayerTreasury extends DataObject implements IPlayerTreasury {
|
|
|
94
99
|
|
|
95
100
|
subtract(value: Yield | number): void {
|
|
96
101
|
if (value instanceof Yield) {
|
|
97
|
-
this.
|
|
102
|
+
this.subtract(value.value());
|
|
98
103
|
|
|
99
104
|
return;
|
|
100
105
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@civ-clone/core-treasury",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": "git@github.com:civ-clone/core-treasury.git",
|
|
6
6
|
"keywords": [
|
|
@@ -12,12 +12,21 @@
|
|
|
12
12
|
"private": false,
|
|
13
13
|
"scripts": {
|
|
14
14
|
"build": "npm run ts:compile && npm run prettier:format",
|
|
15
|
-
"prettier:check": "prettier --config .prettierrc '**/*.ts'",
|
|
15
|
+
"prettier:check": "prettier --config .prettierrc --check '**/*.ts'",
|
|
16
16
|
"prettier:format": "prettier --config .prettierrc '**/*.ts' --write",
|
|
17
|
+
"test": "ts-mocha ./tests/*.test.ts",
|
|
18
|
+
"test:coverage": "c8 ts-mocha ./tests/*.test.ts",
|
|
17
19
|
"ts:compile": "tsc --build tsconfig.json"
|
|
18
20
|
},
|
|
19
21
|
"devDependencies": {
|
|
22
|
+
"@types/chai": "^4.3.3",
|
|
23
|
+
"@types/mocha": "^9.0",
|
|
24
|
+
"@types/node": "^16.0",
|
|
25
|
+
"c8": "^7.3",
|
|
26
|
+
"chai": "^4.2",
|
|
27
|
+
"mocha": "^9.0",
|
|
20
28
|
"prettier": "^2.1.2",
|
|
29
|
+
"ts-mocha": "^9.0",
|
|
21
30
|
"typescript": "^4.0.3"
|
|
22
31
|
},
|
|
23
32
|
"dependencies": {
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import City from '@civ-clone/core-city/City';
|
|
2
|
+
import CityBuild from '@civ-clone/core-city-build/CityBuild';
|
|
3
|
+
import CityBuildRegistry from '@civ-clone/core-city-build/CityBuildRegistry';
|
|
4
|
+
import Player from '@civ-clone/core-player/Player';
|
|
5
|
+
import PlayerTreasury from '../PlayerTreasury';
|
|
6
|
+
import RuleRegistry from '@civ-clone/core-rule/RuleRegistry';
|
|
7
|
+
import Yield from '@civ-clone/core-yield/Yield';
|
|
8
|
+
import { expect } from 'chai';
|
|
9
|
+
|
|
10
|
+
describe('PlayerTreasury', () => {
|
|
11
|
+
const treasury = (value: number): PlayerTreasury => {
|
|
12
|
+
const playerTreasury = new PlayerTreasury(new Player(), Yield);
|
|
13
|
+
|
|
14
|
+
playerTreasury.set(value);
|
|
15
|
+
|
|
16
|
+
return playerTreasury;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
it('adds a number or a Yield', () => {
|
|
20
|
+
const fromNumber = treasury(50),
|
|
21
|
+
fromYield = treasury(50);
|
|
22
|
+
|
|
23
|
+
fromNumber.add(10);
|
|
24
|
+
fromYield.add(new Yield(10));
|
|
25
|
+
|
|
26
|
+
expect(fromNumber.value()).to.equal(60);
|
|
27
|
+
expect(fromYield.value()).to.equal(60);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('subtracts a number or a Yield', () => {
|
|
31
|
+
const fromNumber = treasury(50),
|
|
32
|
+
fromYield = treasury(50);
|
|
33
|
+
|
|
34
|
+
fromNumber.subtract(10);
|
|
35
|
+
fromYield.subtract(new Yield(10));
|
|
36
|
+
|
|
37
|
+
expect(fromNumber.value()).to.equal(40);
|
|
38
|
+
expect(fromYield.value()).to.equal(40);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('sets a number or a Yield', () => {
|
|
42
|
+
const fromNumber = treasury(50),
|
|
43
|
+
fromYield = treasury(50);
|
|
44
|
+
|
|
45
|
+
fromNumber.set(5);
|
|
46
|
+
fromYield.set(new Yield(5));
|
|
47
|
+
|
|
48
|
+
expect(fromNumber.value()).to.equal(5);
|
|
49
|
+
expect(fromYield.value()).to.equal(5);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("doesn't buy, or throw, when the rules give no price", () => {
|
|
53
|
+
const player = new Player(),
|
|
54
|
+
city = { player: () => player } as unknown as City;
|
|
55
|
+
let added = false;
|
|
56
|
+
|
|
57
|
+
const cityBuild = {
|
|
58
|
+
city: () => city,
|
|
59
|
+
remaining: () => 10,
|
|
60
|
+
add: () => (added = true),
|
|
61
|
+
} as unknown as CityBuild,
|
|
62
|
+
cityBuildRegistry = {
|
|
63
|
+
getByCity: () => cityBuild,
|
|
64
|
+
} as unknown as CityBuildRegistry,
|
|
65
|
+
playerTreasury = new PlayerTreasury(
|
|
66
|
+
player,
|
|
67
|
+
Yield,
|
|
68
|
+
cityBuildRegistry,
|
|
69
|
+
new RuleRegistry()
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
playerTreasury.set(100);
|
|
73
|
+
|
|
74
|
+
expect(() => playerTreasury.buy(city)).not.to.throw();
|
|
75
|
+
expect(playerTreasury.value()).to.equal(100);
|
|
76
|
+
expect(added).to.equal(false);
|
|
77
|
+
});
|
|
78
|
+
});
|