From bc9e05ffb61ee0a280789e6fce48a0b15bb7da97 Mon Sep 17 00:00:00 2001 From: xangelo Date: Fri, 25 Aug 2023 23:14:55 -0400 Subject: [PATCH] feat: unequip items if they hit 0 ap in battle --- src/server/equipment.ts | 6 ++++++ src/server/fight.ts | 21 +++++++++++++++++---- src/server/inventory.ts | 13 +++++++++++-- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/server/equipment.ts b/src/server/equipment.ts index 9ebac7d..d1a73ba 100644 --- a/src/server/equipment.ts +++ b/src/server/equipment.ts @@ -40,3 +40,9 @@ export async function unequip(player_id: string, item_id: string) { item_id }).delete(); } + +export async function unequipItems(player_id: string, item_ids: string[]) { + return db('equipped').where({ + player_id + }).whereIn('item_id', item_ids).delete(); +} diff --git a/src/server/fight.ts b/src/server/fight.ts index 4f18e41..2c9383d 100644 --- a/src/server/fight.ts +++ b/src/server/fight.ts @@ -3,9 +3,9 @@ import { clearFight, loadMonster, getMonsterList, saveFightState, loadMonsterFro import { Player, expToLevel, maxHp, totalDefence, maxVigor } from '../shared/player'; import { clearTravelPlan } from './map'; import { updatePlayer } from './player'; -import { getEquippedItems, updateAp, deleteInventoryItem } from './inventory'; +import { getEquippedItems, updateAp } from './inventory'; import { EquippedItemDetails } from '../shared/equipped'; -import { ArmourEquipmentSlot, EquipmentSlot } from '../shared/inventory'; +import { EquipmentSlot } from '../shared/inventory'; import { MonsterWithFaction, MonsterForFight } from '../shared/monsters'; import { getPlayerSkillsAsObject, updatePlayerSkills } from './skills'; import { SkillID, Skills } from '../shared/skills'; @@ -209,9 +209,16 @@ export async function fightRound(player: Player, monster: MonsterWithFaction, d player.vigor = 0; } - await updateAp(player.id, 1, equippedItems.map(i => i.item_id)); + const unequippedItems = await updateAp(player.id, 1, equippedItems.map(i => i.item_id)); await clearFight(player.id); await updatePlayer(player); + + if(unequippedItems.length) { + unequippedItems.forEach(i => { + roundData.roundDetails.push(`Your ${i.name} was too damaged and was unequipped!`); + }); + } + return { roundData, monsters: potentialMonsters, player }; } @@ -239,10 +246,16 @@ export async function fightRound(player: Player, monster: MonsterWithFaction, d roundData.roundDetails.push(`You were killed by the ${monster.name}`); await clearFight(player.id); - await updateAp(player.id, 5, equippedItems.map(i => i.item_id)); + const unequippedItems = await updateAp(player.id, 5, equippedItems.map(i => i.item_id)); await updatePlayer(player); await clearTravelPlan(player.id); + if(unequippedItems.length) { + unequippedItems.forEach(i => { + roundData.roundDetails.push(`Your ${i.name} was too damaged and was unequipped!`); + }); + } + return { roundData, monsters: [], player}; } diff --git a/src/server/inventory.ts b/src/server/inventory.ts index 33c496f..71e5da0 100644 --- a/src/server/inventory.ts +++ b/src/server/inventory.ts @@ -2,6 +2,7 @@ import {InventoryItem, ShopEquipment} from "../shared/inventory"; import { v4 as uuid } from 'uuid'; import { db} from './lib/db'; import {EquippedItemDetails} from "../shared/equipped"; +import { unequipItems } from "./equipment"; export async function addInventoryItem(playerId: string, item: ShopEquipment) { @@ -78,11 +79,19 @@ export async function getEquippedItems(player_id: string): Promise i.currentAp <= 0); + + if(itemsToUnequip.length) { + await unequipItems(player_id, itemsToUnequip.map(i => i.item_id)); + } + + return itemsToUnequip; } export async function deleteInventoryItem(player_id: string, item_id: string) { -- 2.25.1