@civ-clone/base-city-happiness-status 0.0.0-stage → 0.1.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/.gitattributes +2 -0
- package/.github/workflows/build-on-push.yml +39 -0
- package/.prettierrc +4 -0
- package/LICENSE +21 -0
- package/README.md +20 -2
- package/lib/cityStatus.d.ts +50 -0
- package/lib/cityStatus.js +50 -0
- package/lib/cityStatus.js.map +1 -0
- package/lib/cityStatus.ts +85 -0
- package/lib/statusTurn.d.ts +70 -0
- package/lib/statusTurn.js +50 -0
- package/lib/statusTurn.js.map +1 -0
- package/lib/statusTurn.ts +142 -0
- package/package.json +36 -4
- package/tests/cityStatus.test.ts +109 -0
- package/tests/statusTurn.test.ts +187 -0
- package/tsconfig.json +16 -0
package/.gitattributes
ADDED
|
@@ -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
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,21 @@
|
|
|
1
|
-
#
|
|
1
|
+
# base-city-happiness-status
|
|
2
2
|
|
|
3
|
-
|
|
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
|
+
- `lib/statusTurn`: `cityStatusTurn(cities, options)` checks each city's `CivilDisorder` and `CelebrateLeader` rules
|
|
15
|
+
on one set of yields, emits `city:civil-disorder` and `city:leader-celebration`, and records or ends both statuses.
|
|
16
|
+
The ruleset passes its handlers and its own turns of the rule through hooks: `onDisorderContinues` (a second turn of
|
|
17
|
+
disorder, such as a Democracy falling), `onCelebrationChecked` (with whether the city celebrates again, for growth),
|
|
18
|
+
`forgetCelebration` (drop rather than end) and `celebrationSurvivesDisorder`.
|
|
19
|
+
|
|
20
|
+
A library: it registers no rules. See `civ1-city-happiness` for Civilization's civil disorder and "We Love the King
|
|
21
|
+
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
|
+
);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PendingEffect,
|
|
3
|
+
PendingEffectRegistry,
|
|
4
|
+
} from '@civ-clone/core-pending-effect';
|
|
5
|
+
import City from '@civ-clone/core-city/City';
|
|
6
|
+
import { Engine } from '@civ-clone/core-engine/Engine';
|
|
7
|
+
import { RuleRegistry } from '@civ-clone/core-rule/RuleRegistry';
|
|
8
|
+
import Yield from '@civ-clone/core-yield/Yield';
|
|
9
|
+
/** What a city's check found this turn, and what was recorded before it. */
|
|
10
|
+
export type CityStatusCheck = {
|
|
11
|
+
city: City;
|
|
12
|
+
/** The yields the city was judged on, worked out once for both checks. */
|
|
13
|
+
yields: Yield[];
|
|
14
|
+
/** Whether the city is in civil disorder now. */
|
|
15
|
+
disorder: boolean;
|
|
16
|
+
/** Whether the city celebrates now. */
|
|
17
|
+
celebrating: boolean;
|
|
18
|
+
/** The city's disorder as recorded before this turn's check, or `null`. */
|
|
19
|
+
recordedDisorder: PendingEffect | null;
|
|
20
|
+
/** The city's celebration as recorded before this turn's check, or `null`. */
|
|
21
|
+
recordedCelebration: PendingEffect | null;
|
|
22
|
+
};
|
|
23
|
+
/** How a ruleset keeps its cities' disorder and celebrations from one turn to the next. */
|
|
24
|
+
export type CityStatusTurn = {
|
|
25
|
+
/** The status `handler`s the ruleset records disorder and celebration under (see `cityStatus`). */
|
|
26
|
+
disorderHandler: string;
|
|
27
|
+
celebrationHandler: string;
|
|
28
|
+
pendingEffects: PendingEffectRegistry;
|
|
29
|
+
ruleRegistry: RuleRegistry;
|
|
30
|
+
engine: Engine;
|
|
31
|
+
/**
|
|
32
|
+
* Whether a city that celebrated before, and is in disorder now without celebrating, keeps its celebration (Civ1
|
|
33
|
+
* keeps it, without a word, until order returns); otherwise its celebration is ended or forgotten as usual.
|
|
34
|
+
*/
|
|
35
|
+
celebrationSurvivesDisorder?: boolean;
|
|
36
|
+
/** Called for a city in disorder for a second turn running (or more), before its status is updated. */
|
|
37
|
+
onDisorderContinues?: (check: CityStatusCheck) => void;
|
|
38
|
+
/**
|
|
39
|
+
* Called for each city whose celebration is updated, before it is, with whether it celebrates for a second turn
|
|
40
|
+
* running (or more): what a ruleset grows a city for.
|
|
41
|
+
*/
|
|
42
|
+
onCelebrationChecked?: (check: CityStatusCheck, again: boolean) => void;
|
|
43
|
+
/**
|
|
44
|
+
* Whether a celebration that has stopped is dropped without its end being told (no event), rather than ended.
|
|
45
|
+
* Ended, by default.
|
|
46
|
+
*/
|
|
47
|
+
forgetCelebration?: (check: CityStatusCheck) => boolean;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Checks each of `cities` for civil disorder and for celebrating, at the start of their player's turn, emitting
|
|
51
|
+
* `city:civil-disorder` for each city in disorder, and records the statuses that start and ends those that stop
|
|
52
|
+
* (`updateCityStatus`), emitting `city:leader-celebration` as a celebration starts. The handlers' events (registered
|
|
53
|
+
* with `registerCityStatusHandler`) tell of order restored and celebrations ended. The cities are taken in order, and a
|
|
54
|
+
* hook may change the player's government part-way through them.
|
|
55
|
+
*/
|
|
56
|
+
export declare const cityStatusTurn: (
|
|
57
|
+
cities: City[],
|
|
58
|
+
{
|
|
59
|
+
disorderHandler,
|
|
60
|
+
celebrationHandler,
|
|
61
|
+
pendingEffects,
|
|
62
|
+
ruleRegistry,
|
|
63
|
+
engine,
|
|
64
|
+
celebrationSurvivesDisorder,
|
|
65
|
+
onDisorderContinues,
|
|
66
|
+
onCelebrationChecked,
|
|
67
|
+
forgetCelebration,
|
|
68
|
+
}: CityStatusTurn
|
|
69
|
+
) => void;
|
|
70
|
+
export default cityStatusTurn;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cityStatusTurn = void 0;
|
|
4
|
+
const cityStatus_1 = require("./cityStatus");
|
|
5
|
+
const CelebrateLeader_1 = require("@civ-clone/core-city-happiness/Rules/CelebrateLeader");
|
|
6
|
+
const CivilDisorder_1 = require("@civ-clone/core-city-happiness/Rules/CivilDisorder");
|
|
7
|
+
/**
|
|
8
|
+
* Checks each of `cities` for civil disorder and for celebrating, at the start of their player's turn, emitting
|
|
9
|
+
* `city:civil-disorder` for each city in disorder, and records the statuses that start and ends those that stop
|
|
10
|
+
* (`updateCityStatus`), emitting `city:leader-celebration` as a celebration starts. The handlers' events (registered
|
|
11
|
+
* with `registerCityStatusHandler`) tell of order restored and celebrations ended. The cities are taken in order, and a
|
|
12
|
+
* hook may change the player's government part-way through them.
|
|
13
|
+
*/
|
|
14
|
+
const cityStatusTurn = (cities, { disorderHandler, celebrationHandler, pendingEffects, ruleRegistry, engine, celebrationSurvivesDisorder = false, onDisorderContinues = () => { }, onCelebrationChecked = () => { }, forgetCelebration = () => false, }) => cities.forEach((city) => {
|
|
15
|
+
// Both rules read the same yields, and working them out isn't cheap (civ-clone/web-renderer#315).
|
|
16
|
+
const yields = city.yields(), check = {
|
|
17
|
+
city,
|
|
18
|
+
yields,
|
|
19
|
+
disorder: ruleRegistry
|
|
20
|
+
.process(CivilDisorder_1.default, city, yields)
|
|
21
|
+
.some((result) => result),
|
|
22
|
+
celebrating: ruleRegistry
|
|
23
|
+
.process(CelebrateLeader_1.default, city, yields)
|
|
24
|
+
.some((result) => result),
|
|
25
|
+
recordedDisorder: (0, cityStatus_1.cityStatus)(disorderHandler, city, pendingEffects),
|
|
26
|
+
recordedCelebration: (0, cityStatus_1.cityStatus)(celebrationHandler, city, pendingEffects),
|
|
27
|
+
};
|
|
28
|
+
if (check.disorder) {
|
|
29
|
+
engine.emit('city:civil-disorder', city);
|
|
30
|
+
}
|
|
31
|
+
if (check.disorder && check.recordedDisorder !== null) {
|
|
32
|
+
onDisorderContinues(check);
|
|
33
|
+
}
|
|
34
|
+
onCelebrationChecked(check, check.celebrating && check.recordedCelebration !== null);
|
|
35
|
+
// Records disorder that has started, and ends disorder that has stopped.
|
|
36
|
+
(0, cityStatus_1.updateCityStatus)(disorderHandler, city, check.disorder, check.recordedDisorder, pendingEffects);
|
|
37
|
+
if (celebrationSurvivesDisorder && check.disorder && !check.celebrating) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!check.celebrating &&
|
|
41
|
+
check.recordedCelebration !== null &&
|
|
42
|
+
forgetCelebration(check)) {
|
|
43
|
+
(0, cityStatus_1.forgetCityStatuses)(city, [celebrationHandler], pendingEffects);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
(0, cityStatus_1.updateCityStatus)(celebrationHandler, city, check.celebrating, check.recordedCelebration, pendingEffects, () => engine.emit('city:leader-celebration', city));
|
|
47
|
+
});
|
|
48
|
+
exports.cityStatusTurn = cityStatusTurn;
|
|
49
|
+
exports.default = exports.cityStatusTurn;
|
|
50
|
+
//# sourceMappingURL=statusTurn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"statusTurn.js","sourceRoot":"","sources":["statusTurn.ts"],"names":[],"mappings":";;;AAIA,6CAAgF;AAChF,0FAAmF;AAEnF,sFAA+E;AA+C/E;;;;;;GAMG;AACI,MAAM,cAAc,GAAG,CAC5B,MAAc,EACd,EACE,eAAe,EACf,kBAAkB,EAClB,cAAc,EACd,YAAY,EACZ,MAAM,EACN,2BAA2B,GAAG,KAAK,EACnC,mBAAmB,GAAG,GAAG,EAAE,GAAE,CAAC,EAC9B,oBAAoB,GAAG,GAAG,EAAE,GAAE,CAAC,EAC/B,iBAAiB,GAAG,GAAG,EAAE,CAAC,KAAK,GAChB,EACX,EAAE,CACR,MAAM,CAAC,OAAO,CAAC,CAAC,IAAU,EAAQ,EAAE;IAClC,kGAAkG;IAClG,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,EAC1B,KAAK,GAAoB;QACvB,IAAI;QACJ,MAAM;QACN,QAAQ,EAAE,YAAY;aACnB,OAAO,CAAC,uBAAa,EAAE,IAAI,EAAE,MAAM,CAAC;aACpC,IAAI,CAAC,CAAC,MAAe,EAAW,EAAE,CAAC,MAAM,CAAC;QAC7C,WAAW,EAAE,YAAY;aACtB,OAAO,CAAC,yBAAe,EAAE,IAAI,EAAE,MAAM,CAAC;aACtC,IAAI,CAAC,CAAC,MAAe,EAAW,EAAE,CAAC,MAAM,CAAC;QAC7C,gBAAgB,EAAE,IAAA,uBAAU,EAAC,eAAe,EAAE,IAAI,EAAE,cAAc,CAAC;QACnE,mBAAmB,EAAE,IAAA,uBAAU,EAC7B,kBAAkB,EAClB,IAAI,EACJ,cAAc,CACf;KACF,CAAC;IAEJ,IAAI,KAAK,CAAC,QAAQ,EAAE;QAClB,MAAM,CAAC,IAAI,CAAC,qBAAqB,EAAE,IAAI,CAAC,CAAC;KAC1C;IAED,IAAI,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC,gBAAgB,KAAK,IAAI,EAAE;QACrD,mBAAmB,CAAC,KAAK,CAAC,CAAC;KAC5B;IAED,oBAAoB,CAClB,KAAK,EACL,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,mBAAmB,KAAK,IAAI,CACxD,CAAC;IAEF,yEAAyE;IACzE,IAAA,6BAAgB,EACd,eAAe,EACf,IAAI,EACJ,KAAK,CAAC,QAAQ,EACd,KAAK,CAAC,gBAAgB,EACtB,cAAc,CACf,CAAC;IAEF,IAAI,2BAA2B,IAAI,KAAK,CAAC,QAAQ,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE;QACvE,OAAO;KACR;IAED,IACE,CAAC,KAAK,CAAC,WAAW;QAClB,KAAK,CAAC,mBAAmB,KAAK,IAAI;QAClC,iBAAiB,CAAC,KAAK,CAAC,EACxB;QACA,IAAA,+BAAkB,EAAC,IAAI,EAAE,CAAC,kBAAkB,CAAC,EAAE,cAAc,CAAC,CAAC;QAE/D,OAAO;KACR;IAED,IAAA,6BAAgB,EACd,kBAAkB,EAClB,IAAI,EACJ,KAAK,CAAC,WAAW,EACjB,KAAK,CAAC,mBAAmB,EACzB,cAAc,EACd,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,yBAAyB,EAAE,IAAI,CAAC,CACnD,CAAC;AACJ,CAAC,CAAC,CAAC;AA9EQ,QAAA,cAAc,kBA8EtB;AAEL,kBAAe,sBAAc,CAAC"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PendingEffect,
|
|
3
|
+
PendingEffectRegistry,
|
|
4
|
+
} from '@civ-clone/core-pending-effect';
|
|
5
|
+
import { cityStatus, forgetCityStatuses, updateCityStatus } from './cityStatus';
|
|
6
|
+
import CelebrateLeader from '@civ-clone/core-city-happiness/Rules/CelebrateLeader';
|
|
7
|
+
import City from '@civ-clone/core-city/City';
|
|
8
|
+
import CivilDisorder from '@civ-clone/core-city-happiness/Rules/CivilDisorder';
|
|
9
|
+
import { Engine } from '@civ-clone/core-engine/Engine';
|
|
10
|
+
import { RuleRegistry } from '@civ-clone/core-rule/RuleRegistry';
|
|
11
|
+
import Yield from '@civ-clone/core-yield/Yield';
|
|
12
|
+
|
|
13
|
+
/** What a city's check found this turn, and what was recorded before it. */
|
|
14
|
+
export type CityStatusCheck = {
|
|
15
|
+
city: City;
|
|
16
|
+
/** The yields the city was judged on, worked out once for both checks. */
|
|
17
|
+
yields: Yield[];
|
|
18
|
+
/** Whether the city is in civil disorder now. */
|
|
19
|
+
disorder: boolean;
|
|
20
|
+
/** Whether the city celebrates now. */
|
|
21
|
+
celebrating: boolean;
|
|
22
|
+
/** The city's disorder as recorded before this turn's check, or `null`. */
|
|
23
|
+
recordedDisorder: PendingEffect | null;
|
|
24
|
+
/** The city's celebration as recorded before this turn's check, or `null`. */
|
|
25
|
+
recordedCelebration: PendingEffect | null;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** How a ruleset keeps its cities' disorder and celebrations from one turn to the next. */
|
|
29
|
+
export type CityStatusTurn = {
|
|
30
|
+
/** The status `handler`s the ruleset records disorder and celebration under (see `cityStatus`). */
|
|
31
|
+
disorderHandler: string;
|
|
32
|
+
celebrationHandler: string;
|
|
33
|
+
pendingEffects: PendingEffectRegistry;
|
|
34
|
+
ruleRegistry: RuleRegistry;
|
|
35
|
+
engine: Engine;
|
|
36
|
+
/**
|
|
37
|
+
* Whether a city that celebrated before, and is in disorder now without celebrating, keeps its celebration (Civ1
|
|
38
|
+
* keeps it, without a word, until order returns); otherwise its celebration is ended or forgotten as usual.
|
|
39
|
+
*/
|
|
40
|
+
celebrationSurvivesDisorder?: boolean;
|
|
41
|
+
/** Called for a city in disorder for a second turn running (or more), before its status is updated. */
|
|
42
|
+
onDisorderContinues?: (check: CityStatusCheck) => void;
|
|
43
|
+
/**
|
|
44
|
+
* Called for each city whose celebration is updated, before it is, with whether it celebrates for a second turn
|
|
45
|
+
* running (or more): what a ruleset grows a city for.
|
|
46
|
+
*/
|
|
47
|
+
onCelebrationChecked?: (check: CityStatusCheck, again: boolean) => void;
|
|
48
|
+
/**
|
|
49
|
+
* Whether a celebration that has stopped is dropped without its end being told (no event), rather than ended.
|
|
50
|
+
* Ended, by default.
|
|
51
|
+
*/
|
|
52
|
+
forgetCelebration?: (check: CityStatusCheck) => boolean;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Checks each of `cities` for civil disorder and for celebrating, at the start of their player's turn, emitting
|
|
57
|
+
* `city:civil-disorder` for each city in disorder, and records the statuses that start and ends those that stop
|
|
58
|
+
* (`updateCityStatus`), emitting `city:leader-celebration` as a celebration starts. The handlers' events (registered
|
|
59
|
+
* with `registerCityStatusHandler`) tell of order restored and celebrations ended. The cities are taken in order, and a
|
|
60
|
+
* hook may change the player's government part-way through them.
|
|
61
|
+
*/
|
|
62
|
+
export const cityStatusTurn = (
|
|
63
|
+
cities: City[],
|
|
64
|
+
{
|
|
65
|
+
disorderHandler,
|
|
66
|
+
celebrationHandler,
|
|
67
|
+
pendingEffects,
|
|
68
|
+
ruleRegistry,
|
|
69
|
+
engine,
|
|
70
|
+
celebrationSurvivesDisorder = false,
|
|
71
|
+
onDisorderContinues = () => {},
|
|
72
|
+
onCelebrationChecked = () => {},
|
|
73
|
+
forgetCelebration = () => false,
|
|
74
|
+
}: CityStatusTurn
|
|
75
|
+
): void =>
|
|
76
|
+
cities.forEach((city: City): void => {
|
|
77
|
+
// Both rules read the same yields, and working them out isn't cheap (civ-clone/web-renderer#315).
|
|
78
|
+
const yields = city.yields(),
|
|
79
|
+
check: CityStatusCheck = {
|
|
80
|
+
city,
|
|
81
|
+
yields,
|
|
82
|
+
disorder: ruleRegistry
|
|
83
|
+
.process(CivilDisorder, city, yields)
|
|
84
|
+
.some((result: boolean): boolean => result),
|
|
85
|
+
celebrating: ruleRegistry
|
|
86
|
+
.process(CelebrateLeader, city, yields)
|
|
87
|
+
.some((result: boolean): boolean => result),
|
|
88
|
+
recordedDisorder: cityStatus(disorderHandler, city, pendingEffects),
|
|
89
|
+
recordedCelebration: cityStatus(
|
|
90
|
+
celebrationHandler,
|
|
91
|
+
city,
|
|
92
|
+
pendingEffects
|
|
93
|
+
),
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
if (check.disorder) {
|
|
97
|
+
engine.emit('city:civil-disorder', city);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (check.disorder && check.recordedDisorder !== null) {
|
|
101
|
+
onDisorderContinues(check);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
onCelebrationChecked(
|
|
105
|
+
check,
|
|
106
|
+
check.celebrating && check.recordedCelebration !== null
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// Records disorder that has started, and ends disorder that has stopped.
|
|
110
|
+
updateCityStatus(
|
|
111
|
+
disorderHandler,
|
|
112
|
+
city,
|
|
113
|
+
check.disorder,
|
|
114
|
+
check.recordedDisorder,
|
|
115
|
+
pendingEffects
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
if (celebrationSurvivesDisorder && check.disorder && !check.celebrating) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (
|
|
123
|
+
!check.celebrating &&
|
|
124
|
+
check.recordedCelebration !== null &&
|
|
125
|
+
forgetCelebration(check)
|
|
126
|
+
) {
|
|
127
|
+
forgetCityStatuses(city, [celebrationHandler], pendingEffects);
|
|
128
|
+
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
updateCityStatus(
|
|
133
|
+
celebrationHandler,
|
|
134
|
+
city,
|
|
135
|
+
check.celebrating,
|
|
136
|
+
check.recordedCelebration,
|
|
137
|
+
pendingEffects,
|
|
138
|
+
() => engine.emit('city:leader-celebration', city)
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
export default cityStatusTurn;
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@civ-clone/base-city-happiness-status",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
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-city-happiness": "^0.1.2",
|
|
33
|
+
"@civ-clone/core-engine": "^0.1.6",
|
|
34
|
+
"@civ-clone/core-pending-effect": "^0.1.2",
|
|
35
|
+
"@civ-clone/core-rule": "^0.1.9",
|
|
36
|
+
"@civ-clone/core-yield": "^0.1.6"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { CityStatusCheck, cityStatusTurn } from '../lib/statusTurn';
|
|
2
|
+
import { cityStatus, registerCityStatusHandler } from '../lib/cityStatus';
|
|
3
|
+
import CelebrateLeader from '@civ-clone/core-city-happiness/Rules/CelebrateLeader';
|
|
4
|
+
import City from '@civ-clone/core-city/City';
|
|
5
|
+
import CivilDisorder from '@civ-clone/core-city-happiness/Rules/CivilDisorder';
|
|
6
|
+
import Effect from '@civ-clone/core-rule/Effect';
|
|
7
|
+
import Engine from '@civ-clone/core-engine/Engine';
|
|
8
|
+
import { PendingEffectRegistry } from '@civ-clone/core-pending-effect';
|
|
9
|
+
import RuleRegistry from '@civ-clone/core-rule/RuleRegistry';
|
|
10
|
+
import { expect } from 'chai';
|
|
11
|
+
|
|
12
|
+
const UPSET = 'test:upset',
|
|
13
|
+
CHEERFUL = 'test:cheerful';
|
|
14
|
+
|
|
15
|
+
describe('cityStatusTurn', (): void => {
|
|
16
|
+
const setUp = () => {
|
|
17
|
+
const pendingEffects = new PendingEffectRegistry(),
|
|
18
|
+
ruleRegistry = new RuleRegistry(),
|
|
19
|
+
events: [string, unknown][] = [],
|
|
20
|
+
engine = {
|
|
21
|
+
emit: (event: string, target: unknown) => events.push([event, target]),
|
|
22
|
+
} as unknown as Engine,
|
|
23
|
+
// What each city's check finds this turn, by name.
|
|
24
|
+
state: { [name: string]: { disorder: boolean; celebrating: boolean } } =
|
|
25
|
+
{},
|
|
26
|
+
yieldsAsked: string[] = [],
|
|
27
|
+
city = (name: string) =>
|
|
28
|
+
({
|
|
29
|
+
name,
|
|
30
|
+
yields: () => {
|
|
31
|
+
yieldsAsked.push(name);
|
|
32
|
+
|
|
33
|
+
return [name];
|
|
34
|
+
},
|
|
35
|
+
} as unknown as City),
|
|
36
|
+
a = city('a'),
|
|
37
|
+
b = city('b');
|
|
38
|
+
|
|
39
|
+
ruleRegistry.register(
|
|
40
|
+
new CivilDisorder(
|
|
41
|
+
new Effect(
|
|
42
|
+
(city: City, yields: unknown[]): boolean =>
|
|
43
|
+
yields[0] === (city as any).name &&
|
|
44
|
+
state[(city as any).name].disorder
|
|
45
|
+
)
|
|
46
|
+
),
|
|
47
|
+
new CelebrateLeader(
|
|
48
|
+
new Effect(
|
|
49
|
+
(city: City, yields: unknown[]): boolean =>
|
|
50
|
+
yields[0] === (city as any).name &&
|
|
51
|
+
state[(city as any).name].celebrating
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
registerCityStatusHandler(
|
|
57
|
+
UPSET,
|
|
58
|
+
'city:order-restored',
|
|
59
|
+
pendingEffects,
|
|
60
|
+
engine
|
|
61
|
+
);
|
|
62
|
+
registerCityStatusHandler(
|
|
63
|
+
CHEERFUL,
|
|
64
|
+
'city:celebration-ended',
|
|
65
|
+
pendingEffects,
|
|
66
|
+
engine
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const turn = (extra: object = {}) =>
|
|
70
|
+
cityStatusTurn([a, b], {
|
|
71
|
+
disorderHandler: UPSET,
|
|
72
|
+
celebrationHandler: CHEERFUL,
|
|
73
|
+
pendingEffects,
|
|
74
|
+
ruleRegistry,
|
|
75
|
+
engine,
|
|
76
|
+
...extra,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return { pendingEffects, events, state, yieldsAsked, a, b, turn };
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
it('works out the yields once per city, records disorder and celebrations as they start and ends them as they stop', (): void => {
|
|
83
|
+
const { pendingEffects, events, state, yieldsAsked, a, b, turn } = setUp();
|
|
84
|
+
|
|
85
|
+
state.a = { disorder: true, celebrating: false };
|
|
86
|
+
state.b = { disorder: false, celebrating: true };
|
|
87
|
+
|
|
88
|
+
turn();
|
|
89
|
+
|
|
90
|
+
expect(yieldsAsked).eql(['a', 'b']);
|
|
91
|
+
expect(events).eql([
|
|
92
|
+
['city:civil-disorder', a],
|
|
93
|
+
['city:leader-celebration', b],
|
|
94
|
+
]);
|
|
95
|
+
expect(cityStatus(UPSET, a, pendingEffects)).not.null;
|
|
96
|
+
expect(cityStatus(CHEERFUL, b, pendingEffects)).not.null;
|
|
97
|
+
|
|
98
|
+
events.length = 0;
|
|
99
|
+
state.a = { disorder: false, celebrating: false };
|
|
100
|
+
state.b = { disorder: false, celebrating: false };
|
|
101
|
+
|
|
102
|
+
turn();
|
|
103
|
+
|
|
104
|
+
expect(events).eql([
|
|
105
|
+
['city:order-restored', a],
|
|
106
|
+
['city:celebration-ended', b],
|
|
107
|
+
]);
|
|
108
|
+
expect(pendingEffects.entries()).empty;
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('calls the hooks with the check, telling a second turn running from a first', (): void => {
|
|
112
|
+
const { state, a, b, turn } = setUp(),
|
|
113
|
+
continued: City[] = [],
|
|
114
|
+
checked: [City, boolean][] = [];
|
|
115
|
+
|
|
116
|
+
state.a = { disorder: true, celebrating: false };
|
|
117
|
+
state.b = { disorder: false, celebrating: true };
|
|
118
|
+
|
|
119
|
+
const hooks = {
|
|
120
|
+
onDisorderContinues: ({ city }: CityStatusCheck) => continued.push(city),
|
|
121
|
+
onCelebrationChecked: ({ city }: CityStatusCheck, again: boolean) =>
|
|
122
|
+
checked.push([city, again]),
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
turn(hooks);
|
|
126
|
+
turn(hooks);
|
|
127
|
+
|
|
128
|
+
expect(continued).eql([a]);
|
|
129
|
+
expect(checked).eql([
|
|
130
|
+
[a, false],
|
|
131
|
+
[b, false],
|
|
132
|
+
[a, false],
|
|
133
|
+
[b, true],
|
|
134
|
+
]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('keeps a celebration through disorder only when the ruleset says so', (): void => {
|
|
138
|
+
const { pendingEffects, events, state, a, turn } = setUp();
|
|
139
|
+
|
|
140
|
+
state.a = { disorder: false, celebrating: true };
|
|
141
|
+
state.b = { disorder: false, celebrating: false };
|
|
142
|
+
|
|
143
|
+
turn();
|
|
144
|
+
|
|
145
|
+
state.a = { disorder: true, celebrating: false };
|
|
146
|
+
events.length = 0;
|
|
147
|
+
|
|
148
|
+
turn({ celebrationSurvivesDisorder: true });
|
|
149
|
+
|
|
150
|
+
expect(cityStatus(CHEERFUL, a, pendingEffects)).not.null;
|
|
151
|
+
expect(events).eql([['city:civil-disorder', a]]);
|
|
152
|
+
|
|
153
|
+
turn();
|
|
154
|
+
|
|
155
|
+
expect(cityStatus(CHEERFUL, a, pendingEffects)).null;
|
|
156
|
+
expect(events).eql([
|
|
157
|
+
['city:civil-disorder', a],
|
|
158
|
+
['city:civil-disorder', a],
|
|
159
|
+
['city:celebration-ended', a],
|
|
160
|
+
]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('drops a celebration without a word when the ruleset forgets it', (): void => {
|
|
164
|
+
const { pendingEffects, events, state, a, turn } = setUp(),
|
|
165
|
+
asked: boolean[] = [];
|
|
166
|
+
|
|
167
|
+
state.a = { disorder: false, celebrating: true };
|
|
168
|
+
state.b = { disorder: false, celebrating: false };
|
|
169
|
+
|
|
170
|
+
turn();
|
|
171
|
+
|
|
172
|
+
state.a = { disorder: true, celebrating: false };
|
|
173
|
+
events.length = 0;
|
|
174
|
+
|
|
175
|
+
turn({
|
|
176
|
+
forgetCelebration: ({ disorder }: CityStatusCheck) => {
|
|
177
|
+
asked.push(disorder);
|
|
178
|
+
|
|
179
|
+
return disorder;
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
expect(asked).eql([true]);
|
|
184
|
+
expect(cityStatus(CHEERFUL, a, pendingEffects)).null;
|
|
185
|
+
expect(events).eql([['city:civil-disorder', a]]);
|
|
186
|
+
});
|
|
187
|
+
});
|
package/tsconfig.json
ADDED