67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import Conquer from '@burguillosinfo/conquer';
|
|
|
|
import { JsonObject, JsonProperty } from 'typescript-json-serializer';
|
|
import Specie from '@burguillosinfo/conquer/specie';
|
|
import JsonSerializer from '@burguillosinfo/conquer/serializer';
|
|
|
|
@JsonObject()
|
|
export default class ConquerUserCurrentEnemy {
|
|
@JsonProperty()
|
|
private uuid: string;
|
|
|
|
@JsonProperty()
|
|
private species: Specie;
|
|
|
|
@JsonProperty()
|
|
private level: number;
|
|
|
|
@JsonProperty()
|
|
private max_health: number;
|
|
|
|
public getUUID(): string {
|
|
return this.uuid;
|
|
}
|
|
|
|
public getSpecies(): Specie {
|
|
return this.species;
|
|
}
|
|
|
|
public getLevel(): number {
|
|
return this.level;
|
|
}
|
|
|
|
public getMaxHealth(): number {
|
|
return this.max_health;
|
|
}
|
|
|
|
public static async getGlobalEnemies(): Promise<ConquerUserCurrentEnemy[] | null> {
|
|
const urlEnemies = new URL('/conquer/user/enemies/global', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port);
|
|
const response = await fetch(urlEnemies);
|
|
let responseBody;
|
|
try {
|
|
responseBody = await response.json();
|
|
if (response.status !== 200) {
|
|
console.error(responseBody.error);
|
|
return null;
|
|
}
|
|
const enemiesRaw = JsonSerializer.deserialize(responseBody, ConquerUserCurrentEnemy);
|
|
const enemiesReturnArray: ConquerUserCurrentEnemy[] = [];
|
|
if (!(enemiesRaw instanceof Array)) {
|
|
console.error('Incorrect type retrieved from ' + urlEnemies);
|
|
return null;
|
|
}
|
|
for (const enemy of enemiesRaw) {
|
|
if (!(enemy instanceof ConquerUserCurrentEnemy)) {
|
|
console.error('Incorrect type for enemy, maybe null or undef.', enemy);
|
|
return null;
|
|
}
|
|
enemiesReturnArray.push(enemy);
|
|
}
|
|
return enemiesReturnArray;
|
|
} catch(error) {
|
|
console.error(error, 'Invalid response from server seeking for possible battles.');
|
|
return null;
|
|
}
|
|
}
|
|
}
|