From: xangelo Date: Wed, 6 Sep 2023 19:34:46 +0000 (-0400) Subject: feat: chat command to set player level X-Git-Tag: v0.3.6~1 X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=9ced47757e6f1dd2c74e67d0877ff3ce574ce5fb;p=risinglegends.git feat: chat command to set player level `/server set-level xxx` will reset the currently logged in players level and give them the right number of stat points for that level --- diff --git a/src/server/api.ts b/src/server/api.ts index a64809c..0aa2689 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -25,6 +25,7 @@ import { clearTravelPlan, completeTravel, getAllPaths, getAllServices, getCityDe import { signup, login, authEndpoint } from './auth'; import {db} from './lib/db'; import { getPlayerSkills} from './skills'; +import { handleChatCommands } from './chat-commands'; import { fightRound, blockPlayerInFight } from './fight'; @@ -45,9 +46,6 @@ import { renderTravel, travelButton } from './views/travel'; import { renderChatMessage } from './views/chat'; // TEMP! -import { createMonsters } from '../../seeds/monsters'; -import { createAllCitiesAndLocations } from '../../seeds/cities'; -import { createShopItems, createShopEquipment } from '../../seeds/shop_items'; import { Item, PlayerItem, ShopItem } from 'shared/items'; import { equip, unequip } from './equipment'; import { HealthPotionSmall } from '../shared/items/health_potion'; @@ -164,27 +162,11 @@ app.post('/chat', authEndpoint, async (req: Request, res: Response) => { return; } - let message: Message; if(msg.startsWith('/server') && req.player.permissions.includes('admin')) { try { - if(msg === '/server refresh-monsters') { - await createMonsters(); - message = broadcastMessage('server', 'Monster refresh!'); - } - else if(msg === '/server refresh-cities') { - await createAllCitiesAndLocations(); - message = broadcastMessage('server', 'Cities, Locations, and Paths refreshed!'); - } - else if(msg === '/server refresh-shops') { - await createShopItems(); - await createShopEquipment(); - message = broadcastMessage('server', 'Refresh shop items'); - } - else { - const str = msg.split('/server ')[1]; - if(str) { - message = broadcastMessage('server', str); - } + const output = await handleChatCommands(msg, req.player, io); + if(output) { + io.to(cache.get(`socket:${req.player.id}`)).emit('chat', renderChatMessage(output)); } } catch(e) { @@ -199,17 +181,15 @@ app.post('/chat', authEndpoint, async (req: Request, res: Response) => { res.sendStatus(204); } else { - message = broadcastMessage(req.player.username, xss(msg, { + const message = broadcastMessage(req.player.username, xss(msg, { whiteList: {} })); chatHistory.push(message); chatHistory.slice(-10); - } - - if(message) { io.emit('chat', renderChatMessage(message)); - res.sendStatus(204); } + + res.sendStatus(204); }); app.get('/player', authEndpoint, async (req: Request, res: Response) => { diff --git a/src/server/chat-commands.ts b/src/server/chat-commands.ts new file mode 100644 index 0000000..5e43627 --- /dev/null +++ b/src/server/chat-commands.ts @@ -0,0 +1,52 @@ +import { Server } from 'socket.io'; +import { maxHp, maxVigor, Player } from '../shared/player'; +import { createMonsters } from '../../seeds/monsters'; +import { createAllCitiesAndLocations } from '../../seeds/cities'; +import { createShopItems, createShopEquipment } from '../../seeds/shop_items'; +import { broadcastMessage, Message } from '../shared/message'; +import { updatePlayer } from './player'; + +export async function handleChatCommands(msg: string, player: Player, io: Server): Promise { + let message: Message; + if(msg === '/server refresh-monsters') { + await createMonsters(); + message = broadcastMessage('server', 'Monster refresh!'); + } + else if(msg === '/server refresh-cities') { + await createAllCitiesAndLocations(); + message = broadcastMessage('server', 'Cities, Locations, and Paths refreshed!'); + } + else if(msg === '/server refresh-shops') { + await createShopItems(); + await createShopEquipment(); + message = broadcastMessage('server', 'Refresh shop items'); + } + else if(msg.startsWith('/server set-level')) { + const level = parseInt(msg.split(' ').pop()); + if(level < 1) { + message = broadcastMessage('server', 'Needs to be at least level 1'); + } + else { + message = broadcastMessage('server', `Set player level: ${level}`); + + player.level = level; + player.strength = 4; + player.constitution = 4; + player.dexterity = 4; + player.intelligence = 4; + player.hp = maxHp(player.constitution, player.level); + player.vigor = maxVigor(player.constitution, player.level); + player.stat_points = level-1; + + await updatePlayer(player); + } + } + else { + const str = msg.split('/server ')[1]; + if(str) { + message = broadcastMessage('server', str); + } + } + + return message; +}