1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Refactor for reduced nesting

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 12:43:59 +02:00
parent a966c29a2e
commit 0c7db307d6

View File

@ -11,54 +11,56 @@ const createGame = ({ messageToPlayer, questionToPlayer, castDie }: Dependencies
messageToPlayer(`You encounter a monster with ${monsterHitPoints} hit-points`); messageToPlayer(`You encounter a monster with ${monsterHitPoints} hit-points`);
const playerWantsToAttack = await questionToPlayer("Attack the monster?"); const playerWantsToAttack = await questionToPlayer("Attack the monster?");
if (playerWantsToAttack) { if (!playerWantsToAttack) {
while (true) { messageToPlayer(
messageToPlayer("You attack the monster."); "You chose not to attack the monster, and the monster eats you dead, in disappointment.",
);
const dieResult = await castDie(); messageToPlayer("You lose. Game over!");
const playerLandsHitOnMonster = dieResult > 3; return;
if (playerLandsHitOnMonster) { }
monsterHitPoints--;
if (!monsterHitPoints) { while (true) {
messageToPlayer( messageToPlayer("You attack the monster.");
"You successfully land a final hit on the monster, and it is now properly beat.",
);
messageToPlayer("You win. Congratulations!"); const dieResult = await castDie();
return; const playerLandsHitOnMonster = dieResult > 3;
} if (playerLandsHitOnMonster) {
monsterHitPoints--;
if (!monsterHitPoints) {
messageToPlayer( messageToPlayer(
`You successfully land a hit on the monster, and it now only has ${monsterHitPoints} hit-points remaining.`, "You successfully land a final hit on the monster, and it is now properly beat.",
);
} 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 win. Congratulations!");
return; return;
} }
messageToPlayer(
`You successfully land a hit on the monster, and it now only has ${monsterHitPoints} hit-points remaining.`,
);
} 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!");
return;
} }
} }
messageToPlayer(
"You chose not to attack the monster, and the monster eats you dead, in disappointment.",
);
messageToPlayer("You lose. Game over!");
}, },
}); });