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';
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();
roundData.winner = 'monster';
await clearFight(player.id);
- return { roundData, monsters: [] };
+ return { roundData, monsters: [], player };
}
const attackType = data.action === 'attack' ? 'physical' : 'magical';
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}!`);
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);
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)
}
let html = `
-<div class="item-modal-overview">
-<div class="icon">
-<img src="/assets/img/icons/items/${item.icon_name}" title="${item.name}" alt="${item.name}">
-</div>
-<div>
-<h4>${item.name}</h4>
-<p>${item.description}</p>
-</div>
-</div>
-<div class="actions">
-<button class="emit-event close-modal" data-event="item:use:${item.effect_name}" data-args="${item.item_id}">Use</button>
-</div>
+<dialog>
+ <div class="item-modal-overview">
+ <div class="icon">
+ <img src="/assets/img/icons/items/${item.icon_name}" title="${item.name}" alt="${item.name}">
+ </div>
+ <div>
+ <h4>${item.name}</h4>
+ <p>${item.description}</p>
+ </div>
+ </div>
+ <div class="actions">
+ <button hx-put="/item/${item.item_id}" formmethod="dialog" value="cancel" hx-target="#inventory">Use</button>
+ <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
+ </div>
+</dialog>
`;
- return res.json({ description: html });
+ res.send(html);
});
app.get('/city/stores/city:stores/:location_id', 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 = '';
}
const equippedItems = await getEquippedItems(player.id);
- const playerBar = renderPlayerBar(player, equippedItems);
+ const playerBar = renderPlayerBar(fightData.player, equippedItems);
res.send(html + travelSection + playerBar);
});