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';
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';
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) {
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) => {
--- /dev/null
+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;
+}