From: xangelo Date: Sat, 26 Aug 2023 03:14:55 +0000 (-0400) Subject: feat: unequip items if they hit 0 ap in battle X-Git-Tag: v0.3.0~5 X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=bc9e05ffb61ee0a280789e6fce48a0b15bb7da97;p=risinglegends.git feat: unequip items if they hit 0 ap in battle --- 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) {