| 1 |
import { random } from "jga-algorithms-random"; |
| 2 |
|
| 3 |
import { |
| 4 |
DifficultyEnum, |
| 5 |
Encounter, |
| 6 |
Hero, |
| 7 |
HeroEnum, |
| 8 |
Module, |
| 9 |
ModuleEnum, |
| 10 |
ResultEnum, |
| 11 |
Scenario, |
| 12 |
ScenarioEnum, |
| 13 |
} from "./@models"; |
| 14 |
import { EncountersService, HeroesService, ModulesService, ScenariosService } from "./services"; |
| 15 |
|
| 16 |
window.onload = async (): Promise<void> => { |
| 17 |
const body = document.querySelector("body") as HTMLBodyElement; |
| 18 |
const data: [Encounter[], Hero[], Module[], Scenario[]] = await load(); |
| 19 |
|
| 20 |
const encounters: Encounter[] = data[0]; |
| 21 |
const heroes: Hero[] = data[1]; |
| 22 |
const modules: Module[] = data[2]; |
| 23 |
const scenarios: Scenario[] = data[3]; |
| 24 |
|
| 25 |
loadRandomizer(body, encounters, heroes, scenarios, modules); |
| 26 |
loadEncounters(body, encounters); |
| 27 |
loadStats(body, encounters, heroes, scenarios); |
| 28 |
loadProgression(body, heroes, scenarios, encounters); |
| 29 |
loadData(body, heroes, modules, scenarios); |
| 30 |
}; |
| 31 |
|
| 32 |
const load = async (): Promise<[Encounter[], Hero[], Module[], Scenario[]]> => { |
| 33 |
// load json data |
| 34 |
const encountersService = new EncountersService(); |
| 35 |
const encounters = await encountersService.load(); |
| 36 |
|
| 37 |
const heroesService = new HeroesService(); |
| 38 |
const heroes = await heroesService.load(); |
| 39 |
|
| 40 |
const modulesService = new ModulesService(); |
| 41 |
const modules = modulesService.loadFromEnum(ModuleEnum); |
| 42 |
|
| 43 |
const scenariosService = new ScenariosService(); |
| 44 |
const scenarios = scenariosService.loadFromEnum(ScenarioEnum); |
| 45 |
|
| 46 |
return [encounters, heroes, modules, scenarios]; |
| 47 |
}; |
| 48 |
|
| 49 |
const createTitle = (title: string, body: HTMLBodyElement) => { |
| 50 |
const header = document.createElement("h2"); |
| 51 |
header.innerText = title; |
| 52 |
|
| 53 |
body.appendChild(header); |
| 54 |
}; |
| 55 |
|
| 56 |
const createSubTitle = (title: string, body: HTMLBodyElement) => { |
| 57 |
const header = document.createElement("h3"); |
| 58 |
header.innerText = title; |
| 59 |
|
| 60 |
body.appendChild(header); |
| 61 |
}; |
| 62 |
|
| 63 |
const createSection = (title: string, data: Hero[] | Scenario[] | Module[], body: HTMLBodyElement) => { |
| 64 |
createSubTitle(title, body); |
| 65 |
|
| 66 |
const section = document.createElement("section"); |
| 67 |
|
| 68 |
const list = document.createElement("ul"); |
| 69 |
|
| 70 |
data.forEach((item) => { |
| 71 |
const element = document.createElement("li"); |
| 72 |
element.innerText = `${item.name}`; |
| 73 |
|
| 74 |
list.appendChild(element); |
| 75 |
}); |
| 76 |
|
| 77 |
section.appendChild(list); |
| 78 |
|
| 79 |
body.appendChild(section); |
| 80 |
}; |
| 81 |
|
| 82 |
const createStatsTable = ( |
| 83 |
data: Map<string, number>, |
| 84 |
source: Hero[] | Scenario[], |
| 85 |
wins: Map<string, number>, |
| 86 |
body: HTMLBodyElement, |
| 87 |
) => { |
| 88 |
const table = document.createElement("table"); |
| 89 |
|
| 90 |
source.forEach((item) => { |
| 91 |
const itemLine = document.createElement("tr"); |
| 92 |
const itemName = document.createElement("td"); |
| 93 |
const itemCount = document.createElement("td"); |
| 94 |
const itemRate = document.createElement("td"); |
| 95 |
|
| 96 |
const gameNumber = (data.has(item.name) ? data.get(item.name) : 0) as number; |
| 97 |
const winRate = ( |
| 98 |
gameNumber !== 0 ? ((wins.has(item.name) ? wins.get(item.name) : 0) / gameNumber) * 100 : 0 |
| 99 |
) as number; |
| 100 |
|
| 101 |
itemName.innerText = item.name; |
| 102 |
itemCount.innerText = gameNumber.toString(); |
| 103 |
itemRate.innerText = ` (${winRate.toFixed(0).toString()} %) `; |
| 104 |
|
| 105 |
itemLine.appendChild(itemName); |
| 106 |
itemLine.appendChild(itemCount); |
| 107 |
itemLine.appendChild(itemRate); |
| 108 |
|
| 109 |
table.appendChild(itemLine); |
| 110 |
}); |
| 111 |
|
| 112 |
body.appendChild(table); |
| 113 |
}; |
| 114 |
|
| 115 |
const loadRandomizer = ( |
| 116 |
body: HTMLBodyElement, |
| 117 |
encounters: Encounter[], |
| 118 |
heroes: Hero[], |
| 119 |
scenarios: Scenario[], |
| 120 |
modules: Module[], |
| Error |
Row 121, Column 3: "Refactor this function to reduce its Cognitive Complexity from 36 to the 15 allowed."
sonarjs/cognitive-complexity
|
| 121 |
) => { |
| 122 |
createTitle("Randomizer", body); |
| 123 |
|
| 124 |
const gamesHeroes = encounters |
| 125 |
.flatMap((encounter) => [encounter.hero1, encounter.hero2]) |
| 126 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 127 |
|
| 128 |
const gamesModules = encounters |
| 129 |
.map((encounter) => encounter.module) |
| 130 |
.map((module) => module) |
| 131 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 132 |
|
| 133 |
const gamesScenarios = encounters |
| 134 |
.map((encounter) => encounter.scenario) |
| 135 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 136 |
|
| 137 |
const base = encounters.length * 5; |
| 138 |
|
| 139 |
const weightedScenarios: Scenario[] = []; |
| 140 |
const weightedModules: Module[] = []; |
| 141 |
const weightedHeroes: Hero[] = []; |
| 142 |
|
| 143 |
scenarios.forEach((scenario) => { |
| 144 |
const pastOccurrences = gamesScenarios.has(scenario.name) ? gamesScenarios.get(scenario.name) : 0; |
| 145 |
|
| 146 |
for (let i = 0; i < base - pastOccurrences; i++) { |
| 147 |
weightedScenarios.push(scenario); |
| 148 |
} |
| 149 |
}); |
| 150 |
|
| 151 |
modules.forEach((module) => { |
| 152 |
const pastOccurrences = gamesModules.has(module.name) ? gamesModules.get(module.name) : 0; |
| 153 |
|
| 154 |
for (let i = 0; i < base - pastOccurrences; i++) { |
| 155 |
weightedModules.push(module); |
| 156 |
} |
| 157 |
}); |
| 158 |
|
| 159 |
heroes.forEach((hero) => { |
| 160 |
const pastOccurrences = gamesHeroes.has(hero.name) ? gamesHeroes.get(hero.name) : 0; |
| 161 |
|
| 162 |
for (let i = 0; i < base - pastOccurrences; i++) { |
| 163 |
weightedHeroes.push(hero); |
| 164 |
} |
| 165 |
}); |
| 166 |
|
| 167 |
createSubTitle("Scenario", body); |
| 168 |
const randomScenario = weightedScenarios[random(0, weightedScenarios.length - 1)]; |
| 169 |
const scenarioElement = document.createElement("select"); |
| 170 |
const scenarioOption = document.createElement("option"); |
| 171 |
|
| 172 |
scenarioElement.appendChild(scenarioOption); |
| 173 |
|
| 174 |
for (const item in ScenarioEnum) { |
| 175 |
const option = document.createElement("option"); |
| 176 |
option.value = ScenarioEnum[item]; |
| 177 |
option.innerText = ScenarioEnum[item]; |
| 178 |
|
| 179 |
if (randomScenario.name === ScenarioEnum[item]) { |
| 180 |
option.selected = true; |
| 181 |
} |
| 182 |
|
| 183 |
scenarioElement.appendChild(option); |
| 184 |
} |
| 185 |
body.appendChild(scenarioElement); |
| 186 |
|
| 187 |
createSubTitle("Module", body); |
| 188 |
const randomModule = weightedModules[random(0, weightedModules.length - 1)]; |
| 189 |
const moduleElement = document.createElement("select"); |
| 190 |
const moduleOption = document.createElement("option"); |
| 191 |
|
| 192 |
moduleElement.appendChild(moduleOption); |
| 193 |
|
| 194 |
for (const item in ModuleEnum) { |
| 195 |
const option = document.createElement("option"); |
| 196 |
option.value = ModuleEnum[item]; |
| 197 |
option.innerText = ModuleEnum[item]; |
| 198 |
|
| 199 |
if (randomModule.name === ModuleEnum[item]) { |
| 200 |
option.selected = true; |
| 201 |
} |
| 202 |
|
| 203 |
moduleElement.appendChild(option); |
| 204 |
} |
| 205 |
body.appendChild(moduleElement); |
| 206 |
|
| 207 |
createSubTitle("Hero1", body); |
| 208 |
const randomHero1 = weightedHeroes[random(0, weightedHeroes.length - 1)]; |
| 209 |
const hero1Element = document.createElement("select"); |
| 210 |
const hero1Option = document.createElement("option"); |
| 211 |
|
| 212 |
hero1Element.appendChild(hero1Option); |
| 213 |
|
| 214 |
for (const item in HeroEnum) { |
| 215 |
const option = document.createElement("option"); |
| 216 |
option.value = HeroEnum[item]; |
| 217 |
option.innerText = HeroEnum[item]; |
| 218 |
|
| 219 |
if (randomHero1.name === HeroEnum[item]) { |
| 220 |
option.selected = true; |
| 221 |
} |
| 222 |
|
| 223 |
hero1Element.appendChild(option); |
| 224 |
} |
| 225 |
body.appendChild(hero1Element); |
| 226 |
|
| 227 |
createSubTitle("Hero2", body); |
| 228 |
const randomHero2 = weightedHeroes[random(0, weightedHeroes.length - 1)]; |
| 229 |
const hero2Element = document.createElement("select"); |
| 230 |
const hero2Option = document.createElement("option"); |
| 231 |
|
| 232 |
hero2Element.appendChild(hero2Option); |
| 233 |
|
| 234 |
for (const item in HeroEnum) { |
| 235 |
const option = document.createElement("option"); |
| 236 |
option.value = HeroEnum[item]; |
| 237 |
option.innerText = HeroEnum[item]; |
| 238 |
|
| 239 |
if (randomHero2.name === HeroEnum[item]) { |
| 240 |
option.selected = true; |
| 241 |
} |
| 242 |
|
| 243 |
hero2Element.appendChild(option); |
| 244 |
} |
| 245 |
|
| 246 |
body.appendChild(hero2Element); |
| 247 |
|
| 248 |
createSubTitle("Hero3", body); |
| 249 |
const hero3Element = document.createElement("select"); |
| 250 |
const hero3Option = document.createElement("option"); |
| 251 |
|
| 252 |
hero3Element.appendChild(hero3Option); |
| 253 |
|
| 254 |
for (const item in HeroEnum) { |
| 255 |
const option = document.createElement("option"); |
| 256 |
option.value = HeroEnum[item]; |
| 257 |
option.innerText = HeroEnum[item]; |
| 258 |
|
| 259 |
hero3Element.appendChild(option); |
| 260 |
} |
| 261 |
|
| 262 |
body.appendChild(hero3Element); |
| 263 |
|
| 264 |
createSubTitle("Hero4", body); |
| 265 |
|
| 266 |
const hero4Element = document.createElement("select"); |
| 267 |
const hero4Option = document.createElement("option"); |
| 268 |
|
| 269 |
hero4Element.appendChild(hero4Option); |
| 270 |
|
| 271 |
for (const item in HeroEnum) { |
| 272 |
const option = document.createElement("option"); |
| 273 |
option.value = HeroEnum[item]; |
| 274 |
option.innerText = HeroEnum[item]; |
| 275 |
|
| 276 |
hero4Element.appendChild(option); |
| 277 |
} |
| 278 |
|
| 279 |
body.appendChild(hero4Element); |
| 280 |
|
| 281 |
createSubTitle("Result", body); |
| 282 |
const resultElement = document.createElement("select"); |
| 283 |
const option = document.createElement("option"); |
| 284 |
|
| 285 |
resultElement.appendChild(option); |
| 286 |
|
| 287 |
for (const item in ResultEnum) { |
| 288 |
const option = document.createElement("option"); |
| 289 |
option.value = ResultEnum[item]; |
| 290 |
option.innerText = ResultEnum[item]; |
| 291 |
|
| 292 |
resultElement.appendChild(option); |
| 293 |
} |
| 294 |
|
| 295 |
body.appendChild(resultElement); |
| 296 |
|
| 297 |
createSubTitle("Add", body); |
| 298 |
const button = document.createElement("button"); |
| 299 |
button.type = "button"; |
| 300 |
button.innerText = "Add encounter !"; |
| 301 |
button.onclick = () => { |
| 302 |
const encounter = new Encounter({ |
| 303 |
id: null, |
| 304 |
scenario: scenarioElement.value, |
| 305 |
module: moduleElement.value, |
| 306 |
hero1: hero1Element.value, |
| 307 |
hero2: hero2Element.value !== "" ? hero2Element.value : null, |
| 308 |
hero3: hero3Element.value !== "" ? hero3Element.value : null, |
| 309 |
hero4: hero4Element.value !== "" ? hero4Element.value : null, |
| 310 |
difficulty: DifficultyEnum.STANDARD, |
| 311 |
result: resultElement.value, |
| 312 |
}); |
| 313 |
|
| 314 |
console.log(encounter); |
| 315 |
|
| 316 |
const encountersService = new EncountersService(); |
| 317 |
encountersService.save(encounter).then((result) => { |
| 318 |
console.log(result); |
| 319 |
|
| 320 |
window.location.reload(); |
| 321 |
}); |
| 322 |
}; |
| 323 |
|
| 324 |
body.appendChild(button); |
| 325 |
}; |
| 326 |
|
| 327 |
const loadEncounters = (body: HTMLBodyElement, encounters: Encounter[]) => { |
| 328 |
createTitle("Encounters", body); |
| 329 |
|
| 330 |
const table = document.createElement("table"); |
| 331 |
|
| 332 |
const firstRow = document.createElement("tr"); |
| 333 |
|
| 334 |
const scenario = document.createElement("th"); |
| 335 |
scenario.innerText = "Scenario"; |
| 336 |
|
| 337 |
const module = document.createElement("th"); |
| 338 |
module.innerText = "Module"; |
| 339 |
|
| 340 |
const difficulty = document.createElement("th"); |
| 341 |
difficulty.innerText = "Difficulty"; |
| 342 |
|
| 343 |
const hero1 = document.createElement("th"); |
| 344 |
hero1.innerText = "Hero 1"; |
| 345 |
|
| 346 |
const hero2 = document.createElement("th"); |
| 347 |
hero2.innerText = "Hero 2"; |
| 348 |
|
| 349 |
const hero3 = document.createElement("th"); |
| 350 |
hero3.innerText = "Hero 3"; |
| 351 |
|
| 352 |
const hero4 = document.createElement("th"); |
| 353 |
hero4.innerText = "Hero 4"; |
| 354 |
|
| 355 |
const result = document.createElement("th"); |
| 356 |
result.innerText = "Result"; |
| 357 |
|
| 358 |
firstRow.appendChild(scenario); |
| 359 |
firstRow.appendChild(module); |
| 360 |
firstRow.appendChild(difficulty); |
| 361 |
firstRow.appendChild(hero1); |
| 362 |
firstRow.appendChild(hero2); |
| 363 |
firstRow.appendChild(hero3); |
| 364 |
firstRow.appendChild(hero4); |
| 365 |
firstRow.appendChild(result); |
| 366 |
|
| 367 |
table.appendChild(firstRow); |
| 368 |
|
| 369 |
// load encounters |
| 370 |
encounters.forEach((encounter) => { |
| 371 |
const row = document.createElement("tr"); |
| 372 |
|
| 373 |
const scenario = document.createElement("td"); |
| 374 |
scenario.innerText = encounter.scenario as string; |
| 375 |
|
| 376 |
const module = document.createElement("td"); |
| 377 |
module.innerText = encounter.module as string; |
| 378 |
|
| 379 |
const difficulty = document.createElement("td"); |
| 380 |
difficulty.innerText = encounter.difficulty; |
| 381 |
|
| 382 |
const hero1 = document.createElement("td"); |
| 383 |
hero1.innerText = encounter.hero1 as string; |
| 384 |
|
| 385 |
const hero2 = document.createElement("td"); |
| 386 |
hero2.innerText = encounter.hero2 || ("" as string); |
| 387 |
|
| 388 |
const hero3 = document.createElement("td"); |
| 389 |
hero3.innerText = encounter.hero3 || ("" as string); |
| 390 |
|
| 391 |
const hero4 = document.createElement("td"); |
| 392 |
hero4.innerText = encounter.hero4 || ("" as string); |
| 393 |
|
| 394 |
const result = document.createElement("td"); |
| 395 |
result.innerText = encounter.result; |
| 396 |
|
| 397 |
row.appendChild(scenario); |
| 398 |
row.appendChild(module); |
| 399 |
row.appendChild(difficulty); |
| 400 |
row.appendChild(hero1); |
| 401 |
row.appendChild(hero2); |
| 402 |
row.appendChild(hero3); |
| 403 |
row.appendChild(hero4); |
| 404 |
row.appendChild(result); |
| 405 |
|
| 406 |
table.appendChild(row); |
| 407 |
}); |
| 408 |
|
| 409 |
body.appendChild(table); |
| 410 |
}; |
| 411 |
|
| 412 |
const loadStats = (body: HTMLBodyElement, encounters: Encounter[], heroes: Hero[], scenarios: Scenario[]) => { |
| 413 |
createTitle("Stats", body); |
| 414 |
|
| 415 |
// games |
| 416 |
createSubTitle("Games", body); |
| 417 |
const games = document.createElement("div"); |
| 418 |
games.innerText = encounters.length.toString(); |
| 419 |
|
| 420 |
body.appendChild(games); |
| 421 |
|
| 422 |
// games by hero |
| 423 |
createSubTitle("Games / Heroes / Win %", body); |
| 424 |
const gamesHeroes = encounters |
| 425 |
.flatMap((encounter) => [encounter.hero1, encounter.hero2, encounter.hero3, encounter.hero4]) |
| 426 |
.map((hero) => hero) |
| 427 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 428 |
|
| 429 |
const gamesHeroesWins = encounters |
| 430 |
.filter((encounter) => encounter.result === ResultEnum.WON) |
| 431 |
.flatMap((encounter) => [encounter.hero1, encounter.hero2, encounter.hero3, encounter.hero4]) |
| 432 |
.map((hero) => hero) |
| 433 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 434 |
|
| 435 |
createStatsTable(gamesHeroes, heroes, gamesHeroesWins, body); |
| 436 |
|
| 437 |
// games by scenario |
| 438 |
createSubTitle("Games / Scenarios / Win %", body); |
| 439 |
const gamesScenarios = encounters |
| 440 |
.map((encounter) => encounter.scenario) |
| 441 |
.map((scenario) => scenario) |
| 442 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 443 |
|
| 444 |
const gamesScenariosWins = encounters |
| 445 |
.filter((encounter) => encounter.result === ResultEnum.WON) |
| 446 |
.map((encounter) => encounter.scenario) |
| 447 |
.map((scenario) => scenario) |
| 448 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 449 |
|
| 450 |
createStatsTable(gamesScenarios, scenarios, gamesScenariosWins, body); |
| 451 |
|
| 452 |
// number of heroes by games |
| 453 |
createSubTitle("Number of heroes / Games", body); |
| 454 |
const nbHeroesGames = encounters.reduce((acc, e) => { |
| 455 |
const nbHeroes = 1 + (null != e.hero2 ? 1 : 0) + (null != e.hero3 ? 1 : 0) + (null != e.hero4 ? 1 : 0); |
| 456 |
|
| 457 |
acc.set(nbHeroes, (acc.get(nbHeroes) || 0) + 1); |
| 458 |
|
| 459 |
return acc; |
| 460 |
}, new Map()); |
| 461 |
|
| 462 |
const tableHeroesGames = document.createElement("table"); |
| 463 |
|
| 464 |
new Map([...nbHeroesGames].sort()).forEach((value, key) => { |
| 465 |
const itemLine = document.createElement("tr"); |
| 466 |
const itemName = document.createElement("td"); |
| 467 |
const itemCount = document.createElement("td"); |
| 468 |
|
| 469 |
itemName.innerText = key; |
| 470 |
itemCount.innerText = `${value} (${((value / encounters.length) * 100).toFixed(0)} %)`; |
| 471 |
|
| 472 |
itemLine.appendChild(itemName); |
| 473 |
itemLine.appendChild(itemCount); |
| 474 |
|
| 475 |
tableHeroesGames.appendChild(itemLine); |
| 476 |
}); |
| 477 |
|
| 478 |
body.appendChild(tableHeroesGames); |
| 479 |
|
| 480 |
// results |
| 481 |
createSubTitle("Results", body); |
| 482 |
const gamesResults = encounters |
| 483 |
.map((encounter) => encounter.result) |
| 484 |
.sort() |
| 485 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 486 |
|
| 487 |
const tableResults = document.createElement("table"); |
| 488 |
|
| 489 |
gamesResults.forEach((value, key) => { |
| 490 |
const itemLine = document.createElement("tr"); |
| 491 |
const itemName = document.createElement("td"); |
| 492 |
const itemCount = document.createElement("td"); |
| 493 |
|
| 494 |
itemName.innerText = key; |
| 495 |
itemCount.innerText = |
| 496 |
key === ResultEnum.WON ? `${value} (${((value / encounters.length) * 100).toFixed(0)} %)` : value; |
| 497 |
|
| 498 |
itemLine.appendChild(itemName); |
| 499 |
itemLine.appendChild(itemCount); |
| 500 |
|
| 501 |
tableResults.appendChild(itemLine); |
| 502 |
}); |
| 503 |
|
| 504 |
body.appendChild(tableResults); |
| 505 |
}; |
| 506 |
|
| 507 |
const loadProgression = (body: HTMLBodyElement, heroes: Hero[], scenarios: Scenario[], encounters: Encounter[]) => { |
| 508 |
createTitle("Progression", body); |
| 509 |
|
| 510 |
const table = document.createElement("table"); |
| 511 |
|
| 512 |
const progressionTargets = new Map<string, number>(); |
| 513 |
|
| 514 |
// first row |
| 515 |
const firstRow = document.createElement("tr"); |
| 516 |
firstRow.appendChild(document.createElement("td")); |
| 517 |
|
| 518 |
// load scenarios |
| 519 |
scenarios.forEach((scenario) => { |
| 520 |
const td = document.createElement("th"); |
| 521 |
td.innerText = scenario.name; |
| 522 |
|
| 523 |
progressionTargets.set(scenario.name, 0); |
| 524 |
|
| 525 |
firstRow.append(td); |
| 526 |
}); |
| 527 |
|
| 528 |
table.appendChild(firstRow); |
| 529 |
|
| 530 |
const encountersHeroes = encounters |
| 531 |
.filter((encounter) => encounter.result === "Won") |
| 532 |
.reduce((acc, e) => { |
| 533 |
if (!acc.has(e.hero1)) { |
| 534 |
acc.set(e.hero1, new Set()); |
| 535 |
} |
| 536 |
if (!acc.has(e.hero2)) { |
| 537 |
acc.set(e.hero2, new Set()); |
| 538 |
} |
| 539 |
if (!acc.has(e.hero3)) { |
| 540 |
acc.set(e.hero3, new Set()); |
| 541 |
} |
| 542 |
if (!acc.has(e.hero4)) { |
| 543 |
acc.set(e.hero4, new Set()); |
| 544 |
} |
| 545 |
|
| 546 |
acc.get(e.hero1).add(e.scenario); |
| 547 |
acc.get(e.hero2).add(e.scenario); |
| 548 |
acc.get(e.hero3).add(e.scenario); |
| 549 |
acc.get(e.hero4).add(e.scenario); |
| 550 |
|
| 551 |
return acc; |
| 552 |
}, new Map()); |
| 553 |
|
| 554 |
// load heroes |
| 555 |
heroes.forEach((hero) => { |
| 556 |
const row = document.createElement("tr"); |
| 557 |
|
| 558 |
const firstCol = document.createElement("th"); |
| 559 |
firstCol.innerText = hero.name; |
| 560 |
|
| 561 |
row.appendChild(firstCol); |
| 562 |
|
| 563 |
scenarios.forEach((scenario) => { |
| 564 |
const td = document.createElement("td"); |
| 565 |
|
| 566 |
const heroEncounter = encountersHeroes.get(hero.name); |
| 567 |
if (null != heroEncounter && heroEncounter.has(scenario.name)) { |
| 568 |
td.innerText = "✓"; |
| 569 |
if (progressionTargets.has(scenario.name)) { |
| 570 |
progressionTargets.set(scenario.name, progressionTargets.get(scenario.name) + 1); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
row.append(td); |
| 575 |
}); |
| 576 |
|
| 577 |
table.appendChild(row); |
| 578 |
}); |
| 579 |
|
| 580 |
// Show Progression |
| 581 |
const progressionRow = document.createElement("tr"); |
| 582 |
progressionRow.appendChild(document.createElement("td")); |
| 583 |
|
| 584 |
scenarios.forEach((scenario) => { |
| 585 |
const progressionTh = document.createElement("th"); |
| 586 |
|
| 587 |
const progession = ((progressionTargets.get(scenario.name) / heroes.length) * 100).toFixed(0); |
| 588 |
|
| 589 |
progressionTh.innerText = `${progession} %`; |
| 590 |
|
| 591 |
progressionRow.appendChild(progressionTh); |
| 592 |
}); |
| 593 |
|
| 594 |
table.appendChild(progressionRow); |
| 595 |
|
| 596 |
body.appendChild(table); |
| 597 |
}; |
| 598 |
|
| 599 |
const loadData = (body: HTMLBodyElement, heroes: Hero[], modules: Module[], scenarios: Scenario[]) => { |
| 600 |
createTitle("Data", body); |
| 601 |
|
| 602 |
// display heroes |
| 603 |
const heroesData = heroes.map((hero) => { |
| 604 |
hero.name = `${hero.name} (${hero.aspects.join("/")})`; |
| 605 |
|
| 606 |
return hero; |
| 607 |
}); |
| 608 |
createSection("Heroes", heroesData, body); |
| 609 |
// display Scenarios |
| 610 |
createSection("Scenarios", scenarios, body); |
| 611 |
// display Modular sets |
| 612 |
createSection("Modular Encounter Sets", modules, body); |
| 613 |
}; |
| 614 |
|