structures/Clan.js

const { URLs } = require("../util/Constants");

/**
 * Represents a clan (squadron) on War Thunder
 */
class Clan {
  constructor(data) {
    this.data = data;

    /**
     * The squadron name
     * @type {string}
     * @readonly
     */
    this.name = data.name;

    /**
     * The amount of players in the squadron
     * @type {number}
     * @readonly
     */
    this.players = data.players;

    /**
     * The squadron description
     * @type {string}
     * @readonly
     */
    this.description = data.description;

    /**
     * The squadron tag
     */
    this.tag = data.tag;

    this._patch(data);
  }

  /**
   * The date of creation of the squadron
   * @type {string}
   * @readonly
   */
  get createdAt() {
    return this.data.createdAt;
  }

  /**
   * The URL to the squadron's in-game avatar
   * @type {string}
   * @readonly
   */
  get avatar() {
    return `${URLs.static}${this.data.image}`;
  }

  toJSON() {
    const obj = {
      name: this.name,
      tag: this.tag,
      players: this.players,
      description: this.description,
      createdAt: this.createdAt,
      avatar: this.avatar,
      airkills: [this.airkills.get("arcade"), this.airkills.get("realistic"), this.airkills.get("simulator")],
      groundkills: [this.groundkills.get("arcade"), this.groundkills.get("realistic"), this.groundkills.get("simulator")],
      deaths: [this.deaths.get("arcade"), this.deaths.get("realistic"), this.deaths.get("simulator")],
      flighttime: [this.flighttime.get("arcade"), this.flighttime.get("realistic"), this.flighttime.get("simulator")]
    };
    const arr = [];
    for (const member of this.members.values()) arr.push(member);
    obj.members = arr;
    return obj;
  }

  _patch(data) {
    /**
     * The amount of air targets destroyed, mapped by gamemode
     * @type {Map<string,SquadronDifficultyStats>}
     */
    this.airkills = new Map(Object.entries(data.airKills));

    /**
     * The amount of ground targets destroyed, mapped by gamemode
     * @type {Map<string,SquadronDifficultyStats>}
     */
    this.groundkills = new Map(Object.entries(data.groundKills));

    /**
     * The amount of deaths, mapped by gamemode
     * @type {Map<string,SquadronDifficultyStats>}
     */
    this.deaths = new Map(Object.entries(data.deaths));

    /**
     * The total battle time, mapped by gamemode
     * @type {Map<string,SquadronDifficultyStats>}
     */
    this.flighttime = new Map(Object.entries(data.flightTime));

    /**
     * Get the amount of members, mapped by name
     * @type {Map<string,SquadronMemberInfo>}
     */
    this.members = new Map();

    for (const member of data.members) this.members.set(member.name, member);
  }
}

module.exports = Clan;