-
-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathindex.tsx
More file actions
155 lines (146 loc) · 4.83 KB
/
index.tsx
File metadata and controls
155 lines (146 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React from "react";
import { Card, CardBody, CardHeader, Col, Row, Table } from "reactstrap";
import { useVirtualContestSubmissions } from "../../../../../api/APIClient";
import { ProblemLink } from "../../../../../components/ProblemLink";
import { UserNameLabel } from "../../../../../components/UserNameLabel";
import { ProblemId, UserId } from "../../../../../interfaces/Status";
import { isAccepted } from "../../../../../utils";
import { VirtualContestProblem } from "../../../types";
import { ShowDifficultyMode } from "../../../../../utils/ShowDifficultyMode";
const CARD_COLORS = ["success", "danger", "warning", "info", "primary"];
interface LockoutStatus {
userId: string;
point: number;
epochSecond: number;
color: string;
}
interface Props {
readonly showRating: boolean;
readonly showProblems: boolean;
readonly participants: UserId[];
readonly start: number;
readonly end: number;
readonly enableAutoRefresh: boolean;
readonly problems: VirtualContestProblem[];
}
export const LockoutContestTable: React.FC<Props> = (props) => {
const submissions =
useVirtualContestSubmissions(
props.participants,
props.problems.map((p) => p.item.id),
props.start,
props.end,
props.enableAutoRefresh
)
.data?.slice()
.sort((a, b) => a.id - b.id) ?? [];
const colorMap = new Map<string, string>();
for (let i = 0; i < props.participants.length; i++) {
const color = CARD_COLORS[i % CARD_COLORS.length];
colorMap.set(props.participants[i], color);
}
const statusMap = new Map<ProblemId, LockoutStatus>();
const pointMap = new Map<string, number>();
submissions
.filter((s) => isAccepted(s.result))
.forEach((s) => {
if (!statusMap.has(s.problem_id)) {
const point =
props.problems.find((p) => p.item.id === s.problem_id)?.item?.point ??
s.point;
const userId = s.user_id;
const epochSecond = s.epoch_second;
const color = colorMap.get(userId) ?? CARD_COLORS[0];
statusMap.set(s.problem_id, { point, userId, epochSecond, color });
pointMap.set(userId, (pointMap.get(userId) ?? 0) + point);
}
});
const width = Math.min(5, Math.ceil(Math.sqrt(props.problems.length)));
const table = [];
for (let i = 0; i < props.problems.length; i += width) {
const row = [];
for (let j = i; j < width + i; j++) {
if (props.problems.length <= j) {
row.push({
problem: undefined,
status: undefined,
});
} else {
const problem = props.problems[j];
const status = statusMap.get(problem.item.id);
row.push({ problem, status });
}
}
table.push(row);
}
const ranking = props.participants.map((userId) => ({
userId,
point: pointMap.get(userId) ?? 0,
}));
ranking.sort((a, b) => b.point - a.point);
return (
<>
<Table>
<thead>
<tr>
<th>User</th>
<th>Pt</th>
</tr>
</thead>
<tbody>
{ranking.map(({ userId, point }) => (
<tr key={userId}>
<th>
<UserNameLabel userId={userId} showRating={props.showRating} />
</th>
<td>{point}</td>
</tr>
))}
</tbody>
</Table>
{props.showProblems &&
table.map((row, i) => (
<Row key={i}>
{row.map(({ problem, status }, j) => (
<Col key={j}>
{problem ? (
<Card
inverse={!!status}
color={status ? status.color : undefined}
>
<CardHeader tag="h3">
{problem.title && problem.contestId ? (
<ProblemLink
showDifficultyMode={ShowDifficultyMode.None}
problemId={problem.item.id}
contestId={problem.contestId}
problemName={problem.title}
className={status ? "text-white" : "text-link"}
/>
) : (
problem.title
)}
</CardHeader>
<CardBody className="text-center">
{status ? (
<h3>
<p>{status.userId}</p>
<p>+ {status.point} pt</p>
</h3>
) : (
<h3>
{problem.item.point
? `${problem.item.point} pt`
: null}
</h3>
)}
</CardBody>
</Card>
) : null}
</Col>
))}
</Row>
))}
</>
);
};