@@ -48,9 +48,7 @@ function convertMatchToParty(match, index, matches) {
48
48
const party = {
49
49
round : match . next . round ,
50
50
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 } ` ,
54
52
order : ( ( index + 1 ) / ( matches . length + match . round ) ) . toPrecision ( 3 ) ,
55
53
match : {
56
54
id : match . id ,
@@ -62,6 +60,11 @@ function convertMatchToParty(match, index, matches) {
62
60
} ,
63
61
}
64
62
63
+ if ( match . round === 0 && match . singular ) {
64
+ party . id = match . parties [ 0 ] . id
65
+ party . name = match . parties [ 0 ] . name
66
+ }
67
+
65
68
party . order = Number ( party . order )
66
69
67
70
return party
@@ -415,6 +418,31 @@ export function render($chart, parties) {
415
418
$chart . innerHTML = ''
416
419
$chart . style . setProperty ( '--participants' , `${ parties . length } ` )
417
420
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
+
418
446
for ( const roundId in rounds ) {
419
447
const round = rounds [ roundId ]
420
448
@@ -498,8 +526,8 @@ export function render($chart, parties) {
498
526
499
527
$check . type = 'radio'
500
528
$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
503
531
$check . placeholder = party . name
504
532
505
533
if ( roundId > 0 ) {
@@ -509,7 +537,8 @@ export function render($chart, parties) {
509
537
$name . setAttribute ( 'for' , $check . id )
510
538
$participant . append ( $check )
511
539
512
- $check . addEventListener ( 'change' , ( ) => {
540
+ $check . addEventListener ( 'change' , ( { target } ) => {
541
+ /** @type {HTMLInputElement } target */
513
542
if ( $check . checked ) {
514
543
const inputs = $matchInner . querySelectorAll ( 'input[type="radio"]' )
515
544
inputs . forEach ( input => ( input . disabled = true ) )
@@ -522,6 +551,23 @@ export function render($chart, parties) {
522
551
currentRound . classList . add ( 'completed' )
523
552
enableNextRound ( match . next . round , match . next . side )
524
553
}
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
+ } ) )
525
571
} )
526
572
}
527
573
0 commit comments