forked from kenkoooo/AtCoderProblems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
220 lines (210 loc) · 7.06 KB
/
index.tsx
File metadata and controls
220 lines (210 loc) · 7.06 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import React from "react";
import { Badge, Col, Row, UncontrolledTooltip } from "reactstrap";
import {
useUserACRank,
useContests,
useContestToProblems,
useFastRanking,
useFirstRanking,
useProblemModelMap,
useShortRanking,
useUserStreakRank,
useUserSumRank,
useUserSubmission,
} from "../../../api/APIClient";
import {
caseInsensitiveUserId,
isAccepted,
ordinalSuffixOf,
} from "../../../utils";
import { formatMomentDate, getToday } from "../../../utils/DateUtil";
import { RankingEntry } from "../../../interfaces/RankingEntry";
import { ContestId, ProblemId } from "../../../interfaces/Status";
import ProblemModel, {
isProblemModelWithTimeModel,
} from "../../../interfaces/ProblemModel";
import { calculateTopPlayerEquivalentEffort } from "../../../utils/ProblemModelUtil";
import Problem from "../../../interfaces/Problem";
import * as UserUtils from "../UserUtils";
import { calcStreak, countUniqueAcByDate } from "../../../utils/StreakCounter";
import { isRatedContest } from "../../../utils/ContestClassifier";
const findFromRanking = (
ranking: RankingEntry[],
userId: string
): {
rank: number;
count: number;
} => {
const entry = ranking
.sort((a, b) => b.count - a.count)
.find((r) => caseInsensitiveUserId(r.user_id) === userId);
if (entry) {
const count = entry.count;
const rank = ranking.filter((e) => e.count > count).length;
return { rank, count };
} else {
return { rank: ranking.length, count: 0 };
}
};
interface Props {
userId: string;
}
export const AchievementBlock: React.FC<Props> = (props) => {
const contests = useContests().data ?? [];
const contestToProblems =
useContestToProblems() ?? new Map<ContestId, Problem[]>();
const userSubmissions = useUserSubmission(props.userId) ?? [];
const problemModels = useProblemModelMap();
const dailyCount = countUniqueAcByDate(userSubmissions);
const { longestStreak, currentStreak, prevDateLabel } =
calcStreak(dailyCount);
const shortRanking = useShortRanking() ?? [];
const fastRanking = useFastRanking() ?? [];
const firstRanking = useFirstRanking() ?? [];
const solvedProblemIds = UserUtils.solvedProblemIdsFromArray(userSubmissions);
const solvedCount = solvedProblemIds.length;
const acRankEntry = useUserACRank(props.userId);
const acRank = acRankEntry.data?.rank;
const shortRank = findFromRanking(shortRanking, props.userId);
const firstRank = findFromRanking(firstRanking, props.userId);
const fastRank = findFromRanking(fastRanking, props.userId);
const ratedProblemIds = new Set(
contests
.flatMap((contest) => {
const contestProblems = contestToProblems.get(contest.id);
const isRated = isRatedContest(contest, contestProblems?.length ?? 0);
return isRated && contestProblems ? contestProblems : [];
})
.map((problem) => problem.id)
);
const acceptedRatedSubmissions = userSubmissions
.filter((s) => isAccepted(s.result))
.filter((s) => ratedProblemIds.has(s.problem_id));
acceptedRatedSubmissions.sort((a, b) => a.id - b.id);
const ratedPointMap = new Map<ProblemId, number>();
acceptedRatedSubmissions.forEach((s) => {
ratedPointMap.set(s.problem_id, s.point);
});
const ratedPointSum = Array.from(ratedPointMap.values()).reduce(
(sum, point) => sum + point,
0
);
const sumRankEntry = useUserSumRank(props.userId);
const sumRank = sumRankEntry.data?.rank;
const achievements = [
{
key: "Accepted",
value: solvedCount,
rank: acRank,
},
{
key: "Shortest Code",
value: shortRank.count,
rank: shortRank.rank,
},
{
key: "Fastest Code",
value: fastRank.count,
rank: fastRank.rank,
},
{
key: "First AC",
value: firstRank.count,
rank: firstRank.rank,
},
{
key: "Rated Point Sum",
value: ratedPointSum,
rank: sumRank,
},
];
const yesterdayLabel = formatMomentDate(getToday().add(-1, "day"));
const isIncreasing = prevDateLabel >= yesterdayLabel;
const streakRankEntry = useUserStreakRank(props.userId);
const longestStreakRank = streakRankEntry.data?.rank;
const streakSum = dailyCount.length;
const topPlayerEquivalentEffort = solvedProblemIds
.map((problemId: ProblemId) => problemModels?.get(problemId))
.filter((model: ProblemModel | undefined) => model !== undefined)
.filter(isProblemModelWithTimeModel)
.map(calculateTopPlayerEquivalentEffort)
.reduce((a: number, b: number) => a + b, 0);
return (
<>
<Row className="my-2 border-bottom">
<h1>Achievement</h1>
</Row>
<Row className="my-3">
{achievements.map(({ key, value, rank }) => (
<Col key={key} className="text-center" xs="6" md="3">
<h6>{key}</h6>
<h3>{value}</h3>
<h6 className="text-muted">
{rank !== undefined
? `${rank + 1}${ordinalSuffixOf(rank + 1)}`
: ""}
</h6>
</Col>
))}
<Col key="Longest Streak" className="text-center" xs="6" md="3">
<h6>
Longest Streak{" "}
<Badge pill id="longestStreakTooltip">
?
</Badge>
<UncontrolledTooltip
target="longestStreakTooltip"
placement="right"
>
The longest streak is based on{" "}
<strong>Japan Standard Time</strong> (JST, UTC+9).
</UncontrolledTooltip>
</h6>
<h3>{longestStreak} days</h3>
<h6 className="text-muted">
{longestStreakRank !== undefined
? `${longestStreakRank + 1}${ordinalSuffixOf(
longestStreakRank + 1
)}`
: ""}
</h6>
</Col>
<Col key="Current Streak" className="text-center" xs="6" md="3">
<h6>
Current Streak{" "}
<Badge pill id="currentStreakTooltip">
?
</Badge>
<UncontrolledTooltip
target="currentStreakTooltip"
placement="right"
>
The current streak is based on <strong>Local Time</strong>.
</UncontrolledTooltip>
</h6>
<h3>{isIncreasing ? currentStreak : 0} days</h3>
<h6 className="text-muted">{`Last AC: ${prevDateLabel}`}</h6>
</Col>
<Col key="Streak Sum" className="text-center" xs="6" md="3">
<h6>Streak Sum</h6>
<h3>{streakSum} days</h3>
</Col>
<Col key="TEE" className="text-center" xs="6" md="3">
<h6>
TEE{" "}
<Badge pill id="teeToolTip">
?
</Badge>
<UncontrolledTooltip target="teeToolTip" placement="right">
<strong>Top player-Equivalent Effort</strong>. The estimated time
in seconds required for a contestant with 4000 rating to solve all
the problems this contestant have solved.
</UncontrolledTooltip>
</h6>
<h3>{Math.round(topPlayerEquivalentEffort)}</h3>
</Col>
<Col />
</Row>
</>
);
};