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