import JsonSerializer from '@burguillosinfo/conquer/serializer'; import { JsonObject, JsonProperty } from 'typescript-json-serializer'; import Conquer from '@burguillosinfo/conquer' @JsonObject() export default class ConquerTeam { @JsonProperty() private kind: string; @JsonProperty() private uuid: string; @JsonProperty() private name: string; @JsonProperty() private description: string; @JsonProperty() private points: number; @JsonProperty() private color: string; public getUUID(): string { return this.uuid; } public getName(): string { return this.name; } public getDescription(): string { return this.description; } public getColor(): string { return this.color; } constructor(uuid: string, name: string, description: string, points: number, color: string) { this.kind = 'ConquerTeam'; this.uuid = uuid; this.name = name; this.description = description; this.points = points; this.color = color; } public static async getTeams(): Promise { const urlTeam = new URL('/conquer/teams', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port) try { const response = await fetch(urlTeam) if (response.status !== 200) { throw new Error('Invalid response fetching teams.') } const teamData = await response.json() const teams = JsonSerializer.deserialize(teamData, ConquerTeam); if (teams === undefined || teams === null) { Conquer.fail('Teams cannot be null, server error.'); } if (!(teams instanceof Array)) { throw new Error('Unable to parse team.'); } const teamsSanitized: ConquerTeam[] = []; for (const team of teams) { if (!(team instanceof ConquerTeam)) { console.error('Received null team from server, fix this error.'); continue; } teamsSanitized.push(team); } return teamsSanitized; } catch (error) { console.error(error) throw new Error('Unable to fetch Teams.'); } } public static async getTeam(uuid: string): Promise { const urlTeam = new URL('/conquer/team/' + uuid, window.location.protocol + '//' + window.location.hostname + ':' + window.location.port) try { const response = await fetch(urlTeam) if (response.status !== 200) { throw new Error('Invalid response fetching team.') } const teamData = await response.json() let team = JsonSerializer.deserialize(teamData, ConquerTeam); if (team === undefined) { team = null; } if (!(team instanceof ConquerTeam)) { throw new Error('Unable to parse team.'); } return team; } catch (error) { console.error(error) throw new Error('Unable to fetch Team.'); } } public static async getSelfTeam(): Promise { const urlTeam = new URL('/conquer/user/team', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port) try { const response = await fetch(urlTeam) if (response.status !== 200) { throw new Error('Invalid response fetching team.') } const teamData = await response.json() let team = JsonSerializer.deserialize(teamData, ConquerTeam); if (team === undefined) { team = null; } if (team !== null && !(team instanceof ConquerTeam)) { throw new Error('Unable to parse team.'); } return team; } catch (error) { console.error(error) throw new Error('Unable to fetch Team.'); } } }