Skip to content

Commit 4967cd5

Browse files
authored
feat: use static data for constants (#205)
* feat: use static data for constants * fix: package.json
1 parent 5255186 commit 4967cd5

File tree

5 files changed

+31
-117
lines changed

5 files changed

+31
-117
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "clashofclans.js",
3-
"version": "3.3.18",
3+
"version": "3.3.19",
44
"description": "JavaScript library for interacting with the Clash of Clans API",
55
"author": "https://clashofclans.js.org",
66
"license": "MIT",

src/struct/Player.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Label } from './Label';
77
import { League } from './League';
88
import { LegendStatistics } from './LegendStatistics';
99
import { PlayerClan } from './PlayerClan';
10-
import { Hero, HeroEquipment, Spell, Troop } from './Unit';
10+
import { Equipment, Hero, Spell, Troop } from './Unit';
1111

1212
/** Represents a Clash of Clans Player. */
1313
export class Player {
@@ -90,7 +90,7 @@ export class Player {
9090
public heroes: Hero[];
9191

9292
/** An array of player's hero equipment. */
93-
public heroEquipment: HeroEquipment[];
93+
public heroEquipment: Equipment[];
9494

9595
/** The player's clan capital house details. */
9696
public playerHouse?: APIPlayerHouse | null;
@@ -128,7 +128,7 @@ export class Player {
128128
this.troops = data.troops.map((unit) => new Troop(data, unit));
129129
this.spells = data.spells.map((unit) => new Spell(data, unit));
130130
this.heroes = data.heroes.map((unit) => new Hero(data, unit));
131-
this.heroEquipment = data.heroEquipment.map((unit) => new HeroEquipment(data, unit));
131+
this.heroEquipment = data.heroEquipment.map((unit) => new Equipment(data, unit));
132132

133133
this.playerHouse = data.playerHouse ?? null;
134134
}

src/struct/Unit.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class Unit {
7474
/** @internal */
7575
public originalName!: string | null;
7676
/** @internal */
77-
public equipment!: HeroEquipment[];
77+
public equipment!: Equipment[];
7878

7979
// #endregion static
8080

@@ -136,7 +136,7 @@ export class Unit {
136136
if (rawUnit.category === 'hero') this.regenerationTime = rawUnit.regenerationTimes[this.level - 1] ?? 0;
137137
this.hallMaxLevel =
138138
rawUnit.levels[(this.village === 'home' ? data.townHallLevel : data.builderHallLevel!) - 1] ?? this.maxLevel;
139-
this.equipment = (unit.equipment ?? []).map((unit) => new HeroEquipment(data, unit));
139+
this.equipment = (unit.equipment ?? []).map((unit) => new Equipment(data, unit));
140140
}
141141

142142
this.seasonal = Boolean(rawUnit?.seasonal);
@@ -199,8 +199,8 @@ export class Hero extends Unit {
199199
public regenerationTime!: number;
200200

201201
/** Hero Equipment */
202-
public equipment!: HeroEquipment[];
202+
public equipment!: Equipment[];
203203
}
204204

205205
/** Represents a Player's Hero Equipment. */
206-
export class HeroEquipment extends Unit {}
206+
export class Equipment extends Unit {}

src/util/Constants.ts

+21-107
Original file line numberDiff line numberDiff line change
@@ -3,125 +3,39 @@ import RAW from '../util/raw.json';
33
export const APIBaseURL = 'https://api.clashofclans.com/v1';
44
export const DevSiteAPIBaseURL = 'https://developer.clashofclans.com/api';
55

6-
export const ElixirTroops = [
7-
'Barbarian',
8-
'Archer',
9-
'Giant',
10-
'Goblin',
11-
'Wall Breaker',
12-
'Balloon',
13-
'Wizard',
14-
'Healer',
15-
'Dragon',
16-
'P.E.K.K.A',
17-
'Baby Dragon',
18-
'Miner',
19-
'Electro Dragon',
20-
'Yeti',
21-
'Dragon Rider',
22-
'Electro Titan',
23-
'Root Rider',
24-
'Thrower'
25-
];
6+
export const SuperTroops = RAW.RAW_SUPER_UNITS.map((unit) => unit.name);
267

27-
export const DarkElixirTroops = [
28-
'Minion',
29-
'Hog Rider',
30-
'Valkyrie',
31-
'Golem',
32-
'Witch',
33-
'Lava Hound',
34-
'Bowler',
35-
'Ice Golem',
36-
'Headhunter',
37-
'Apprentice Warden',
38-
'Druid'
39-
];
8+
export const ElixirTroops = RAW.RAW_UNITS.filter(
9+
(unit) => !SuperTroops.includes(unit.name) && unit.subCategory === 'troop' && unit.upgrade.resource === 'Elixir'
10+
).map((unit) => unit.name);
11+
12+
export const DarkElixirTroops = RAW.RAW_UNITS.filter(
13+
(unit) => !SuperTroops.includes(unit.name) && unit.subCategory === 'troop' && unit.upgrade.resource === 'Dark Elixir'
14+
).map((unit) => unit.name);
4015

4116
export const HomeTroops = [...ElixirTroops, ...DarkElixirTroops];
4217

43-
export const SiegeMachines = [
44-
'Wall Wrecker',
45-
'Battle Blimp',
46-
'Stone Slammer',
47-
'Siege Barracks',
48-
'Log Launcher',
49-
'Flame Flinger',
50-
'Battle Drill'
51-
];
18+
export const SiegeMachines = RAW.RAW_UNITS.filter((unit) => unit.subCategory === 'siege').map((unit) => unit.name);
5219

53-
export const SuperTroops = [
54-
'Super Barbarian',
55-
'Super Archer',
56-
'Super Giant',
57-
'Sneaky Goblin',
58-
'Super Wall Breaker',
59-
'Rocket Balloon',
60-
'Super Wizard',
61-
'Super Dragon',
62-
'Inferno Dragon',
63-
'Super Minion',
64-
'Super Valkyrie',
65-
'Super Witch',
66-
'Ice Hound',
67-
'Super Bowler',
68-
'Super Miner',
69-
'Super Hog Rider'
70-
];
20+
export const HeroEquipment = RAW.RAW_UNITS.filter((unit) => unit.subCategory === 'equipment').map((unit) => unit.name);
7121

72-
export const ElixirSpells = [
73-
'Lightning Spell',
74-
'Healing Spell',
75-
'Rage Spell',
76-
'Jump Spell',
77-
'Freeze Spell',
78-
'Clone Spell',
79-
'Invisibility Spell',
80-
'Recall Spell',
81-
'Revive Spell'
82-
];
22+
export const ElixirSpells = RAW.RAW_UNITS.filter((unit) => unit.subCategory === 'spell' && unit.upgrade.resource === 'Elixir').map(
23+
(unit) => unit.name
24+
);
8325

84-
export const DarkElixirSpells = ['Poison Spell', 'Earthquake Spell', 'Haste Spell', 'Skeleton Spell', 'Bat Spell', 'Overgrowth Spell'];
26+
export const DarkElixirSpells = RAW.RAW_UNITS.filter((unit) => unit.subCategory === 'spell' && unit.upgrade.resource === 'Dark Elixir').map(
27+
(unit) => unit.name
28+
);
8529

8630
export const Spells = [...ElixirSpells, ...DarkElixirSpells];
8731

88-
export const BuilderTroops = [
89-
'Raged Barbarian',
90-
'Sneaky Archer',
91-
'Boxer Giant',
92-
'Beta Minion',
93-
'Bomber',
94-
'Baby Dragon',
95-
'Cannon Cart',
96-
'Night Witch',
97-
'Drop Ship',
98-
'Super P.E.K.K.A',
99-
'Hog Glider',
100-
'Electrofire Wizard'
101-
];
32+
export const BuilderTroops = RAW.RAW_UNITS.filter((unit) => unit.subCategory === 'troop' && unit.village === 'builderBase').map(
33+
(unit) => unit.name
34+
);
10235

103-
export const Heroes = [
104-
'Barbarian King',
105-
'Archer Queen',
106-
'Grand Warden',
107-
'Royal Champion',
108-
'Minion Prince',
109-
'Battle Machine',
110-
'Battle Copter'
111-
];
36+
export const Heroes = RAW.RAW_UNITS.filter((unit) => unit.subCategory === 'hero').map((unit) => unit.name);
11237

113-
export const HeroPets = [
114-
'L.A.S.S.I',
115-
'Electro Owl',
116-
'Mighty Yak',
117-
'Unicorn',
118-
'Poison Lizard',
119-
'Diggy',
120-
'Frosty',
121-
'Phoenix',
122-
'Spirit Fox',
123-
'Angry Jelly'
124-
];
38+
export const HeroPets = RAW.RAW_UNITS.filter((unit) => unit.subCategory === 'pet').map((unit) => unit.name);
12539

12640
export const UnrankedLeagueData = {
12741
id: 29000000,

0 commit comments

Comments
 (0)