Skip to content

Commit 88d354b

Browse files
authored
feat: add ability to replace participant name on next round (#19)
close #6
2 parents 3b55e2a + 0519f30 commit 88d354b

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

lib/main.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ function convertMatchToParty(match, index, matches) {
4848
const party = {
4949
round: match.next.round,
5050
side: match.next.side,
51-
name: match.round === 0 && match.singular
52-
? match.parties[0].name
53-
: `Winner match ${match.id}`,
51+
name: `Winner match ${match.id}`,
5452
order: ((index + 1) / (matches.length + match.round)).toPrecision(3),
5553
match: {
5654
id: match.id,
@@ -62,6 +60,11 @@ function convertMatchToParty(match, index, matches) {
6260
},
6361
}
6462

63+
if (match.round === 0 && match.singular) {
64+
party.id = match.parties[0].id
65+
party.name = match.parties[0].name
66+
}
67+
6568
party.order = Number(party.order)
6669

6770
return party
@@ -415,6 +418,31 @@ export function render($chart, parties) {
415418
$chart.innerHTML = ''
416419
$chart.style.setProperty('--participants', `${parties.length}`)
417420

421+
$chart.addEventListener('winner-selected', (e) => {
422+
const { round, match, winner } = e.detail
423+
424+
const nextMatch = rounds[round.id].matches.find(({ parties }) => {
425+
return parties.find((party) => {
426+
return party.side === match.next.side && party.match?.id === match.id
427+
})
428+
})
429+
430+
const $match = document.getElementById(`round-${round.id}-match-${nextMatch.id}`)
431+
const $party = $match.children.item(match.next.side === 'blue' ? 0 : 1)
432+
433+
if (winner?.name) {
434+
rounds[round.id].parties.forEach((party) => {
435+
if (party.match.id === match.id) {
436+
party.id = winner.id
437+
party.name = winner.name
438+
}
439+
})
440+
441+
$party.children.item(0).textContent = winner.name
442+
$party.children.item(1).value = winner.id
443+
}
444+
})
445+
418446
for (const roundId in rounds) {
419447
const round = rounds[roundId]
420448

@@ -498,8 +526,8 @@ export function render($chart, parties) {
498526

499527
$check.type = 'radio'
500528
$check.name = $matchInner.id
501-
$check.id = `${$matchInner.id}-${party.side}`
502-
$check.value = party.side
529+
$check.id = `${$matchInner.id}-${party.id}`
530+
$check.value = party.id
503531
$check.placeholder = party.name
504532

505533
if (roundId > 0) {
@@ -509,7 +537,8 @@ export function render($chart, parties) {
509537
$name.setAttribute('for', $check.id)
510538
$participant.append($check)
511539

512-
$check.addEventListener('change', () => {
540+
$check.addEventListener('change', ({ target }) => {
541+
/** @type {HTMLInputElement} target */
513542
if ($check.checked) {
514543
const inputs = $matchInner.querySelectorAll('input[type="radio"]')
515544
inputs.forEach(input => (input.disabled = true))
@@ -522,6 +551,23 @@ export function render($chart, parties) {
522551
currentRound.classList.add('completed')
523552
enableNextRound(match.next.round, match.next.side)
524553
}
554+
555+
/** @type {import('./types').Side} */
556+
const side = target.parentElement.getAttribute('data-side')
557+
558+
/** @type {import('./types').Party[]} */
559+
const parties = round.parties.map((party) => {
560+
if (round.id === 1) {
561+
return party[side] || {}
562+
}
563+
return party
564+
})
565+
566+
const winner = parties.find(party => party.id === Number(target.value))
567+
568+
$chart.dispatchEvent(new CustomEvent('winner-selected', {
569+
detail: { winner, match, round },
570+
}))
525571
})
526572
}
527573

lib/parties.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export function generateParties(count) {
1010

1111
for (let i = 1; i <= count; i++) {
1212
participants.push({
13+
id: i,
1314
name: `Participant ${i}`,
1415
continent: `Continent ${i}`,
1516
order: i,

lib/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type Side = 'blue' | 'red'
22

33
export interface Party {
4+
id: number
45
name: string
56
round: number
67
side: Side

0 commit comments

Comments
 (0)