feat: display equippable hand option only
authorxangelo <me@xangelo.ca>
Wed, 6 Sep 2023 17:25:27 +0000 (13:25 -0400)
committerxangelo <me@xangelo.ca>
Wed, 6 Sep 2023 17:25:27 +0000 (13:25 -0400)
When you are equipping items it will only show you the equip button for
the hand that you have available

src/server/views/inventory.ts

index 6026775d1c91152eb01586d7a2d5ecce36b64227..88e3c8c338342180b9e93d84d5b45adf2970d3af 100644 (file)
@@ -121,21 +121,30 @@ function renderInventoryItem(item: EquippedItemDetails , action: (item: Equipped
 }
 
 function renderInventorySection(inventory: EquippedItemDetails[]): string {
+  const used_slots = inventory.filter(i => i.is_equipped).map(i => i.equipment_slot);
+
   return inventory.map(item => {
     return renderInventoryItem(item, item => {
       if(item.is_equipped) {
         return `<button type="button" class="unequip-item red" hx-post="/player/unequip/${item.item_id}">Unequip</button>`;
       }
       else {
-        if(item.equipment_slot === 'ANY_HAND') {
-          return `<button type="button" class="equip-item" hx-post="/player/equip/${item.item_id}/LEFT_HAND">Equip L</button>
-<button type="button" class="equip-item" hx-post="/player/equip/${item.item_id}/RIGHT_HAND">Equip R</button>`;
-        }
-        else if(item.equipment_slot === 'LEFT_HAND') {
-          return `<button type="button" class="equip-item" hx-post="/player/equip/${item.item_id}/${item.equipment_slot}">Equip Left</button>`;
-        }
-        else if(item.equipment_slot === 'RIGHT_HAND') {
-          return `<button type="button" class="equip-item" hx-post="/player/equip/${item.item_id}/${item.equipment_slot}">Equip Right</button>`;
+        if(['ANY_HAND', 'LEFT_HAND', 'RIGHT_HAND'].includes(item.equipment_slot)) {
+          const str: string[] = [
+            `<button type="button" class="equip-item" hx-post="/player/equip/${item.item_id}/LEFT_HAND">Equip L</button>`,
+            `<button type="button" class="equip-item" hx-post="/player/equip/${item.item_id}/RIGHT_HAND">Equip R</button>`
+          ];
+
+          if(used_slots.includes('LEFT_HAND') && !used_slots.includes('RIGHT_HAND')) {
+            return str[1];
+          }
+          else if(used_slots.includes('RIGHT_HAND') && !used_slots.includes('LEFT_HAND')) {
+            return str[0];
+          }
+          else if(used_slots.includes('LEFT_HAND') && used_slots.includes('RIGHT_HAND')) {
+            return "";
+          }
+          return str.join("");
         }
         else {
           return `<button type="button" class="equip-item" hx-post="/player/equip/${item.item_id}/${item.equipment_slot}">Equip</button>`;