| 1 |
import { random } from "jga-algorithms-random"; |
| 2 |
|
| Warning |
Row 3, Column 13: "'encountersSource' is defined but never used."
@typescript-eslint/no-unused-vars
|
| 3 |
import * as encountersSource from "./assets/data/encounters.json"; |
| Warning |
Row 4, Column 13: "'heroesSource' is defined but never used."
@typescript-eslint/no-unused-vars
|
| 4 |
import * as heroesSource from "./assets/data/heroes.json"; |
| Warning |
Row 5, Column 13: "'modulesSource' is defined but never used."
@typescript-eslint/no-unused-vars
|
| 5 |
import * as modulesSource from "./assets/data/modules.json"; |
| Warning |
Row 6, Column 13: "'packsSource' is defined but never used."
@typescript-eslint/no-unused-vars
|
| 6 |
import * as packsSource from "./assets/data/packs.json"; |
| Warning |
Row 7, Column 13: "'scenariosSource' is defined but never used."
@typescript-eslint/no-unused-vars
|
| 7 |
import * as scenariosSource from "./assets/data/scenarios.json"; |
| 8 |
|
| Error |
Row 9, Column 9: "Replace `·DifficultyEnum,·Encounter,·Hero,·HeroEnum,·Module,·ModuleEnum,·ResultEnum,·Scenario,·ScenarioEnum·` with `⏎····DifficultyEnum,⏎····Encounter,⏎····Hero,⏎····HeroEnum,⏎····Module,⏎····ModuleEnum,⏎····ResultEnum,⏎····Scenario,⏎····ScenarioEnum,⏎`"
prettier/prettier
|
| 9 |
import { DifficultyEnum, Encounter, Hero, HeroEnum, Module, ModuleEnum, ResultEnum, Scenario, ScenarioEnum } from "./@models"; |
| 10 |
import { EncountersService, HeroesService, ModulesService, ScenariosService } from "./services"; |
| 11 |
|
| 12 |
window.onload = async (): Promise<void> => { |
| 13 |
const body = document.querySelector("body") as HTMLBodyElement; |
| 14 |
const data: [Encounter[], Hero[], Module[], Scenario[]] = await load(); |
| 15 |
|
| 16 |
const encounters: Encounter[] = data[0]; |
| 17 |
const heroes: Hero[] = data[1]; |
| 18 |
const modules: Module[] = data[2]; |
| 19 |
const scenarios: Scenario[] = data[3]; |
| 20 |
|
| 21 |
loadRandomizer(body, encounters, heroes, scenarios, modules); |
| 22 |
loadEncounters(body, encounters); |
| 23 |
loadStats(body, encounters, heroes, scenarios); |
| 24 |
loadProgression(body, heroes, scenarios, encounters); |
| 25 |
loadData(body, heroes, modules, scenarios); |
| 26 |
}; |
| 27 |
|
| 28 |
const load = async (): Promise<[Encounter[], Hero[], Module[], Scenario[]]> => { |
| 29 |
// load json data |
| 30 |
const encountersService = new EncountersService(); |
| 31 |
const encounters = await encountersService.load(); |
| 32 |
|
| 33 |
const heroesService = new HeroesService(); |
| 34 |
const heroes = heroesService.loadFromEnum(HeroEnum); |
| 35 |
|
| 36 |
const modulesService = new ModulesService(); |
| 37 |
const modules = modulesService.loadFromEnum(ModuleEnum); |
| 38 |
|
| 39 |
const scenariosService = new ScenariosService(); |
| Error |
Row 40, Column 66: "Delete `;`"
prettier/prettier
|
| 40 |
const scenarios = scenariosService.loadFromEnum(ScenarioEnum);; |
| 41 |
|
| 42 |
return [encounters, heroes, modules, scenarios]; |
| 43 |
}; |
| 44 |
|
| 45 |
const createTitle = (title: string, body: HTMLBodyElement) => { |
| 46 |
const header = document.createElement("h2"); |
| 47 |
header.innerText = title; |
| 48 |
|
| 49 |
body.appendChild(header); |
| 50 |
}; |
| 51 |
|
| 52 |
const createSubTitle = (title: string, body: HTMLBodyElement) => { |
| 53 |
const header = document.createElement("h3"); |
| 54 |
header.innerText = title; |
| 55 |
|
| 56 |
body.appendChild(header); |
| 57 |
}; |
| 58 |
|
| Warning |
Row 59, Column 45: "Unexpected any. Specify a different type."
@typescript-eslint/no-explicit-any
|
| 59 |
const createSection = (title: string, data: any[], body: HTMLBodyElement) => { |
| 60 |
createSubTitle(title, body); |
| 61 |
|
| 62 |
const section = document.createElement("section"); |
| 63 |
|
| 64 |
const list = document.createElement("ul"); |
| 65 |
|
| 66 |
data.forEach((item) => { |
| 67 |
const element = document.createElement("li"); |
| 68 |
element.innerText = `${item.name}`; |
| 69 |
|
| 70 |
list.appendChild(element); |
| 71 |
}); |
| 72 |
|
| 73 |
section.appendChild(list); |
| 74 |
|
| 75 |
body.appendChild(section); |
| 76 |
}; |
| 77 |
|
| Warning |
Row 78, Column 45: "Unexpected any. Specify a different type."
@typescript-eslint/no-explicit-any
|
| 78 |
const createStatsTable = (data: Map<string, any>, source: Hero[] | Scenario[], body: HTMLBodyElement) => { |
| 79 |
const table = document.createElement("table"); |
| 80 |
|
| 81 |
source.forEach((item) => { |
| 82 |
const itemLine = document.createElement("tr"); |
| 83 |
const itemName = document.createElement("td"); |
| 84 |
const itemCount = document.createElement("td"); |
| 85 |
|
| 86 |
itemName.innerText = item.name; |
| 87 |
itemCount.innerText = data.has(item.name) ? data.get(item.name) : 0; |
| 88 |
|
| 89 |
itemLine.appendChild(itemName); |
| 90 |
itemLine.appendChild(itemCount); |
| 91 |
|
| 92 |
table.appendChild(itemLine); |
| 93 |
}); |
| 94 |
|
| 95 |
body.appendChild(table); |
| 96 |
}; |
| 97 |
|
| 98 |
const loadRandomizer = ( |
| 99 |
body: HTMLBodyElement, |
| 100 |
encounters: Encounter[], |
| 101 |
heroes: Hero[], |
| 102 |
scenarios: Scenario[], |
| 103 |
modules: Module[], |
| Error |
Row 104, Column 3: "Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed."
sonarjs/cognitive-complexity
|
| 104 |
) => { |
| 105 |
createTitle("Randomizer", body); |
| 106 |
|
| 107 |
const gamesHeroes = encounters |
| 108 |
.flatMap((encounter) => [encounter.hero1, encounter.hero2]) |
| 109 |
.map((hero) => hero.name) |
| 110 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 111 |
|
| 112 |
const gamesModules = encounters |
| 113 |
.map((encounter) => encounter.module) |
| 114 |
.map((module) => module.name) |
| 115 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 116 |
|
| 117 |
const gamesScenarios = encounters |
| 118 |
.map((encounter) => encounter.scenario) |
| 119 |
.map((scenario) => scenario.name) |
| 120 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 121 |
|
| 122 |
const base = encounters.length * 5; |
| 123 |
|
| 124 |
const weightedScenarios: Scenario[] = []; |
| 125 |
const weightedModules: Module[] = []; |
| 126 |
const weightedHeroes: Hero[] = []; |
| 127 |
|
| 128 |
scenarios.forEach((scenario) => { |
| 129 |
const pastOccurrences = gamesScenarios.has(scenario.name) ? gamesScenarios.get(scenario.name) : 0; |
| 130 |
|
| 131 |
for (let i = 0; i < base - pastOccurrences; i++) { |
| 132 |
weightedScenarios.push(scenario); |
| 133 |
} |
| 134 |
}); |
| 135 |
|
| 136 |
modules.forEach((module) => { |
| 137 |
const pastOccurrences = gamesModules.has(module.name) ? gamesModules.get(module.name) : 0; |
| 138 |
|
| 139 |
for (let i = 0; i < base - pastOccurrences; i++) { |
| 140 |
weightedModules.push(module); |
| 141 |
} |
| 142 |
}); |
| 143 |
|
| 144 |
heroes.forEach((hero) => { |
| 145 |
const pastOccurrences = gamesHeroes.has(hero.name) ? gamesHeroes.get(hero.name) : 0; |
| 146 |
|
| 147 |
for (let i = 0; i < base - pastOccurrences; i++) { |
| 148 |
weightedHeroes.push(hero); |
| 149 |
} |
| 150 |
}); |
| 151 |
|
| 152 |
createSubTitle("Scenario", body); |
| 153 |
const randomScenario = weightedScenarios[random(0, weightedScenarios.length - 1)]; |
| 154 |
const randomScenarioElement = document.createElement("div"); |
| 155 |
randomScenarioElement.innerText = randomScenario.name; |
| 156 |
body.appendChild(randomScenarioElement); |
| 157 |
|
| 158 |
createSubTitle("Module", body); |
| 159 |
const randomModule = weightedModules[random(0, weightedModules.length - 1)]; |
| 160 |
const randomModuleElement = document.createElement("div"); |
| 161 |
randomModuleElement.innerText = randomModule.name; |
| 162 |
body.appendChild(randomModuleElement); |
| 163 |
|
| 164 |
createSubTitle("Hero1", body); |
| 165 |
const randomHero1 = weightedHeroes[random(0, weightedHeroes.length - 1)]; |
| 166 |
const randomHero1Element = document.createElement("div"); |
| 167 |
randomHero1Element.innerText = randomHero1.name; |
| 168 |
body.appendChild(randomHero1Element); |
| 169 |
|
| 170 |
createSubTitle("Hero2", body); |
| 171 |
const randomHero2 = weightedHeroes[random(0, weightedHeroes.length - 1)]; |
| 172 |
const randomHero2Element = document.createElement("div"); |
| 173 |
randomHero2Element.innerText = randomHero2.name; |
| 174 |
body.appendChild(randomHero2Element); |
| 175 |
|
| 176 |
createSubTitle("Result", body); |
| 177 |
const resultElement = document.createElement("select"); |
| 178 |
const option = document.createElement("option"); |
| 179 |
|
| 180 |
resultElement.appendChild(option); |
| 181 |
|
| Error |
Row 182, Column 14: "'item' is never reassigned. Use 'const' instead."
prefer-const
|
| 182 |
for (let item in ResultEnum) { |
| 183 |
const option = document.createElement("option"); |
| 184 |
option.value = ResultEnum[item]; |
| 185 |
option.innerText = ResultEnum[item]; |
| 186 |
|
| 187 |
resultElement.appendChild(option); |
| 188 |
} |
| 189 |
|
| 190 |
body.appendChild(resultElement); |
| 191 |
|
| 192 |
createSubTitle("Add", body); |
| 193 |
const button = document.createElement("button"); |
| 194 |
button.type = "button"; |
| 195 |
button.innerText = "Add encounter !"; |
| 196 |
button.onclick = () => { |
| 197 |
const encounter = new Encounter({ |
| 198 |
id: null, |
| 199 |
scenario: randomScenario.name, |
| 200 |
module: randomModule.name, |
| 201 |
hero1: randomHero1.name, |
| 202 |
hero2: randomHero2.name, |
| 203 |
difficulty: DifficultyEnum.STANDARD, |
| 204 |
result: resultElement.value, |
| 205 |
}); |
| 206 |
|
| 207 |
const encountersService = new EncountersService(); |
| 208 |
encountersService.save(encounter).then((result) => console.log(result)); |
| 209 |
|
| 210 |
window.location.reload(); |
| 211 |
}; |
| 212 |
|
| 213 |
body.appendChild(button); |
| 214 |
}; |
| 215 |
|
| 216 |
const loadEncounters = (body: HTMLBodyElement, encounters: Encounter[]) => { |
| 217 |
createTitle("Encounters", body); |
| 218 |
|
| 219 |
const table = document.createElement("table"); |
| 220 |
|
| 221 |
const firstRow = document.createElement("tr"); |
| 222 |
|
| 223 |
const scenario = document.createElement("th"); |
| 224 |
scenario.innerText = "Scenario"; |
| 225 |
|
| 226 |
const module = document.createElement("th"); |
| 227 |
module.innerText = "Module"; |
| 228 |
|
| 229 |
const difficulty = document.createElement("th"); |
| 230 |
difficulty.innerText = "Difficulty"; |
| 231 |
|
| 232 |
const hero1 = document.createElement("th"); |
| 233 |
hero1.innerText = "Hero 1"; |
| 234 |
|
| 235 |
const hero2 = document.createElement("th"); |
| 236 |
hero2.innerText = "Hero 2"; |
| 237 |
|
| 238 |
const result = document.createElement("th"); |
| 239 |
result.innerText = "Result"; |
| 240 |
|
| 241 |
firstRow.appendChild(scenario); |
| 242 |
firstRow.appendChild(module); |
| 243 |
firstRow.appendChild(difficulty); |
| 244 |
firstRow.appendChild(hero1); |
| 245 |
firstRow.appendChild(hero2); |
| 246 |
firstRow.appendChild(result); |
| 247 |
|
| 248 |
table.appendChild(firstRow); |
| 249 |
|
| 250 |
// load encounters |
| 251 |
encounters.forEach((encounter) => { |
| 252 |
const row = document.createElement("tr"); |
| 253 |
|
| 254 |
const scenario = document.createElement("td"); |
| 255 |
scenario.innerText = encounter.scenario.name as string; |
| 256 |
|
| 257 |
const module = document.createElement("td"); |
| 258 |
module.innerText = encounter.module.name as string; |
| 259 |
|
| 260 |
const difficulty = document.createElement("td"); |
| 261 |
difficulty.innerText = encounter.difficulty; |
| 262 |
|
| 263 |
const hero1 = document.createElement("td"); |
| 264 |
hero1.innerText = encounter.hero1.name as string; |
| 265 |
|
| 266 |
const hero2 = document.createElement("td"); |
| 267 |
hero2.innerText = encounter.hero2.name as string; |
| 268 |
|
| 269 |
const result = document.createElement("td"); |
| 270 |
result.innerText = encounter.result; |
| 271 |
|
| 272 |
row.appendChild(scenario); |
| 273 |
row.appendChild(module); |
| 274 |
row.appendChild(difficulty); |
| 275 |
row.appendChild(hero1); |
| 276 |
row.appendChild(hero2); |
| 277 |
row.appendChild(result); |
| 278 |
|
| 279 |
table.appendChild(row); |
| 280 |
}); |
| 281 |
|
| 282 |
body.appendChild(table); |
| 283 |
}; |
| 284 |
|
| 285 |
const loadStats = (body: HTMLBodyElement, encounters: Encounter[], heroes: Hero[], scenarios: Scenario[]) => { |
| 286 |
createTitle("Stats", body); |
| 287 |
|
| 288 |
// games |
| 289 |
createSubTitle("Games", body); |
| 290 |
const games = document.createElement("div"); |
| 291 |
games.innerText = encounters.length.toString(); |
| 292 |
|
| 293 |
body.appendChild(games); |
| 294 |
|
| 295 |
// games by hero |
| 296 |
createSubTitle("Games / Heroes", body); |
| 297 |
const gamesHeroes = encounters |
| 298 |
.flatMap((encounter) => [encounter.hero1, encounter.hero2]) |
| 299 |
.map((hero) => hero.name) |
| 300 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 301 |
|
| 302 |
createStatsTable(gamesHeroes, heroes, body); |
| 303 |
|
| 304 |
// games by scenario |
| 305 |
createSubTitle("Games / Scenarios", body); |
| 306 |
const gamesScenarios = encounters |
| 307 |
.map((encounter) => encounter.scenario) |
| 308 |
.map((scenario) => scenario.name) |
| 309 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 310 |
|
| 311 |
createStatsTable(gamesScenarios, scenarios, body); |
| 312 |
// results |
| 313 |
createSubTitle("Results", body); |
| 314 |
const gamesResults = encounters |
| 315 |
.map((encounter) => encounter.result) |
| 316 |
.sort() |
| 317 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 318 |
|
| 319 |
const tableResults = document.createElement("table"); |
| 320 |
|
| 321 |
gamesResults.forEach((value, key) => { |
| 322 |
const itemLine = document.createElement("tr"); |
| 323 |
const itemName = document.createElement("td"); |
| 324 |
const itemCount = document.createElement("td"); |
| 325 |
|
| 326 |
itemName.innerText = key; |
| 327 |
itemCount.innerText = value; |
| 328 |
|
| 329 |
itemLine.appendChild(itemName); |
| 330 |
itemLine.appendChild(itemCount); |
| 331 |
|
| 332 |
tableResults.appendChild(itemLine); |
| 333 |
}); |
| 334 |
|
| 335 |
body.appendChild(tableResults); |
| 336 |
|
| 337 |
// results by hero |
| 338 |
createSubTitle("Results / Heroes", body); |
| 339 |
// results by scenario |
| 340 |
createSubTitle("Results / Scenarios", body); |
| 341 |
}; |
| 342 |
|
| 343 |
const loadProgression = (body: HTMLBodyElement, heroes: Hero[], scenarios: Scenario[], encounters: Encounter[]) => { |
| 344 |
createTitle("Progression", body); |
| 345 |
|
| 346 |
const table = document.createElement("table"); |
| 347 |
|
| 348 |
// first row |
| 349 |
const firstRow = document.createElement("tr"); |
| 350 |
firstRow.appendChild(document.createElement("td")); |
| 351 |
|
| 352 |
// load scenarios |
| 353 |
scenarios.forEach((scenario) => { |
| 354 |
const td = document.createElement("th"); |
| 355 |
td.innerText = scenario.name; |
| 356 |
|
| 357 |
firstRow.append(td); |
| 358 |
}); |
| 359 |
|
| 360 |
table.appendChild(firstRow); |
| 361 |
|
| 362 |
const encountersHeroes = encounters |
| 363 |
.filter((encounter) => encounter.result === "Won") |
| 364 |
.reduce((acc, e) => { |
| 365 |
if (!acc.has(e.hero1.name)) { |
| 366 |
acc.set(e.hero1.name, new Set()); |
| 367 |
} |
| 368 |
if (!acc.has(e.hero2.name)) { |
| 369 |
acc.set(e.hero2.name, new Set()); |
| 370 |
} |
| 371 |
|
| 372 |
acc.get(e.hero1.name).add(e.scenario.name); |
| 373 |
acc.get(e.hero2.name).add(e.scenario.name); |
| 374 |
|
| 375 |
return acc; |
| 376 |
}, new Map()); |
| 377 |
|
| 378 |
// load heroes |
| 379 |
heroes.forEach((hero) => { |
| 380 |
const row = document.createElement("tr"); |
| 381 |
|
| 382 |
const firstCol = document.createElement("th"); |
| 383 |
firstCol.innerText = hero.name; |
| 384 |
|
| 385 |
row.appendChild(firstCol); |
| 386 |
|
| 387 |
scenarios.forEach((scenario) => { |
| 388 |
const td = document.createElement("td"); |
| 389 |
|
| 390 |
const heroEncounter = encountersHeroes.get(hero.name); |
| 391 |
if (null != heroEncounter && heroEncounter.has(scenario.name)) { |
| 392 |
td.innerText = "✓"; |
| 393 |
} |
| 394 |
|
| 395 |
row.append(td); |
| 396 |
}); |
| 397 |
|
| 398 |
table.appendChild(row); |
| 399 |
}); |
| 400 |
|
| 401 |
body.appendChild(table); |
| 402 |
}; |
| 403 |
|
| 404 |
const loadData = (body: HTMLBodyElement, heroes: Hero[], modules: Module[], scenarios: Scenario[]) => { |
| 405 |
createTitle("Data", body); |
| 406 |
|
| 407 |
// display heroes |
| 408 |
createSection("Heroes", heroes, body); |
| 409 |
createSection("Scenarios", scenarios, body); |
| 410 |
createSection("Modular Encounter Sets", modules, body); |
| 411 |
}; |
| 412 |
|