X-Git-Url: https://git.xangelo.ca/?p=sketchy-heroes.git;a=blobdiff_plain;f=src%2Fpublic%2Fapp%2Fapi.ts;fp=src%2Fpublic%2Fapp%2Fapi.ts;h=545fd1b7f6921255006c4886f03f8af47a2b99a8;hp=25b1a3262b4beab5869b9442a52dcd79eac28ffc;hb=b5d3cc37fddebff8dcdf1ef0cdd3a626811f14d3;hpb=20dc560a75cfd6ddc8a66956315a30001779ec24 diff --git a/src/public/app/api.ts b/src/public/app/api.ts index 25b1a32..545fd1b 100644 --- a/src/public/app/api.ts +++ b/src/public/app/api.ts @@ -3,7 +3,8 @@ import axios from 'axios'; import { Events } from './events'; import { LoginOutputType, AccountCreateType, MoveOutputType, FightStartType, FightRoundOutput } from 'src/routes'; import {actionLog} from './components'; -import {$fightButton} from './dom'; +import {disableFightButton, disablePickItemButton, enableFightButton, enablePickItemButton} from './dom'; +import {pickWorldDropOutput} from 'src/routes/inventory'; type ApiResponse = { status: 'ok' | 'error', @@ -84,7 +85,7 @@ export class Api extends Events { $('body').removeClass().addClass(`sky-gradient-${str}`); } - async getPlayerInfo() { + async syncPlayerInfo() { if(!this.player) { throw new Error('Not authenticated'); } @@ -94,6 +95,8 @@ export class Api extends Events { // this also sets the background based on the time! this.setGameTime(res.meta.gameTime); + + setTimeout(this.syncPlayerInfo.bind(this), 10000); } async login(username: string, password: string): Promise { @@ -109,7 +112,7 @@ export class Api extends Events { this.setGameTime(res.meta.gameTime); - setInterval(this.getPlayerInfo.bind(this), 5000); + this.syncPlayerInfo(); return res.payload; } @@ -125,16 +128,12 @@ export class Api extends Events { if(res.payload.generatedMonster !== null) { actionLog(`You see a ${res.payload.generatedMonster.name}`); if(this.player.hp > 0 && this.player.stamina > 0) { - $fightButton().prop('disabled', false) - .removeClass(['disabled', 'hidden']) - .attr('data-fight-id', res.payload.generatedMonster.id); + enableFightButton({fightId: res.payload.generatedMonster.id}); } } else { actionLog(res.payload.displayText, true); - $fightButton().prop('disabled', true) - .addClass(['disabled', 'hidden']) - .attr('data-fight-id', 'unset'); + disableFightButton(); } return res.payload; @@ -166,6 +165,18 @@ export class Api extends Events { return res.payload; } + async pickItem(dropId: string): Promise { + if(this.player === null) { + throw new Error('Not authenticated'); + } + + disablePickItemButton(); + + const res = await this.post(`/v1/accounts/${this.player.id}/pick/${dropId}`); + + actionLog(`You picked up the item!`, false); + } + async fight(fightId: string): Promise { if(this.player === null) { throw new Error('Not authenticated'); @@ -205,20 +216,19 @@ export class Api extends Events { actionLog(`You were defeated by the ${output.monster.name}`, false); } - Object.keys(output.reward).forEach(rewardType => { - switch(rewardType) { - case 'exp': - actionLog(`You gained ${output.reward.exp} Exp`, false); - break; - case 'currency': - actionLog(`You gained ${output.reward.currency} Steel`, false); - break; - } - }); + if(output.reward.exp) { + actionLog(`You gained ${output.reward.exp} Exp`, false); + } + + if(output.reward.currency) { + actionLog(`You gained ${output.reward.currency} Steel`, false); + } + + if(output.reward.worldDrop) { + enablePickItemButton({itemId: output.reward.worldDrop.id}, output.reward.worldDrop.name); + } - $fightButton().prop('disabled', true) - .addClass(['disabled', 'hidden']) - .attr('data-fight-id', 'unset'); + disableFightButton(); return output; }