]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Participant.js
sort results by time
[alttp.git] / resources / js / helpers / Participant.js
1 export const compareResult = round => (a, b) => {
2         const a_result = findResult(a, round);
3         const b_result = findResult(b, round);
4         const a_time = a_result ? a_result.time : 0;
5         const b_time = b_result ? b_result.time : 0;
6         if (a_time) {
7                 if (b_time) {
8                         if (a_time < b_time) return -1;
9                         if (b_time < a_time) return 1;
10                         return 0;
11                 }
12                 return -1;
13         }
14         if (b_time) {
15                 return 1;
16         }
17         return 0;
18 };
19
20 export const compareUsername = (a, b) => {
21         const a_name = a && a.user && a.user.username ? a.user.username : '';
22         const b_name = b && b.user && b.user.username ? b.user.username : '';
23         return a_name.localeCompare(b_name);
24 };
25
26 export const findResult = (participant, round) => {
27         if (!participant || !participant.user_id) return null;
28         if (!round || !round.results || !round.results.length) return null;
29         return round.results.find(result => result.user_id === participant.user_id);
30 };
31
32 export const sortByResult = (participants, round) => {
33         if (!participants || !participants.length) return participants;
34         if (!round || !round.results || !round.results.length) return participants;
35         return participants.sort(compareResult(round));
36 };
37
38 export default {
39         compareResult,
40         compareUsername,
41         findResult,
42         sortByResult,
43 };