mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Make monster pool extendable instead of single hard-coded monster
Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
parent
287afa5483
commit
a38a06f916
@ -0,0 +1,10 @@
|
|||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import { sample } from "lodash/fp";
|
||||||
|
|
||||||
|
const getRandomSampleFromCollectionInjectable = getInjectable({
|
||||||
|
id: "get-random-sample-from-collection",
|
||||||
|
instantiate: () => sample,
|
||||||
|
causesSideEffects: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default getRandomSampleFromCollectionInjectable;
|
||||||
@ -12,7 +12,7 @@ const handleInitialMonsterEncounterInjectable = getInjectable({
|
|||||||
const monster = di.inject(monsterInjectable);
|
const monster = di.inject(monsterInjectable);
|
||||||
|
|
||||||
return async () => {
|
return async () => {
|
||||||
messageToPlayer(`You encounter a monster with ${monster.hitPoints} hit-points`);
|
messageToPlayer(`You encounter a monster "${monster.name}" with ${monster.hitPoints} hit-points`);
|
||||||
|
|
||||||
const playerWantsToAttack = await questionToPlayer("Attack the monster?");
|
const playerWantsToAttack = await questionToPlayer("Attack the monster?");
|
||||||
if (playerWantsToAttack) {
|
if (playerWantsToAttack) {
|
||||||
|
|||||||
@ -1,22 +1,28 @@
|
|||||||
import { Dependencies, gameInjectable } from "./monster-beatdown.injectable";
|
import { Dependencies, gameInjectable } from "./monster-beatdown.injectable";
|
||||||
import asyncFn, { AsyncFnMock } from "@async-fn/jest";
|
import asyncFn, { AsyncFnMock } from "@async-fn/jest";
|
||||||
import { getPromiseStatus } from "@k8slens/test-utils";
|
import { getPromiseStatus } from "@k8slens/test-utils";
|
||||||
import { createContainer } from "@ogre-tools/injectable";
|
import { createContainer, DiContainer, getInjectable } from "@ogre-tools/injectable";
|
||||||
import messageToPlayerInjectable from "./message-to-player.injectable";
|
import messageToPlayerInjectable from "./message-to-player.injectable";
|
||||||
import castDieInjectable from "./cast-die.injectable";
|
import castDieInjectable from "./cast-die.injectable";
|
||||||
import questionToPlayerInjectable from "./question-to-player.injectable";
|
import questionToPlayerInjectable from "./question-to-player.injectable";
|
||||||
import { registerFeature } from "@k8slens/feature-core";
|
import { registerFeature } from "@k8slens/feature-core";
|
||||||
import { gabrielFeature } from "./feature";
|
import { gabrielFeature } from "./feature";
|
||||||
|
import { monsterInjectionToken } from "./monster.injectable";
|
||||||
|
import { find } from "lodash/fp";
|
||||||
|
import getRandomSampleFromCollectionInjectable from "./get-random-sample-from-collection.injectable";
|
||||||
|
|
||||||
describe("monster-beatdown", () => {
|
describe("monster-beatdown, given there are monsters in the monster pool", () => {
|
||||||
let game: { start: () => Promise<void> };
|
let game: { start: () => Promise<void> };
|
||||||
let messageToPlayerMock: jest.Mock<Dependencies["messageToPlayer"]>;
|
let messageToPlayerMock: jest.Mock<Dependencies["messageToPlayer"]>;
|
||||||
let questionToPlayerMock: AsyncFnMock<Dependencies["questionToPlayer"]>;
|
let questionToPlayerMock: AsyncFnMock<Dependencies["questionToPlayer"]>;
|
||||||
let castDieMock: AsyncFnMock<Dependencies["castDie"]>;
|
let castDieMock: AsyncFnMock<Dependencies["castDie"]>;
|
||||||
let gamePromise: Promise<void>;
|
let gamePromise: Promise<void>;
|
||||||
|
let di: DiContainer;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const di = createContainer("monster-beatdown");
|
di = createContainer("monster-beatdown");
|
||||||
|
|
||||||
|
di.preventSideEffects();
|
||||||
|
|
||||||
registerFeature(di, gabrielFeature);
|
registerFeature(di, gabrielFeature);
|
||||||
|
|
||||||
@ -28,6 +34,15 @@ describe("monster-beatdown", () => {
|
|||||||
questionToPlayerMock = asyncFn();
|
questionToPlayerMock = asyncFn();
|
||||||
castDieMock = asyncFn();
|
castDieMock = asyncFn();
|
||||||
|
|
||||||
|
const someMonsterInjectable = getInjectable({
|
||||||
|
id: "some-janne-monster",
|
||||||
|
instantiate: () => ({ name: "Janne", hitPoints: 3 }),
|
||||||
|
injectionToken: monsterInjectionToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
di.register(someMonsterInjectable);
|
||||||
|
di.override(getRandomSampleFromCollectionInjectable, () => find({ name: "Janne" }));
|
||||||
|
|
||||||
game = di.inject(gameInjectable);
|
game = di.inject(gameInjectable);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -50,9 +65,9 @@ describe("monster-beatdown", () => {
|
|||||||
gamePromise = game.start();
|
gamePromise = game.start();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("player encounters a monster", () => {
|
it("player encounters one of the monsters", () => {
|
||||||
expect(messageToPlayerMock.mock.calls).toEqual([
|
expect(messageToPlayerMock.mock.calls).toEqual([
|
||||||
["You encounter a monster with 3 hit-points"],
|
['You encounter a monster "Janne" with 3 hit-points'],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,24 @@
|
|||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
|
||||||
|
import getRandomSampleFromCollectionInjectable from "./get-random-sample-from-collection.injectable";
|
||||||
|
|
||||||
|
export type Monster = {
|
||||||
|
name: string;
|
||||||
|
hitPoints: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const monsterInjectionToken = getInjectionToken<Monster>({ id: "monster-injection-token" });
|
||||||
|
|
||||||
const monsterInjectable = getInjectable({
|
const monsterInjectable = getInjectable({
|
||||||
id: "monster",
|
id: "monster",
|
||||||
instantiate: () => ({ hitPoints: 3 }),
|
|
||||||
|
instantiate: (di): Monster => {
|
||||||
|
const allMonsters = di.injectMany(monsterInjectionToken);
|
||||||
|
const getRandomSampleFromCollection = di.inject(getRandomSampleFromCollectionInjectable);
|
||||||
|
|
||||||
|
const randomMonster = getRandomSampleFromCollection(allMonsters);
|
||||||
|
|
||||||
|
return randomMonster!;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default monsterInjectable;
|
export default monsterInjectable;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user