-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleague-champion.service.ts
241 lines (226 loc) · 8.01 KB
/
league-champion.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Champion } from '../models/champion.model';
import { HashTable } from '../models/hashTable.model';
import { Language } from '../models/language.model';
@Injectable({
providedIn: 'root',
})
export class LeagueChampionService {
champions!: HashTable<Champion[]>;
languages: Language[];
language: Language = {
name: 'English',
code: 'en_US',
charged: true,
sliced_code: 'en',
};
version!: string;
role: string = '';
search: string = '';
constructor(private router: Router) {
this.languages = [];
this.champions = {};
//wait for getVerison to finish
this.getVersion().then(() => {
this.fetchAllLanguages().then(() => {
this.languages[0].charged = true;
this.fetchAllChampions(this.language.code);
});
});
}
async getVersion(): Promise<void> {
//get the current version of the game
const versionResponse = await fetch(
'http://ddragon.leagueoflegends.com/api/versions.json'
);
const version = (await versionResponse.json())[0];
this.version = version;
}
async fetchAllChampions(language: string): Promise<void> {
console.log('fetchAllChampions');
let temp_champions: Champion[] = [];
let i = 0;
//get all champions for the given language
const fetched_champions = await this.getAllChamps(language);
for (const champion in fetched_champions) {
const champId = fetched_champions[champion].id;
//get champion data for the given champion id and language
const champResponse = await fetch(
`http://ddragon.leagueoflegends.com/cdn/${this.version}/data/${this.language.code}/champion/${champId}.json`
);
if (!champResponse.ok) {
throw 'Error :' + champResponse.status;
}
const champData = await champResponse.json();
const champ = champData.data[champId];
let skinChampId = champ.id;
//set the name of the champion for the default skin
champ.skins[0].name = champ.name;
//fix exception on Fiddlesticks
if (champ.id == 'Fiddlesticks') {
skinChampId = 'FiddleSticks';
}
const roles = champ.tags;
const mainImage =
'http://ddragon.leagueoflegends.com/cdn/img/champion/splash/' +
champ.id +
'_0.jpg';
const icon =
`http://ddragon.leagueoflegends.com/cdn/${this.version}/img/champion/` +
champ.id +
'.png';
//create a champion object with all the data
let championObj: Champion = new Champion(
i,
champ.name,
champ.lore,
mainImage,
icon,
this.getSkins(champ.skins, skinChampId),
0,
false,
true,
roles
);
temp_champions.push(championObj);
i++;
}
//when all champions are fetched, go to the home page
this.router.navigateByUrl('/');
this.language.charged = true;
this.champions[language.slice(0, 2)] = temp_champions;
}
async fetchAllLanguages(): Promise<void> {
const global_res = await fetch(
'http://ddragon.leagueoflegends.com/cdn/languages.json'
);
if (!global_res.ok) {
throw 'Error :' + global_res.status;
}
let regionNames = new Intl.DisplayNames(['en'], { type: 'region' });
const data = await global_res.json();
for (const language of data) {
const regionCode = language.slice(-2);
//get the name of the region from the region code
const name = regionNames.of(regionCode);
const code = language;
if (name != undefined) {
const languageObject: Language = {
name: name,
code: code,
charged: false,
sliced_code: code.slice(0, 2),
};
//fix exception on Indonesian
if (languageObject.code != 'id_ID') {
this.languages.push(languageObject);
}
}
}
}
getSkins(skins: any[], skinChampId: number): any[] {
return skins.map((skin) => {
return {
num: skin.num,
name: skin.name,
splashPath:
'http://ddragon.leagueoflegends.com/cdn/img/champion/splash/' +
skinChampId +
'_' +
skin.num +
'.jpg',
chromas: skin.chromas,
};
});
}
async getAllChamps(language: string): Promise<any[]> {
const global_res = await fetch(
`http://ddragon.leagueoflegends.com/cdn/${this.version}/data/${language}/champion.json`
);
if (!global_res.ok) {
throw 'Error :' + global_res.status;
}
const data = await global_res.json();
const fetched_champions = data.data;
return fetched_champions;
}
getAllLanguages(): Language[] {
return this.languages;
}
changeLanguage(language: string): void {
this.language = this.languages.find(
(lang) => lang.code == language
) as Language;
//if the language is not charged, fetch all champions for the given language
if (this.champions[language.slice(0, 2)] == undefined) {
this.fetchAllChampions(this.language.code);
} else {
this.language.charged = true;
this.router.navigateByUrl('/');
}
}
getChampionByName(name: string): Champion | null {
for (let champion in this.champions[this.language.sliced_code]) {
if (
this.champions[this.language.sliced_code][champion].name == name
) {
return this.champions[this.language.sliced_code][champion];
}
}
return null;
}
getAllChampions(): Champion[] {
return this.champions[this.language.sliced_code];
}
filterChampions(): void {
if (this.champions == undefined) {
return;
}
for (let champion in this.champions[this.language.sliced_code]) {
//if champion name contains the filter string
if (
this.champions[this.language.sliced_code][champion].name
.toLowerCase()
.includes(this.search.toLowerCase())
) {
//if the champion has the role selected or if no role is selected
if (
(this.role != '' &&
this.champions[this.language.sliced_code][
champion
].roles.includes(this.role)) ||
this.role == ''
) {
this.champions[this.language.sliced_code][champion].show =
true;
} else {
this.champions[this.language.sliced_code][champion].show =
false;
}
} else {
this.champions[this.language.sliced_code][champion].show =
false;
}
}
}
setRole(role: string): void {
this.role = role;
this.filterChampions();
}
setSearch(search: string): void {
this.search = search;
this.filterChampions();
}
resetRole(): void {
this.role = '';
this.filterChampions();
}
resetSearch(): void {
this.search = '';
this.filterChampions();
}
searchIsEmpty(): boolean {
return this.search == '' && this.role == '';
}
}