structures/Profile.js

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

/**
 * Represents a player on War Thunder
 */
class User {
  constructor(data) {
    this.data = data;
    /**
     * The in-game nickname of the player
     * @type {string}
     * @readonly
     */
    this.nick = data.profile.nick;

    /**
     * The experience level of the player
     * @type {number}
     * @readonly
     */
    this.level = data.profile.level;

    /**
     * The squadron name of the player, if he is in any
     * @type {?string}
     * @readonly
     */
    this.squadron = data.profile.squadron || null;

    /**
     * The title of the player, if he has set any
     * @type {?string}
     * @readonly
     */
    this.title = data.profile.title || null;

    this._patch(data);
  }

  /**
   * The date the player has been registered
   * @type {string}
   * @readonly
   */
  get createdAt() {
    return this.data.profile.registered;
  }

  /**
   * The URL to the player's profile picture
   * @type {?string}
   * @readonly
   */
  get image() {
    return this.data.profile.image ? `${URLs.static}/${this.data.profile.image}` : null;
  }

  toJSON() {
    const obj = {
      nick: this.nick,
      level: this.level,
      squadron: this.squadron,
      title: this.title,
      createdAt: this.createdAt,
      image: this.image
    };
    const countries = [];
    const stats = [];
    for (const country of [...this.countries.values()]) countries.push(country);
    for (const stat of [...this.stats.values()]) stats.push(stat);
    obj.countries = countries;
    obj.stats = stats;

    return obj;
  }

  _patch(data) {
    /**
     * Per-country info for the player
     * @type {Map<string,CountryInfo>}
     */
    this.countries = new Map();
    this.countries.set("usa", data.profile.usa);
    this.countries.set("ussr", data.profile.ussr);
    this.countries.set("britain", data.profile.britain);
    this.countries.set("germany", data.profile.germany);
    this.countries.set("japan", data.profile.japan);
    this.countries.set("italy", data.profile.italy);
    this.countries.set("france", data.profile.france);

    /**
     * The player's statistics
     * @type {Map<string,ProfileStats>}
     */
    this.stats = new Map(Object.entries(data.stats));
  }
}

module.exports = User;