exploring/fighting is functional
[sketchy-heroes.git] / src / public / app / game.ts
1 import { Api } from './api';
2 import { playerOverview } from './sections/overview';
3 import $ from 'jquery';
4 import {$exploreButton, $fightButton} from './dom';
5
6 const api = new Api('http://localhost:9090');
7
8 api.on('player', playerOverview);
9
10 async function main() {
11   try {
12     await api.login('xangelo', 'test');
13   }
14   catch(e) {
15     await api.signup('xangelo', 'test', 'test');
16     await api.login('xangelo', 'test');
17   }
18 }
19
20 $exploreButton().on('click', async e => {
21   e.preventDefault();
22   e.stopPropagation();
23
24   await api.move();
25 });
26
27 $fightButton().on('click', async  e => {
28   e.preventDefault();
29   e.stopPropagation();
30
31   const fightId = $(e.target).attr('data-fight-id');
32
33   if(!fightId || fightId === 'unset') {
34     return;
35   }
36
37   await api.fight(fightId);
38
39 });
40
41 $('.stat-increase').on('click', e => {
42   e.preventDefault();
43   e.stopPropagation();
44
45   const stat = $(e.target).attr('data-stat');
46
47   if(!stat) {
48     throw new Error('Invalid stat increase');
49   }
50
51   api.increaseStat(stat);
52 });
53
54 main();