fork/replay-manager-for-slippi
git clone https://git.julia.blue/fork/replay-manager-for-slippi
0a17a95
2.4.0
1import { useState } from 'react' ; 2import { 3Button , 4CircularProgress , 5Dialog , 6DialogActions , 7DialogContent , 8DialogContentText , 9DialogTitle , 10IconButton , 11Stack , 12Tooltip , 13} from '@mui/material' ; 14import { HourglassTop , SaveAs } from '@mui/icons-material' ; 15import styled from '@emotion/styled' ; 16import { MatchResult , SlotState } from '@parry-gg/client' ; 17import { 18ChallongeMatchItem , 19Id , 20Mode , 21Set , 22StartggGame , 23StartggSet , 24State , 25} from '../common/types' ; 26import { assertInteger , assertString } from '../common/asserts' ; 27 28function createStartggGameData ( 29entrant1Score :number , 30entrant2Score :number , 31entrant1Id :Id , 32entrant2Id :Id , 33) :StartggGame [] { 34const gameData :StartggGame [] = []; 35if ( entrant1Score > entrant2Score ) { 36for ( let n = 1 ; n <= entrant1Score + entrant2Score ; n += 1 ) { 37gameData . push ({ 38gameNum :n , 39winnerId :assertInteger ( n <= entrant1Score ?entrant1Id :entrant2Id ), 40entrant1Score :0 , 41entrant2Score :0 , 42selections :[], 43}); 44} 45} else if ( entrant1Score < entrant2Score ) { 46for ( let n = 1 ; n <= entrant1Score + entrant2Score ; n += 1 ) { 47gameData . push ({ 48gameNum :n , 49winnerId :assertInteger ( n <= entrant2Score ?entrant2Id :entrant1Id ), 50entrant1Score :0 , 51entrant2Score :0 , 52selections :[], 53}); 54} 55} 56return gameData ; 57} 58 59const EntrantNames = styled ( Stack ) ` 60flex-grow: 1; 61min-width: 0; 62` ; 63const Name = styled . div ` 64overflow-x: hidden; 65text-overflow: ellipsis; 66white-space: nowrap; 67` ; 68const ThinButton = styled ( Button ) ` 69min-width: 54px; 70` ; 71 72export default function ManualReport ({ 73 mode, 74 reportChallongeSet, 75 reportStartggSet, 76 reportParryggSet, 77 reportOfflineModeSet, 78 selectedSet, 79} :{ 80mode :Mode ; 81reportChallongeSet :( 82matchId :string , 83items :ChallongeMatchItem [], 84) => Promise < Set >; 85reportStartggSet :( 86set :StartggSet , 87originalSet :Set , 88) => Promise < Set | undefined >; 89reportParryggSet :( 90result :MatchResult . AsObject , 91originalSet :Set , 92) => Promise < Set | undefined >; 93reportOfflineModeSet :( set :StartggSet ) => Promise < Set >; 94selectedSet :Set ; 95}) { 96const [ open , setOpen ] = useState ( false ); 97const [ reporting , setReporting ] = useState ( false ); 98 99// startgg 100const [ entrant1Dq , setEntrant1Dq ] = useState ( false ); 101const [ entrant2Dq , setEntrant2Dq ] = useState ( false ); 102const [ entrant1Win , setEntrant1Win ] = useState ( false ); 103const [ entrant2Win , setEntrant2Win ] = useState ( false ); 104 105// challonge 106const [ entrant1Score , setEntrant1Score ] = useState ( 0 ); 107const [ entrant2Score , setEntrant2Score ] = useState ( 0 ); 108 109const resetFormToZero = () => { 110setEntrant1Dq ( false ); 111setEntrant2Dq ( false ); 112setEntrant1Win ( false ); 113setEntrant2Win ( false ); 114setEntrant1Score ( 0 ); 115setEntrant2Score ( 0 ); 116}; 117 118const resetFormToSelectedSet = () => { 119setEntrant1Dq ( 120selectedSet . state === State . COMPLETED && selectedSet . entrant1Score === - 1 , 121); 122setEntrant2Dq ( 123selectedSet . state === State . COMPLETED && selectedSet . entrant2Score === - 1 , 124); 125setEntrant1Win ( 126selectedSet . state === State . COMPLETED && 127selectedSet . winnerId === selectedSet . entrant1Id , 128); 129setEntrant2Win ( 130selectedSet . state === State . COMPLETED && 131selectedSet . winnerId === selectedSet . entrant2Id , 132); 133setEntrant1Score ( 134selectedSet . entrant1Score === null 135 ?0 136 :Math . max ( selectedSet . entrant1Score , 0 ), 137); 138setEntrant2Score ( 139selectedSet . entrant2Score === null 140 ?0 141 :Math . max ( selectedSet . entrant2Score , 0 ), 142); 143}; 144 145const [ reportError , setReportError ] = useState ( '' ); 146const [ reportErrorOpen , setReportErrorOpen ] = useState ( false ); 147 148let winnerId :Id = 0 ; 149if ( 150mode === Mode . STARTGG || 151mode === Mode . PARRYGG || 152mode === Mode . OFFLINE_MODE 153) { 154if ( entrant1Win || entrant2Dq ) { 155winnerId = selectedSet . entrant1Id ; 156} else if ( entrant2Win || entrant1Dq ) { 157winnerId = selectedSet . entrant2Id ; 158} 159} else if ( mode === Mode . CHALLONGE ) { 160if ( entrant1Score > entrant2Score ) { 161winnerId = selectedSet . entrant1Id ; 162} else if ( entrant1Score < entrant2Score ) { 163winnerId = selectedSet . entrant2Id ; 164} 165} 166const getStartggSet = () => ({ 167setId :selectedSet . id , 168winnerId :assertInteger ( winnerId ), 169isDQ :entrant1Dq || entrant2Dq , 170gameData :createStartggGameData ( 171entrant1Score , 172entrant2Score , 173selectedSet . entrant1Id , 174selectedSet . entrant2Id , 175), 176}); 177const challongeMatchItems :ChallongeMatchItem [] = [ 178{ 179participant_id :selectedSet . entrant1Id . toString ( 10 ), 180score_set :entrant1Score . toString ( 10 ), 181rank :winnerId === selectedSet . entrant1Id ?1 :2 , 182advancing :winnerId === selectedSet . entrant1Id , 183}, 184{ 185participant_id :selectedSet . entrant2Id . toString ( 10 ), 186score_set :entrant2Score . toString ( 10 ), 187rank :winnerId === selectedSet . entrant2Id ?1 :2 , 188advancing :winnerId === selectedSet . entrant2Id , 189}, 190]; 191 192const parryggSetResult :MatchResult . AsObject = { 193slotsList :[ 194{ 195slot :0 , 196score :entrant1Dq ?0 :entrant1Score , 197state :entrant1Dq 198 ?SlotState . SLOT_STATE_DQ 199 :SlotState . SLOT_STATE_NUMERIC , 200}, 201{ 202slot :1 , 203score :entrant2Dq ?0 :entrant2Score , 204state :entrant2Dq 205 ?SlotState . SLOT_STATE_DQ 206 :SlotState . SLOT_STATE_NUMERIC , 207}, 208], 209}; 210 211return ( 212<> 213< Tooltip arrow title = "Report Manually" > 214< div > 215< IconButton 216color = "primary" 217disabled = { 218! ( 219typeof selectedSet . id === 'string' || 220( Number . isInteger ( selectedSet . id ) && selectedSet . id > 0 ) 221) 222} 223size = "small" 224onClick = {() => { 225resetFormToSelectedSet (); 226setOpen ( true ); 227}} 228> 229< SaveAs /> 230</ IconButton > 231</ div > 232</ Tooltip > 233< Dialog open = { open } onClose = {() => setOpen ( false )}> 234< DialogTitle > Report set manually </ DialogTitle > 235< DialogContent sx = {{ width :'500px' }}> 236< Stack 237direction = "row" 238alignItems = "center" 239justifyContent = "center" 240typography = "caption" 241> 242{ selectedSet . fullRoundText } 243{ selectedSet . state === State . STARTED && ( 244<> 245 246< Tooltip arrow placement = "top" title = "Started" > 247< HourglassTop fontSize = "small" /> 248</ Tooltip > 249</> 250)} 251</ Stack > 252< Stack 253alignItems = "end" 254marginTop = "4px" 255spacing = "8px" 256sx = {{ typography :'body2' }} 257> 258< Stack 259alignItems = "center" 260direction = "row" 261justifyContent = "space-between" 262width = "100%" 263> 264< EntrantNames > 265< Name >{ selectedSet . entrant1Participants [ 0 ]. displayName }</ Name > 266{ selectedSet . entrant1Participants . length > 1 && ( 267< Name >{ selectedSet . entrant1Participants [ 1 ]. displayName }</ Name > 268)} 269</ EntrantNames > 270< Stack direction = "row" spacing = "8px" > 271{( mode === Mode . STARTGG || 272mode === Mode . PARRYGG || 273mode === Mode . OFFLINE_MODE ) && ( 274<> 275< ThinButton 276color = "secondary" 277variant = { entrant1Dq ?'contained' :'outlined' } 278disabled = { selectedSet . state === State . COMPLETED } 279onClick = {() => { 280resetFormToZero (); 281setEntrant1Dq ( true ); 282setEntrant2Win ( true ); 283}} 284> 285DQ 286</ ThinButton > 287< ThinButton 288color = "secondary" 289variant = { entrant1Score === 0 ?'contained' :'outlined' } 290disabled = { 291selectedSet . state === State . COMPLETED && 292selectedSet . winnerId === selectedSet . entrant1Id 293} 294onClick = {() => { 295setEntrant1Score ( 0 ); 296if ( entrant2Score > 0 ) { 297setEntrant1Win ( false ); 298setEntrant2Win ( true ); 299} else if ( entrant2Score === 0 ) { 300setEntrant1Win ( false ); 301setEntrant2Win ( false ); 302} 303}} 304> 3050 306</ ThinButton > 307< ThinButton 308color = "secondary" 309variant = { entrant1Score === 1 ?'contained' :'outlined' } 310disabled = { 311selectedSet . state === State . COMPLETED && 312(( selectedSet . winnerId === selectedSet . entrant1Id && 313entrant2Score >= 1 ) || 314( selectedSet . winnerId === selectedSet . entrant2Id && 315entrant2Score <= 1 )) 316} 317onClick = {() => { 318setEntrant1Dq ( false ); 319setEntrant2Dq ( false ); 320setEntrant1Score ( 1 ); 321if ( entrant2Score < 1 ) { 322setEntrant1Win ( true ); 323setEntrant2Win ( false ); 324} else if ( entrant2Score > 1 ) { 325setEntrant1Win ( false ); 326setEntrant2Win ( true ); 327} else { 328setEntrant1Win ( false ); 329setEntrant2Win ( false ); 330} 331}} 332> 3331 334</ ThinButton > 335< ThinButton 336color = "secondary" 337variant = { entrant1Score === 2 ?'contained' :'outlined' } 338disabled = { 339selectedSet . state === State . COMPLETED && 340(( selectedSet . winnerId === selectedSet . entrant1Id && 341entrant2Score >= 2 ) || 342( selectedSet . winnerId === selectedSet . entrant2Id && 343entrant2Score <= 2 )) 344} 345onClick = {() => { 346setEntrant1Dq ( false ); 347setEntrant2Dq ( false ); 348setEntrant1Score ( 2 ); 349if ( entrant2Score < 2 ) { 350setEntrant1Win ( true ); 351setEntrant2Win ( false ); 352} else if ( entrant2Score > 2 ) { 353setEntrant1Win ( false ); 354setEntrant2Win ( true ); 355} else { 356setEntrant1Win ( false ); 357setEntrant2Win ( false ); 358} 359}} 360> 3612 362</ ThinButton > 363< ThinButton 364color = "secondary" 365variant = { entrant1Score === 3 ?'contained' :'outlined' } 366disabled = { 367selectedSet . state === State . COMPLETED && 368selectedSet . winnerId === selectedSet . entrant2Id 369} 370onClick = {() => { 371setEntrant1Dq ( false ); 372setEntrant2Dq ( false ); 373setEntrant1Score ( 3 ); 374if ( entrant2Score < 3 ) { 375setEntrant1Win ( true ); 376setEntrant2Win ( false ); 377} else if ( entrant2Score === 3 ) { 378setEntrant1Win ( false ); 379setEntrant2Win ( false ); 380} 381}} 382> 3833 384</ ThinButton > 385{( mode === Mode . STARTGG || mode === Mode . OFFLINE_MODE ) && ( 386< ThinButton 387color = "secondary" 388variant = { entrant1Win ?'contained' :'outlined' } 389disabled = { selectedSet . state === State . COMPLETED } 390onClick = {() => { 391resetFormToZero (); 392setEntrant1Win ( true ); 393setEntrant2Dq ( false ); 394}} 395> 396W 397</ ThinButton > 398)} 399</> 400)} 401{ mode === Mode . CHALLONGE && ( 402<> 403< Button 404color = "secondary" 405variant = { entrant1Score === - 1 ?'contained' :'outlined' } 406onClick = {() => { 407setEntrant1Score ( - 1 ); 408}} 409> 410-1 411</ Button > 412< Button 413color = "secondary" 414variant = { entrant1Score === 0 ?'contained' :'outlined' } 415onClick = {() => { 416setEntrant1Score ( 0 ); 417}} 418> 4190 420</ Button > 421< Button 422color = "secondary" 423variant = { entrant1Score === 1 ?'contained' :'outlined' } 424onClick = {() => { 425setEntrant1Score ( 1 ); 426}} 427> 4281 429</ Button > 430< Button 431color = "secondary" 432variant = { entrant1Score === 2 ?'contained' :'outlined' } 433onClick = {() => { 434setEntrant1Score ( 2 ); 435}} 436> 4372 438</ Button > 439< Button 440color = "secondary" 441variant = { entrant1Score === 3 ?'contained' :'outlined' } 442onClick = {() => { 443setEntrant1Score ( 3 ); 444}} 445> 4463 447</ Button > 448</> 449)} 450</ Stack > 451</ Stack > 452< Stack 453alignItems = "center" 454direction = "row" 455justifyContent = "space-between" 456width = "100%" 457> 458< EntrantNames > 459< Name >{ selectedSet . entrant2Participants [ 0 ]. displayName }</ Name > 460{ selectedSet . entrant2Participants . length > 1 && ( 461< Name >{ selectedSet . entrant2Participants [ 1 ]. displayName }</ Name > 462)} 463</ EntrantNames > 464< Stack direction = "row" spacing = "8px" > 465{( mode === Mode . STARTGG || 466mode === Mode . PARRYGG || 467mode === Mode . OFFLINE_MODE ) && ( 468<> 469< ThinButton 470color = "secondary" 471variant = { entrant2Dq ?'contained' :'outlined' } 472disabled = { selectedSet . state === State . COMPLETED } 473onClick = {() => { 474resetFormToZero (); 475setEntrant2Dq ( true ); 476setEntrant1Win ( true ); 477}} 478> 479DQ 480</ ThinButton > 481< ThinButton 482color = "secondary" 483variant = { entrant2Score === 0 ?'contained' :'outlined' } 484disabled = { 485selectedSet . state === State . COMPLETED && 486selectedSet . winnerId === selectedSet . entrant2Id 487} 488onClick = {() => { 489setEntrant2Score ( 0 ); 490if ( entrant1Score > 0 ) { 491setEntrant1Win ( true ); 492setEntrant2Win ( false ); 493} else if ( entrant1Score === 0 ) { 494setEntrant1Win ( false ); 495setEntrant2Win ( false ); 496} 497}} 498> 4990 500</ ThinButton > 501< ThinButton 502color = "secondary" 503variant = { entrant2Score === 1 ?'contained' :'outlined' } 504disabled = { 505selectedSet . state === State . COMPLETED && 506(( selectedSet . winnerId === selectedSet . entrant2Id && 507entrant1Score >= 1 ) || 508( selectedSet . winnerId === selectedSet . entrant1Id && 509entrant1Score <= 1 )) 510} 511onClick = {() => { 512setEntrant1Dq ( false ); 513setEntrant2Dq ( false ); 514setEntrant2Score ( 1 ); 515if ( entrant1Score < 1 ) { 516setEntrant1Win ( false ); 517setEntrant2Win ( true ); 518} else if ( entrant1Score > 1 ) { 519setEntrant1Win ( true ); 520setEntrant2Win ( false ); 521} else { 522setEntrant1Win ( false ); 523setEntrant2Win ( false ); 524} 525}} 526> 5271 528</ ThinButton > 529< ThinButton 530color = "secondary" 531variant = { entrant2Score === 2 ?'contained' :'outlined' } 532disabled = { 533selectedSet . state === State . COMPLETED && 534(( selectedSet . winnerId === selectedSet . entrant2Id && 535entrant1Score >= 2 ) || 536( selectedSet . winnerId === selectedSet . entrant1Id && 537entrant1Score <= 2 )) 538} 539onClick = {() => { 540setEntrant1Dq ( false ); 541setEntrant2Dq ( false ); 542setEntrant2Score ( 2 ); 543if ( entrant1Score < 2 ) { 544setEntrant1Win ( false ); 545setEntrant2Win ( true ); 546} else if ( entrant1Score > 2 ) { 547setEntrant1Win ( true ); 548setEntrant2Win ( false ); 549} else { 550setEntrant1Win ( false ); 551setEntrant2Win ( false ); 552} 553}} 554> 5552 556</ ThinButton > 557< ThinButton 558color = "secondary" 559variant = { entrant2Score === 3 ?'contained' :'outlined' } 560disabled = { 561selectedSet . state === State . COMPLETED && 562selectedSet . winnerId === selectedSet . entrant1Id 563} 564onClick = {() => { 565setEntrant1Dq ( false ); 566setEntrant2Dq ( false ); 567setEntrant2Score ( 3 ); 568if ( entrant1Score < 3 ) { 569setEntrant1Win ( false ); 570setEntrant2Win ( true ); 571} else if ( entrant1Score === 3 ) { 572setEntrant1Win ( false ); 573setEntrant2Win ( false ); 574} 575}} 576> 5773 578</ ThinButton > 579{( mode === Mode . STARTGG || mode === Mode . OFFLINE_MODE ) && ( 580< ThinButton 581color = "secondary" 582variant = { entrant2Win ?'contained' :'outlined' } 583disabled = { selectedSet . state === State . COMPLETED } 584onClick = {() => { 585resetFormToZero (); 586setEntrant1Dq ( false ); 587setEntrant2Win ( true ); 588}} 589> 590W 591</ ThinButton > 592)} 593</> 594)} 595{ mode === Mode . CHALLONGE && ( 596<> 597< Button 598color = "secondary" 599variant = { entrant2Score === - 1 ?'contained' :'outlined' } 600onClick = {() => { 601setEntrant2Score ( - 1 ); 602}} 603> 604-1 605</ Button > 606< Button 607color = "secondary" 608variant = { entrant2Score === 0 ?'contained' :'outlined' } 609onClick = {() => { 610setEntrant2Score ( 0 ); 611}} 612> 6130 614</ Button > 615< Button 616color = "secondary" 617variant = { entrant2Score === 1 ?'contained' :'outlined' } 618onClick = {() => { 619setEntrant2Score ( 1 ); 620}} 621> 6221 623</ Button > 624< Button 625color = "secondary" 626variant = { entrant2Score === 2 ?'contained' :'outlined' } 627onClick = {() => { 628setEntrant2Score ( 2 ); 629}} 630> 6312 632</ Button > 633< Button 634color = "secondary" 635variant = { entrant2Score === 3 ?'contained' :'outlined' } 636onClick = {() => { 637setEntrant2Score ( 3 ); 638}} 639> 6403 641</ Button > 642</> 643)} 644</ Stack > 645</ Stack > 646</ Stack > 647</ DialogContent > 648< DialogActions > 649< Button 650disabled = { reporting || ! winnerId } 651endIcon = { reporting ?< CircularProgress size = "24px" /> :< SaveAs />} 652onClick = { async () => { 653setReporting ( true ); 654try { 655if ( mode === Mode . STARTGG ) { 656await reportStartggSet ( getStartggSet (), selectedSet ); 657} else if ( mode === Mode . CHALLONGE ) { 658await reportChallongeSet ( 659assertString ( selectedSet . id ), 660challongeMatchItems , 661); 662} else if ( mode === Mode . PARRYGG ) { 663await reportParryggSet ( parryggSetResult , selectedSet ); 664} else if ( mode === Mode . OFFLINE_MODE ) { 665await reportOfflineModeSet ( getStartggSet ()); 666} 667resetFormToZero (); 668setOpen ( false ); 669} catch ( e :any ) { 670const message = e instanceof Error ?e . message :e ; 671setReportError ( message ); 672setReportErrorOpen ( true ); 673} finally { 674setReporting ( false ); 675} 676}} 677variant = "contained" 678> 679Report Manually 680</ Button > 681</ DialogActions > 682</ Dialog > 683< Dialog 684open = { reportErrorOpen } 685onClose = {() => { 686setReportErrorOpen ( false ); 687setReportError ( '' ); 688}} 689> 690< DialogTitle > Report error! </ DialogTitle > 691< DialogContent > 692< DialogContentText >{ reportError }</ DialogContentText > 693</ DialogContent > 694< DialogActions > 695< Button 696onClick = {() => { 697setReportErrorOpen ( false ); 698setReportError ( '' ); 699}} 700> 701Close 702</ Button > 703</ DialogActions > 704</ Dialog > 705</> 706); 707}