import {EquippedItemDetails} from '../shared/equipped';
import {ArmourEquipmentSlot, EquipmentSlot} from '../shared/inventory';
import { clearTravelPlan, completeTravel, getAllPaths, getAllServices, getCityDetails, getService, getTravelPlan, stepForward, travel } from './map';
-import { signup, login, authEndpoint } from './auth';
+import { signup, login, authEndpoint, AuthRequest } from './auth';
import {db} from './lib/db';
import { getPlayerSkills, getPlayerSkillsAsObject, updatePlayerSkills } from './skills';
import {SkillID, Skills} from '../shared/skills';
app.use(healerRouter);
-app.get('/chat/history', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.get('/chat/history', authEndpoint, async (req: AuthRequest, res: Response) => {
let html = chatHistory.map(renderChatMessage);
res.send(html.join("\n"));
});
-app.post('/chat', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.post('/chat', authEndpoint, async (req: AuthRequest, res: Response) => {
const msg = req.body.message.trim();
if(!msg || !msg.length) {
}
else {
- message = broadcastMessage(player.username, msg);
+ message = broadcastMessage(req.player.username, msg);
chatHistory.push(message);
chatHistory.slice(-10);
});
-app.get('/player', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
+app.get('/player', authEndpoint, async (req: AuthRequest, res: Response) => {
+ const inventory = await getEquippedItems(req.player.id);
- const inventory = await getEquippedItems(player.id);
-
- res.send(renderPlayerBar(player, inventory) + (await renderProfilePage(player)));
+ res.send(renderPlayerBar(req.player, inventory) + (await renderProfilePage(req.player)));
});
-app.get('/player/skills', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
- const skills = await getPlayerSkills(player.id);
+app.get('/player/skills', authEndpoint, async (req: AuthRequest, res: Response) => {
+ const skills = await getPlayerSkills(req.player.id);
res.send(renderSkills(skills));
});
-app.get('/player/inventory', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.get('/player/inventory', authEndpoint, async (req: AuthRequest, res: Response) => {
const [inventory, items] = await Promise.all([
- getInventory(player.id),
- getPlayersItems(player.id)
+ getInventory(req.player.id),
+ getPlayersItems(req.player.id)
]);
res.send(renderInventoryPage(inventory, items));
});
-app.post('/player/equip/:item_id/:slot', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
- const inventoryItem = await getInventoryItem(player.id, req.params.item_id);
- const equippedItems = await getEquippedItems(player.id);
+app.post('/player/equip/:item_id/:slot', authEndpoint, async (req: AuthRequest, res: Response) => {
+ const inventoryItem = await getInventoryItem(req.player.id, req.params.item_id);
+ const equippedItems = await getEquippedItems(req.player.id);
const requestedSlot = req.params.slot;
let desiredSlot: EquipmentSlot = inventoryItem.equipment_slot;
}
- await equip(player.id, inventoryItem, desiredSlot);
- const socketId = cache.get(`socket:${player.id}`).toString();
- io.to(socketId).emit('updatePlayer', player);
+ await equip(req.player.id, inventoryItem, desiredSlot);
+ const socketId = cache.get(`socket:${req.player.id}`).toString();
+ io.to(socketId).emit('updatePlayer', req.player);
io.to(socketId).emit('alert', {
type: 'success',
text: `You equipped your ${inventoryItem.name}`
}
const [inventory, items] = await Promise.all([
- getInventory(player.id),
- getPlayersItems(player.id)
+ getInventory(req.player.id),
+ getPlayersItems(req.player.id)
]);
- res.send(renderInventoryPage(inventory, items, inventoryItem.type) + renderPlayerBar(player, inventory));
+ res.send(renderInventoryPage(inventory, items, inventoryItem.type) + renderPlayerBar(req.player, inventory));
});
-app.post('/player/unequip/:item_id', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.post('/player/unequip/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
const [item, ] = await Promise.all([
- getInventoryItem(player.id, req.params.item_id),
- unequip(player.id, req.params.item_id)
+ getInventoryItem(req.player.id, req.params.item_id),
+ unequip(req.player.id, req.params.item_id)
]);
const [inventory, items] = await Promise.all([
- getInventory(player.id),
- getPlayersItems(player.id)
+ getInventory(req.player.id),
+ getPlayersItems(req.player.id)
]);
- res.send(renderInventoryPage(inventory, items, item.type) + renderPlayerBar(player, inventory));
+ res.send(renderInventoryPage(inventory, items, item.type) + renderPlayerBar(req.player, inventory));
});
-app.get('/player/explore', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
-
- const fight = await loadMonsterFromFight(player.id);
- let closestTown = player.city_id;
+app.get('/player/explore', authEndpoint, async (req: AuthRequest, res: Response) => {
+ const fight = await loadMonsterFromFight(req.player.id);
+ let closestTown = req.player.city_id;
if(fight) {
// ok lets display the fight screen!
res.send(renderFight(data));
}
else {
- const travelPlan = await getTravelPlan(player.id);
+ const travelPlan = await getTravelPlan(req.player.id);
if(travelPlan) {
// traveling!
- const travelPlan = await getTravelPlan(player.id);
+ const travelPlan = await getTravelPlan(req.player.id);
const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
}
// STEP_DELAY
- const nextAction = cache[`step:${player.id}`] || 0;
+ const nextAction = cache[`step:${req.player.id}`] || 0;
res.send(renderTravel({
things,
else {
// display the city info!
const [city, locations, paths] = await Promise.all([
- getCityDetails(player.city_id),
- getAllServices(player.city_id),
- getAllPaths(player.city_id)
+ getCityDetails(req.player.city_id),
+ getAllServices(req.player.city_id),
+ getAllPaths(req.player.city_id)
]);
res.send(await renderMap({city, locations, paths}, closestTown));
});
// used to purchase equipment from a particular shop
-app.put('/location/:location_id/equipment/:item_id', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.put('/location/:location_id/equipment/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
const item = await getShopEquipment(parseInt(req.params.item_id), parseInt(req.params.location_id));
if(!item) {
return res.sendStatus(400);
}
- if(player.gold < item.cost) {
+ if(req.player.gold < item.cost) {
res.send(Alert.ErrorAlert(`Sorry, you need at least ${item.cost.toLocaleString()}G to purchase this.`));
return;
}
- player.gold -= item.cost;
+ req.player.gold -= item.cost;
- await updatePlayer(player);
- await addInventoryItem(player.id, item);
+ await updatePlayer(req.player);
+ await addInventoryItem(req.player.id, item);
- const equippedItems = await getEquippedItems(player.id);
+ const equippedItems = await getEquippedItems(req.player.id);
- res.send(renderPlayerBar(player, equippedItems) + Alert.SuccessAlert(`You purchased ${item.name}`));
+ res.send(renderPlayerBar(req.player, equippedItems) + Alert.SuccessAlert(`You purchased ${item.name}`));
});
// used to purchase items from a particular shop
-app.put('/location/:location_id/items/:item_id', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.put('/location/:location_id/items/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
const item: (ShopItem & Item) = await getItemFromShop(parseInt(req.params.item_id), parseInt(req.params.location_id));
if(!item) {
return res.sendStatus(400);
}
- if(player.gold < item.price_per_unit) {
+ if(req.player.gold < item.price_per_unit) {
res.send(Alert.ErrorAlert(`Sorry, you need at least ${item.price_per_unit.toLocaleString()}G to purchase this.`));
return;
}
- player.gold -= item.price_per_unit;
+ req.player.gold -= item.price_per_unit;
- await updatePlayer(player);
- await givePlayerItem(player.id, item.id, 1);
+ await updatePlayer(req.player);
+ await givePlayerItem(req.player.id, item.id, 1);
- const equippedItems = await getEquippedItems(player.id);
+ const equippedItems = await getEquippedItems(req.player.id);
- res.send(renderPlayerBar(player, equippedItems) + Alert.SuccessAlert(`You purchased a ${item.name}`));
+ res.send(renderPlayerBar(req.player, equippedItems) + Alert.SuccessAlert(`You purchased a ${item.name}`));
});
// used to display equipment modals in a store, validates that
// the equipment is actually in this store before displaying
// the modal
-app.get('/location/:location_id/equipment/:item_id/overview', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.get('/location/:location_id/equipment/:item_id/overview', authEndpoint, async (req: AuthRequest, res: Response) => {
const equipment = await getShopEquipment(parseInt(req.params.item_id), parseInt(req.params.location_id));
if(!equipment) {
<img src="https://via.placeholder.com/64x64" title="${equipment.name}" alt="${equipment.name}">
</div>
<div>
- ${renderEquipmentDetails(equipment, player)}
+ ${renderEquipmentDetails(equipment, req.player)}
</div>
</div>
<div class="actions">
// used to display item modals in a store, validates that
// the item is actually in this store before displaying
// the modal
-app.get('/location/:location_id/items/:item_id/overview', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.get('/location/:location_id/items/:item_id/overview', authEndpoint, async (req: AuthRequest, res: Response) => {
const item: (ShopItem & Item) = await getItemFromShop(parseInt(req.params.item_id), parseInt(req.params.location_id));
if(!item) {
res.send(html);
});
-app.put('/item/:item_id', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
- const item: PlayerItem = await getItemFromPlayer(player.id, parseInt(req.params.item_id));
+app.put('/item/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
+ const item: PlayerItem = await getItemFromPlayer(req.player.id, parseInt(req.params.item_id));
if(!item) {
console.log(`Can't find item [${req.params.item_id}]`);
switch(item.effect_name) {
case 'heal_small':
- const hpGain = HealthPotionSmall.effect(player);
+ const hpGain = HealthPotionSmall.effect(req.player);
- player.hp += hpGain;
+ req.player.hp += hpGain;
- if(player.hp > maxHp(player.constitution, player.level)) {
- player.hp = maxHp(player.constitution, player.level);
+ if(req.player.hp > maxHp(req.player.constitution, req.player.level)) {
+ req.player.hp = maxHp(req.player.constitution, req.player.level);
}
break;
}
- await updateItemCount(player.id, item.item_id, -1);
- await updatePlayer(player);
+ await updateItemCount(req.player.id, item.item_id, -1);
+ await updatePlayer(req.player);
- const inventory = await getInventory(player.id);
+ const inventory = await getInventory(req.player.id);
const equippedItems = inventory.filter(i => i.is_equipped);
- const items = await getPlayersItems(player.id);
+ const items = await getPlayersItems(req.player.id);
res.send(
[
- renderPlayerBar(player, equippedItems),
+ renderPlayerBar(req.player, equippedItems),
renderInventoryPage(inventory, items, 'ITEMS'),
Alert.SuccessAlert(`You used the ${item.name}`)
].join("")
});
-app.get('/modal/items/:item_id', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
- const item: PlayerItem = await getItemFromPlayer(player.id, parseInt(req.params.item_id));
+app.get('/modal/items/:item_id', authEndpoint, async (req: AuthRequest, res: Response) => {
+ const item: PlayerItem = await getItemFromPlayer(req.player.id, parseInt(req.params.item_id));
if(!item) {
logger.log(`Invalid item [${req.params.item_id}]`);
res.send(html);
});
-app.get('/city/stores/city:stores/:location_id', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.get('/city/stores/city:stores/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => {
const location = await getService(parseInt(req.params.location_id));
- if(!location || location.city_id !== player.city_id) {
+ if(!location || location.city_id !== req.player.city_id) {
logger.log(`Invalid location: [${req.params.location_id}]`);
res.sendStatus(400);
}
getShopItems(location.id)
]);
- const html = await renderStore(shopEquipment, shopItems, player);
+ const html = await renderStore(shopEquipment, shopItems, req.player);
res.send(html);
});
-app.get('/city/explore/city:explore/:location_id', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
+app.get('/city/explore/city:explore/:location_id', authEndpoint, async (req: AuthRequest, res: Response) => {
const location = await getService(parseInt(req.params.location_id));
- if(!location || location.city_id !== player.city_id) {
+ if(!location || location.city_id !== req.player.city_id) {
logger.log(`Invalid location: [${req.params.location_id}]`);
res.sendStatus(400);
res.send(renderMonsterSelector(monsters));
});
-app.post('/travel', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
+app.post('/travel', authEndpoint, async (req: AuthRequest, res: Response) => {
const destination_id = parseInt(req.body.destination_id);
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
if(!destination_id || isNaN(destination_id)) {
logger.log(`Invalid destination_id [${req.body.destination_id}]`);
return res.sendStatus(400);
}
- const travelPlan = travel(player, req.body.destination_id);
+ const travelPlan = travel(req.player, req.body.destination_id);
res.json(travelPlan);
});
-app.post('/fight/turn', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
- const monster = await loadMonsterWithFaction(player.id);
+app.post('/fight/turn', authEndpoint, async (req: AuthRequest, res: Response) => {
+ const monster = await loadMonsterWithFaction(req.player.id);
if(!monster) {
res.send(Alert.ErrorAlert('Not in a fight'));
return;
}
- const fightData = await fightRound(player, monster, {
+ const fightData = await fightRound(req.player, monster, {
action: req.body.action,
target: req.body.fightTarget
});
let travelSection = '';
if(monster.fight_trigger === 'travel' && fightData.roundData.winner === 'player') {
// you're travellinga dn you won.. display the keep walking!
- const travelPlan = await getTravelPlan(player.id);
+ const travelPlan = await getTravelPlan(req.player.id);
const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
travelSection = travelButton(0);
}
- const equippedItems = await getEquippedItems(player.id);
+ const equippedItems = await getEquippedItems(req.player.id);
const playerBar = renderPlayerBar(fightData.player, equippedItems);
res.send(html + travelSection + playerBar);
});
-app.post('/fight', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
- if(player.hp <= 0) {
+app.post('/fight', authEndpoint, async (req: AuthRequest, res: Response) => {
+ if(req.player.hp <= 0) {
logger.log(`Player didn\'t have enough hp`);
return res.sendStatus(400);
}
return res.sendStatus(400);
}
- const fight = await createFight(player.id, monster, fightTrigger);
+ const fight = await createFight(req.player.id, monster, fightTrigger);
const data: MonsterForFight = {
res.send(renderFight(data));
});
-app.post('/travel/step', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
- const stepTimerKey = `step:${player.id}`;
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
+app.post('/travel/step', authEndpoint, async (req: AuthRequest, res: Response) => {
+ const stepTimerKey = `step:${req.player.id}`;
- const travelPlan = await getTravelPlan(player.id);
+ const travelPlan = await getTravelPlan(req.player.id);
if(!travelPlan) {
res.send(Alert.ErrorAlert('You don\'t have a travel plan'));
return;
travelPlan.current_position++;
if(travelPlan.current_position >= travelPlan.total_distance) {
- const travel = await completeTravel(player.id);
+ const travel = await completeTravel(req.player.id);
- player.city_id = travel.destination_id;
- await movePlayer(travel.destination_id, player.id);
+ req.player.city_id = travel.destination_id;
+ await movePlayer(travel.destination_id, req.player.id);
const [city, locations, paths] = await Promise.all([
getCityDetails(travel.destination_id),
]);
delete cache[stepTimerKey];
- res.send(await renderMap({city, locations, paths}, player.city_id));
+ res.send(await renderMap({city, locations, paths}, req.player.city_id));
}
else {
const walkingText: string[] = [
];
// update existing plan..
// decide if they will run into anything
- const travelPlan = await stepForward(player.id);
+ const travelPlan = await stepForward(req.player.id);
const closest: number = (travelPlan.current_position / travelPlan.total_distance) > 0.5 ? travelPlan.destination_id : travelPlan.source_id;
}
});
-app.post('/travel/:destination_id', authEndpoint, async (req: Request, res: Response) => {
- const authToken = req.headers['x-authtoken'].toString();
- const player: Player = await loadPlayer(authToken)
-
- if(!player) {
- logger.log(`Couldnt find player with id ${authToken}`);
- return res.sendStatus(400);
- }
-
- if(player.hp <= 0) {
+app.post('/travel/:destination_id', authEndpoint, async (req: AuthRequest, res: Response) => {
+ if(req.player.hp <= 0) {
logger.log(`Player didn\'t have enough hp`);
res.send(Alert.ErrorAlert('Sorry, you need some HP to start travelling.'));
return;
return;
}
- await travel(player, destination.id);
+ await travel(req.player, destination.id);
res.send(renderTravel({
things: [],
nextAction: 0,
walkingText: '',
- closestTown: player.city_id
+ closestTown: req.player.city_id
}));
});