From 6fb15e2d2d1259df1a0d2cab503f492c91b93429 Mon Sep 17 00:00:00 2001 From: xangelo Date: Sat, 5 Aug 2023 05:08:36 -0400 Subject: [PATCH] fix: migrate item usage to htmx --- src/server/api.ts | 98 +++++++++++++++++++++------- src/server/map.ts | 2 +- src/server/views/inventory.ts | 4 +- src/server/views/monster-selector.ts | 4 +- src/server/views/player-bar.ts | 2 +- src/server/views/stores.ts | 2 +- 6 files changed, 83 insertions(+), 29 deletions(-) diff --git a/src/server/api.ts b/src/server/api.ts index 46d4204..41f6032 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -9,13 +9,13 @@ import http from 'http'; import { Server, Socket } from 'socket.io'; import { logger } from './lib/logger'; import { loadPlayer, createPlayer, updatePlayer, movePlayer } from './player'; -import * as _ from 'lodash'; +import { random, sample } from 'lodash'; import {broadcastMessage, Message} from '../shared/message'; import {expToLevel, maxHp, Player} from '../shared/player'; import {clearFight, createFight, getMonsterList, getRandomMonster, loadMonster, loadMonsterFromFight, loadMonsterWithFaction, saveFightState} from './monster'; import {FightRound} from '../shared/fight'; import {addInventoryItem, deleteInventoryItem, getEquippedItems, getInventory, getInventoryItem, updateAp} from './inventory'; -import { getItemFromPlayer, getItemFromShop, getPlayersItems, getShopItems, givePlayerItem } from './items'; +import { getItemFromPlayer, getItemFromShop, getPlayersItems, getShopItems, givePlayerItem, updateItemCount } from './items'; import {FightTrigger, Monster, MonsterForFight, MonsterWithFaction} from '../shared/monsters'; import {getShopEquipment, getShopItem, listShopItems } from './shopEquipment'; import {EquippedItemDetails} from '../shared/equipped'; @@ -45,7 +45,7 @@ import { createAllCitiesAndLocations } from '../../seeds/cities'; import { createShopItems } from '../../seeds/shop_items'; import { Item, PlayerItem, ShopItem } from 'shared/items'; import { equip, unequip } from './equipment'; -import { random, sample } from 'lodash'; +import { HealthPotionSmall } from '../shared/items/health_potion'; dotenv(); @@ -256,7 +256,7 @@ async function fightRound(player: Player, monster: MonsterWithFaction, data: {a roundData.winner = 'monster'; await clearFight(player.id); - return { roundData, monsters: [] }; + return { roundData, monsters: [], player }; } const attackType = data.action === 'attack' ? 'physical' : 'magical'; @@ -369,7 +369,7 @@ async function fightRound(player: Player, monster: MonsterWithFaction, data: {a await clearFight(player.id); await updatePlayer(player); - return { roundData, monsters: potentialMonsters }; + return { roundData, monsters: potentialMonsters, player }; } roundData.roundDetails.push(`The ${monster.name} targeted your ${target}!`); @@ -419,13 +419,13 @@ async function fightRound(player: Player, monster: MonsterWithFaction, data: {a await updatePlayer(player); await clearTravelPlan(player.id); - return { roundData, monsters: []}; + return { roundData, monsters: [], player}; } await updatePlayer(player); await saveFightState(player.id, monster); - return { roundData, monsters: []}; + return { roundData, monsters: [], player}; }; app.use(healerRouter); @@ -761,6 +761,57 @@ app.get('/location/:location_id/items/:item_id/overview', authEndpoint, async (r 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)); + + if(!item) { + console.log(`Can't find item [${req.params.item_id}]`); + return; + } + + if(item.amount < 1) { + res.send(Alert.ErrorAlert(`You dont have enough ${item.name}`)); + return; + } + + item.amount -= 1; + + switch(item.effect_name) { + case 'heal_small': + const hpGain = HealthPotionSmall.effect(player); + + player.hp += hpGain; + + if(player.hp > maxHp(player.constitution, player.level)) { + player.hp = maxHp(player.constitution, player.level); + } + break; + } + + await updateItemCount(player.id, item.item_id, -1); + await updatePlayer(player); + + const inventory = await getInventory(player.id); + const equippedItems = inventory.filter(i => i.is_equipped); + const items = await getPlayersItems(player.id); + + res.send( + [ + renderPlayerBar(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) @@ -777,21 +828,24 @@ app.get('/modal/items/:item_id', authEndpoint, async (req: Request, res: Respons } let html = ` -
-
-${item.name} -
-
-

${item.name}

-

${item.description}

-
-
-
- -
+ +
+
+ ${item.name} +
+
+

${item.name}

+

${item.description}

+
+
+
+ + +
+
`; - return res.json({ description: html }); + res.send(html); }); app.get('/city/stores/city:stores/:location_id', authEndpoint, async (req: Request, res: Response) => { @@ -885,7 +939,7 @@ app.post('/fight/turn', authEndpoint, async (req: Request, res: Response) => { ); if(fightData.monsters.length && monster.fight_trigger === 'explore') { - html += renderMonsterSelector(fightData.monsters); + html += renderMonsterSelector(fightData.monsters, monster.ref_id); } let travelSection = ''; @@ -897,7 +951,7 @@ app.post('/fight/turn', authEndpoint, async (req: Request, res: Response) => { } const equippedItems = await getEquippedItems(player.id); - const playerBar = renderPlayerBar(player, equippedItems); + const playerBar = renderPlayerBar(fightData.player, equippedItems); res.send(html + travelSection + playerBar); }); diff --git a/src/server/map.ts b/src/server/map.ts index d255c97..54f0de8 100644 --- a/src/server/map.ts +++ b/src/server/map.ts @@ -85,7 +85,7 @@ export async function stepForward(player_id: string): Promise { } export async function clearTravelPlan(player_id: string): Promise { - return completeTravel(player_id); + return db('travel').where({player_id}).delete(); } export async function completeTravel(player_id: string): Promise { diff --git a/src/server/views/inventory.ts b/src/server/views/inventory.ts index 9616ad8..08fce8e 100644 --- a/src/server/views/inventory.ts +++ b/src/server/views/inventory.ts @@ -52,7 +52,7 @@ ${map.LEGS ? map.LEGS.name : 'LEGS'} function renderInventoryItems(items: PlayerItem[]): string { return items.map(item => { return ` -
+
${item.amount.toLocaleString()}
`; @@ -159,7 +159,7 @@ export function renderInventoryPage(inventory: EquippedItemDetails[], items: Pla }); const html = ` -
+
${renderEquipmentPlacementGrid(inventory)}
diff --git a/src/server/views/monster-selector.ts b/src/server/views/monster-selector.ts index 370de3b..8acb82f 100644 --- a/src/server/views/monster-selector.ts +++ b/src/server/views/monster-selector.ts @@ -1,11 +1,11 @@ import { Monster, MonsterForFight } from "../../shared/monsters"; -export function renderMonsterSelector(monsters: Monster[] | MonsterForFight[]): string { +export function renderMonsterSelector(monsters: Monster[] | MonsterForFight[], activeMonsterId: number = 0): string { let html = `
`; diff --git a/src/server/views/player-bar.ts b/src/server/views/player-bar.ts index 13b5b0b..effbbdc 100644 --- a/src/server/views/player-bar.ts +++ b/src/server/views/player-bar.ts @@ -61,7 +61,7 @@ export function renderPlayerBar(player: Player, inventory: EquippedItemDetails[]
${calcAp(inventory)}
${progressBar(player.hp, maxHp(player.constitution, player.level), 'hp-bar', '#ff7070')} - ${progressBar(player.hp, expToLevel(player.level + 1), 'exp-bar', '#5997f9')} + ${progressBar(player.exp, expToLevel(player.level + 1), 'exp-bar', '#5997f9')}
`; diff --git a/src/server/views/stores.ts b/src/server/views/stores.ts index c3cf881..6c237d1 100644 --- a/src/server/views/stores.ts +++ b/src/server/views/stores.ts @@ -118,7 +118,7 @@ export async function renderStore(equipment: ShopEquipment[], items: (ShopItem & }); let html = `
-
+
${finalListing.join("\n")}
`; -- 2.25.1