@@ -16,8 +16,9 @@ let currentId = 0;
16
16
let autoSwitchTeamsPoints = 0 ;
17
17
let joinCode = '' ;
18
18
let lastGameDayMatch = 0 ;
19
- let courtId = null
19
+ let courtId = null ;
20
20
const localStorage = window . localStorage ;
21
+ let playersToNextGame = [ ] ;
21
22
22
23
const createSpinner = ( id ) => {
23
24
const wrapper = document . createElement ( 'span' ) ;
@@ -77,15 +78,10 @@ socket.on('game-day:updated', async () => {
77
78
lastGameDayMatch = activeGame . lastMatch ;
78
79
currentId = activeGame . id ;
79
80
joinCode = activeGame . joinCode ;
80
- courtId = activeGame . courtId || null
81
-
82
- if ( playingTeams . length === 0 ) {
83
- const playersToTeam = findNextMatchPlayers ( )
84
- if ( playersToTeam . length > 0 ) {
85
- playingTeams = await generateTeams ( playersToTeam ) ;
86
- await upsertGameDay ( ) ;
87
- }
88
- }
81
+ courtId = activeGame . courtId || null ;
82
+ playersToNextGame = activeGame . playersToNextGame || [ ] ;
83
+
84
+ // await upsertGameDay();
89
85
90
86
$ ( '#add-new-player-select' ) . empty ( ) ;
91
87
getPlayers ( )
@@ -96,7 +92,7 @@ socket.on('game-day:updated', async () => {
96
92
97
93
98
94
currentMatchMaxPoints = maxPoints ;
99
- updateCurrentMatch ( playingTeams ) ;
95
+ updatePlayerList ( ) ;
100
96
$ ( "#new-match-day" ) . hide ( ) ;
101
97
$ ( "#new-match-day-form" ) . hide ( ) ;
102
98
$ ( "#new-match-day-button" ) . hide ( ) ;
@@ -208,9 +204,10 @@ async function updateGameDay(isLive = true) {
208
204
matches,
209
205
isLive,
210
206
autoSwitchTeamsPoints,
211
- playedOn : new Date ( )
207
+ playedOn : new Date ( ) ,
208
+ playersToNextGame,
212
209
} )
213
- } )
210
+ } ) ;
214
211
215
212
if ( response . ok ) {
216
213
socket . emit ( "game-day:updated" , currentId ) ;
@@ -337,14 +334,26 @@ function updatePlayerList() {
337
334
const rows = playersToList . map ( player => {
338
335
const isPlayingHere = playingTeams . flat ( ) . some ( p => p . name === player . name ) ;
339
336
const isPlayingSomewhereElse = otherPlayingTeams . flat ( ) . some ( p => p . name === player . name ) ;
340
- const rowClass = isPlayingHere ? 'is-selected' : isPlayingSomewhereElse ? 'is-selected is-warning' : '' ;
337
+ const shouldBeOnNextMatch = playersToNextGame . some ( p => p . name === player . name ) ;
341
338
const formatter = new Intl . NumberFormat ( 'pt-BR' , { maximumFractionDigits : 2 , minimumFractionDigits : 2 } ) ;
342
339
const elo = devMode ? `${ formatter . format ( player . mu ) } /${ formatter . format ( player . sigma ) } ` : '' ;
343
340
const playingClass = isPlayingSomewhereElse
344
341
? ''
345
342
: player . playing
346
343
? 'remove-player is-clickable'
347
344
: 'is-danger remove-player is-clickable' ;
345
+
346
+ let rowClass = '' ;
347
+ if ( isPlayingHere ) {
348
+ rowClass = 'is-selected' ;
349
+ } else if ( isPlayingSomewhereElse ) {
350
+ rowClass = 'is-selected is-warning' ;
351
+ } else if ( shouldBeOnNextMatch ) {
352
+ rowClass = 'is-info' ;
353
+ } else {
354
+ rowClass = player . playing ? 'remove-player is-clickable' : 'is-danger remove-player is-clickable' ;
355
+ }
356
+
348
357
return `
349
358
<tr class="${ rowClass } ">
350
359
<th>${ player . name } </th>
@@ -413,6 +422,9 @@ $("#all-player-list").on("click", ".remove-player", async function() {
413
422
414
423
const playerIndex = players . findIndex ( player => player . name === playerName ) ;
415
424
players [ playerIndex ] . playing = ! players [ playerIndex ] . playing ;
425
+
426
+ // Remove this player from next match
427
+ playersToNextGame = playersToNextGame . filter ( player => player . name !== playerName ) ;
416
428
417
429
await upsertGameDay ( ) ;
418
430
updatePlayerList ( ) ;
@@ -529,7 +541,7 @@ async function updateCurrentMatch(teams) {
529
541
}
530
542
531
543
$ ( "#start-match-day" ) . click ( async function ( ) {
532
- const startMatchDaySpinner = createSpinner ( 'start-match-day-spinner' ) ;
544
+ const startMatchDaySpinner = createSpinner ( 'start-match-day-spinner' ) ;
533
545
const button = document . getElementById ( 'start-match-day' ) ;
534
546
button . insertAdjacentElement ( 'afterend' , startMatchDaySpinner ) ;
535
547
button . setAttribute ( 'disabled' , 'disabled' ) ;
@@ -552,8 +564,6 @@ $("#start-match-day").click(async function() {
552
564
maxPoints = parseInt ( $ ( "#max-points" ) . val ( ) ) ;
553
565
currentMatchMaxPoints = maxPoints ;
554
566
playersPerTeam = $ ( "#players-per-team" ) . val ( ) ;
555
-
556
- console . log ( 'players' , players )
557
567
558
568
if ( playersPerTeam * 2 > players . length ) {
559
569
alert ( "Sem jogadores suficientes para começar a partida." ) ;
@@ -566,7 +576,8 @@ $("#start-match-day").click(async function() {
566
576
. filter ( p => p . playing )
567
577
. slice ( 0 , playersPerTeam * 2 ) ;
568
578
if ( firstPlayers . length < playersPerTeam * 2 ) {
569
- alert ( "Não há jogadores suficientes para começar a partida." ) ;
579
+ alert ( "Não há jogadores suficientes para começar a partida nesta quadra agora." ) ;
580
+
570
581
return ;
571
582
}
572
583
playingTeams = await generateTeams ( firstPlayers ) ;
@@ -645,8 +656,18 @@ async function endMatch(victoryTeam) {
645
656
players [ playerIndex ] . sigma = player . sigma ;
646
657
} ) ;
647
658
659
+ // Match is ended, no one is playing
660
+ playingTeams = [ ] ;
648
661
649
- $ ( "#match" ) . hide ( ) ;
662
+ $ ( "#team-1-captain" ) . text ( "" ) ;
663
+ $ ( "#team-2-captain" ) . text ( "" ) ;
664
+ $ ( "#team-1-players" ) . empty ( ) ;
665
+ $ ( "#team-2-players" ) . empty ( ) ;
666
+ $ ( "#score-team-1" ) . text ( "00" ) ;
667
+ $ ( "#score-team-2" ) . text ( "00" ) ;
668
+
669
+ updatePlayerList ( ) ;
670
+
650
671
}
651
672
652
673
function findPlayerByName ( players , name ) {
@@ -691,6 +712,7 @@ function findNextMatchPlayers(winners = []) {
691
712
692
713
693
714
async function startNewMatch ( winningPlayers ) {
715
+ $ ( "#start-next-match" ) . hide ( ) ;
694
716
$ ( "#match" ) . show ( ) ;
695
717
$ ( "#score-team-1" ) . text ( "00" ) ;
696
718
$ ( "#score-team-2" ) . text ( "00" ) ;
@@ -702,9 +724,14 @@ async function startNewMatch(winningPlayers) {
702
724
703
725
const nextMatchPlayers = findNextMatchPlayers ( winningPlayers ) ;
704
726
currentMatchMaxPoints = maxPoints ;
727
+
728
+ // Remove players from this match from players next game
729
+ playersToNextGame = playersToNextGame . filter ( player => ! findPlayerByName ( nextMatchPlayers , player . name ) ) ;
730
+
705
731
randomServe ( ) ;
706
732
await updateCurrentMatch ( await generateTeams ( nextMatchPlayers ) ) ;
707
733
await upsertGameDay ( ) ;
734
+ updatePlayerList ( ) ;
708
735
}
709
736
710
737
function randomServe ( ) {
@@ -746,19 +773,35 @@ $(".score-point").click(async function() {
746
773
}
747
774
748
775
if ( team1Score >= currentMatchMaxPoints && diff >= 2 ) {
749
- await endMatch ( 0 ) ;
750
- await startNewMatch ( playingTeams [ 0 ] ) ;
776
+ let confirmEndGame = window . confirm ( `Time ${ playingTeams [ 0 ] [ 0 ] . name } venceu a partida? Confirmar para encerrar.` ) ;
777
+
778
+ if ( confirmEndGame ) {
779
+ $ ( "#start-next-match" ) . show ( ) ;
780
+ playersToNextGame = playersToNextGame . concat ( playingTeams [ 0 ] ) ;
781
+ await endMatch ( 0 ) ;
782
+ await upsertGameDay ( ) ;
783
+ }
751
784
} else if ( team2Score >= currentMatchMaxPoints && diff >= 2 ) {
752
- await endMatch ( 1 ) ;
753
- await startNewMatch ( playingTeams [ 1 ] ) ;
785
+ let confirmEndGame = window . confirm ( `Time ${ playingTeams [ 1 ] [ 0 ] . name } venceu a partida? Confirmar para encerrar.` ) ;
786
+
787
+ if ( confirmEndGame ) {
788
+ $ ( "#start-next-match" ) . show ( ) ;
789
+ playersToNextGame = playersToNextGame . concat ( playingTeams [ 1 ] ) ;
790
+ await endMatch ( 1 ) ;
791
+ await upsertGameDay ( ) ;
792
+ }
754
793
}
755
-
794
+
756
795
if ( autoSwitchTeamsPoints > 0 ) {
757
796
const totalPoints = team1Score + team2Score ;
758
797
if ( totalPoints % autoSwitchTeamsPoints === 0 ) swapTeams ( ) ;
759
798
}
760
799
} ) ;
761
800
801
+ $ ( "#start-next-match" ) . click ( async function ( ) {
802
+ await startNewMatch ( playersToNextGame ) ;
803
+ } ) ;
804
+
762
805
$ ( ".undo-point" ) . click ( function ( ) {
763
806
const teamIndex = $ ( this ) . attr ( "id" ) ;
764
807
@@ -767,10 +810,12 @@ $(".undo-point").click(function() {
767
810
768
811
if ( teamIndex === "undo-1" ) {
769
812
team1Score -= 1 ;
770
- $ ( "#score-team-1" ) . text ( team1Score >= 0 ? team1Score : 0 ) ;
813
+ const value = team1Score >= 0 ? team1Score : 0 ;
814
+ $ ( "#score-team-1" ) . text ( value . toString ( ) . padStart ( 2 , 0 ) ) ;
771
815
} else if ( teamIndex === "undo-2" ) {
772
816
team2Score -= 1 ;
773
- $ ( "#score-team-2" ) . text ( team2Score >= 0 ? team2Score : 0 ) ;
817
+ const value = team2Score >= 0 ? team2Score : 0 ;
818
+ $ ( "#score-team-2" ) . text ( value . toString ( ) . padStart ( 2 , 0 ) ) ;
774
819
}
775
820
776
821
upsertGameDay ( ) ;
@@ -869,24 +914,6 @@ $("#change-match-day").click(function() {
869
914
$ ( "#max-points" ) . val ( maxPoints ) ;
870
915
} ) ;
871
916
872
- $ ( "#end-current-match" ) . click ( async function ( ) {
873
- let team1Score = parseInt ( $ ( "#score-team-1" ) . text ( ) ) ;
874
- let team2Score = parseInt ( $ ( "#score-team-2" ) . text ( ) ) ;
875
-
876
- if ( team1Score === team2Score ) {
877
- alert ( "Partida empatada, não é possível encerrar. Decida um vencedor." ) ;
878
- return ;
879
- }
880
-
881
- const winningTeam = team1Score > team2Score ? 0 : 1 ;
882
- const losingTeam = 1 - winningTeam ;
883
-
884
- const confirm = window . confirm ( "Deseja realmente encerrar a partida?" ) ;
885
- if ( confirm ) {
886
- await startNewMatch ( playingTeams [ winningTeam ] , playingTeams [ losingTeam ] ) ;
887
- }
888
- } ) ;
889
-
890
917
async function swapTeams ( ) {
891
918
const temp = playingTeams [ 0 ] ;
892
919
playingTeams [ 0 ] = playingTeams [ 1 ] ;
@@ -1060,3 +1087,10 @@ $(document).ready(async function (){
1060
1087
socket . emit ( 'join' , currentId ) ;
1061
1088
}
1062
1089
} ) ;
1090
+
1091
+ history . pushState ( null , null , window . top . location . pathname + window . top . location . search ) ;
1092
+ window . addEventListener ( 'popstate' , ( e ) => {
1093
+ e . preventDefault ( ) ;
1094
+ // Insert Your Logic Here, You Can Do Whatever You Want
1095
+ history . pushState ( null , null , window . top . location . pathname + window . top . location . search ) ;
1096
+ } ) ;
0 commit comments