1 import { ShopEquipment } from "../../shared/inventory";
2 import { ShopItem, Item } from "../../shared/items";
3 import { capitalize, merge } from "lodash";
4 import { Player } from "../../shared/player";
5 import { LocationWithCity } from "shared/map";
6 import { ProgressBar } from "./components/progress-bar";
8 type RenderStatOptions = {
12 function renderStat(title: string, display: string, val: any, options?: RenderStatOptions): string {
15 } as RenderStatOptions, options);
17 let valSign: string = '';
18 if(typeof val === 'number') {
19 valSign = val > 0 ? '+' : '-';
22 return `<span title="${title}"><span class="requirement-title">${display}</span>: <span class="requirement-value ${opts.unsigned ? '' : (val > 0 ? "success": "error")}">${opts.unsigned ? val : `${valSign}${val}`}</span></span>`;
25 function renderStatBoost(name: string, val: number | string, title?: string): string {
26 let valSign: string = '';
27 if(typeof val === 'number') {
28 valSign = val > 0 ? '+' : '-';
30 return `<span class="requirement-title" title="${title}">${name}</span>: <span class="requirement-value ${typeof val === 'number' ? (val > 0 ? "success": "error") : ""}">${valSign}${val}</span>`;
33 function renderRequirement(name: string, val: number | string, currentVal?: number): string {
34 let colorIndicator = '';
36 colorIndicator = currentVal >= val ? 'success' : 'error';
38 return `<span class="requirement-title">${name}</span>: <span class="requirement-value ${colorIndicator}">${val.toLocaleString()}</span>`;
41 function renderShopItem(item: (ShopItem & Item), action: (item: (ShopItem & Item)) => string): string {
42 return `<div class="store-list">
43 <div class="store-icon">
44 <img src="/assets/img/icons/items/${item.icon_name}" title="${item.name}"><div class="store-actions">${action(item)}</div>
47 <div class="name">${item.name}</div>
48 <div class="requirements">
51 ${item.hasOwnProperty('id') ? `<div class="store-cost">${item.price_per_unit.toLocaleString()}G</div>` : ''}
56 export function renderEquipmentDetails(item: ShopEquipment, player: Player): string {
59 <div class="name">${item.name}${item.equipment_slot === 'TWO_HANDED' ? ' (2H)': ''}</div>
60 <div class="requirements">
61 ${item.requirements.level ? renderRequirement('LVL', item.requirements.level, player.level): ''}
62 ${item.requirements.strength ? renderRequirement('STR', item.requirements.strength, player.strength): ''}
63 ${item.requirements.constitution ? renderRequirement('CON', item.requirements.constitution, player.constitution): ''}
64 ${item.requirements.dexterity ? renderRequirement('DEX', item.requirements.dexterity, player.dexterity): ''}
65 ${item.requirements.intelligence ? renderRequirement('INT', item.requirements.intelligence, player.intelligence): ''}
66 ${renderRequirement('PRF', item.profession)}
68 <div class="stat-mods">
69 ${item.boosts.defence ? renderStatBoost('DEF', item.boosts.defence) : ''}
70 ${item.boosts.strength ? renderStatBoost('STR', item.boosts.strength) : ''}
71 ${item.boosts.constitution ? renderStatBoost('CON', item.boosts.constitution) : ''}
72 ${item.boosts.dexterity ? renderStatBoost('DEX', item.boosts.dexterity) : ''}
73 ${item.boosts.intelligence ? renderStatBoost('INT', item.boosts.intelligence) : ''}
74 ${item.boosts.damage ? renderStatBoost(item.affectedSkills.includes('restoration_magic') ? 'HP' : 'DMG', item.boosts.damage) : ''}
75 ${item.boosts.damage_mitigation ? renderStatBoost('MIT', item.boosts.damage_mitigation.toString())+'%' : ''}
76 ${['SPELL'].includes(item.type) ? '' : renderStat('Durability', 'DUR', item.maxAp, { unsigned: true })}
78 ${item.hasOwnProperty('id') ? `<div class="store-cost">${item.cost.toLocaleString()}G</div>` : ''}
84 function renderShopEquipment(item: ShopEquipment, action: (item: ShopEquipment) => string, player: Player): string {
85 return `<div class="store-list">
86 <div class="store-icon" style="background-image: url('${item.icon ? `/assets/img/icons/equipment/${item.icon}` : 'https://via.placeholder.com/64x64'}')">
87 <div class="store-actions">${action(item)}</div>
89 ${renderEquipmentDetails(item, player)}
95 export async function renderStore(equipment: ShopEquipment[], items: (ShopItem & Item)[], player: Player, location: LocationWithCity): Promise<string> {
96 const listing: Record<string, string> = {};
97 const listingTypes = new Set<string>();
99 equipment.forEach(item => {
100 const filter = item.type === 'ARMOUR' ? item.equipment_slot : item.type;
102 listingTypes.add(filter);
103 if(!listing[filter]) {
104 listing[filter] = '';
107 listing[filter] += renderShopEquipment(item, i => {
108 const id = `data-id="${i.id}"`;
109 return `<button type="button" hx-get="/location/${i.location_id}/equipment/${i.id}/overview" hx-target="#modal-wrapper">Buy</button>`
114 if(items && items.length) {
115 listingTypes.add('ITEMS');
116 listing['ITEMS'] = '';
117 items.forEach(item => {
118 listing['ITEMS'] += renderShopItem(item, i => {
119 return `<button type="button" hx-get="/location/${i.location_id}/items/${i.id}/overview" hx-target="#modal-wrapper">Buy</button>`;
124 let activeTab: string = listingTypes.keys().next().value;
126 const nav: string[] = [];
127 const finalListing: string[] = [];
129 listingTypes.forEach(type => {
130 nav.push(`<a href="#" data-filter="${type}" class="${activeTab === type ? 'active': ''}">${capitalize(type)}</a>`);
131 finalListing.push(`<div class="filter-result ${activeTab === type ? 'active': 'hidden'}" data-filter="${type}" id="filter_${type}">${listing[type]}</div>`);
135 <div class="city-title-wrapper"><div class="city-title">${location.city_name}</div></div>
136 <div class="city-details">
137 <h3 class="location-name"><span>${location.name}</span></h3>
138 <div class="shop-inventory-listing filter-container">
139 <nav class="filter" id="shop-inventory-listing">${nav.join("")}</nav><div class="inventory-listing listing">
140 ${finalListing.join("\n")}