From 4bf369dfb7f5f3c0886870a0a34415db2fa3bf70 Mon Sep 17 00:00:00 2001 From: xangelo Date: Tue, 5 Sep 2023 10:55:30 -0400 Subject: [PATCH] chore: remove unused websocket handlers --- src/events/client.ts | 2 - src/events/equipping-items/server.ts | 108 -------------------- src/events/explore/server.ts | 26 ----- src/events/items/server.ts | 54 ---------- src/events/profession-changing/client.ts | 24 ----- src/events/profession-changing/server.ts | 107 -------------------- src/events/profession-changing/shared.ts | 1 - src/events/server.ts | 7 -- src/events/stat-points/server.ts | 36 ------- src/events/stores/server.ts | 30 ------ src/events/travel/client.ts | 18 ---- src/events/travel/server.ts | 123 ----------------------- src/events/travel/shared.ts | 16 --- 13 files changed, 552 deletions(-) delete mode 100644 src/events/client.ts delete mode 100644 src/events/equipping-items/server.ts delete mode 100644 src/events/explore/server.ts delete mode 100644 src/events/items/server.ts delete mode 100644 src/events/profession-changing/client.ts delete mode 100644 src/events/profession-changing/server.ts delete mode 100644 src/events/profession-changing/shared.ts delete mode 100644 src/events/server.ts delete mode 100644 src/events/stat-points/server.ts delete mode 100644 src/events/stores/server.ts delete mode 100644 src/events/travel/client.ts delete mode 100644 src/events/travel/server.ts delete mode 100644 src/events/travel/shared.ts diff --git a/src/events/client.ts b/src/events/client.ts deleted file mode 100644 index 4d0754f..0000000 --- a/src/events/client.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './profession-changing/client'; -export * from './travel/client'; diff --git a/src/events/equipping-items/server.ts b/src/events/equipping-items/server.ts deleted file mode 100644 index 7b00316..0000000 --- a/src/events/equipping-items/server.ts +++ /dev/null @@ -1,108 +0,0 @@ -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'; -import { getPlayersItems } from "../../server/items"; - -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); - const items = await getPlayersItems(api.player.id); - api.socket.emit('inventory', { - inventory, - items - }); - calcAp(inventory, api.socket); - - } -} - -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); - const items = await getPlayersItems(api.player.id); - api.socket.emit('inventory', { - inventory, - items - }); - } -} diff --git a/src/events/explore/server.ts b/src/events/explore/server.ts deleted file mode 100644 index ed12d9d..0000000 --- a/src/events/explore/server.ts +++ /dev/null @@ -1,26 +0,0 @@ -import {Monster, MonsterForList} from "../../shared/monsters"; -import {SocketEvent} from "../../server/socket-event.server"; -import {getMonsterList} from "../../server/monster"; - -export const exploreInCity: SocketEvent = { - eventName: 'city:explore', - handler: async (api, data: { args: string }) => { - // @TODO add check to make sure player is in this town and can actually explore this area - const locationId = parseInt(data.args); - - if(!locationId || isNaN(locationId)) { - return; - } - - const monsters: Monster[] = await getMonsterList(locationId); - let monsterList: MonsterForList[] = monsters.map(monster => { - return { - id: monster.id, - name: monster.name, - level: monster.level - } - }); - - api.socket.emit('explore:fights', monsterList); - } -} diff --git a/src/events/items/server.ts b/src/events/items/server.ts deleted file mode 100644 index 0c9023d..0000000 --- a/src/events/items/server.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { getItemFromPlayer, getPlayersItems, updateItemCount } from "../../server/items"; -import {API, SocketEvent} from "../../server/socket-event.server"; -import { maxHp } from "../../shared/player"; -import { updatePlayer } from "../../server/player"; -import { HealthPotionSmall } from "../../shared/items/health_potion"; -import { getInventory } from "../../server/inventory"; - -export const smallHeal: SocketEvent = { - eventName: 'item:use:heal_small', - handler: async (api: API, data: {args: string}): Promise => { - const itemId = parseInt(data.args); - const item = await getItemFromPlayer(api.player.id, itemId); - - if(!item) { - console.log(`Can't find item [${data.args}]`); - return; - } - - if(item.amount < 1) { - api.socket.emit('alert', { - type: 'error', - text: `You don't have enough ${item.name}` - }); - return; - } - - item.amount -= 1; - - const hpGain = HealthPotionSmall.effect(api.player); - - api.player.hp += hpGain; - - if(api.player.hp > maxHp(api.player.constitution, api.player.level)) { - api.player.hp = maxHp(api.player.constitution, api.player.level); - } - - await updateItemCount(api.player.id, item.item_id, -1); - await updatePlayer(api.player); - const inventory = await getInventory(api.player.id); - const items = await getPlayersItems(api.player.id); - - api.socket.emit('updatePlayer', api.player); - api.socket.emit('inventory', { - inventory, - items - }); - api.socket.emit('alert', { - type: 'success', - text: `You used the Small Health Potion` - }); - - return; - } -} diff --git a/src/events/profession-changing/client.ts b/src/events/profession-changing/client.ts deleted file mode 100644 index 5a045c6..0000000 --- a/src/events/profession-changing/client.ts +++ /dev/null @@ -1,24 +0,0 @@ -import {SocketEvent} from "../../client/socket-event.client"; -import $ from 'jquery'; -import { EVENT_NAME } from "./shared"; - -function generalTextReplacementHandler(api, data: { text: string }) { - $('#map').html(data.text); -} - -export const professionRecruiter: SocketEvent = new SocketEvent(EVENT_NAME, generalTextReplacementHandler); - -export const professionChangeWarrior: SocketEvent = new SocketEvent( - `${EVENT_NAME}:warrior`, - generalTextReplacementHandler -); - -export const professionChangeMage: SocketEvent = new SocketEvent( - `${EVENT_NAME}:mage`, - generalTextReplacementHandler -); - -export const professionChangeRogue: SocketEvent = new SocketEvent( - `${EVENT_NAME}:rogue`, - generalTextReplacementHandler -); diff --git a/src/events/profession-changing/server.ts b/src/events/profession-changing/server.ts deleted file mode 100644 index 60882e8..0000000 --- a/src/events/profession-changing/server.ts +++ /dev/null @@ -1,107 +0,0 @@ -import {API, SocketEvent} from "../../server/socket-event.server"; -import { EVENT_NAME } from './shared'; -import { changeProfession } from "../../server/player"; -import {Profession} from "shared/profession"; -import {broadcastMessage} from "../../shared/message"; - -const MIN_LEVEL = 25; - -export const professionRecruiter: SocketEvent = { - eventName: EVENT_NAME, - handler: (api, data): Promise => { - - let text: string = ''; - - if(api.player.profession === 'Wanderer') { - text = ` - Welcome to the Professional Services at Windcross! -

