| 1 |
<svelte:options customElement="mc-suggested" /> |
| 2 |
|
| 3 |
<script lang="ts"> |
| 4 |
import { random } from "@jga/algorithms"; |
| 5 |
|
| 6 |
import type { Encounter, Hero, ModularSet, Scenario } from "$models"; |
| 7 |
import { DifficultyEnum, ResultEnum } from "$models"; |
| 8 |
|
| 9 |
import { EncountersService, HeroesService, ModularSetsService, ScenariosService } from "$services"; |
| 10 |
|
| 11 |
import { excludeHeroes, notEmpty } from "./functions"; |
| 12 |
|
| 13 |
import Button from "$ui/button/src/button.svelte"; |
| 14 |
import Select from "$lib/select.svelte"; |
| 15 |
import Title from "$lib/title.svelte"; |
| 16 |
|
| 17 |
// eslint-disable-next-line import/no-mutable-exports |
| 18 |
export let heroes: Hero[] = []; |
| 19 |
// eslint-disable-next-line import/no-mutable-exports |
| 20 |
export let encounters: Encounter[] = []; |
| 21 |
// eslint-disable-next-line import/no-mutable-exports |
| 22 |
export let scenarios: Scenario[] = []; |
| 23 |
|
| 24 |
let gamesScenarios: Map<string, number>; |
| 25 |
let unorderedScenarios: Map<string, number>; |
| 26 |
let orderedScenarios: Map<string, number>; |
| 27 |
|
| 28 |
let scenariosWinEncounters: Map<string, Hero[]>; |
| 29 |
let progressionsEncounters: Map<Scenario, Partial<Encounter>[]>; |
| 30 |
let suggestedEncounters: Partial<Encounter>[]; |
| 31 |
|
| 32 |
$: { |
| 33 |
// order scenarios by play count |
| 34 |
// eslint-disable-next-line prefer-const |
| 35 |
gamesScenarios = encounters |
| 36 |
.map((encounter) => encounter.scenario.name) |
| 37 |
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map()); |
| 38 |
|
| 39 |
// eslint-disable-next-line prefer-const |
| 40 |
unorderedScenarios = scenarios.reduce((acc, e) => { |
| 41 |
acc.set(e, (acc.get(e) || 0) + (gamesScenarios.get(e.name) || 0)); |
| 42 |
|
| 43 |
return acc; |
| 44 |
}, new Map()); |
| 45 |
|
| 46 |
// eslint-disable-next-line prefer-const |
| 47 |
orderedScenarios = new Map(Array.from(unorderedScenarios.entries()).sort((a, b) => a[1] - b[1])); |
| 48 |
|
| 49 |
// eslint-disable-next-line prefer-const |
| 50 |
scenariosWinEncounters = encounters |
| 51 |
.filter((encounter) => encounter.result === ResultEnum.WON) |
| 52 |
.reduce((acc, e) => { |
| 53 |
if (!acc.has(e.scenario.name)) { |
| 54 |
acc.set(e.scenario.name, []); |
| 55 |
} |
| 56 |
|
| 57 |
const newHeroes = acc.get(e.scenario.name); |
| 58 |
[e] |
| 59 |
.flatMap((encounter) => encounter.heroes) |
| 60 |
.filter((hero) => hero != null) |
| 61 |
.map((hero) => hero.name) |
| 62 |
.filter((hero, i, array) => array.indexOf(hero) === i) |
| 63 |
.forEach((hero) => newHeroes.push(hero)); |
| 64 |
|
| 65 |
acc.set(e.scenario.name, newHeroes.sort()); |
| 66 |
|
| 67 |
return acc; |
| 68 |
}, new Map()); |
| 69 |
|
| 70 |
// eslint-disable-next-line prefer-const |
| 71 |
progressionsEncounters = new Map(); |
| 72 |
Array.from(orderedScenarios.keys()).forEach((scenario) => { |
| 73 |
heroes.forEach((hero) => { |
| 74 |
if ( |
| 75 |
!scenariosWinEncounters.has(scenario.name) || |
| 76 |
!scenariosWinEncounters.get(scenario.name).includes(hero.name) |
| 77 |
) { |
| 78 |
if (!progressionsEncounters.has(scenario)) { |
| 79 |
progressionsEncounters.set(scenario, []); |
| 80 |
} |
| 81 |
|
| 82 |
const tryHeroes = progressionsEncounters.get(scenario) as Partial<Encounter>[]; |
| 83 |
tryHeroes.push({ |
| 84 |
scenario, |
| 85 |
modularSets: scenario.modularSets, |
| 86 |
difficulty: DifficultyEnum.STANDARD, |
| 87 |
heroes: [hero], |
| 88 |
}); |
| 89 |
|
| 90 |
progressionsEncounters.set(scenario, tryHeroes); |
| 91 |
} |
| 92 |
}); |
| 93 |
}); |
| 94 |
|
| 95 |
// eslint-disable-next-line prefer-const |
| 96 |
suggestedEncounters = []; |
| 97 |
// Pick a random one for each progression scenario |
| 98 |
progressionsEncounters.forEach((scenario) => { |
| 99 |
const scenarioEncounters = Array.from(scenario); |
| 100 |
const scenarioEncounter = scenarioEncounters[random(0, scenarioEncounters.length - 1)]; |
| 101 |
|
| 102 |
suggestedEncounters.push(scenarioEncounter); |
| 103 |
}); |
| 104 |
} |
| 105 |
|
| 106 |
async function add(event) { |
| 107 |
const { parentNode } = event.detail.parentNode; |
| 108 |
|
| 109 |
const isRandom = parentNode.classList.contains("random"); |
| 110 |
|
| 111 |
const scenarioElement = parentNode.children[0]; |
| 112 |
const modulesElement = parentNode.children[1]; |
| 113 |
const difficultyElement = parentNode.children[2]; |
| 114 |
const hero1Element = parentNode.children[3]; |
| 115 |
const hero2Element = parentNode.children[4]; |
| 116 |
const hero3Element = parentNode.children[5]; |
| 117 |
const hero4Element = parentNode.children[6]; |
| 118 |
const resultElement = parentNode.children[7]; |
| 119 |
|
| 120 |
const scenariosService = new ScenariosService(); |
| 121 |
const heroesService = new HeroesService(); |
| 122 |
const modularSetsService = new ModularSetsService(); |
| 123 |
|
| 124 |
const encounterHeroes = []; |
| 125 |
|
| 126 |
const scenarioValue = isRandom ? scenarioElement.children[0].value : scenarioElement.innerHTML; |
| 127 |
|
| 128 |
const scenario = await scenariosService.find(scenarioValue); |
| 129 |
const hero1 = await heroesService.find(hero1Element.children[0].value); |
| 130 |
encounterHeroes.push(hero1); |
| 131 |
|
| 132 |
if (hero2Element.children[0].value !== "") { |
| 133 |
const hero2 = await heroesService.find(hero2Element.children[0].value); |
| 134 |
encounterHeroes.push(hero2); |
| 135 |
} |
| 136 |
|
| 137 |
if (hero3Element.children[0].value !== "") { |
| 138 |
const hero3 = await heroesService.find(hero3Element.children[0].value); |
| 139 |
encounterHeroes.push(hero3); |
| 140 |
} |
| 141 |
|
| 142 |
if (hero4Element.children[0].value !== "") { |
| 143 |
const hero4 = await heroesService.find(hero4Element.children[0].value); |
| 144 |
encounterHeroes.push(hero4); |
| 145 |
} |
| 146 |
|
| 147 |
const difficulty = |
| 148 |
Object.values(DifficultyEnum).find((x) => x === difficultyElement.children[0].value) || |
| 149 |
DifficultyEnum.STANDARD; |
| 150 |
|
| 151 |
const result = |
| 152 |
Object.values(ResultEnum).find((x) => x === resultElement.children[0].value) || ResultEnum.FORFEIT; |
| 153 |
|
| 154 |
// Retrieve modular sets |
| 155 |
const encounterModularSetsValues = isRandom |
| 156 |
? Array.from(modulesElement.children[0].selectedOptions, (option) => option.value) |
| 157 |
: modulesElement.innerHTML.split(", "); |
| 158 |
|
| 159 |
const rawEncounterModularSets = await Promise.all( |
| 160 |
encounterModularSetsValues.map((modularSet) => modularSetsService.find(modularSet)), |
| 161 |
); |
| 162 |
const encounterModules: ModularSet[] = rawEncounterModularSets.filter(notEmpty); |
| 163 |
|
| 164 |
const encounter = { |
| 165 |
id: null, |
| 166 |
scenario, |
| 167 |
heroes: encounterHeroes, |
| 168 |
modularSets: encounterModules, |
| 169 |
difficulty, |
| 170 |
result, |
| 171 |
}; |
| 172 |
|
| 173 |
// eslint-disable-next-line no-console |
| 174 |
console.log(encounter); |
| 175 |
|
| 176 |
const encountersService = new EncountersService(); |
| 177 |
encountersService |
| 178 |
.save(encounter) |
| 179 |
.then(() => window.location.reload()) |
| 180 |
// eslint-disable-next-line no-console |
| 181 |
.catch((error) => console.error(error)); |
| 182 |
} |
| 183 |
</script> |
| 184 |
|
| 185 |
<Title title="Suggested encounters" /> |
| 186 |
|
| 187 |
<table> |
| 188 |
<thead> |
| Error |
Row 189, Column 5: "Insert `····`"
prettier/prettier
|
| 189 |
<tr> |
| Error |
Row 190, Column 1: "Insert `····`"
prettier/prettier
|
| 190 |
<th>Scenario</th> |
| Error |
Row 191, Column 1: "Insert `····`"
prettier/prettier
|
| 191 |
<th>Modular Sets</th> |
| Error |
Row 192, Column 9: "Insert `····`"
prettier/prettier
|
| 192 |
<th>Difficulty</th> |
| Error |
Row 193, Column 9: "Insert `····`"
prettier/prettier
|
| 193 |
<th>Hero 1</th> |
| Error |
Row 194, Column 1: "Insert `····`"
prettier/prettier
|
| 194 |
<th>Hero 2</th> |
| Error |
Row 195, Column 9: "Insert `····`"
prettier/prettier
|
| 195 |
<th>Hero 3</th> |
| Error |
Row 196, Column 1: "Replace `········` with `············`"
prettier/prettier
|
| 196 |
<th>Hero 4</th> |
| Error |
Row 197, Column 1: "Insert `····`"
prettier/prettier
|
| 197 |
<th>Result</th> |
| Error |
Row 198, Column 9: "Insert `····`"
prettier/prettier
|
| 198 |
<th></th> |
| Error |
Row 199, Column 1: "Insert `····`"
prettier/prettier
|
| 199 |
</tr> |
| 200 |
</thead> |
| 201 |
<tbody> |
| Error |
Row 202, Column 1: "Insert `····`"
prettier/prettier
|
| 202 |
<!-- Add progression encounters --> |
| Error |
Row 203, Column 5: "Insert `····`"
prettier/prettier
|
| 203 |
{#each suggestedEncounters as encounter} |
| Error |
Row 204, Column 9: "Insert `····`"
prettier/prettier
|
| 204 |
<tr class="suggested"> |
| Error |
Row 205, Column 1: "Insert `····`"
prettier/prettier
|
| 205 |
<td>{encounter.scenario.name}</td> |
| Error |
Row 206, Column 13: "Insert `····`"
prettier/prettier
|
| 206 |
<td>{encounter.modularSets?.map((module) => module.name).join(", ") || ""}</td> |
| Error |
Row 207, Column 13: "Insert `····`"
prettier/prettier
|
| 207 |
<td> |
| Error |
Row 208, Column 1: "Insert `····`"
prettier/prettier
|
| 208 |
<Select defaultValue="{DifficultyEnum.STANDARD}" source="{Object.values(DifficultyEnum)}" /> |
| Error |
Row 209, Column 13: "Insert `····`"
prettier/prettier
|
| 209 |
</td> |
| Error |
Row 210, Column 13: "Insert `····`"
prettier/prettier
|
| 210 |
<td> |
| Error |
Row 211, Column 1: "Replace `················` with `····················`"
prettier/prettier
|
| 211 |
<Select |
| Error |
Row 212, Column 1: "Insert `····`"
prettier/prettier
|
| 212 |
defaultValue="{encounter.heroes[0].name}" |
| Error |
Row 213, Column 1: "Insert `····`"
prettier/prettier
|
| 213 |
source="{excludeHeroes(heroes, encounter.scenario.name, scenariosWinEncounters).map( |
| Error |
Row 214, Column 1: "Expected indentation of 24 spaces but found 28."
indent
|
| 214 |
(hero) => hero.name, |
| Error |
Row 215, Column 1: "Expected indentation of 20 spaces but found 24."
indent
|
| 215 |
)}" |
| Error |
Row 216, Column 1: "Insert `····`"
prettier/prettier
|
| 216 |
/> |
| Error |
Row 217, Column 13: "Insert `····`"
prettier/prettier
|
| 217 |
</td> |
| Error |
Row 218, Column 1: "Insert `····`"
prettier/prettier
|
| 218 |
<td> |
| Error |
Row 219, Column 17: "Insert `····`"
prettier/prettier
|
| 219 |
<Select |
| Error |
Row 220, Column 1: "Insert `····`"
prettier/prettier
|
| 220 |
source="{excludeHeroes(heroes, encounter.scenario.name, scenariosWinEncounters).map( |
| Error |
Row 221, Column 1: "Expected indentation of 24 spaces but found 28."
indent
|
| 221 |
(hero) => hero.name, |
| Error |
Row 222, Column 1: "Expected indentation of 20 spaces but found 24."
indent
|
| 222 |
)}" |
| Error |
Row 223, Column 17: "Insert `····`"
prettier/prettier
|
| 223 |
/> |
| Error |
Row 224, Column 1: "Insert `····`"
prettier/prettier
|
| 224 |
</td> |
| Error |
Row 225, Column 13: "Insert `····`"
prettier/prettier
|
| 225 |
<td> |
| Error |
Row 226, Column 1: "Insert `····`"
prettier/prettier
|
| 226 |
<Select |
| Error |
Row 227, Column 1: "Insert `····`"
prettier/prettier
|
| 227 |
source="{excludeHeroes(heroes, encounter.scenario.name, scenariosWinEncounters).map( |
| Error |
Row 228, Column 1: "Expected indentation of 24 spaces but found 28."
indent
|
| 228 |
(hero) => hero.name, |
| Error |
Row 229, Column 1: "Expected indentation of 20 spaces but found 24."
indent
|
| 229 |
)}" |
| Error |
Row 230, Column 17: "Insert `····`"
prettier/prettier
|
| 230 |
/> |
| Error |
Row 231, Column 1: "Insert `····`"
prettier/prettier
|
| 231 |
</td> |
| Error |
Row 232, Column 1: "Insert `····`"
prettier/prettier
|
| 232 |
<td> |
| Error |
Row 233, Column 17: "Insert `····`"
prettier/prettier
|
| 233 |
<Select |
| Error |
Row 234, Column 1: "Insert `····`"
prettier/prettier
|
| 234 |
source="{excludeHeroes(heroes, encounter.scenario.name, scenariosWinEncounters).map( |
| Error |
Row 235, Column 1: "Expected indentation of 24 spaces but found 28."
indent
|
| 235 |
(hero) => hero.name, |
| Error |
Row 236, Column 1: "Expected indentation of 20 spaces but found 24."
indent
|
| 236 |
)}" |
| Error |
Row 237, Column 17: "Insert `····`"
prettier/prettier
|
| 237 |
/> |
| Error |
Row 238, Column 1: "Insert `····`"
prettier/prettier
|
| 238 |
</td> |
| Error |
Row 239, Column 13: "Insert `····`"
prettier/prettier
|
| 239 |
<td> |
| Error |
Row 240, Column 1: "Insert `····`"
prettier/prettier
|
| 240 |
<Select source="{Object.values(ResultEnum)}" /> |
| Error |
Row 241, Column 13: "Insert `····`"
prettier/prettier
|
| 241 |
</td> |
| Error |
Row 242, Column 13: "Insert `····`"
prettier/prettier
|
| 242 |
<td> |
| Error |
Row 243, Column 1: "Insert `····`"
prettier/prettier
|
| 243 |
<Button label="Add" on:buttonClicked="{add}" /> |
| Error |
Row 244, Column 13: "Insert `····`"
prettier/prettier
|
| 244 |
</td> |
| Error |
Row 245, Column 1: "Replace `········` with `············`"
prettier/prettier
|
| 245 |
</tr> |
| Error |
Row 246, Column 1: "Insert `····`"
prettier/prettier
|
| 246 |
{/each} |
| 247 |
</tbody> |
| 248 |
</table> |
| 249 |
|