From 0a5986c10ec4515d1cbe4483edcb6e08d9db894d Mon Sep 17 00:00:00 2001 From: xangelo Date: Wed, 14 Jun 2023 04:53:46 -0400 Subject: [PATCH] infra: migrate equip/unequip to new event system --- src/events/equipping-items/server.ts | 103 +++++++++++++++++++++++++++ src/events/server.ts | 1 + src/server/api.ts | 73 +------------------ src/server/error.ts | 7 -- 4 files changed, 107 insertions(+), 77 deletions(-) create mode 100644 src/events/equipping-items/server.ts delete mode 100644 src/server/error.ts diff --git a/src/events/equipping-items/server.ts b/src/events/equipping-items/server.ts new file mode 100644 index 0000000..802f66b --- /dev/null +++ b/src/events/equipping-items/server.ts @@ -0,0 +1,103 @@ +import {SocketEvent} from "../../server/socket-event.server"; +import { Socket } from 'socket.io'; +import {EquipmentSlot} from '../../shared/inventory'; +import {getEquippedItems, getInventory, getInventoryItem} from '../../server/inventory'; +import { equip, unequip } from '../../server/equipment'; +import { logger } from '../../server/lib/logger'; +import {EquippedItemDetails} from '../../shared/equipped'; + +class EquipmentInSlotError extends Error { + code: number; + constructor() { + super('You already have something in that slot'); + this.code = 23505; + } +} + + +function calcAp(inventoryItem: EquippedItemDetails[], socket: Socket) { + const ap: Record = {}; + inventoryItem.forEach(item => { + if(item.is_equipped && item.type === 'ARMOUR') { + ap[item.equipment_slot] = { + currentAp: item.currentAp, + maxAp: item.maxAp + }; + } + }); + + socket.emit('calc:ap', {ap}); +} + +export const equipItem: SocketEvent = { + eventName: 'equip', + handler: async (api, data: {id: string, slot: EquipmentSlot}) => { + const inventoryItem = await getInventoryItem(api.player.id, data.id); + const equippedItems = await getEquippedItems(api.player.id); + let desiredSlot: EquipmentSlot = inventoryItem.equipment_slot; + + try { + // handes the situation where you're trying to equip an item + // that can be equipped to any hand + if(inventoryItem.equipment_slot === 'ANY_HAND') { + if(data.slot === 'LEFT_HAND' || data.slot === 'RIGHT_HAND') { + // get the players current equipment in that slot! + if(equippedItems.some(v => { + return v.equipment_slot === data.slot || v.equipment_slot === 'TWO_HANDED'; + })) { + throw new EquipmentInSlotError(); + } + else { + desiredSlot = data.slot; + } + } + } + + if(data.slot === 'TWO_HANDED') { + if(equippedItems.some(v => { + return v.equipment_slot === 'LEFT_HAND' || v.equipment_slot === 'RIGHT_HAND'; + })) { + throw new EquipmentInSlotError(); + } + } + + + await equip(api.player.id, inventoryItem, desiredSlot); + api.socket.emit('alert', { + type: 'success', + text: `You equipped your ${inventoryItem.name}` + }); + } + catch(e) { + if(e.code.toString() === '23505') { + api.socket.emit('alert', { + type: 'error', + text: 'You already have an item equipped in that slot' + }); + } + else { + logger.log(e); + } + } + + const inventory = await getInventory(api.player.id); + calcAp(inventory, api.socket); + api.socket.emit('inventory', { + inventory + }); + + } +} + +export const unequipItem: SocketEvent = { + eventName: 'unequip', + handler: async (api, data: {id: string}) => { + await unequip(api.player.id, data.id); + + const inventory = await getInventory(api.player.id); + calcAp(inventory, api.socket); + api.socket.emit('inventory', { + inventory + }); + } +} diff --git a/src/events/server.ts b/src/events/server.ts index c319f00..e71d0cc 100644 --- a/src/events/server.ts +++ b/src/events/server.ts @@ -1 +1,2 @@ export * from './profession-changing/server'; +export * from './equipping-items/server'; diff --git a/src/server/api.ts b/src/server/api.ts index 361f1a8..0e874cf 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -5,7 +5,6 @@ import express, {Request, Response} from 'express'; import http from 'http'; import { Server, Socket } from 'socket.io'; import { logger } from './lib/logger'; -import { EquipmentInSlotError } from './error'; import { loadPlayer, createPlayer, updatePlayer } from './player'; import * as _ from 'lodash'; import {broadcastMessage, Message} from '../shared/message'; @@ -13,10 +12,9 @@ import {expToLevel, maxHp, Player} from '../shared/player'; import { professionList } from '../shared/profession'; import {clearFight, createFight, getMonsterList, loadMonster, loadMonsterFromFight, saveFightState} from './monster'; import {FightRound} from '../shared/fight'; -import {addInventoryItem, deleteInventoryItem, getEquippedItems, getInventory, getInventoryItem, updateAp} from './inventory'; +import {addInventoryItem, deleteInventoryItem, getEquippedItems, getInventory, updateAp} from './inventory'; import {Monster, MonsterForList} from '../shared/monsters'; import {getShopItem, listShopItems} from './shopItem'; -import {equip, unequip} from './equipment'; import { v4 as uuid } from 'uuid'; import {EquippedItemDetails} from '../shared/equipped'; import {ArmourEquipmentSlot, EquipmentSlot} from '../shared/inventory'; @@ -103,6 +101,7 @@ io.on('connection', async socket => { calcAp(inventory, socket); setServerStats(); + io.emit('chathistory', chatHistory); socket.emit('chat', broadcastMessage('server', `${player.username} just logged in`)); @@ -173,7 +172,7 @@ io.on('connection', async socket => { else { text.push('Healing to full health will cost you 10G.'); } - text.push(`
`); + text.push(`
`); } else { text.push(`You seem like you're in pretty good health right now, not sure I can do anything...`); @@ -239,72 +238,6 @@ io.on('connection', async socket => { }); }); - socket.on('equip', async (data: {id: string, slot: EquipmentSlot }) => { - const inventoryItem = await getInventoryItem(player.id, data.id); - const equippedItems = await getEquippedItems(player.id); - let desiredSlot: EquipmentSlot = inventoryItem.equipment_slot; - - try { - // handes the situation where you're trying to equip an item - // that can be equipped to any hand - if(inventoryItem.equipment_slot === 'ANY_HAND') { - if(data.slot === 'LEFT_HAND' || data.slot === 'RIGHT_HAND') { - // get the players current equipment in that slot! - if(equippedItems.some(v => { - return v.equipment_slot === data.slot || v.equipment_slot === 'TWO_HANDED'; - })) { - throw new EquipmentInSlotError(); - } - else { - desiredSlot = data.slot; - } - } - } - - if(data.slot === 'TWO_HANDED') { - if(equippedItems.some(v => { - return v.equipment_slot === 'LEFT_HAND' || v.equipment_slot === 'RIGHT_HAND'; - })) { - throw new EquipmentInSlotError(); - } - } - - - await equip(player.id, inventoryItem, desiredSlot); - socket.emit('alert', { - type: 'success', - text: `You equipped your ${inventoryItem.name}` - }); - } - catch(e) { - if(e.code.toString() === '23505') { - socket.emit('alert', { - type: 'error', - text: 'You already have an item equipped in that slot' - }); - } - else { - logger.log(e); - } - } - - const inventory = await getInventory(player.id); - calcAp(inventory, socket); - socket.emit('inventory', { - inventory - }); - }); - - socket.on('unequip', async (data: {id: string }) => { - await unequip(player.id, data.id); - - const inventory = await getInventory(player.id); - calcAp(inventory, socket); - socket.emit('inventory', { - inventory - }); - }); - socket.on('fight', async (data: {action: 'attack' | 'cast' | 'flee', target: 'head' | 'body' | 'arms' | 'legs'}) => { const monster = await loadMonsterFromFight(player.id); const playerSkills = await getPlayerSkillsAsObject(player.id); diff --git a/src/server/error.ts b/src/server/error.ts deleted file mode 100644 index 87c2c22..0000000 --- a/src/server/error.ts +++ /dev/null @@ -1,7 +0,0 @@ -export class EquipmentInSlotError extends Error { - code: number; - constructor() { - super('You already have something in that slot'); - this.code = 23505; - } -} -- 2.25.1