Our duty is to help Wanderers such as yourself become more than they are. By helping you achieve new levels in service of the King, we can ensure that the Kingdom of Khatis continues to grow!

-

You have 3 choices laid before you.

-

You could become a great and mighty Warrior! Wielding powerful swords and maces.

-

You could become a powerful Mage! Casting spells to rain fire upon our enemies.

-

You could become a lithe Rogue! Attacking our enemies swiftly when they least expect!

- `; - - if(api.player.level < MIN_LEVEL) { - text += `

Unfortunately.. you have to be at least level ${MIN_LEVEL} to take part in our training...

`; - } - else { - text += ` -

Be Careful! Once you change your profession, you will never again be a Wanderer

-
- - - -
- ` - } - } - else { - - let town = 'UNSET'; - let place = 'UNSETPLACE'; - switch(api.player.profession) { - case 'Warrior': - town = 'Stether'; - place = 'Highbreaker Inn' - break; - case 'Mage': - town = 'Davelfell'; - place = 'Mages Tower'; - break; - case 'Rogue': - town = 'Ferbalt Gap'; - place = 'Keepers Tavern'; - break; - } - - text = `

Welcome ${api.player.profession}!`; - text += `

Unfortunately I won't be of much help to you now that you are no longer a wanderer...

`; - text += `

However, you should visit the ${place} in ${town} that can probably provide some guidance!

