1
0
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:
Iku-turso 2023-03-14 13:21:45 +02:00
parent 0c7db307d6
commit be1fa6932a
2 changed files with 78 additions and 49 deletions

View File

@ -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 };
};

View File

@ -1,67 +1,63 @@
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 });
const playerWantsToAttack = await questionToPlayer("Attack the monster?"); return {
if (!playerWantsToAttack) { start: async () => {
messageToPlayer( messageToPlayer(`You encounter a monster with ${monster.hitPoints} hit-points`);
"You chose not to attack the monster, and the monster eats you dead, in disappointment.",
);
messageToPlayer("You lose. Game over!");
return;
}
while (true) {
messageToPlayer("You attack the monster.");
const dieResult = await castDie();
const playerLandsHitOnMonster = dieResult > 3;
if (playerLandsHitOnMonster) {
monsterHitPoints--;
if (!monsterHitPoints) {
messageToPlayer(
"You successfully land a final hit on the monster, and it is now properly beat.",
);
messageToPlayer("You win. Congratulations!");
return;
}
const playerWantsToAttack = await questionToPlayer("Attack the monster?");
if (!playerWantsToAttack) {
messageToPlayer( messageToPlayer(
`You successfully land a hit on the monster, and it now only has ${monsterHitPoints} hit-points remaining.`, "You chose not to attack the monster, and the monster eats you dead, in disappointment.",
);
} else {
messageToPlayer(
`You fail to land a hit on the monster, and it still has ${monsterHitPoints} hit-points remaining.`,
);
}
const playerWantsToAttackAgain = await questionToPlayer("Do you want to attack again?");
if (!playerWantsToAttackAgain) {
messageToPlayer(
"You lose your nerve mid-beat-down, and try to run away. You get eaten by a sad, disappointed monster.",
); );
messageToPlayer("You lose. Game over!"); messageToPlayer("You lose. Game over!");
return; return;
} }
}
}, while (true) {
}); messageToPlayer("You attack the monster.");
const dieResult = await castDie();
const playerLandsHitOnMonster = dieResult > 3;
if (playerLandsHitOnMonster) {
const { monsterIsDead } = handleLandedHitOnMonster();
if (monsterIsDead) {
return;
}
} else {
messageToPlayer(
`You fail to land a hit on the monster, and it still has ${monster.hitPoints} hit-points remaining.`,
);
}
const playerWantsToAttackAgain = await questionToPlayer("Do you want to attack again?");
if (!playerWantsToAttackAgain) {
messageToPlayer(
"You lose your nerve mid-beat-down, and try to run away. You get eaten by a sad, disappointed monster.",
);
messageToPlayer("You lose. Game over!");
return;
}
}
},
};
};
export { createGame }; export { createGame };