Skip to content

Commit

Permalink
feat: use raw data for units (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Nov 25, 2021
1 parent 3a1efb5 commit aa16962
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/struct/ClanWarLeagueGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class ClanWarLeagueGroup {
/**
* This returns an array of {@link ClanWar} which fetches all wars in parallel.
* @param clanTag Optional clan tag. If present, this will only return wars which belong to this clan.
* @param options Override options for the request.
*/
public async getWars(clanTag?: string, options?: OverrideOptions) {
const rounds = this.rounds.filter((round) => !round.warTags.includes('#0'));
Expand Down Expand Up @@ -126,7 +127,7 @@ export class ClanWarLeagueGroup {
.filter((war) => war.clan.tag === clanTag);
}

/** Returns # (1-7) of the round for the specified warTag. */
/** Returns the index of the round for this specified warTag. */
public getRoundIndex(warTag: string): number | null {
return this.rounds.find((round) => round.warTags.includes(warTag))?.round ?? null;
}
Expand Down
10 changes: 7 additions & 3 deletions src/struct/Player.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HERO_PETS, SIEGE_MACHINES, UNRANKED_LEAGUE_DATA } from '../util/Constants';
import { RAW_SUPER_UNITS, RAW_UNITS } from '../util/raw.json';
import { OverrideOptions } from '../rest/RequestHandler';
import { LegendStatistics } from './LegendStatistics';
import { Achievement } from './Achievement';
Expand All @@ -9,6 +10,8 @@ import { APIPlayer } from '../types';
import { League } from './League';
import { Label } from './Label';

const UNIT_NAMES = [...RAW_UNITS.map((unit) => unit.name), ...RAW_SUPER_UNITS.map((unit) => unit.name)];

/** Represents a Clash of Clans Player. */
export class Player {
/** The player's name. */
Expand Down Expand Up @@ -114,9 +117,10 @@ export class Player {
this.legendStatistics = data.legendStatistics ? new LegendStatistics(data.legendStatistics) : null;
this.achievements = data.achievements.map((data) => new Achievement(data));
this.labels = data.labels.map((data) => new Label(data));
this.troops = data.troops.map((data) => new Troop(data));
this.spells = data.spells.map((data) => new Spell(data));
this.heroes = data.heroes.map((data) => new Hero(data));

this.troops = data.troops.filter((unit) => UNIT_NAMES.includes(unit.name)).map((unit) => new Troop(data, unit));
this.spells = data.spells.filter((unit) => UNIT_NAMES.includes(unit.name)).map((unit) => new Spell(data, unit));
this.heroes = data.heroes.filter((unit) => UNIT_NAMES.includes(unit.name)).map((unit) => new Hero(data, unit));
}

/** Fetch detailed clan info for the player's clan. */
Expand Down
121 changes: 110 additions & 11 deletions src/struct/Unit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RAW_SUPER_UNITS, RAW_UNITS } from '../util/raw.json';
import { SUPER_TROOPS } from '../util/Constants';
import { APIPlayerItem } from '../types';
import { APIPlayerItem, APIPlayer } from '../types';

/** Represents a player's unit. */
export class Unit {
Expand All @@ -15,11 +16,99 @@ export class Unit {
/** The village type of this unit. */
public village: 'home' | 'builderBase';

public constructor(data: APIPlayerItem) {
this.name = data.name;
this.level = data.level;
this.maxLevel = data.maxLevel;
this.village = data.village;
// #region static

/** Id of this unit. */
public id!: number;

/** Housing space of this unit. */
public housingSpace!: number;

/** Town/Builder hall's max level of this unit. */
public hallMaxLevel!: number;

/** Unlock Town/Builder Hall level of this unit. */
public unlockHallLevel!: number;

/** Unlock cost of this unit. */
public unlockCost!: number;

/** Unlock time of this unit. */
public unlockTime!: number;

/** Unlock resource of this unit. */
public unlockResource!: string;

/** Unlock building of this unit. */
public unlockBuilding!: string;

/** Unlock building level of this unit. */
public unlockBuildingLevel!: number;

/** Upgrade cost of this unit. */
public upgradeCost!: number;

/** Upgrade resource of this unit. */
public upgradeResource!: string;

/** Upgrade time of this unit. */
public upgradeTime!: number;

/** @internal */
public minOriginalLevel!: number | null;
/** @internal */
public originalName!: string | null;

// #endregion static

public constructor(data: APIPlayer, unit: APIPlayerItem) {
this.name = unit.name;
this.level = unit.level;
this.maxLevel = unit.maxLevel;
this.village = unit.village;

const rawSuperUnit = RAW_SUPER_UNITS.find((unit) => unit.name === this.name && this.isHomeBase);
const rawUnit = RAW_UNITS.find((unit) => unit.name === this.name && unit.village === this.village);

if (rawSuperUnit) {
this.id = rawSuperUnit.id;
this.housingSpace = rawSuperUnit.housingSpace;

this.originalName = rawSuperUnit.original;
this.minOriginalLevel = rawSuperUnit.minOriginalLevel;

const original = RAW_UNITS.find((unit) => unit.village === 'home' && unit.name === rawSuperUnit.original)!;
this.unlockHallLevel = original.levels.findIndex((level) => level >= rawSuperUnit.minOriginalLevel) + 1;
this.unlockCost = original.unlock.cost;
this.unlockTime = original.unlock.time;
this.unlockResource = original.unlock.resource;
this.unlockBuilding = original.unlock.building;
this.unlockBuildingLevel = original.unlock.buildingLevel;

const origin = data.troops.find((troop) => troop.village === 'home' && troop.name === original.name)!;
this.level = origin.level;
this.maxLevel = origin.maxLevel;

this.upgradeCost = original.upgrade.cost[origin.level - 1] ?? 0;
this.upgradeResource = original.upgrade.resource;
this.upgradeTime = original.upgrade.time[origin.level - 1] ?? 0;
this.hallMaxLevel = original.levels[data.townHallLevel - 1];
}

if (rawUnit) {
this.id = rawUnit.id;
this.housingSpace = rawUnit.housingSpace;
this.unlockCost = rawUnit.unlock.cost;
this.unlockTime = rawUnit.unlock.time;
this.unlockResource = rawUnit.unlock.resource;
this.unlockBuilding = rawUnit.unlock.building;
this.unlockHallLevel = rawUnit.unlock.hall;
this.unlockBuildingLevel = rawUnit.unlock.buildingLevel;
this.upgradeResource = rawUnit.upgrade.resource;
this.upgradeCost = rawUnit.upgrade.cost[this.level - 1] ?? 0;
this.upgradeTime = rawUnit.upgrade.time[this.level - 1] ?? 0;
this.hallMaxLevel = rawUnit.levels[(this.village === 'home' ? data.townHallLevel : data.builderHallLevel!) - 1];
}
}

/** Whether the unit belongs to the home base. */
Expand Down Expand Up @@ -47,16 +136,26 @@ export class Unit {
export class Troop extends Unit {
public name!: string;

public superTroopIsActive: boolean;
/** Whether this troop is currently active of boosted. */
public isActive: boolean;

/** Origin troop's minimum level of this super troop. */
public minOriginalLevel!: number | null;

/** Origin troop's name of this super troop. */
public originalName!: string | null;

public constructor(data: APIPlayer, unit: APIPlayerItem) {
super(data, unit);

public constructor(data: APIPlayerItem) {
super(data);
this.superTroopIsActive = data.superTroopIsActive ?? false;
this.originalName = this.originalName ?? null;
this.isActive = unit.superTroopIsActive ?? false;
this.minOriginalLevel = this.minOriginalLevel ?? null;
}

/** Whether this troop is a Super Troop. */
public get isSuperTroop() {
return this.superTroopIsActive || (this.isHomeBase && SUPER_TROOPS.includes(this.name));
return this.isActive || (this.isHomeBase && SUPER_TROOPS.includes(this.name));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/util/raw.json

Large diffs are not rendered by default.

0 comments on commit aa16962

Please sign in to comment.