res.send(html);
});
-app.put('/item/:item_id', authEndpoint, async (req: Request, 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}]`);
- 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(req.player);
-
- req.player.hp += hpGain;
-
- if(req.player.hp > maxHp(req.player.constitution, req.player.level)) {
- req.player.hp = maxHp(req.player.constitution, req.player.level);
- }
- break;
- }
-
- await updateItemCount(req.player.id, item.item_id, -1);
- await updatePlayer(req.player);
-
- const inventory = await getInventory(req.player.id);
- const items = await getPlayersItems(req.player.id);
-
- res.send(
- [
- renderPlayerBar(req.player),
- 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 item: PlayerItem = await getItemFromPlayer(req.player.id, parseInt(req.params.item_id));
-
- if(!item) {
- logger.log(`Invalid item [${req.params.item_id}]`);
- return res.sendStatus(400);
- }
-
- let html = `
-<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" class="red">Use</button>
- <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
- </div>
-</dialog>
-`;
-
- res.send(html);
-});
-
app.get('/city/stores/city:stores/:location_id', authEndpoint, async (req: Request, res: Response) => {
const location = await getService(parseInt(req.params.location_id));
import { authEndpoint } from '../auth';
import { blockPlayerInFight } from '../fight';
import { blockPlayerInDungeon } from '../dungeon';
+import { updatePlayer } from '../player';
import { getEquippedItems, getInventory, getInventoryItem } from '../inventory';
-import { getPlayersItems } from '../items';
+import { getPlayersItems, getItemFromPlayer, updateItemCount } from '../items';
import { equip, unequip } from '../equipment';
import { renderInventoryPage } from '../views/inventory';
import { renderPlayerBar } from '../views/player-bar';
+import * as Alert from '../views/alert';
import { EquipmentSlot } from '../../shared/inventory';
+import { PlayerItem } from '../../shared/items';
+import { maxHp } from '../../shared/player';
+import { HealthPotionSmall } from '../../shared/items/health_potion';
export const inventoryRouter = Router();
res.send(renderInventoryPage(inventory, items, inventoryItem.type) + renderPlayerBar(req.player));
});
+inventoryRouter.put('/item/:item_id', authEndpoint, async (req: Request, 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}]`);
+ 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(req.player);
+
+ req.player.hp += hpGain;
+
+ if(req.player.hp > maxHp(req.player.constitution, req.player.level)) {
+ req.player.hp = maxHp(req.player.constitution, req.player.level);
+ }
+ break;
+ }
+
+ await updateItemCount(req.player.id, item.item_id, -1);
+ await updatePlayer(req.player);
+
+ const inventory = await getInventory(req.player.id);
+ const items = await getPlayersItems(req.player.id);
+
+ res.send(
+ [
+ renderPlayerBar(req.player),
+ renderInventoryPage(inventory, items, 'ITEMS'),
+ Alert.SuccessAlert(`You used the ${item.name}`)
+ ].join("")
+ );
+
+});
+
+inventoryRouter.get('/modal/items/:item_id', authEndpoint, async (req: Request, 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}]`);
+ return res.sendStatus(400);
+ }
+
+ let html = `
+<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" class="red">Use</button>
+ <button class="close-modal" formmethod="dialog" value="cancel">Cancel</button>
+ </div>
+</dialog>
+`;
+
+ res.send(html);
+});
+
+