--- /dev/null
+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<any | EquipmentSlot, {currentAp: number, maxAp: number}> = {};
+ 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
+ });
+ }
+}
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';
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';
calcAp(inventory, socket);
setServerStats();
+
io.emit('chathistory', chatHistory);
socket.emit('chat', broadcastMessage('server', `${player.username} just logged in`));
else {
text.push('Healing to full health will cost you 10G.');
}
- text.push(`<br><button type="button" class="city-emit-event" data-event="city:service:healer:heal">Heal!</button>`);
+ text.push(`<br><button type="button" class="city-emit-event" data-event="city:service:healer:heal">heal!</button>`);
}
else {
text.push(`You seem like you're in pretty good health right now, not sure I can do anything...`);
});
});
- 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);