fork/replay-manager-for-slippi

git clone https://git.julia.blue/fork/replay-manager-for-slippi

Nicoletmanual update restrictions0a17a95

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