1 import { io } from 'socket.io-client';
2 import $ from 'jquery';
3 import {expToLevel, maxHp, Player, StatDef, StatDisplay} from '../shared/player';
4 import { authToken, http } from './http';
5 import { CustomEventManager } from './events';
6 import {Fight, FightTrigger, MonsterForFight, MonsterForList} from '../shared/monsters';
7 import {FightRound} from '../shared/fight';
8 import { City, Location, LocationType, Path } from '../shared/map'
9 import { v4 as uuid } from 'uuid';
10 import {EquipmentSlot, ShopItem} from '../shared/inventory';
11 import { capitalize, each } from 'lodash';
12 import {EquippedItemDetails} from '../shared/equipped';
13 import { Skill, Skills } from '../shared/skills';
14 import { configureChat } from './chat';
15 import { SocketEvent } from './socket-event.client';
16 import * as EventList from '../events/client';
17 import { TimeManager } from '../shared/time';
18 import { TravelDTO } from '../events/travel/shared';
21 const time = new TimeManager();
22 const cache = new Map<string, any>();
23 const events = new CustomEventManager();
26 'x-authtoken': authToken()
30 configureChat(socket);
32 function setTimeGradient() {
33 const gradientName = time.gradientName();
34 const $body = $('body');
35 $body.removeClass(Array.from($body[0].classList)).addClass(gradientName);
36 $body.addClass(time.getTimePeriod());
39 if(time.get24Hour() >= 5 && time.get24Hour() < 9) {
42 else if(time.get24Hour() >= 9 && time.get24Hour() < 17) {
45 else if(time.get24Hour() >= 17 && time.get24Hour() < 20) {
52 $('#time-of-day').html(`<img src="/assets/img/icons/time-of-day/${icon}.png"> ${time.getHour()}${time.getAmPm()}`);
56 setInterval(setTimeGradient, 60 * 1000);
58 function icon(name: string): string {
59 return `<img src="/assets/img/${name}.png" class="icon">`;
63 function generateProgressBar(current: number, max: number, color: string, displayPercent: boolean = true): string {
66 percent = Math.floor((current / max) * 100);
68 const display = `${displayPercent? `${percent}% - `: ''}`;
69 return `<div class="progress-bar" style="background: linear-gradient(to right, ${color}, ${color} ${percent}%, transparent ${percent}%, transparent)" title="${display}${current}/${max}">${display}${current}/${max}</div>`;
72 function progressBar(current: number, max: number, el: string, color: string) {
75 percent = Math.floor((current / max) * 100);
79 `linear-gradient(to right, ${color}, ${color} ${percent}%, transparent ${percent}%, transparent)`
80 ).attr('title', `${percent}% - ${current}/${max}`).html(`${current}/${max} - ${percent}%`);
83 function updatePlayer() {
84 const player: Player = cache.get('player');
86 $('#username').html(`${player.username}, level ${player.level} ${player.profession}`);
90 maxHp(player.constitution, player.level),
97 expToLevel(player.level + 1),
102 ['strength', 'constitution', 'dexterity', 'intelligence','hp','exp'].forEach(s => {
103 $(`.${s}`).html(player[s]);
106 $('.maxHp').html(maxHp(player.constitution, player.level).toString());
107 $('.expToLevel').html(expToLevel(player.level + 1).toString());
108 $('.gold').html(player.gold.toLocaleString());
110 if(player.account_type === 'session') {
111 $('#signup-prompt').html(`<p>Hey there! It looks like you're using a SESSION account. This allows you to play with away, but the account is tied to your current device, and will be lost if you ever clear your cookies.</p><p>
113 <div class="form-group">
114 <label>Username:</label>
115 <input type="text" name="username">
117 <div class="form-group">
118 <label>Password:</label>
119 <input type="password" name="password">
121 <button type="submit" class="success">Create your Account</button>
122 <button type="button" id="login">Login To Existing Account</button>
123 </form></p>`).removeClass('hidden');
126 $('#extra-inventory-info').html(renderStatDetails());
129 socket.on('connect', () => {
130 console.log(`Connected: ${socket.id}`);
133 socket.on('ready', bootstrap);
135 socket.on('server-stats', (data: {onlinePlayers: number}) => {
136 $('#server-stats').html(`${data.onlinePlayers} players online`);
139 each(EventList, event => {
140 console.log(`Binding Event ${event.eventName}`);
141 if(event instanceof SocketEvent) {
142 socket.on(event.eventName, event.handler.bind(null, {
149 console.log('Skipped binding', event);
153 socket.on('calc:ap', (data: {ap: Record<EquipmentSlot, { currentAp: number, maxAp: number}>}) => {
158 ${generateProgressBar(ap.HEAD?.currentAp || 0, ap.HEAD?.maxAp || 0, '#7be67b')}
162 ${generateProgressBar(ap.ARMS?.currentAp || 0, ap.ARMS?.maxAp || 0, '#7be67b')}
166 ${generateProgressBar(ap.CHEST?.currentAp || 0, ap.CHEST?.maxAp || 0, '#7be67b')}
170 ${generateProgressBar(ap.LEGS?.currentAp || 0, ap.LEGS?.maxAp || 0, '#7be67b')}
173 $('#ap-bar').html(html);
176 events.on('tab:skills', () => {
177 $('#skills').html('');
178 socket.emit('skills');
181 socket.on('skills', (data: {skills: Skill[]}) => {
182 let html = `<table id="skill-list">
183 ${data.skills.map((skill: Skill) => {
184 const definition = Skills.get(skill.id);
185 const percent = skill.exp / definition.expToLevel(skill.level + 1);
188 <td class="skill-level">${skill.level.toLocaleString()}</td>
189 <td class="skill-description" title="Total Exp: ${skill.exp.toLocaleString()}/${definition.expToLevel(skill.level + 1).toLocaleString()}">
190 <span class="skill-exp">${(percent * 100).toPrecision(2)}% to next level</span>
191 <b>${definition.display}</b>
192 <p>${definition.description}</p>
199 $('#skills').html(html);
202 function Alert(text: string, type: string = 'error') {
204 $('#alerts').append(`<div class="alert ${type}" id="alert-${id}">${text}</div>`);
207 $(`#alert-${id}`).remove();
211 events.on('alert', (data: {type: string, text: string}) => {
212 Alert(data.text, data.type);
215 socket.on('alert', data => {
216 events.emit('alert', [data]);
219 socket.on('init', (data: {version: string}) => {
220 $('#version').html(`v${data.version}`);
223 socket.on('authToken', (authToken: string) => {
224 console.log(`recv auth token ${authToken}`);
225 localStorage.setItem('authToken', authToken);
229 socket.on('player', (player: Player) => {
230 cache.set('player', player);
234 async function fetchState() {
235 const res = await http.get('/state');
236 cache.set('state', res);
239 $('nav a').on('click', async e => {
243 const tabsDisabledInFight = [
249 const state = cache.get('state');
251 const $tabContainer = $(`#${$(e.target).data('container')}`);
252 let tabSection = $(e.target).data('section');
254 if(tabsDisabledInFight.includes(tabSection) && state?.fight !== null) {
255 Alert('You are currently in a fight', 'error');
256 // we want to force users over to the explore tab
257 // if they are currently in a fight and trying to
258 // access a disabled section
259 tabSection = 'explore';
262 $tabContainer.find('.tab').removeClass('active');
264 $(`#${tabSection}`).addClass('active');
266 if(e.target.innerHTML !== 'Settings') {
267 $(e.target).closest('nav').find('a').removeClass('active');
268 $(`nav a[data-section=${tabSection}]`).addClass('active');
271 events.emit(`tab:${tabSection}`);
274 events.on('tab:inventory', () => {
275 socket.emit('inventory');
278 function renderEquipmentPlacementGrid(items: EquippedItemDetails[]) {
279 const placeholder = 'https://via.placeholder.com/64x64';
281 const map: Record<EquipmentSlot, EquippedItemDetails> = items.filter(item => item.is_equipped).reduce((acc, item) => {
282 acc[item.equipment_slot] = item;
287 <table id="character-equipment-placement">
291 <td style="background-image: url('${placeholder}');" title="${map.HEAD ? map.HEAD.name : 'Empty'}">
292 ${map.HEAD ? map.HEAD.name : 'HEAD'}
294 <td style="background-image: url('${placeholder}');" title="${map.ARMS ? map.ARMS.name : 'Empty'}">
295 ${map.ARMS ? map.ARMS.name : 'ARMS'}
299 <td style="background-image: url('${placeholder}');" title="${map.LEFT_HAND ? map.LEFT_HAND.name : (map.TWO_HANDED ? map.TWO_HANDED.name : '')}">
300 ${map.LEFT_HAND ? map.LEFT_HAND.name : (map.TWO_HANDED ? map.TWO_HANDED.name : 'L_HAND')}
302 <td style="background-image: url('${placeholder}');" title="${map.CHEST ? map.CHEST.name : ''}">
303 ${map.CHEST ? map.CHEST.name : 'CHEST'}
305 <td style="background-image: url('${placeholder}');" title="${map.RIGHT_HAND ? map.RIGHT_HAND.name : (map.TWO_HANDED ? map.TWO_HANDED.name : '')}">
306 ${map.RIGHT_HAND ? map.RIGHT_HAND.name : (map.TWO_HANDED ? map.TWO_HANDED.name : 'R_HAND')}
312 <td style="background-image: url('${placeholder}');" title="${map.LEGS ? map.LEGS.name : ''}">
313 ${map.LEGS ? map.LEGS.name : 'LEGS'}
323 function statPointIncreaser(stat: StatDisplay) {
324 return `<button class="increase-stat emit-event" data-event="spend-stat-point" data-args="${stat.id}">+</button>`;
326 function renderStatDetails() {
327 const player: Player = cache.get('player');
328 let html = '<table id="stat-breakdown">';
330 StatDef.forEach(stat => {
332 <th>${stat.display}</th>
333 <td class="${stat.id}">
335 ${player.stat_points ? statPointIncreaser(stat) : ''}
340 html += `<tr><th>Stat Points</th><td class="stat_points">${player.stat_points}</td></tr>`;
347 $('body').on('click', 'nav.filter', e => {
351 const $target = $(e.target);
352 const filter = $target.data('filter');
354 $('.filter-result').removeClass('active').addClass('hidden');
355 $(`#filter_${filter}`).addClass('active').removeClass('hidden');
357 $target.closest('nav').find('a').removeClass('active');
358 $target.addClass('active');
360 const cacheKey = `active-${$(e.target).closest('nav').attr('id')}`;
362 cache.set(cacheKey, filter);
365 function renderInventorySection(inventory: EquippedItemDetails[]): string {
366 return inventory.map(item => {
367 return renderInventoryItem(item, item => {
368 if(item.is_equipped) {
369 return `<button type="button" class="unequip-item error" data-id="${item.item_id}">Unequip</button>`;
372 if(item.equipment_slot === 'ANY_HAND') {
373 return `<button type="button" class="equip-item" data-id="${item.item_id}" data-slot="LEFT_HAND">Equip L</button>
374 <button type="button" class="equip-item" data-id="${item.item_id}" data-slot="RIGHT_HAND">Equip R</button>`;
376 else if(item.equipment_slot === 'LEFT_HAND') {
377 return `<button type="button" class="equip-item" data-id="${item.item_id}" data-slot="${item.equipment_slot}">Equip Left</button>`;
379 else if(item.equipment_slot === 'RIGHT_HAND') {
380 return `<button type="button" class="equip-item" data-id="${item.item_id}" data-slot="${item.equipment_slot}">Equip Right</button>`;
382 else if(item.equipment_slot === 'TWO_HANDED') {
383 return `<button type="button" class="equip-item" data-id="${item.item_id}" data-slot="${item.equipment_slot}">Equip</button>`;
386 return `<button type="button" class="equip-item" data-id="${item.item_id}" data-slot="${item.equipment_slot}">Equip</button>`;
393 socket.on('inventory', (data: {inventory: EquippedItemDetails[]}) => {
394 const player: Player = cache.get('player');
395 const activeSection = cache.get('active-inventory-section') || 'ARMOUR';
396 // split the inventory into sections!
397 const sectionedInventory: {ARMOUR: EquippedItemDetails[], WEAPON: EquippedItemDetails[], SPELL: EquippedItemDetails[]} = {
403 data.inventory.forEach(item => {
404 sectionedInventory[item.type].push(item);
408 <div id="inventory-page">
409 <div id="character-summary">
410 ${renderEquipmentPlacementGrid(data.inventory)}
411 <div id="extra-inventory-info">
412 ${renderStatDetails()}
415 <div id="inventory-section" class="filter-container">
416 <nav class="filter" id="inventory-section">
417 <a href="#" data-filter="ARMOUR" class="${activeSection === 'ARMOUR' ? 'active': ''}">Armour</a>
418 <a href="#" data-filter="WEAPON" class="${activeSection === 'WEAPON' ? 'active': ''}">Weapons</a>
419 <a href="#" data-filter="SPELL" class="${activeSection === 'SPELL' ? 'active': ''}">Spells</a>
421 <div class="inventory-listing listing">
422 <div class="filter-result ${activeSection === 'ARMOUR' ? 'active' : 'hidden'}" id="filter_ARMOUR">
423 ${renderInventorySection(sectionedInventory.ARMOUR)}
425 <div class="filter-result ${activeSection === 'WEAPON' ? 'active' : 'hidden'}" id="filter_WEAPON">
426 ${renderInventorySection(sectionedInventory.WEAPON)}
428 <div class="filter-result ${activeSection === 'SPELL' ? 'active' : 'hidden'}" id="filter_SPELL">
429 ${renderInventorySection(sectionedInventory.SPELL)}
435 $('#inventory').html(html);
438 $('body').on('click', '.equip-item', e => {
442 const $target = $(e.target);
443 const id = $target.data('id');
444 const slot = $target.data('slot');
446 socket.emit('equip', {id, slot});
449 $('body').on('click', '.unequip-item', e => {
453 socket.emit('unequip', { id: $(e.target).data('id') });
456 function setMapBackground(city_id: number) {
458 'background-image': `linear-gradient(to left top, rgba(255,255,255,0) 0%,rgb(255,255,255) 100%), linear-gradient(to left, rgba(255, 255, 255, 0) 0%, rgb(255, 255, 255) 100%), url("/assets/img/map/${city_id}.jpeg")`
462 events.on('renderMap', async function renderMap(data: { city: City, locations: Location[], paths: Path[]}) {
463 if(!data && cache.has('currentMapHTML')) {
464 $('#map').html(cache.get('currentMapHTML'));
469 console.error('oh no.. this got triggered without any city data');
472 setMapBackground(data.city.id);
474 const servicesParsed: Record<LocationType, string[]> = {
480 data.locations.forEach(l => {
481 servicesParsed[l.type].push(`<a href="#" class="city-emit-event" data-event="${l.event_name}" data-args="${l.id}">${l.name}</a>`);
484 let html = `<h1>${data.city.name}</h1>
485 <div class="city-details">`;
487 if(servicesParsed.SERVICES.length) {
488 html += `<div><h3>Services</h3>${servicesParsed.SERVICES.join("<br>")}</div>`
490 if(servicesParsed.STORES.length) {
491 html += `<div><h3>Stores</h3>${servicesParsed.STORES.join("<br>")}</div>`
493 if(servicesParsed.EXPLORE.length) {
494 html += `<div><h3>Explore</h3>${servicesParsed.EXPLORE.join("<br>")}</div>`
499 ${data.paths.map(path => {
500 return `<a href="#" class="city-emit-event" data-event="city:travel" data-args="${path.ending_city}">${path.ending_city_name}</a>`
506 cache.set('currentMapHTML', html);
508 $('#map').html(html);
511 function renderRequirement(name: string, val: number | string, currentVal?: number): string {
512 let colorIndicator = '';
514 colorIndicator = currentVal >= val ? 'success' : 'error';
516 return `<span class="requirement-title">${name}</span>: <span class="requirement-value ${colorIndicator}">${val.toLocaleString()}</span>`;
519 function renderStatBoost(name: string, val: number | string): string {
520 let valSign: string = '';
521 if(typeof val === 'number') {
522 valSign = val > 0 ? '+' : '-';
524 return `<span class="requirement-title">${name}</span>: <span class="requirement-value ${typeof val === 'number' ? (val > 0 ? "success": "error") : ""}">${valSign}${val}</span>`;
527 function renderInventoryItem(item: EquippedItemDetails , action: (item: EquippedItemDetails) => string): string {
528 return `<div class="store-list">
530 <img src="https://via.placeholder.com/64x64">
532 <div class="details">
533 <div class="name">${item.name}</div>
534 <div class="requirements">
535 ${item.requirements.level ? renderRequirement('LVL', item.requirements.level): ''}
536 ${item.requirements.strength ? renderRequirement('STR', item.requirements.strength): ''}
537 ${item.requirements.constitution ? renderRequirement('CON', item.requirements.constitution): ''}
538 ${item.requirements.dexterity ? renderRequirement('DEX', item.requirements.dexterity): ''}
539 ${item.requirements.intelligence ? renderRequirement('INT', item.requirements.intelligence): ''}
540 ${renderRequirement('PRF', item.profession)}
542 <div class="stat-mods">
543 ${item.boosts.strength ? renderStatBoost('STR', item.boosts.strength) : ''}
544 ${item.boosts.constitution ? renderStatBoost('CON', item.boosts.constitution) : ''}
545 ${item.boosts.dexterity ? renderStatBoost('DEX', item.boosts.dexterity) : ''}
546 ${item.boosts.intelligence ? renderStatBoost('INT', item.boosts.intelligence) : ''}
547 ${item.boosts.damage ? renderStatBoost('DMG', item.boosts.damage) : ''}
548 ${item.boosts.damage_mitigation ? renderStatBoost('MIT', item.boosts.damage_mitigation.toString())+'%' : ''}
549 ${['WEAPON','SPELL'].includes(item.type) ? '': generateProgressBar(item.currentAp, item.maxAp, '#7be67b')}
551 ${item.hasOwnProperty('id') ? `<div>${item.cost.toLocaleString()}G</div>` : ''}
553 <div class="store-actions">
559 function renderShopItem(item: ShopItem, action: (item: ShopItem) => string): string {
560 const player: Player = cache.get('player');
561 return `<div class="store-list">
563 <img src="https://via.placeholder.com/64x64">
565 <div class="details">
566 <div class="name">${item.name}${item.equipment_slot === 'TWO_HANDED' ? ' (2H)': ''}</div>
567 <div class="requirements">
568 ${item.requirements.level ? renderRequirement('LVL', item.requirements.level, player.level): ''}
569 ${item.requirements.strength ? renderRequirement('STR', item.requirements.strength, player.strength): ''}
570 ${item.requirements.constitution ? renderRequirement('CON', item.requirements.constitution, player.constitution): ''}
571 ${item.requirements.dexterity ? renderRequirement('DEX', item.requirements.dexterity, player.dexterity): ''}
572 ${item.requirements.intelligence ? renderRequirement('INT', item.requirements.intelligence, player.intelligence): ''}
573 ${renderRequirement('PRF', item.profession)}
575 <div class="stat-mods">
576 ${item.boosts.strength ? renderStatBoost('STR', item.boosts.strength) : ''}
577 ${item.boosts.constitution ? renderStatBoost('CON', item.boosts.constitution) : ''}
578 ${item.boosts.dexterity ? renderStatBoost('DEX', item.boosts.dexterity) : ''}
579 ${item.boosts.intelligence ? renderStatBoost('INT', item.boosts.intelligence) : ''}
580 ${item.boosts.damage ? renderStatBoost(item.affectedSkills.includes('restoration_magic') ? 'HP' : 'DMG', item.boosts.damage) : ''}
581 ${item.boosts.damage_mitigation ? renderStatBoost('MIT', item.boosts.damage_mitigation.toString())+'%' : ''}
582 ${['WEAPON','SPELL'].includes(item.type) ? '' : renderStatBoost('AP', item.maxAp.toString())}
584 ${item.hasOwnProperty('id') ? `<div>${item.cost.toLocaleString()}G</div>` : ''}
586 <div class="store-actions">
593 socket.on('city:stores', (data: ShopItem[]) => {
594 const listing: Record<string, string> = {};
595 const listingTypes = new Set<string>();
596 data.forEach(item => {
597 if(item.type === 'ARMOUR') {
598 listingTypes.add(item.equipment_slot);
600 if(!listing[item.equipment_slot]) {
601 listing[item.equipment_slot] = '';
604 listing[item.equipment_slot] += renderShopItem(item, i => {
605 const id = `data-id="${i.id}"`;
606 return `<button type="button" class="purchase-item" ${id} data-type="${i.type}" data-equipment-slot="${i.equipment_slot}" data-cost="${i.cost}">Buy</button>`
610 listingTypes.add(item.type);
611 if(!listing[item.type]) {
612 listing[item.type] = '';
615 listing[item.type] += renderShopItem(item, i => {
616 const id = `data-id="${i.id}"`;
617 return `<button type="button" class="purchase-item" ${id} data-type="${i.type}" data-equipment-slot="${i.equipment_slot}" data-cost="${i.cost}">Buy</button>`
622 const sections = Object.keys(listing);
623 let activeSection = cache.get('active-shop-inventory-listing');
625 if(!sections.includes(activeSection)) {
626 activeSection = sections[0];
629 const nav: string[] = [];
630 const finalListing: string[] = [];
632 listingTypes.forEach(type => {
633 nav.push(`<a href="#" data-filter="${type}" class="${activeSection === type ? 'active': ''}">${capitalize(type)}</a>`);
634 finalListing.push(`<div class="filter-result ${activeSection === type ? 'active': 'hidden'}" id="filter_${type}">${listing[type]}</div>`);
637 let html = `<div class="shop-inventory-listing filter-container">
638 <nav class="filter" id="shop-inventory-listing">${nav.join(" ")}</nav><div class="inventory-listing listing">
639 ${finalListing.join("\n")}
643 $('#map').html(html);
647 $('body').on('click', '.purchase-item', e => {
651 const player = cache.get('player');
652 const cost = parseInt($(e.target).data('cost'));
655 const id = $(e.target).data('id');
656 if(player.gold < cost) {
657 events.emit('alert', [{
659 text: 'You don\'t have enough gold!'
664 const type = $(e.target).data('type');
665 socket.emit('purchase', {
671 $('body').on('click', '.emit-event', e => {
672 const $el = $(e.target);
674 const eventName = $el.data('event');
675 const args = $el.data('args');
677 const rawBlock = $el.data('block');
680 const block = parseInt(rawBlock) || 0;
682 if(block > Date.now()) {
683 Alert('Sorry, clicked too quick');
688 console.log(`Sending event ${eventName}`, { args });
689 socket.emit(eventName, { args });
692 $('body').on('click', '.city-emit-event', e => {
693 const $el = $(e.target);
695 const eventName = $el.data('event');
696 const args = $el.data('args');
698 console.log(`Sending event ${eventName}`, { args });
699 socket.emit(eventName, { args });
702 $('body').on('click', '.emit-event-internal', e => {
703 const $el = $(e.target);
705 const eventName = $el.data('event');
706 const args: string[] = $el.data('args')?.toString().split('|');
708 console.log(`Trigger internal event [${eventName}]`);
709 events.emit(eventName, args);
713 socket.on('explore:fights', (monsters: MonsterForList[]) => {
714 const lastSelectedMonster = cache.get('last-selected-monster');
715 if(monsters.length) {
716 let sel = `<form id="fight-selector"><select id="monster-selector">
717 ${monsters.map(monster => {
718 return `<option value="${monster.id}" ${lastSelectedMonster == monster.id ? 'selected': ''}>${monster.name}</option>`;
720 </select> <button type="submit">Fight</button></option>`;
726 events.on('tab:explore', async () => {
727 const state = cache.get('state');
728 if(cache.get('player').hp <= 0 || (!state.fight && !state.travel)) {
729 // get the details of the city
731 let data: {city: City, locations: Location[], paths: Path[]};
732 if(!cache.has(('currentMapHTML')) || cache.get('player').hp <= 0) {
733 data = await http.get(`/city/${cache.get('player').city_id}`);
735 events.emit('renderMap', [data, state.travel]);
738 else if(state.fight) {
739 setMapBackground(state.closestTown);
740 $('#map').html(renderFight(state.fight));
742 else if(state.travel) {
744 events.emit('renderTravel', [state.travel]);
748 function updateStepButton() {
749 const raw = $('#keep-walking').data('block');
750 const time = parseInt(raw) || 0;
753 const wait = Math.ceil((time - Date.now())/ 1000);
755 $('#keep-walking').prop('disabled', true).html(`Keep walking (${wait}s)`);
756 setTimeout(updateStepButton, 300);
761 $('#keep-walking').prop('disabled', false).html(`Keep walking`);
764 function renderTravel(data: TravelDTO) {
765 setMapBackground(data.closestTown);
767 let promptText = data.walkingText;
768 const blockTime = data.nextAction || 0;
769 let html = `<div id="travelling">`;
771 html += '<div id="travelling-actions">';
772 html += `<button class="emit-event" id="keep-walking" data-event="travel:step" data-block="${blockTime}">Keep Walking</button>`;
774 if(data.things.length) {
775 // ok you found something, for now we only support
776 // monsters, but eventually that will change
777 promptText = `You see a ${data.things[0].name}`;
778 html += `<button class="emit-event-internal" data-event="startFight" data-args="${data.things[0].id}|travel">Fight</button>`;
781 // end #travelling-actions
783 html += `<p>${promptText}</p>`;
785 // end #travelling div
788 $('#map').html(html);
795 events.on('renderTravel', renderTravel);
797 function renderFight(monster: MonsterForFight | Fight) {
798 const hpPercent = Math.floor((monster.hp / monster.maxHp) * 100);
800 let html = `<div id="fight-container">
801 <div id="defender-info">
802 <div class="avatar-container">
803 <img id="avatar" src="https://via.placeholder.com/64x64">
805 <div id="defender-stat-bars">
806 <div id="defender-name">${monster.name}</div>
807 <div class="progress-bar" id="defender-hp-bar" style="background: linear-gradient(to right, red, red ${hpPercent}%, transparent ${hpPercent}%, transparent)" title="${hpPercent}% - ${monster.hp}/${monster.maxHp}">${hpPercent}% - ${monster.hp} / ${monster.maxHp}</div>
810 <div id="fight-actions">
811 <select id="fight-target">
812 <option value="head">Head</option>
813 <option value="body">Body</option>
814 <option value="arms">Arms</option>
815 <option value="legs">Legs</option>
817 <button type="button" class="fight-action" data-action="attack">Attack</button>
818 <button type="button" class="fight-action" data-action="cast">Cast</button>
819 <button type="button" class="fight-action" data-action="flee">Flee</button>
821 <div id="fight-results"></div>
827 socket.on('updatePlayer', (player: Player) => {
828 cache.set('player', player);
832 socket.on('fight-over', (data: {roundData: FightRound, monsters: MonsterForFight[]}) => {
833 const { roundData, monsters } = data;
834 cache.set('player', roundData.player);
837 $('#map').html(renderFight(roundData.monster));
839 let html: string[] = roundData.roundDetails.map(d => `<div>${d}</div>`);
841 if(roundData.winner === 'player') {
842 html.push(`<div>You defeated the ${roundData.monster.name}!</div>`);
843 if(roundData.rewards.gold) {
844 html.push(`<div>You gained ${roundData.rewards.gold} gold`);
846 if(roundData.rewards.exp) {
847 html.push(`<div>You gained ${roundData.rewards.exp} exp`);
849 if(roundData.rewards.levelIncrease) {
850 html.push(`<div>You gained a level! ${roundData.player.level}`);
854 if(roundData.player.hp === 0) {
855 // prompt to return to town and don't let them do anything
856 html.push(`<p>You were killed...</p>`);
857 html.push('<p><button class="emit-event-internal" data-event="tab:explore" data-args="">Back to Town</button></p>');
860 switch(roundData.fightTrigger) {
862 if(monsters.length) {
863 const lastSelectedMonster = cache.get('last-selected-monster');
864 // put this above the fight details
865 html.unshift(`<h2>Fight Again</h2><form id="fight-selector"><select id="monster-selector">
866 ${monsters.map(monster => {
867 return `<option value="${monster.id}" ${lastSelectedMonster == monster.id ? 'selected': ''}>${monster.name}</option>`;
869 </select> <button type="submit">Fight</button></option>
870 </select></form><hr>`);
874 html.push(`<p><button class="emit-event" data-event="travel:step">Keep Walking</button></p>`);
877 console.error(`Unknown fight trigger [${roundData.fightTrigger}]`, roundData);
884 $('#fight-results').html(html.join("\n"));
885 $('#fight-actions').html('');
889 socket.on('fight-round', (data: FightRound) => {
890 $('.fight-action').prop('disabled', false);
891 cache.set('player', data.player);
894 $('#map').html(renderFight(data.monster));
896 $('#fight-results').html(data.roundDetails.map(d => `<div>${d}</div>`).join("\n"));
899 async function startFight(monsterId: string, fightTrigger: FightTrigger = 'explore') {
900 // check your HP first
901 if(cache.get('player').hp <= 0) {
902 events.emit('alert', [{
904 text: 'You don\'t have enough HP to go looking for a fight'
909 cache.set('last-selected-monster', monsterId);
912 const monster: MonsterForFight = await http.post('/fight', {
917 $('#map').html(renderFight(monster));
920 events.emit('alert', [{
922 text: 'Sorry, you can\'t start that fight'
927 events.on('startFight', startFight);
929 $('body').on('submit', '#fight-selector', async e => {
933 const monsterId = $('#monster-selector option:selected').val();
936 startFight(monsterId.toString());
939 console.error(`Invalid monster id [${monsterId}]`);
944 $('body').on('click', '.fight-action', e => {
948 const action = $(e.target).data('action');
949 const target = $('#fight-target option:selected').val();
950 $('.fight-action').prop('disabled', true);
952 socket.emit('fight', { action, target });
955 $('body').on('submit', '#signup', async e => {
959 const data: Record<string, string> = {};
961 $(e.target).serializeArray().forEach(v => data[v.name] = v.value);
963 const res = await http.post('/signup', data);
966 events.emit('alert', [{
972 cache.set('player', res.player);
974 $('#signup-prompt').remove();
979 $('body').on('click', '#login', async e => {
983 const data: Record<string, string> = {};
985 $(e.target).closest('form').serializeArray().forEach(v => data[v.name] = v.value);
987 if(data.username && data.password) {
988 const res = await http.post('/login', data);
990 events.emit('alert', [{type: 'error', text: res.error}]);
993 localStorage.setItem('authToken', res.player.id);
994 window.location.reload();
999 $('body').on('click', '#logout', async e => {
1001 e.stopPropagation();
1005 const player = cache.get('player');
1006 if(/^Player[\d]+$/.test(player.username)) {
1007 const clear = confirm('Are you sure? You will not be able to retrieve this character unless you set up an email/password');
1020 socket.emit('logout');
1021 localStorage.clear();
1022 window.location.reload();
1027 function bootstrap() {
1028 console.log('Server connection verified');
1029 socket.emit('inventory');
1030 $('nav a').first().click();