@civ-clone/base-city-happiness-status 0.0.0-stage → 0.1.0

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/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ **/*.js linguist-generated=true
2
+
@@ -0,0 +1,39 @@
1
+ name: Build and test application on push to remote
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v4
10
+ - uses: pnpm/action-setup@v4
11
+ with:
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/.prettierrc ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": true
4
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Dom Hastings
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,16 @@
1
- # Temporary Holding Version
1
+ # base-city-happiness-status
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Keeps a city's status, such as being in disorder or celebrating, from one turn to the next and in the save. Each status
4
+ is a `core-pending-effect` `PendingEffect` on the city, named by the ruleset's own handler id, so the save records it:
5
+
6
+ - `lib/cityStatus`:
7
+ - `cityStatus(handler, city, pendingEffects?)`: the city's recorded status, or `null`. Read before this turn's
8
+ update, it's the status carried over from the last turn (a second turn of disorder, a celebration that goes on).
9
+ - `updateCityStatus(handler, city, active, recorded, pendingEffects?, onStart?)`: records a status that has started
10
+ (calling `onStart` first) and ends one that has stopped.
11
+ - `registerCityStatusHandler(handler, event, pendingEffects?, engine?)`: emits `event` with the city when its status
12
+ ends. Register it when the rules are created, so a status loaded from a save can still end.
13
+ - `forgetCityStatuses(city, handlers, pendingEffects?)`: drops statuses without ending them, for a city that's gone.
14
+
15
+ A library: it registers no rules. See `civ1-city-happiness` for Civilization's civil disorder and "We Love the King
16
+ Day".
@@ -0,0 +1,50 @@
1
+ import { Engine } from '@civ-clone/core-engine/Engine';
2
+ import {
3
+ PendingEffect,
4
+ PendingEffectRegistry,
5
+ } from '@civ-clone/core-pending-effect';
6
+ import City from '@civ-clone/core-city/City';
7
+ /**
8
+ * A city's status (in disorder, celebrating and the like) is kept as a `PendingEffect` on the city, named by the
9
+ * ruleset's `handler`. Pending effects are saved, so a loaded game knows which cities already had a status, and a
10
+ * status recorded at the start of one turn is still there at the start of the next: that's how a ruleset tells a
11
+ * city's second turn of disorder, or of celebrating, from its first.
12
+ */
13
+ export declare const cityStatus: (
14
+ handler: string,
15
+ city: City,
16
+ pendingEffects?: PendingEffectRegistry
17
+ ) => PendingEffect | null;
18
+ /**
19
+ * Emits `event` with the city when its status `handler` ends. Register it whenever the rules are created, so a status
20
+ * loaded from a save can still end.
21
+ */
22
+ export declare const registerCityStatusHandler: (
23
+ handler: string,
24
+ event: string,
25
+ pendingEffects?: PendingEffectRegistry,
26
+ engine?: Engine
27
+ ) => void;
28
+ /**
29
+ * Records the city's status `handler` when it has started (`active` with nothing `recorded`), calling `onStart` first,
30
+ * and ends it when it has stopped (not `active` with one `recorded`), which emits the event its handler was registered
31
+ * with. `recorded` is the city's status as it was before this turn's check (`cityStatus`), so a ruleset can act on a
32
+ * status carried over from the last turn before it's updated.
33
+ */
34
+ export declare const updateCityStatus: (
35
+ handler: string,
36
+ city: City,
37
+ active: boolean,
38
+ recorded: PendingEffect | null,
39
+ pendingEffects?: PendingEffectRegistry,
40
+ onStart?: () => void
41
+ ) => void;
42
+ /**
43
+ * Drops the city's statuses named in `handlers` without ending them, so no event is emitted: for a city that's gone,
44
+ * which would otherwise stay in the save.
45
+ */
46
+ export declare const forgetCityStatuses: (
47
+ city: City,
48
+ handlers: string[],
49
+ pendingEffects?: PendingEffectRegistry
50
+ ) => void;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.forgetCityStatuses = exports.updateCityStatus = exports.registerCityStatusHandler = exports.cityStatus = void 0;
4
+ const Engine_1 = require("@civ-clone/core-engine/Engine");
5
+ const core_pending_effect_1 = require("@civ-clone/core-pending-effect");
6
+ /**
7
+ * A city's status (in disorder, celebrating and the like) is kept as a `PendingEffect` on the city, named by the
8
+ * ruleset's `handler`. Pending effects are saved, so a loaded game knows which cities already had a status, and a
9
+ * status recorded at the start of one turn is still there at the start of the next: that's how a ruleset tells a
10
+ * city's second turn of disorder, or of celebrating, from its first.
11
+ */
12
+ const cityStatus = (handler, city, pendingEffects = core_pending_effect_1.instance) => {
13
+ var _a;
14
+ return (_a = pendingEffects
15
+ .getByTarget(city)
16
+ .find((pendingEffect) => pendingEffect.handler() === handler)) !== null && _a !== void 0 ? _a : null;
17
+ };
18
+ exports.cityStatus = cityStatus;
19
+ /**
20
+ * Emits `event` with the city when its status `handler` ends. Register it whenever the rules are created, so a status
21
+ * loaded from a save can still end.
22
+ */
23
+ const registerCityStatusHandler = (handler, event, pendingEffects = core_pending_effect_1.instance, engine = Engine_1.instance) => pendingEffects.handler(handler, (pendingEffect) => engine.emit(event, pendingEffect.target()));
24
+ exports.registerCityStatusHandler = registerCityStatusHandler;
25
+ /**
26
+ * Records the city's status `handler` when it has started (`active` with nothing `recorded`), calling `onStart` first,
27
+ * and ends it when it has stopped (not `active` with one `recorded`), which emits the event its handler was registered
28
+ * with. `recorded` is the city's status as it was before this turn's check (`cityStatus`), so a ruleset can act on a
29
+ * status carried over from the last turn before it's updated.
30
+ */
31
+ const updateCityStatus = (handler, city, active, recorded, pendingEffects = core_pending_effect_1.instance, onStart = () => { }) => {
32
+ if (active && recorded === null) {
33
+ onStart();
34
+ pendingEffects.register(new core_pending_effect_1.PendingEffect(handler, city));
35
+ }
36
+ if (!active && recorded !== null) {
37
+ pendingEffects.discharge(recorded);
38
+ }
39
+ };
40
+ exports.updateCityStatus = updateCityStatus;
41
+ /**
42
+ * Drops the city's statuses named in `handlers` without ending them, so no event is emitted: for a city that's gone,
43
+ * which would otherwise stay in the save.
44
+ */
45
+ const forgetCityStatuses = (city, handlers, pendingEffects = core_pending_effect_1.instance) => pendingEffects
46
+ .getByTarget(city)
47
+ .filter((pendingEffect) => handlers.includes(pendingEffect.handler()))
48
+ .forEach((pendingEffect) => pendingEffects.unregister(pendingEffect));
49
+ exports.forgetCityStatuses = forgetCityStatuses;
50
+ //# sourceMappingURL=cityStatus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cityStatus.js","sourceRoot":"","sources":["cityStatus.ts"],"names":[],"mappings":";;;AAAA,0DAGuC;AACvC,wEAIwC;AAGxC;;;;;GAKG;AACI,MAAM,UAAU,GAAG,CACxB,OAAe,EACf,IAAU,EACV,iBAAwC,8BAA6B,EAC/C,EAAE;;IACxB,OAAA,MAAA,cAAc;SACX,WAAW,CAAC,IAAI,CAAC;SACjB,IAAI,CACH,CAAC,aAA4B,EAAW,EAAE,CACxC,aAAa,CAAC,OAAO,EAAE,KAAK,OAAO,CACtC,mCAAI,IAAI,CAAA;CAAA,CAAC;AAVD,QAAA,UAAU,cAUT;AAEd;;;GAGG;AACI,MAAM,yBAAyB,GAAG,CACvC,OAAe,EACf,KAAa,EACb,iBAAwC,8BAA6B,EACrE,SAAiB,iBAAc,EACzB,EAAE,CACR,cAAc,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,aAA4B,EAAE,EAAE,CAC/D,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,aAAa,CAAC,MAAM,EAAE,CAAC,CAC3C,CAAC;AARS,QAAA,yBAAyB,6BAQlC;AAEJ;;;;;GAKG;AACI,MAAM,gBAAgB,GAAG,CAC9B,OAAe,EACf,IAAU,EACV,MAAe,EACf,QAA8B,EAC9B,iBAAwC,8BAA6B,EACrE,UAAsB,GAAG,EAAE,GAAE,CAAC,EACxB,EAAE;IACR,IAAI,MAAM,IAAI,QAAQ,KAAK,IAAI,EAAE;QAC/B,OAAO,EAAE,CAAC;QAEV,cAAc,CAAC,QAAQ,CAAC,IAAI,mCAAa,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;KAC3D;IAED,IAAI,CAAC,MAAM,IAAI,QAAQ,KAAK,IAAI,EAAE;QAChC,cAAc,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;KACpC;AACH,CAAC,CAAC;AAjBW,QAAA,gBAAgB,oBAiB3B;AAEF;;;GAGG;AACI,MAAM,kBAAkB,GAAG,CAChC,IAAU,EACV,QAAkB,EAClB,iBAAwC,8BAA6B,EAC/D,EAAE,CACR,cAAc;KACX,WAAW,CAAC,IAAI,CAAC;KACjB,MAAM,CAAC,CAAC,aAA4B,EAAW,EAAE,CAChD,QAAQ,CAAC,QAAQ,CAAC,aAAa,CAAC,OAAO,EAAE,CAAC,CAC3C;KACA,OAAO,CAAC,CAAC,aAA4B,EAAQ,EAAE,CAC9C,cAAc,CAAC,UAAU,CAAC,aAAa,CAAC,CACzC,CAAC;AAZO,QAAA,kBAAkB,sBAYzB"}
@@ -0,0 +1,85 @@
1
+ import {
2
+ Engine,
3
+ instance as engineInstance,
4
+ } from '@civ-clone/core-engine/Engine';
5
+ import {
6
+ PendingEffect,
7
+ PendingEffectRegistry,
8
+ instance as pendingEffectRegistryInstance,
9
+ } from '@civ-clone/core-pending-effect';
10
+ import City from '@civ-clone/core-city/City';
11
+
12
+ /**
13
+ * A city's status (in disorder, celebrating and the like) is kept as a `PendingEffect` on the city, named by the
14
+ * ruleset's `handler`. Pending effects are saved, so a loaded game knows which cities already had a status, and a
15
+ * status recorded at the start of one turn is still there at the start of the next: that's how a ruleset tells a
16
+ * city's second turn of disorder, or of celebrating, from its first.
17
+ */
18
+ export const cityStatus = (
19
+ handler: string,
20
+ city: City,
21
+ pendingEffects: PendingEffectRegistry = pendingEffectRegistryInstance
22
+ ): PendingEffect | null =>
23
+ pendingEffects
24
+ .getByTarget(city)
25
+ .find(
26
+ (pendingEffect: PendingEffect): boolean =>
27
+ pendingEffect.handler() === handler
28
+ ) ?? null;
29
+
30
+ /**
31
+ * Emits `event` with the city when its status `handler` ends. Register it whenever the rules are created, so a status
32
+ * loaded from a save can still end.
33
+ */
34
+ export const registerCityStatusHandler = (
35
+ handler: string,
36
+ event: string,
37
+ pendingEffects: PendingEffectRegistry = pendingEffectRegistryInstance,
38
+ engine: Engine = engineInstance
39
+ ): void =>
40
+ pendingEffects.handler(handler, (pendingEffect: PendingEffect) =>
41
+ engine.emit(event, pendingEffect.target())
42
+ );
43
+
44
+ /**
45
+ * Records the city's status `handler` when it has started (`active` with nothing `recorded`), calling `onStart` first,
46
+ * and ends it when it has stopped (not `active` with one `recorded`), which emits the event its handler was registered
47
+ * with. `recorded` is the city's status as it was before this turn's check (`cityStatus`), so a ruleset can act on a
48
+ * status carried over from the last turn before it's updated.
49
+ */
50
+ export const updateCityStatus = (
51
+ handler: string,
52
+ city: City,
53
+ active: boolean,
54
+ recorded: PendingEffect | null,
55
+ pendingEffects: PendingEffectRegistry = pendingEffectRegistryInstance,
56
+ onStart: () => void = () => {}
57
+ ): void => {
58
+ if (active && recorded === null) {
59
+ onStart();
60
+
61
+ pendingEffects.register(new PendingEffect(handler, city));
62
+ }
63
+
64
+ if (!active && recorded !== null) {
65
+ pendingEffects.discharge(recorded);
66
+ }
67
+ };
68
+
69
+ /**
70
+ * Drops the city's statuses named in `handlers` without ending them, so no event is emitted: for a city that's gone,
71
+ * which would otherwise stay in the save.
72
+ */
73
+ export const forgetCityStatuses = (
74
+ city: City,
75
+ handlers: string[],
76
+ pendingEffects: PendingEffectRegistry = pendingEffectRegistryInstance
77
+ ): void =>
78
+ pendingEffects
79
+ .getByTarget(city)
80
+ .filter((pendingEffect: PendingEffect): boolean =>
81
+ handlers.includes(pendingEffect.handler())
82
+ )
83
+ .forEach((pendingEffect: PendingEffect): void =>
84
+ pendingEffects.unregister(pendingEffect)
85
+ );
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@civ-clone/base-city-happiness-status",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Keeps a city's status, such as disorder or celebrating, from one turn to the next and in the save.",
5
+ "repository": "git@github.com:civ-clone/base-city-happiness-status.git",
6
+ "keywords": [
7
+ "typescript",
8
+ "civilization"
9
+ ],
10
+ "author": "dom111 <dom111@users.noreply.github.com>",
11
+ "license": "MIT",
12
+ "private": false,
13
+ "scripts": {
14
+ "build": "npm run ts:compile && npm run prettier:format",
15
+ "prettier:check": "prettier --config .prettierrc --check '**/*.ts'",
16
+ "prettier:format": "prettier --config .prettierrc '**/*.ts' --write",
17
+ "test": "ts-mocha ./tests/*.test.ts",
18
+ "ts:compile": "tsc --build tsconfig.json"
19
+ },
20
+ "devDependencies": {
21
+ "@types/chai": "^4.0.0",
22
+ "@types/mocha": "^10.0.0",
23
+ "@types/node": "^16.0.0",
24
+ "chai": "^4.0.0",
25
+ "mocha": "^10.0.0",
26
+ "prettier": "^2.8.8",
27
+ "ts-mocha": "^10.0.0",
28
+ "typescript": "^4.0.0"
29
+ },
30
+ "dependencies": {
31
+ "@civ-clone/core-city": "^0.1.24",
32
+ "@civ-clone/core-engine": "^0.1.6",
33
+ "@civ-clone/core-pending-effect": "^0.1.2"
34
+ }
35
+ }
@@ -0,0 +1,109 @@
1
+ import {
2
+ PendingEffect,
3
+ PendingEffectRegistry,
4
+ } from '@civ-clone/core-pending-effect';
5
+ import {
6
+ cityStatus,
7
+ forgetCityStatuses,
8
+ registerCityStatusHandler,
9
+ updateCityStatus,
10
+ } from '../lib/cityStatus';
11
+ import City from '@civ-clone/core-city/City';
12
+ import Engine from '@civ-clone/core-engine/Engine';
13
+ import { expect } from 'chai';
14
+
15
+ const UPSET = 'test:upset',
16
+ CHEERFUL = 'test:cheerful';
17
+
18
+ describe('cityStatus', (): void => {
19
+ const setUp = () => {
20
+ const pendingEffects = new PendingEffectRegistry(),
21
+ events: [string, unknown][] = [],
22
+ engine = {
23
+ emit: (event: string, target: unknown) => events.push([event, target]),
24
+ } as unknown as Engine,
25
+ city = {} as City,
26
+ otherCity = {} as City;
27
+
28
+ registerCityStatusHandler(UPSET, 'city:calm', pendingEffects, engine);
29
+ registerCityStatusHandler(CHEERFUL, 'city:glum', pendingEffects, engine);
30
+
31
+ return { pendingEffects, events, city, otherCity };
32
+ };
33
+
34
+ it('has no status until one is recorded', (): void => {
35
+ const { pendingEffects, city } = setUp();
36
+
37
+ expect(cityStatus(UPSET, city, pendingEffects)).null;
38
+ });
39
+
40
+ it('records a status that has started, calling onStart first, and keeps it while it goes on', (): void => {
41
+ const { pendingEffects, events, city, otherCity } = setUp(),
42
+ started: number[] = [];
43
+
44
+ updateCityStatus(UPSET, city, true, null, pendingEffects, () =>
45
+ started.push(pendingEffects.entries().length)
46
+ );
47
+
48
+ const recorded = cityStatus(UPSET, city, pendingEffects);
49
+
50
+ expect(started).eql([0]);
51
+ expect(recorded?.handler()).equal(UPSET);
52
+ expect(recorded?.target()).equal(city);
53
+ expect(cityStatus(CHEERFUL, city, pendingEffects)).null;
54
+ expect(cityStatus(UPSET, otherCity, pendingEffects)).null;
55
+
56
+ // The next turn: still active, and the status recorded last turn is kept rather than recorded again.
57
+ updateCityStatus(UPSET, city, true, recorded, pendingEffects, () =>
58
+ started.push(-1)
59
+ );
60
+
61
+ expect(started).eql([0]);
62
+ expect(pendingEffects.getByTarget(city).length).equal(1);
63
+ expect(cityStatus(UPSET, city, pendingEffects)).equal(recorded);
64
+ expect(events).eql([]);
65
+ });
66
+
67
+ it('ends a status that has stopped, emitting its event', (): void => {
68
+ const { pendingEffects, events, city } = setUp();
69
+
70
+ updateCityStatus(UPSET, city, true, null, pendingEffects);
71
+ updateCityStatus(
72
+ UPSET,
73
+ city,
74
+ false,
75
+ cityStatus(UPSET, city, pendingEffects),
76
+ pendingEffects
77
+ );
78
+
79
+ expect(cityStatus(UPSET, city, pendingEffects)).null;
80
+ expect(events).eql([['city:calm', city]]);
81
+ });
82
+
83
+ it('does nothing for a status that is neither active nor recorded', (): void => {
84
+ const { pendingEffects, events, city } = setUp();
85
+
86
+ updateCityStatus(UPSET, city, false, null, pendingEffects);
87
+
88
+ expect(pendingEffects.entries()).eql([]);
89
+ expect(events).eql([]);
90
+ });
91
+
92
+ it('forgets only the named statuses of the city, without ending them', (): void => {
93
+ const { pendingEffects, events, city, otherCity } = setUp(),
94
+ unrelated = new PendingEffect('test:other', city);
95
+
96
+ pendingEffects.register(unrelated);
97
+ updateCityStatus(UPSET, city, true, null, pendingEffects);
98
+ updateCityStatus(CHEERFUL, city, true, null, pendingEffects);
99
+ updateCityStatus(UPSET, otherCity, true, null, pendingEffects);
100
+
101
+ forgetCityStatuses(city, [UPSET, CHEERFUL], pendingEffects);
102
+
103
+ expect(cityStatus(UPSET, city, pendingEffects)).null;
104
+ expect(cityStatus(CHEERFUL, city, pendingEffects)).null;
105
+ expect(cityStatus(UPSET, otherCity, pendingEffects)).not.null;
106
+ expect(pendingEffects.getByTarget(city)).eql([unrelated]);
107
+ expect(events).eql([]);
108
+ });
109
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "lib": [
5
+ "es2020"
6
+ ],
7
+ "module": "commonjs",
8
+ "sourceMap": true,
9
+ "strict": true,
10
+ "target": "ES2018"
11
+ },
12
+ "exclude": [
13
+ "node_modules",
14
+ "tests"
15
+ ]
16
+ }