refactor: simplify player damage to monster workflow
authorxangelo <git@xangelo.ca>
Thu, 13 Jul 2023 18:26:36 +0000 (14:26 -0400)
committerxangelo <git@xangelo.ca>
Thu, 13 Jul 2023 18:26:36 +0000 (14:26 -0400)
This majorly simplifies the player damage to monster damage workflow. It
also identified a couple bugs with how we were calculating damage.

src/server/api.ts

index 885e49b809b121e1e3a6c15f3c71f95bfc56c369..5123f19a040c69c408ef7a4e2bc34fee74584ef9 100644 (file)
@@ -321,77 +321,36 @@ io.on('connection', async socket => {
     const playerFinalHeal = Math.floor(boost.hp + hpHealAfterMasteries);
 
     roundData.roundDetails.push(`You targeted the monsters ${data.target.toUpperCase()} with ${attackType} damage!`);
-    if(data.target === 'arms') {
-      if(monster.armsAp > 0) {
-        monster.armsAp -= playerFinalDamage;
-
-        roundData.roundDetails.push(`You dealt ${playerFinalDamage} damage to their armour`);
-        if(monster.armsAp < 0) {
-
-          roundData.roundDetails.push(`You destroyed the ${monster.name}'s armour!'`);
-          roundData.roundDetails.push(`You dealt ${monster.armsAp * -1} damage to their HP`);
-          monster.hp += monster.armsAp;
-          monster.armsAp = 0;
-        }
-      }
-      else {
-        roundData.roundDetails.push(`You hit the ${monster.name} for ${playerFinalDamage} damage.`);
-        monster.hp -= playerFinalDamage;
-      }
-    }
-    else if (data.target === 'head') {
-      if(monster.helmAp > 0) {
-        monster.helmAp -= playerFinalDamage;
-
-        roundData.roundDetails.push(`You dealt ${playerFinalDamage} damage to their armour`);
-        if(monster.helmAp < 0) {
-
-          roundData.roundDetails.push(`You destroyed the ${monster.name}'s armour!'`);
-          roundData.roundDetails.push(`You dealt ${monster.armsAp * 1} damage to their HP`);
-          monster.hp += monster.helmAp;
-          monster.helmAp = 0;
-        }
-      }
-      else {
-        roundData.roundDetails.push(`You hit the ${monster.name} for ${playerFinalDamage} damage.`);
-        monster.hp -= playerFinalDamage;
-      }
+    let armourKey: string;
+    switch(data.target) {
+      case 'arms':
+        armourKey = 'armsAp';
+      break;
+      case 'head':
+        armourKey = 'helmAp';
+      break;
+      case 'legs':
+        armourKey = 'legsAp';
+      break;
+      case 'body':
+        armourKey = 'chestAp';
+      break;
     }
-    else if(data.target === 'legs') {
-      if(monster.legsAp > 0) {
-        monster.legsAp -= playerFinalDamage;
 
-        roundData.roundDetails.push(`You dealt ${playerFinalDamage} damage to their armour`);
-        if(monster.legsAp < 0) {
+    if(monster[armourKey] && monster[armourKey] > 0) {
+      monster[armourKey] -= playerFinalDamage;
 
-          roundData.roundDetails.push(`You destroyed the ${monster.name}'s armour!'`);
-          roundData.roundDetails.push(`You dealt ${monster.legsAp * 1} damage to their HP`);
-          monster.hp += monster.legsAp;
-          monster.legsAp = 0;
-        }
-      }
-      else {
-        roundData.roundDetails.push(`You hit the ${monster.name} for ${playerFinalDamage} damage.`);
-        monster.hp -= playerFinalDamage;
+      roundData.roundDetails.push(`You dealt ${playerFinalDamage} damage to their armour`);
+      if(monster[armourKey] < 0) {
+        roundData.roundDetails.push(`You destroyed the ${monster.name}'s armour!'`);
+        roundData.roundDetails.push(`You dealt ${monster[armourKey] * -1} damage to their HP`);
+        monster.hp += monster[armourKey];
+        monster[armourKey] = 0;
       }
     }
     else {
-      if(monster.chestAp > 0) {
-        monster.chestAp -= playerFinalDamage;
-
-        roundData.roundDetails.push(`You dealt ${playerFinalDamage} damage to their armour`);
-
-        if(monster.chestAp < 0) {
-          roundData.roundDetails.push(`You destroyed the ${monster.name}'s armour!'`);
-          roundData.roundDetails.push(`You dealt ${monster.chestAp * 1} damage to their HP`);
-          monster.hp += monster.chestAp;
-          monster.chestAp = 0;
-        }
-      }
-      else {
-        roundData.roundDetails.push(`You hit the ${monster.name} for ${playerFinalDamage} damage.`);
-        monster.hp -= playerFinalDamage;
-      }
+      roundData.roundDetails.push(`You hit the ${monster.name} for ${playerFinalDamage} damage.`);
+      monster.hp -= playerFinalDamage;
     }
 
     if(monster.hp <= 0) {