# Spot the bug - #164: Turn Combat Engine

**URL:** <https://forum.kirupa.com/t/spot-the-bug-164-turn-combat-engine/683570>\
**Category:** web dev\
**Created:** [October 4, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-164-turn-combat-engine/683570 "2026-10-04T07:00:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [October 4, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-164-turn-combat-engine/683570/1 "2026-10-04T07:00:08Z")

</div>

This dungeon turn processor is skipping the wrong monster state.

```js
function resolveTurn(entity, action, environment) {
  let actionLog = [];
  let statusEffect = entity.status ?? "normal";

  try {
    if (environment.hazard === "miasma" && !entity.immune) {
      throw new Error("suffocating");
    }

    switch (action.type) {
      case "attack":
        actionLog.push(`${entity.name} attacks for ${action.power} dmg`);
        break;
      case "cast":
        if (entity.mana < action.cost) {
          throw new Error("out of mana");
        }
        entity.mana -= action.cost;
        actionLog.push(`${entity.name} casts ${action.spell}`);
        break;
      case "defend":
        entity.guard = true;
        actionLog.push(`${entity.name} braces for impact`);
        break;
      default:
        throw new Error("invalid action");
    }
  } catch (err) {
    actionLog.push(`${entity.name} stumbled: ${err.message}`);
    return { success: false, log: actionLog, finalStatus: statusEffect };
  } finally {
    if (statusEffect === "poisoned") {
      entity.hp = Math.max(0, entity.hp - 5);
      actionLog.push(`${entity.name} takes poison damage`);
    }
    return { success: true, log: actionLog, finalStatus: statusEffect };
  }
}

```

Reply with what is broken and how you would fix it.