`; - } - - - api.socket.emit('city:services:profession_recruitor_windcross', { - text - }); - - return; - } -}; - -async function generalFirstLevelProfessionChange(api: API, data: unknown, profession: Profession) { - console.log(`${api.player.username} is becoming a ${profession}!`); - const update = await changeProfession(api.player.id, profession); - - api.player.level = update.level; - api.player.exp = update.exp; - api.player.profession = profession; - - api.socket.emit('updatePlayer', api.player); - api.socket.emit(`${EVENT_NAME}:${profession.toLowerCase()}`, { - text: `Congratulations! You are now a ${profession}!` - }); - - api.io.emit('chat', broadcastMessage('server', `${api.player.username} is now a ${profession}`)); -} - -export const professionChangeWarrior: SocketEvent = { - eventName: `${EVENT_NAME}:warrior`, - handler: async (api, data) => { - generalFirstLevelProfessionChange(api, data, 'Warrior'); - } -} - -export const professionChangeMage: SocketEvent = { - eventName: `${EVENT_NAME}:mage`, - handler: async (api, data) => { - generalFirstLevelProfessionChange(api, data, 'Mage'); - } -} - -export const professionChangeRogue: SocketEvent = { - eventName: `${EVENT_NAME}:rogue`, - handler: async (api, data) => { - generalFirstLevelProfessionChange(api, data, 'Rogue'); - } -} diff --git a/src/events/profession-changing/shared.ts b/src/events/profession-changing/shared.ts deleted file mode 100644 index bbd9879..0000000 --- a/src/events/profession-changing/shared.ts +++ /dev/null @@ -1 +0,0 @@ -export const EVENT_NAME: string = 'city:services:profession_recruitor_windcross'; diff --git a/src/events/server.ts b/src/events/server.ts deleted file mode 100644 index 6f9811a..0000000 --- a/src/events/server.ts +++ /dev/null @@ -1,7 +0,0 @@ -export * from './profession-changing/server'; -export * from './equipping-items/server'; -export * from './travel/server'; -export * from './explore/server'; -export * from './stores/server'; -export * from './stat-points/server'; -export * from './items/server'; diff --git a/src/events/stat-points/server.ts b/src/events/stat-points/server.ts deleted file mode 100644 index 697959a..0000000 --- a/src/events/stat-points/server.ts +++ /dev/null @@ -1,36 +0,0 @@ -import {SocketEvent} from "../../server/socket-event.server"; -import { professionList } from "../../shared/profession"; -import { updatePlayer } from "../../server/player"; -import { Stat } from "../../shared/stats"; - -export const spendStatPoint: SocketEvent = { - eventName: 'spend-stat-point', - handler: async (api, data: { args: any }): Promise => { - - if(!Stat[data.args]) { - api.socket.emit('alert', { - type: 'error', - text: `Invalid stat type [${data.args}]` - }); - return; - } - - const statToIncrease: Stat = data.args as Stat; - const costToIncrease: number = professionList[api.player.profession].classStats.includes(statToIncrease) ? 1 : 2; - - if(api.player.stat_points < costToIncrease) { - api.socket.emit('alert', { - type: 'error', - text: 'You don\'t have enough stat points' - }); - } - else { - api.player[statToIncrease] += costToIncrease; - api.player.stat_points -= costToIncrease; - - await updatePlayer(api.player); - api.socket.emit('updatePlayer', api.player); - } - - } -} diff --git a/src/events/stores/server.ts b/src/events/stores/server.ts deleted file mode 100644 index 1acbd8c..0000000 --- a/src/events/stores/server.ts +++ /dev/null @@ -1,30 +0,0 @@ -import {SocketEvent} from "../../server/socket-event.server"; -import {listShopItems} from '../../server/shopEquipment'; -import { getShopItems } from "../../server/items"; -import { logger } from '../../server/lib/logger'; - -export const stores: SocketEvent = { - eventName: 'city:stores', - handler: async (api, data: {args: string}) => { - const storeId = parseInt(data.args); - - if(!storeId || isNaN(storeId)) { - logger.log(`Invalid store id: ${storeId}`); - } - - const [shopEquipemnt, shopItems] = await Promise.all([ - listShopItems({location_id: storeId}), - getShopItems(storeId) - ]); - - if(shopEquipemnt && shopEquipemnt.length) { - api.socket.emit('city:stores', { - equipment: shopEquipemnt, - items: shopItems - }); - } - else { - logger.log(`Insufficient shop items: ${shopEquipemnt.length}`); - } - } -} diff --git a/src/events/travel/client.ts b/src/events/travel/client.ts deleted file mode 100644 index df11e92..0000000 --- a/src/events/travel/client.ts +++ /dev/null @@ -1,18 +0,0 @@ -import {SocketEvent} from "../../client/socket-event.client"; -import $ from 'jquery'; -import { TravelDTO } from './shared'; - -export const travel: SocketEvent = new SocketEvent( - 'city:travel', - (api, data) => { - api.events.emit('renderTravel',[data as TravelDTO]); - } -); - -export const renderCity: SocketEvent = new SocketEvent( - 'city:display', - async (api, data) => { - api.cache.delete('currentMapHTML'); - api.events.emit('renderMap', [data]); - } -) diff --git a/src/events/travel/server.ts b/src/events/travel/server.ts deleted file mode 100644 index 26b81bd..0000000 --- a/src/events/travel/server.ts +++ /dev/null @@ -1,123 +0,0 @@ -import {movePlayer} from "../../server/player"; -import {getCityDetails, getAllServices, getAllPaths, travel, getTravelPlan, stepForward, completeTravel} from "../../server/map"; -import {SocketEvent} from "../../server/socket-event.server"; -import { sample, random } from 'lodash'; -import { getRandomMonster } from "../../server/monster"; -import { STEP_DELAY } from "./shared"; - -const MONSTER_ENCOUNTER_CHANCE = 30; - -export const explore: SocketEvent = { - eventName: 'city:travel', - handler: async (api, data: { args: string }) => { - if(api.player.hp <= 0) { - api.socket.emit('alert', { - type: 'error', - text: 'Sorry, you need some HP to start your travel' - }); - return; - } - - const destinationCity = parseInt(data.args); - - - if(!destinationCity || isNaN(destinationCity)) { - console.log(`Invalid destination city [${destinationCity}]`); - return; - } - - try { - const city = await getCityDetails(destinationCity); - - if(!city) { - console.log(`Invalid destination city [${destinationCity}]`); - return; - } - - console.log(`${api.player.username} attempting travel to ${city.name}`); - const travelPlan = await travel(api.player, city.id); - - api.socket.emit('city:travel', { - things: [], - nextAction: 0, - closestTown: api.player.city_id, - walkingText: '' - }); - } - catch(e) { - console.log(e); - } - } -} - -const walkingText: string[] = [ - 'You take a step forward', - 'You keep moving' -]; - -export const nextStep: SocketEvent = { - eventName: 'travel:step', - handler: async (api, data: { args: string }) => { - const stepTimerKey = `step:${api.player.id}`; - const travelPlan = await getTravelPlan(api.player.id); - - if(api.cache[stepTimerKey]) { - if(api.cache[stepTimerKey] > Date.now()) { - // clicked too fast - return; - } - } - - if(!travelPlan) { - return; - } - - travelPlan.current_position++; - if(travelPlan.current_position >= travelPlan.total_distance) { - const travel = await completeTravel(api.player.id); - - api.player.city_id = travel.destination_id; - await movePlayer(travel.destination_id, api.player.id); - - const [city, locations, paths] = await Promise.all([ - getCityDetails(travel.destination_id), - getAllServices(travel.destination_id), - getAllPaths(travel.destination_id) - ]); - - api.socket.emit('city:display', { - city, - locations, - paths - }); - - delete api.cache[stepTimerKey]; - } - else { - // update existing plan.. - // decide if they will run into anything - const travelPlan = await stepForward(api.player.id); - - const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id; - - const chanceToSeeMonster = random(0, 100); - const things: any[] = []; - if(chanceToSeeMonster < MONSTER_ENCOUNTER_CHANCE) { - const monster = await getRandomMonster([closest]); - things.push(monster); - } - - const nextAction = Date.now() + STEP_DELAY; - - api.cache[stepTimerKey] = nextAction; - - api.socket.emit('city:travel', { - things, - nextAction, - closestTown: closest, - walkingText: sample(walkingText) - }); - - } - } -}; diff --git a/src/events/travel/shared.ts b/src/events/travel/shared.ts deleted file mode 100644 index d6f2f10..0000000 --- a/src/events/travel/shared.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {City, Location, Path} from "../../shared/map" - -export type DTO = { - city: City, - locations: Location[], - paths: Path[] -} - -export type TravelDTO = { - things: any[], - nextAction: number; - walkingText: string, - closestTown: number; -} - -export const STEP_DELAY = 3000; -- 2.25.1