mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Refactor to extract a responsibility
Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
parent
0c7db307d6
commit
be1fa6932a
@ -0,0 +1,33 @@
|
|||||||
|
import type { Dependencies } from "./monster-beatdown";
|
||||||
|
|
||||||
|
export type Monster = {
|
||||||
|
hitPoints: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const handleLandedHitOnMonsterFor =
|
||||||
|
({
|
||||||
|
monster,
|
||||||
|
messageToPlayer,
|
||||||
|
}: {
|
||||||
|
monster: Monster;
|
||||||
|
messageToPlayer: Dependencies["messageToPlayer"];
|
||||||
|
}) =>
|
||||||
|
() => {
|
||||||
|
monster.hitPoints--;
|
||||||
|
|
||||||
|
if (monster.hitPoints) {
|
||||||
|
messageToPlayer(
|
||||||
|
`You successfully land a hit on the monster, and it now only has ${monster.hitPoints} hit-points remaining.`,
|
||||||
|
);
|
||||||
|
|
||||||
|
return { monsterIsDead: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
messageToPlayer(
|
||||||
|
"You successfully land a final hit on the monster, and it is now properly beat.",
|
||||||
|
);
|
||||||
|
|
||||||
|
messageToPlayer("You win. Congratulations!");
|
||||||
|
|
||||||
|
return { monsterIsDead: true };
|
||||||
|
};
|
||||||
@ -1,14 +1,19 @@
|
|||||||
|
import { handleLandedHitOnMonsterFor, Monster } from "./handle-landed-hit-on-monster-for";
|
||||||
|
|
||||||
export type Dependencies = {
|
export type Dependencies = {
|
||||||
messageToPlayer: (message: string) => void;
|
messageToPlayer: (message: string) => void;
|
||||||
questionToPlayer: (message: string) => Promise<boolean>;
|
questionToPlayer: (message: string) => Promise<boolean>;
|
||||||
castDie: () => Promise<number>;
|
castDie: () => Promise<number>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const createGame = ({ messageToPlayer, questionToPlayer, castDie }: Dependencies) => ({
|
const createGame = ({ messageToPlayer, questionToPlayer, castDie }: Dependencies) => {
|
||||||
start: async () => {
|
const monster: Monster = { hitPoints: 3 };
|
||||||
let monsterHitPoints = 3;
|
|
||||||
|
|
||||||
messageToPlayer(`You encounter a monster with ${monsterHitPoints} hit-points`);
|
const handleLandedHitOnMonster = handleLandedHitOnMonsterFor({ messageToPlayer, monster });
|
||||||
|
|
||||||
|
return {
|
||||||
|
start: async () => {
|
||||||
|
messageToPlayer(`You encounter a monster with ${monster.hitPoints} hit-points`);
|
||||||
|
|
||||||
const playerWantsToAttack = await questionToPlayer("Attack the monster?");
|
const playerWantsToAttack = await questionToPlayer("Attack the monster?");
|
||||||
if (!playerWantsToAttack) {
|
if (!playerWantsToAttack) {
|
||||||
@ -28,24 +33,13 @@ const createGame = ({ messageToPlayer, questionToPlayer, castDie }: Dependencies
|
|||||||
|
|
||||||
const playerLandsHitOnMonster = dieResult > 3;
|
const playerLandsHitOnMonster = dieResult > 3;
|
||||||
if (playerLandsHitOnMonster) {
|
if (playerLandsHitOnMonster) {
|
||||||
monsterHitPoints--;
|
const { monsterIsDead } = handleLandedHitOnMonster();
|
||||||
|
if (monsterIsDead) {
|
||||||
if (!monsterHitPoints) {
|
|
||||||
messageToPlayer(
|
|
||||||
"You successfully land a final hit on the monster, and it is now properly beat.",
|
|
||||||
);
|
|
||||||
|
|
||||||
messageToPlayer("You win. Congratulations!");
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
messageToPlayer(
|
|
||||||
`You successfully land a hit on the monster, and it now only has ${monsterHitPoints} hit-points remaining.`,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
messageToPlayer(
|
messageToPlayer(
|
||||||
`You fail to land a hit on the monster, and it still has ${monsterHitPoints} hit-points remaining.`,
|
`You fail to land a hit on the monster, and it still has ${monster.hitPoints} hit-points remaining.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +56,8 @@ const createGame = ({ messageToPlayer, questionToPlayer, castDie }: Dependencies
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export { createGame };
|
export { createGame };
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user