forked from JoanSForgeFlow/p2-typescript-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
263 lines (248 loc) · 11.6 KB
/
main.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { writeFile } from "fs/promises";
import { loadPokemons, Pokemon } from "./pokemon";
import { loadPokemonDetails, PokemonDetails } from "./pokemon-detail"
function toPokemonType(type: string): PokemonType {
return type as PokemonType;
}
function renderPokemonIndex(pokemons: Array<Pokemon>): string {
const pokemonLinks = pokemons.map(
(pokemon) => `
<li>
<a class="pokemon-card" href="${String(pokemon.id).padStart(4, '0')}_${pokemon.name}.html" data-types='${JSON.stringify(pokemon.types)}' data-baby='${pokemon.is_baby}' data-legendary='${pokemon.is_legendary}' data-mythical='${pokemon.is_mythical}' style="background-image: linear-gradient(135deg, ${getTypeColor(toPokemonType(pokemon.types[0]))} 0%, ${getTypeColor(toPokemonType(pokemon.types[0]))} 50%, ${pokemon.types[1] ? getTypeColor(toPokemonType(pokemon.types[1])) : getTypeColor(toPokemonType(pokemon.types[0]))} 50%, ${pokemon.types[1] ? getTypeColor(toPokemonType(pokemon.types[1])) : getTypeColor(toPokemonType(pokemon.types[0]))} 100%);">
<div class="pokemon-id">#${String(pokemon.id).padStart(4, '0')}</div>
<img src="${pokemon.imageUrl}" alt="${pokemon.name}" />
<h2>${pokemon.name.charAt(0).toUpperCase() + pokemon.name.slice(1)}</h2>
</a>
</li>`
).join('\n');
return `
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pokédex</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>Pokédex</h1>
<div class="search-filter-container">
<input type="text" id="search-input" class="search-input" placeholder="Search Pokémon..." />
<select id="type-select" class="type-select">
<option value="">All types</option>
<option value="normal" class="tag normal">Normal</option>
<option value="fighting" class="tag fighting">Fighting</option>
<option value="flying" class="tag flying">Flying</option>
<option value="poison" class="tag poison">Poison</option>
<option value="ground" class="tag ground">Ground</option>
<option value="rock" class="tag rock">Rock</option>
<option value="bug" class="tag bug">Bug</option>
<option value="ghost" class="tag ghost">Ghost</option>
<option value="steel" class="tag steel">Steel</option>
<option value="fire" class="tag fire">Fire</option>
<option value="water" class="tag water">Water</option>
<option value="grass" class="tag grass">Grass</option>
<option value="electric" class="tag electric">Electric</option>
<option value="psychic" class="tag psychic">Psychic</option>
<option value="ice" class="tag ice">Ice</option>
<option value="dragon" class="tag dragon">Dragon</option>
<option value="dark" class="tag dark">Dark</option>
<option value="fairy" class="tag fairy">Fairy</option>
</select>
<div class="special-filter-container">
<input type="checkbox" id="baby-filter" value="baby" />
<label for="baby-filter">Babies</label>
<input type="checkbox" id="legendary-filter" value="legendary" />
<label for="legendary-filter">Legendaries</label>
<input type="checkbox" id="mythical-filter" value="mythical" />
<label for="mythical-filter">Mythics</label>
</div>
</div>
<ul>
${pokemonLinks}
</ul>
<script>
function filterPokemons() {
const searchText = document.getElementById("search-input").value.toLowerCase();
const selectedType = document.getElementById("type-select").value;
const babyFilter = document.getElementById("baby-filter").checked;
const legendaryFilter = document.getElementById("legendary-filter").checked;
const mythicalFilter = document.getElementById("mythical-filter").checked;
const pokemonCards = document.querySelectorAll(".pokemon-card");
for (const pokemonCard of pokemonCards) {
const pokemonName = pokemonCard.querySelector("h2").textContent.toLowerCase();
const pokemonTypes = JSON.parse(pokemonCard.dataset.types);
const isBaby = JSON.parse(pokemonCard.dataset.baby);
const isLegendary = JSON.parse(pokemonCard.dataset.legendary);
const isMythical = JSON.parse(pokemonCard.dataset.mythical);
const nameMatch = searchText === "" || pokemonName.includes(searchText);
const typeMatch = selectedType === "" || pokemonTypes.includes(selectedType);
const babyMatch = !babyFilter || isBaby;
const legendaryMatch = !legendaryFilter || isLegendary;
const mythicalMatch = !mythicalFilter || isMythical;
if (nameMatch && typeMatch && babyMatch && legendaryMatch && mythicalMatch) {
pokemonCard.parentElement.style.display = "block";
} else {
pokemonCard.parentElement.style.display = "none";
}
}
}
document.getElementById("search-input").addEventListener("input", filterPokemons);
document.getElementById("type-select").addEventListener("input", filterPokemons);
document.getElementById("baby-filter").addEventListener("change", filterPokemons);
document.getElementById("legendary-filter").addEventListener("change", filterPokemons);
document.getElementById("mythical-filter").addEventListener("change", filterPokemons);
</script>
</body>
</html>`;
}
function renderPokemonDetail(pokemon: PokemonDetails): string {
const types = (pokemon.types || []).map(type => `<span class="tag ${type.toLowerCase()}">${type}</span>`).join(' ');
const superWeakTo = pokemon.superWeakTo.map(type => `<span class="tag ${type.toLowerCase()}">${type}</span>`).join(' ');
const weakTo = pokemon.weakTo.map(type => `<span class="tag ${type.toLowerCase()}">${type}</span>`).join(' ');
const normal = pokemon.normal.map(type => `<span class="tag ${type.toLowerCase()}">${type}</span>`).join(' ');
const resistantTo = pokemon.resistantTo.map(type => `<span class="tag ${type.toLowerCase()}">${type}</span>`).join(' ');
const superResistantTo = pokemon.superResistantTo.map(type => `<span class="tag ${type.toLowerCase()}">${type}</span>`).join(' ');
const immuneTo = pokemon.immuneTo.map(type => `<span class="tag ${type.toLowerCase()}">${type}</span>`).join(' ');
const heightInMeters = (pokemon.height / 10).toFixed(1) + " m";
const weightInKilograms = (pokemon.weight / 10).toFixed(1) + " kg";
const abilitiesTableRows = pokemon.abilities
.map(ability => `
<tr>
<td class="attribute abilities-text">${ability.name.charAt(0).toUpperCase() + ability.name.slice(1)}:</td>
<td class="value abilities-text">${ability.description}</td>
</tr>`)
.join('\n');
const getStatBar = (value: number, maxValue: number) => {
const percentage = Math.round((value / maxValue) * 100);
const greenThreshold = 35;
const yellowThreshold = 20;
let backgroundColor;
if (percentage >= greenThreshold) {
backgroundColor = 'limegreen';
} else if (percentage >= yellowThreshold) {
backgroundColor = 'gold';
} else {
backgroundColor = 'tomato';
}
return `
<div class="stat-bar-container">
<div class="stat-bar">
<div class="stat-value" style="width: ${percentage}%; background-color: ${backgroundColor};"></div>
</div>
</div>
`;
};
const statsTableRows = pokemon.stats
.map(stat => `
<tr>
<td class="attribute abilities-text">${stat.name.charAt(0).toUpperCase() + stat.name.slice(1)}:</td>
<td class="value abilities-text">${stat.value}</td>
<td class="value abilities-bar">${getStatBar(stat.value, 255)}</td>
</tr>`)
.join('\n');
return `
<html>
${head(pokemon.name)}
<body>
<h1>${pokemon.name.charAt(0).toUpperCase() + pokemon.name.slice(1)}</h1>
<h2 class="section-title">Pokédex Description</h2>
<p class="description">${pokemon.pokedexDescription}</p>
<div class="pokemon-container">
<img src="${pokemon.officialArtworkUrl}" alt="${pokemon.name}" />
<table>
<tr><td class="attribute">Type:</td><td class="value">${types}</td></tr>
<tr><td class="attribute">Height:</td><td class="value">${heightInMeters}</td></tr>
<tr><td class="attribute">Weight:</td><td class="value">${weightInKilograms}</td></tr>
<tr><td class="attribute">Super Weak To:</td><td class="value">${superWeakTo}</td></tr>
<tr><td class="attribute">Weak To:</td><td class="value">${weakTo}</td></tr>
<tr><td class="attribute">Normal Damage:</td><td class="value">${normal}</td></tr>
<tr><td class="attribute">Resistant To:</td><td class="value">${resistantTo}</td></tr>
<tr><td class="attribute">Super Resistant To:</td><td class="value">${superResistantTo}</td></tr>
<tr><td class="attribute">Immune To:</td><td class="value">${immuneTo}</td></tr>
</table>
</div>
<div class="table-container">
<table class="abilities-table">
<tr>
<th colspan="2" class="section-title-cell">Pokémon Abilities</th>
</tr>
${abilitiesTableRows}
</table>
<table>
<tr>
<th colspan="3" class="section-title-cell">Pokémon Stats</th>
</tr>
${statsTableRows}
</table>
</div>
<a href="index.html" class="back-button">Back to Menu</a>
</body>
</html>`;
}
type PokemonType =
| 'normal'
| 'fighting'
| 'flying'
| 'poison'
| 'ground'
| 'rock'
| 'bug'
| 'ghost'
| 'steel'
| 'fire'
| 'water'
| 'grass'
| 'electric'
| 'psychic'
| 'ice'
| 'dragon'
| 'dark'
| 'fairy';
function getTypeColor(type: PokemonType): string {
const colors: Record<PokemonType, string> = {
normal: "rgba(168, 168, 120, 0.5)",
fighting: "rgba(192, 48, 40, 0.5)",
flying: "rgba(168, 144, 240, 0.5)",
poison: "rgba(160, 64, 160, 0.5)",
ground: "rgba(224, 192, 104, 0.5)",
rock: "rgba(184, 160, 56, 0.5)",
bug: "rgba(168, 184, 32, 0.5)",
ghost: "rgba(112, 88, 152, 0.5)",
steel: "rgba(184, 184, 208, 0.5)",
fire: "rgba(240, 128, 48, 0.5)",
water: "rgba(104, 144, 240, 0.5)",
grass: "rgba(120, 200, 80, 0.5)",
electric: "rgba(248, 208, 48, 0.5)",
psychic: "rgba(248, 88, 136, 0.5)",
ice: "rgba(152, 216, 216, 0.5)",
dragon: "rgba(112, 56, 248, 0.5)",
dark: "rgba(112, 88, 72, 0.5)",
fairy: "rgba(238, 153, 172, 0.5)",
};
return colors[type] || "#ccc";
}
function head(title: string): string {
return `
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<link rel="stylesheet" href="css/pokemon_styles.css">
</head>`;
}
(async () => {
const pokemons = await loadPokemons(386);
const indexHtml = renderPokemonIndex(pokemons);
await writeFile("index.html", indexHtml);
for (const pokemon of pokemons) {
const pokemonDetail = await loadPokemonDetails(pokemon.id);
if (!pokemonDetail) {
console.warn(`Failed to load details for Pokémon with ID ${pokemon.id}.`);
continue;
}
const detailHtml = renderPokemonDetail(pokemonDetail);
await writeFile(`${String(pokemonDetail.id).padStart(4, '0')}_${pokemonDetail.name}.html`, detailHtml);
}
})();