1 import * as otel from './tracing';
2 import { version } from "../../package.json";
3 import { config as dotenv } from 'dotenv';
4 import { join } from 'path';
5 import express, {Request, Response} from 'express';
6 import bodyParser from 'body-parser';
8 import http from 'http';
9 import { Server } from 'socket.io';
10 import { logger } from './lib/logger';
11 import { loadPlayer, createPlayer, updatePlayer, movePlayer } from './player';
12 import { random, sample } from 'lodash';
13 import {broadcastMessage, Message} from '../shared/message';
14 import {expToLevel, maxHp, Player} from '../shared/player';
15 import {clearFight, createFight, getMonsterList, getRandomMonster, loadMonster, loadMonsterFromFight, loadMonsterWithFaction, saveFightState} from './monster';
16 import {FightRound} from '../shared/fight';
17 import {addInventoryItem, deleteInventoryItem, getEquippedItems, getInventory, getInventoryItem, updateAp} from './inventory';
18 import { getItemFromPlayer, getItemFromShop, getPlayersItems, getShopItems, givePlayerItem, updateItemCount } from './items';
19 import {FightTrigger, Monster, MonsterForFight, MonsterWithFaction} from '../shared/monsters';
20 import {getShopEquipment, listShopItems } from './shopEquipment';
21 import {EquippedItemDetails} from '../shared/equipped';
22 import {ArmourEquipmentSlot, EquipmentSlot} from '../shared/inventory';
23 import { clearTravelPlan, completeTravel, getAllPaths, getAllServices, getCityDetails, getService, getTravelPlan, stepForward, travel } from './map';
24 import { signup, login, authEndpoint, AuthRequest } from './auth';
25 import {db} from './lib/db';
26 import { getPlayerSkills, getPlayerSkillsAsObject, updatePlayerSkills } from './skills';
27 import {SkillID, Skills} from '../shared/skills';
29 import { router as healerRouter } from './locations/healer';
31 import * as Alert from './views/alert';
32 import { renderPlayerBar } from './views/player-bar'
33 import { renderEquipmentDetails, renderStore } from './views/stores';
34 import { renderMap } from './views/map';
35 import { renderProfilePage } from './views/profile';
36 import { renderSkills } from './views/skills';
37 import { renderInventoryPage } from './views/inventory';
38 import { renderMonsterSelector } from './views/monster-selector';
39 import { renderFight, renderRoundDetails } from './views/fight';
40 import { renderTravel, travelButton } from './views/travel';
41 import { renderChatMessage } from './views/chat';
44 import { createMonsters } from '../../seeds/monsters';
45 import { createAllCitiesAndLocations } from '../../seeds/cities';
46 import { createShopItems, createShopEquipment } from '../../seeds/shop_items';
47 import { Item, PlayerItem, ShopItem } from 'shared/items';
48 import { equip, unequip } from './equipment';
49 import { HealthPotionSmall } from '../shared/items/health_potion';
55 const app = express();
56 const server = http.createServer(app);
58 app.use(express.static(join(__dirname, '..', '..', 'public')));
59 app.use(bodyParser.urlencoded({ extended: true }));
60 app.use(express.json());
62 const io = new Server(server);
64 const cache = new Map<string, any>();
65 const chatHistory: Message[] = [];
67 app.use((req, res, next) => {
68 console.log(req.method, req.url);
72 io.on('connection', async socket => {
73 logger.log(`socket ${socket.id} connected, authToken: ${socket.handshake.headers['x-authtoken']}`);
75 let authToken = socket.handshake.headers['x-authtoken'].toString() === 'null' ? null : socket.handshake.headers['x-authtoken'].toString();
79 logger.log(`Attempting to load player with id ${authToken}`);
80 player = await loadPlayer(authToken);
83 logger.log(`Creating player`);
84 player = await createPlayer();
85 authToken = player.id;
86 socket.handshake.headers['x-authtoken'] = authToken;
89 logger.log(`Socket [${socket.id}] auth token: ${player.id}`);
91 // ref to get the socket id for a particular player
92 cache.set(`socket:${player.id}`, socket.id);
93 // ref to get the player object
94 cache.set(`token:${player.id}`, player);
96 socket.emit('authToken', player.id);
98 socket.emit('chat', renderChatMessage(broadcastMessage('server', `${player.username} just logged in`)));
100 socket.on('disconnect', () => {
101 console.log(`Player ${player.username} left`);
102 io.emit('status', `${io.sockets.sockets.size} Online (v${version})`);
105 io.emit('status', `${io.sockets.sockets.size} Online (v${version})`);
107 // this is a special event to let the client know it can start
109 socket.emit('ready');
112 async function fightRound(player: Player, monster: MonsterWithFaction, data: {action: 'attack' | 'cast' | 'flee', target: 'head' | 'body' | 'arms' | 'legs'}) {
113 const playerSkills = await getPlayerSkillsAsObject(player.id);
114 const roundData: FightRound = {
117 winner: 'in-progress',
118 fightTrigger: monster.fight_trigger,
126 const equippedItems = await getEquippedItems(player.id);
128 // we only use this if the player successfully defeated the monster
129 // they were fighting, then we load the other monsters in this area
130 // so they can "fight again"
131 let potentialMonsters: MonsterForFight[] = [];
134 * cumulative chance of head/arms/body/legs
138 * we use the factor to decide how many decimal places
142 const monsterTarget = [0.2, 0.4, 0.9, 1];
143 const targets: ArmourEquipmentSlot[] = ['HEAD', 'CHEST', 'ARMS', 'LEGS'];
145 const rand = Math.ceil(Math.random() * factor);
146 let target: ArmourEquipmentSlot = 'CHEST';
147 monsterTarget.forEach((i, idx) => {
148 if (rand > (i * factor)) {
149 target = targets[idx] as ArmourEquipmentSlot;
162 const equipment: Map<EquipmentSlot, EquippedItemDetails> = new Map<EquipmentSlot, EquippedItemDetails>();
163 const weapons: EquippedItemDetails[] = [];
164 let anyDamageSpells: boolean = false;
165 equippedItems.forEach(item => {
166 if(item.type === 'ARMOUR') {
167 equipment.set(item.equipment_slot, item);
169 else if(item.type === 'WEAPON') {
172 else if(item.type === 'SPELL') {
173 if(item.affectedSkills.includes('destruction_magic')) {
174 anyDamageSpells = true;
179 boost.strength += item.boosts.strength;
180 boost.constitution += item.boosts.constitution;
181 boost.dexterity += item.boosts.dexterity;
182 boost.intelligence += item.boosts.intelligence;
184 if(item.type === 'SPELL' && item.affectedSkills.includes('restoration_magic')) {
185 boost.hp += item.boosts.damage;
188 boost.damage += item.boosts.damage;
192 // if you flee'd, then we want to check your dex vs. the monsters
193 // but we want to give you the item/weapon boosts you need
194 // if not then you're going to get hit.
195 if(data.action === 'flee') {
196 roundData.roundDetails.push(`You managed to escape from the ${monster.name}!`)
197 roundData.winner = 'monster';
198 await clearFight(player.id);
200 return { roundData, monsters: [], player };
203 const attackType = data.action === 'attack' ? 'physical' : 'magical';
204 const primaryStat = data.action === 'attack' ? player.strength : player.intelligence;
205 const boostStat = data.action === 'attack' ? boost.strength : boost.intelligence;
207 const playerDamage = Math.floor(((primaryStat + boostStat) * 1.3) + boost.damage);
208 const skillsUsed: Record<SkillID | any, number> = {};
209 let hpHealAfterMasteries: number = -1;
210 let playerDamageAfterMasteries: number = 0;
212 weapons.forEach(item => {
213 item.affectedSkills.forEach(id => {
214 if(id === 'restoration_magic') {
215 if(hpHealAfterMasteries < 0) {
216 hpHealAfterMasteries = 0;
218 hpHealAfterMasteries += Skills.get(id).effect(playerSkills.get(id));
221 playerDamageAfterMasteries += playerDamage * Skills.get(id).effect(playerSkills.get(id));
224 if(!skillsUsed[id]) {
231 await updatePlayerSkills(player.id, skillsUsed);
233 const playerFinalDamage = (data.action === 'cast' && !anyDamageSpells) ? 0 : Math.floor(playerDamage + playerDamageAfterMasteries);
234 const playerFinalHeal = Math.floor(boost.hp + hpHealAfterMasteries);
236 roundData.roundDetails.push(`You targeted the monsters ${data.target.toUpperCase()} with ${attackType} damage!`);
237 let armourKey: string;
238 switch(data.target) {
240 armourKey = 'armsAp';
243 armourKey = 'helmAp';
246 armourKey = 'legsAp';
249 armourKey = 'chestAp';
253 if(monster[armourKey] && monster[armourKey] > 0) {
254 monster[armourKey] -= playerFinalDamage;
256 roundData.roundDetails.push(`You dealt ${playerFinalDamage} damage to their armour`);
257 if(monster[armourKey] < 0) {
258 roundData.roundDetails.push(`You destroyed the ${monster.name}'s armour!'`);
259 roundData.roundDetails.push(`You dealt ${monster[armourKey] * -1} damage to their HP`);
260 monster.hp += monster[armourKey];
261 monster[armourKey] = 0;
265 roundData.roundDetails.push(`You hit the ${monster.name} for ${playerFinalDamage} damage.`);
266 monster.hp -= playerFinalDamage;
269 if(monster.hp <= 0) {
270 roundData.monster.hp = 0;
271 roundData.winner = 'player';
273 roundData.rewards.exp = monster.exp;
274 roundData.rewards.gold = monster.gold;
276 player.gold += monster.gold;
277 player.exp += monster.exp;
279 if(player.exp >= expToLevel(player.level + 1)) {
280 player.exp -= expToLevel(player.level + 1)
282 roundData.rewards.levelIncrease = true;
283 let statPointsGained = 1;
285 if(player.profession !== 'Wanderer') {
286 statPointsGained = 2;
289 player.stat_points += statPointsGained;
291 roundData.roundDetails.push(`You gained ${statPointsGained} stat points!`);
293 player.hp = maxHp(player.constitution, player.level);
295 // get the monster location if it was an EXPLORED fight
296 if(roundData.fightTrigger === 'explore') {
297 const rawMonster = await loadMonster(monster.ref_id);
298 const monsterList = await getMonsterList(rawMonster.location_id);
299 potentialMonsters = monsterList.map(monster => {
303 level: monster.level,
305 maxHp: monster.maxHp,
306 fight_trigger: 'explore'
311 await clearFight(player.id);
312 await updatePlayer(player);
313 return { roundData, monsters: potentialMonsters, player };
316 roundData.roundDetails.push(`The ${monster.name} targeted your ${target}!`);
317 if(equipment.has(target)) {
318 const item = equipment.get(target);
320 const mitigationPercentage = item.boosts.damage_mitigation || 0;
321 const damageAfterMitigation = Math.floor(monster.strength * ((100-mitigationPercentage)/100));
323 item.currentAp -= damageAfterMitigation;
325 if(item.currentAp < 0) {
326 roundData.roundDetails.push(`Your ${item.name} amour was destroyed`);
327 roundData.roundDetails.push(`The ${monster.name} hit your HP for ${item.currentAp * -1} damage!`);
328 player.hp += item.currentAp;
330 await deleteInventoryItem(player.id, item.item_id);
333 roundData.roundDetails.push(`Your ${target} took ${damageAfterMitigation} damage!`);
334 await updateAp(player.id, item.item_id, item.currentAp, item.maxAp);
339 roundData.roundDetails.push(`The ${monster.name} hit you for ${monster.strength} damage`);
340 player.hp -= monster.strength;
343 if(playerFinalHeal > 0) {
344 player.hp += playerFinalHeal;
345 if(player.hp > maxHp(player.constitution, player.level)) {
346 player.hp = maxHp(player.constitution, player.level);
348 roundData.roundDetails.push(`You healed for ${playerFinalHeal} HP`);
351 // update the players inventory for this item!
355 roundData.winner = 'monster';
357 roundData.roundDetails.push(`You were killed by the ${monster.name}`);
359 await clearFight(player.id);
360 await updatePlayer(player);
361 await clearTravelPlan(player.id);
363 return { roundData, monsters: [], player};
366 await updatePlayer(player);
367 await saveFightState(player.id, monster);
369 return { roundData, monsters: [], player};
372 app.use(healerRouter);
375 app.get('/chat/history', authEndpoint, async (req: AuthRequest, res: Response) => {
376 let html = chatHistory.map(renderChatMessage);
378 res.send(html.join("\n"));
381 app.post('/chat', authEndpoint, async (req: AuthRequest, res: Response) => {
382 const msg = req.body.message.trim();
384 if(!msg || !msg.length) {
389 let message: Message;
390 if(msg.startsWith('/server lmnop')) {
391 if(msg === '/server lmnop refresh-monsters') {
392 await createMonsters();
393 message = broadcastMessage('server', 'Monster refresh!');
395 else if(msg === '/server lmnop refresh-cities') {
396 await createAllCitiesAndLocations();
397 message = broadcastMessage('server', 'Cities, Locations, and Paths refreshed!');
399 else if(msg === '/server lmnop refresh-shops') {
400 await createShopItems();
401 await createShopEquipment();
402 message = broadcastMessage('server', 'Refresh shop items');
405 const str = msg.split('/server lmnop ')[1];
407 message = broadcastMessage('server', str);
412 message = broadcastMessage(req.player.username, msg);
413 chatHistory.push(message);
414 chatHistory.slice(-10);
418 io.emit('chat', renderChatMessage(message));
423 app.get('/player', authEndpoint, async (req: AuthRequest, res: Response) => {
424 const inventory = await getEquippedItems(req.player.id);
426 res.send(renderPlayerBar(req.player, inventory) + renderProfilePage(req.player));
429 app.post('/player/stat/:stat', authEndpoint, async (req: AuthRequest, res: Response) => {
430 const stat = req.params.stat;
431 if(!['strength', 'constitution', 'dexterity', 'intelligence'].includes(stat)) {
432 res.send(Alert.ErrorAlert(`Sorry, that's not a valid stat to increase`));
436 if(req.player.stat_points <= 0) {
437 res.send(Alert.ErrorAlert(`Sorry, you don't have enough stat points`));
441 req.player.stat_points -= 1;
444 updatePlayer(req.player);
446 const equippedItems = await getEquippedItems(req.player.id);
447 res.send(renderPlayerBar(req.player, equippedItems) + renderProfilePage(req.player));
450 app.get('/player/skills', authEndpoint, async (req: AuthRequest, res: Response) => {
451 const skills = await getPlayerSkills(req.player.id);
453 res.send(renderSkills(skills));
456 app.get('/player/inventory', authEndpoint, async (req: AuthRequest, res: Response) => {
457 const [inventory, items] = await Promise.all([
458 getInventory(req.player.id),
459 getPlayersItems(req.player.id)
462 res.send(renderInventoryPage(inventory, items));
465 app.post('/player/equip/:item_id/:slot', authEndpoint, async (req: AuthRequest, res: Response) => {
466 const inventoryItem = await getInventoryItem(req.player.id, req.params.item_id);
467 const equippedItems = await getEquippedItems(req.player.id);
468 const requestedSlot = req.params.slot;
469 let desiredSlot: EquipmentSlot = inventoryItem.equipment_slot;
472 // handes the situation where you're trying to equip an item
473 // that can be equipped to any hand
474 if(inventoryItem.equipment_slot === 'ANY_HAND') {
475 if(requestedSlot === 'LEFT_HAND' || requestedSlot === 'RIGHT_HAND') {
476 // get the players current equipment in that slot!
477 if(equippedItems.some(v => {
478 return v.equipment_slot === requestedSlot || v.equipment_slot === 'TWO_HANDED';
483 desiredSlot = requestedSlot;
488 if(requestedSlot === 'TWO_HANDED') {
489 if(equippedItems.some(v => {
490 return v.equipment_slot === 'LEFT_HAND' || v.equipment_slot === 'RIGHT_HAND';
497 await equip(req.player.id, inventoryItem, desiredSlot);
498 const socketId = cache.get(`socket:${req.player.id}`).toString();
499 io.to(socketId).emit('updatePlayer', req.player);
500 io.to(socketId).emit('alert', {
502 text: `You equipped your ${inventoryItem.name}`
509 const [inventory, items] = await Promise.all([
510 getInventory(req.player.id),
511 getPlayersItems(req.player.id)
514 res.send(renderInventoryPage(inventory, items, inventoryItem.type) + renderPlayerBar(req.player, inventory));
517 app.post('/player/unequip/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
518 const [item, ] = await Promise.all([
519 getInventoryItem(req.player.id, req.params.item_id),
520 unequip(req.player.id, req.params.item_id)
523 const [inventory, items] = await Promise.all([
524 getInventory(req.player.id),
525 getPlayersItems(req.player.id)
528 res.send(renderInventoryPage(inventory, items, item.type) + renderPlayerBar(req.player, inventory));
531 app.get('/player/explore', authEndpoint, async (req: AuthRequest, res: Response) => {
532 const fight = await loadMonsterFromFight(req.player.id);
533 let closestTown = req.player.city_id;
536 // ok lets display the fight screen!
537 const data: MonsterForFight = {
543 fight_trigger: fight.fight_trigger
546 res.send(renderFight(data));
549 const travelPlan = await getTravelPlan(req.player.id);
552 const travelPlan = await getTravelPlan(req.player.id);
554 const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
556 const chanceToSeeMonster = random(0, 100);
557 const things: any[] = [];
558 if(chanceToSeeMonster <= 30) {
559 const monster = await getRandomMonster([closest]);
560 things.push(monster);
564 const nextAction = cache[`step:${req.player.id}`] || 0;
566 res.send(renderTravel({
569 closestTown: closest,
574 // display the city info!
575 const [city, locations, paths] = await Promise.all([
576 getCityDetails(req.player.city_id),
577 getAllServices(req.player.city_id),
578 getAllPaths(req.player.city_id)
581 res.send(await renderMap({city, locations, paths}, closestTown));
587 // used to purchase equipment from a particular shop
588 app.put('/location/:location_id/equipment/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
589 const item = await getShopEquipment(parseInt(req.params.item_id), parseInt(req.params.location_id));
592 logger.log(`Invalid item [${req.params.item_id}]`);
593 return res.sendStatus(400);
596 if(req.player.gold < item.cost) {
597 res.send(Alert.ErrorAlert(`Sorry, you need at least ${item.cost.toLocaleString()}G to purchase this.`));
601 req.player.gold -= item.cost;
603 await updatePlayer(req.player);
604 await addInventoryItem(req.player.id, item);
606 const equippedItems = await getEquippedItems(req.player.id);
608 res.send(renderPlayerBar(req.player, equippedItems) + Alert.SuccessAlert(`You purchased ${item.name}`));
611 // used to purchase items from a particular shop
612 app.put('/location/:location_id/items/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
613 const item: (ShopItem & Item) = await getItemFromShop(parseInt(req.params.item_id), parseInt(req.params.location_id));
616 logger.log(`Invalid item [${req.params.item_id}]`);
617 return res.sendStatus(400);
620 if(req.player.gold < item.price_per_unit) {
621 res.send(Alert.ErrorAlert(`Sorry, you need at least ${item.price_per_unit.toLocaleString()}G to purchase this.`));
625 req.player.gold -= item.price_per_unit;
627 await updatePlayer(req.player);
628 await givePlayerItem(req.player.id, item.id, 1);
630 const equippedItems = await getEquippedItems(req.player.id);
632 res.send(renderPlayerBar(req.player, equippedItems) + Alert.SuccessAlert(`You purchased a ${item.name}`));
635 // used to display equipment modals in a store, validates that
636 // the equipment is actually in this store before displaying
638 app.get('/location/:location_id/equipment/:item_id/overview', authEndpoint, async (req: AuthRequest, res: Response) => {
639 const equipment = await getShopEquipment(parseInt(req.params.item_id), parseInt(req.params.location_id));
642 logger.log(`Invalid equipment [${req.params.item_id}]`);
643 return res.sendStatus(400);
648 <div class="item-modal-overview">
650 <img src="${equipment.icon ? `/assets/img/icons/equipment/${equipment.icon}` : 'https://via.placeholder.com/64x64'}" title="${equipment.name}" alt="${equipment.name}">
653 ${renderEquipmentDetails(equipment, req.player)}
656 <div class="actions">
657 <button hx-put="/location/${equipment.location_id}/equipment/${equipment.id}" formmethod="dialog" value="cancel">Buy</button>
658 <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
666 // used to display item modals in a store, validates that
667 // the item is actually in this store before displaying
669 app.get('/location/:location_id/items/:item_id/overview', authEndpoint, async (req: AuthRequest, res: Response) => {
670 const item: (ShopItem & Item) = await getItemFromShop(parseInt(req.params.item_id), parseInt(req.params.location_id));
673 logger.log(`Invalid item [${req.params.item_id}]`);
674 return res.sendStatus(400);
679 <div class="item-modal-overview">
681 <img src="/assets/img/icons/items/${item.icon_name}" title="${item.name}" alt="${item.name}">
684 <h4>${item.name}</h4>
685 <p>${item.description}</p>
688 <div class="actions">
689 <button hx-put="/location/${item.location_id}/items/${item.id}" formmethod="dialog" value="cancel">Buy</button>
690 <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
698 app.put('/item/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
699 const item: PlayerItem = await getItemFromPlayer(req.player.id, parseInt(req.params.item_id));
702 console.log(`Can't find item [${req.params.item_id}]`);
706 if(item.amount < 1) {
707 res.send(Alert.ErrorAlert(`You dont have enough ${item.name}`));
713 switch(item.effect_name) {
715 const hpGain = HealthPotionSmall.effect(req.player);
717 req.player.hp += hpGain;
719 if(req.player.hp > maxHp(req.player.constitution, req.player.level)) {
720 req.player.hp = maxHp(req.player.constitution, req.player.level);
725 await updateItemCount(req.player.id, item.item_id, -1);
726 await updatePlayer(req.player);
728 const inventory = await getInventory(req.player.id);
729 const equippedItems = inventory.filter(i => i.is_equipped);
730 const items = await getPlayersItems(req.player.id);
734 renderPlayerBar(req.player, equippedItems),
735 renderInventoryPage(inventory, items, 'ITEMS'),
736 Alert.SuccessAlert(`You used the ${item.name}`)
742 app.get('/modal/items/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
743 const item: PlayerItem = await getItemFromPlayer(req.player.id, parseInt(req.params.item_id));
746 logger.log(`Invalid item [${req.params.item_id}]`);
747 return res.sendStatus(400);
752 <div class="item-modal-overview">
754 <img src="/assets/img/icons/items/${item.icon_name}" title="${item.name}" alt="${item.name}">
757 <h4>${item.name}</h4>
758 <p>${item.description}</p>
761 <div class="actions">
762 <button hx-put="/item/${item.item_id}" formmethod="dialog" value="cancel" hx-target="#inventory">Use</button>
763 <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
771 app.get('/city/stores/city:stores/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => {
772 const location = await getService(parseInt(req.params.location_id));
774 if(!location || location.city_id !== req.player.city_id) {
775 logger.log(`Invalid location: [${req.params.location_id}]`);
778 const [shopEquipment, shopItems] = await Promise.all([
779 listShopItems({location_id: location.id}),
780 getShopItems(location.id)
783 const html = await renderStore(shopEquipment, shopItems, req.player);
788 app.get('/city/explore/city:explore/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => {
789 const location = await getService(parseInt(req.params.location_id));
790 if(!location || location.city_id !== req.player.city_id) {
792 logger.log(`Invalid location: [${req.params.location_id}]`);
796 const monsters: Monster[] = await getMonsterList(location.id);
797 res.send(renderMonsterSelector(monsters));
800 app.post('/travel', authEndpoint, async (req: AuthRequest, res: Response) => {
801 const destination_id = parseInt(req.body.destination_id);
803 if(!destination_id || isNaN(destination_id)) {
804 logger.log(`Invalid destination_id [${req.body.destination_id}]`);
805 return res.sendStatus(400);
808 const travelPlan = travel(req.player, req.body.destination_id);
810 res.json(travelPlan);
813 app.post('/fight/turn', authEndpoint, async (req: AuthRequest, res: Response) => {
814 const monster = await loadMonsterWithFaction(req.player.id);
817 res.send(Alert.ErrorAlert('Not in a fight'));
821 const fightData = await fightRound(req.player, monster, {
822 action: req.body.action,
823 target: req.body.fightTarget
826 let html = renderFight(
828 renderRoundDetails(fightData.roundData),
829 fightData.roundData.winner === 'in-progress'
832 if(fightData.monsters.length && monster.fight_trigger === 'explore') {
833 html += renderMonsterSelector(fightData.monsters, monster.ref_id);
836 let travelSection = '';
837 if(monster.fight_trigger === 'travel' && fightData.roundData.winner === 'player') {
838 // you're travellinga dn you won.. display the keep walking!
839 const travelPlan = await getTravelPlan(req.player.id);
840 const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
841 travelSection = travelButton(0);
844 const equippedItems = await getEquippedItems(req.player.id);
845 const playerBar = renderPlayerBar(fightData.player, equippedItems);
847 res.send(html + travelSection + playerBar);
850 app.post('/fight', authEndpoint, async (req: AuthRequest, res: Response) => {
851 if(req.player.hp <= 0) {
852 logger.log(`Player didn\'t have enough hp`);
853 return res.sendStatus(400);
856 const monsterId: number = req.body.monsterId;
857 const fightTrigger: FightTrigger = req.body.fightTrigger ?? 'travel';
860 logger.log(`Missing monster Id ${monsterId}`);
861 return res.sendStatus(400);
864 if(!fightTrigger || !['travel', 'explore'].includes(fightTrigger)) {
865 logger.log(`Invalid fight trigger [${fightTrigger}]`);
866 return res.sendStatus(400);
869 const monster = await loadMonster(monsterId);
872 logger.log(`Couldnt find monster for ${monsterId}`);
873 return res.sendStatus(400);
876 const fight = await createFight(req.player.id, monster, fightTrigger);
879 const data: MonsterForFight = {
885 fight_trigger: fight.fight_trigger
888 res.send(renderFight(data));
891 app.post('/travel/step', authEndpoint, async (req: AuthRequest, res: Response) => {
892 const stepTimerKey = `step:${req.player.id}`;
894 const travelPlan = await getTravelPlan(req.player.id);
896 res.send(Alert.ErrorAlert('You don\'t have a travel plan'));
900 if(cache[stepTimerKey]) {
901 if(cache[stepTimerKey] > Date.now()) {
902 res.send(Alert.ErrorAlert('Hmm.. travelling too quickly'));
907 travelPlan.current_position++;
909 if(travelPlan.current_position >= travelPlan.total_distance) {
910 const travel = await completeTravel(req.player.id);
912 req.player.city_id = travel.destination_id;
913 await movePlayer(travel.destination_id, req.player.id);
915 const [city, locations, paths] = await Promise.all([
916 getCityDetails(travel.destination_id),
917 getAllServices(travel.destination_id),
918 getAllPaths(travel.destination_id)
921 delete cache[stepTimerKey];
922 res.send(await renderMap({city, locations, paths}, req.player.city_id));
925 const walkingText: string[] = [
926 'You take a step forward',
929 // update existing plan..
930 // decide if they will run into anything
931 const travelPlan = await stepForward(req.player.id);
933 const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
935 const chanceToSeeMonster = random(0, 100);
936 const things: any[] = [];
937 if(chanceToSeeMonster <= 30) {
938 const monster = await getRandomMonster([closest]);
939 things.push(monster);
943 const nextAction = Date.now() + 3000;
945 cache[stepTimerKey] = nextAction;
947 res.send(renderTravel({
950 closestTown: closest,
951 walkingText: sample(walkingText)
957 app.post('/travel/:destination_id', authEndpoint, async (req: AuthRequest, res: Response) => {
958 if(req.player.hp <= 0) {
959 logger.log(`Player didn\'t have enough hp`);
960 res.send(Alert.ErrorAlert('Sorry, you need some HP to start travelling.'));
964 const destination = await getCityDetails(parseInt(req.params.destination_id));
967 res.send(Alert.ErrorAlert(`Thats not a valid desination`));
971 await travel(req.player, destination.id);
973 res.send(renderTravel({
977 closestTown: req.player.city_id
981 app.get('/settings', authEndpoint, async (req: AuthRequest, res: Response) => {
984 if(req.player.account_type === 'session') {
985 warning += `<div class="alert error">If you log out without signing up first, this account is lost forever.</div>`;
988 html += '<a href="#" hx-post="/logout">Logout</a>';
989 res.send(warning + html);
992 app.post('/logout', authEndpoint, async (req: AuthRequest, res: Response) => {
993 // ref to get the socket id for a particular player
994 cache.delete(`socket:${req.player.id}`);
995 // ref to get the player object
996 cache.delete(`token:${req.player.id}`);
998 logger.log(`${req.player.username} logged out`);
1004 app.post('/signup', async (req: Request, res: Response) => {
1005 const {username, password} = req.body;
1006 const authToken = req.headers['x-authtoken'];
1008 if(!username || !password || !authToken) {
1009 res.sendStatus(400);
1015 const player = await loadPlayer(authToken.toString());
1016 logger.log(`Attempted claim for ${player.username}`);
1018 await signup(authToken.toString(), username, password);
1020 await db('players').where({id: player.id}).update({
1021 account_type: 'auth',
1025 logger.log(`Player claimed ${player.username} => ${username}`);
1027 io.emit('chat', broadcastMessage('server', `${player.username} is now ${username}`));
1029 player.username = username;
1030 player.account_type = 'auth';
1038 if(e?.constraint === 'players_username_unique') {
1040 error: 'That username is already taken.'
1044 res.send({error: 'Please try again'}).status(500);
1049 app.post('/login', async (req: Request, res: Response) => {
1050 const {username, password} = req.body;
1052 const player = await login(username, password);
1057 res.json({error: 'That user doesnt exist'}).status(500);
1061 server.listen(process.env.API_PORT, () => {
1062 logger.log(`Listening on port ${process.env.API_PORT}`);