feat: chat command to set player level
authorxangelo <me@xangelo.ca>
Wed, 6 Sep 2023 19:34:46 +0000 (15:34 -0400)
committerxangelo <me@xangelo.ca>
Wed, 6 Sep 2023 19:34:46 +0000 (15:34 -0400)
`/server set-level xxx` will reset the currently logged in players level
and give them the right number of stat points for that level

src/server/api.ts
src/server/chat-commands.ts [new file with mode: 0644]

index a64809cab87cad94bf06e567f74305cebb775e1c..0aa2689c95a6e594999ccffc22309771648ee358 100644 (file)
@@ -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 (file)
index 0000000..5e43627
--- /dev/null
@@ -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<Message> {
+  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;
+}