proc-gen equipment drops from monsters can be picked up
[sketchy-heroes.git] / src / public / app / api.ts
index 25b1a3262b4beab5869b9442a52dcd79eac28ffc..545fd1b7f6921255006c4886f03f8af47a2b99a8 100644 (file)
@@ -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<T> = {
   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<LoginOutputType> {
@@ -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<void> {
+    if(this.player === null) {
+      throw new Error('Not authenticated');
+    }
+
+    disablePickItemButton();
+
+    const res = await this.post<pickWorldDropOutput>(`/v1/accounts/${this.player.id}/pick/${dropId}`);
+
+    actionLog(`You picked up the item!`, false);
+  }
+
   async fight(fightId: string): Promise<FightRoundOutput> {
     if(this.player === null) {
       throw new Error('Not authenticated');
@@ -205,20 +216,19 @@ export class Api extends Events {
       actionLog(`<span class="text-danger">You were defeated by the ${output.monster.name}</span>`, false);
     }
 
-    Object.keys(output.reward).forEach(rewardType => {
-      switch(rewardType) {
-        case 'exp':
-          actionLog(`You gained <span class="text-info">${output.reward.exp}</span> Exp`, false);
-          break;
-        case 'currency':
-          actionLog(`You gained <span class="text-warning">${output.reward.currency}</span> Steel`, false);
-          break;
-      }
-    });
+    if(output.reward.exp) {
+      actionLog(`You gained <span class="text-info">${output.reward.exp}</span> Exp`, false);
+    }
+
+    if(output.reward.currency) {
+      actionLog(`You gained <span class="text-warning">${output.reward.currency}</span> 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;
   }