]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Participant.js
add forfeit result
[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.forfeit ? a_result.time : 0;
5         const b_time = b_result && !b_result.forfeit ? 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         const a_forfeit = a_result && a_result.forfeit;
18         const b_forfeit = b_result && b_result.forfeit;
19         if (a_forfeit) {
20                 if (b_forfeit) {
21                         return 0;
22                 }
23                 return -1;
24         }
25         if (b_forfeit) {
26                 return 1;
27         }
28         return 0;
29 };
30
31 export const compareUsername = (a, b) => {
32         const a_name = a && a.user && a.user.username ? a.user.username : '';
33         const b_name = b && b.user && b.user.username ? b.user.username : '';
34         return a_name.localeCompare(b_name);
35 };
36
37 export const findResult = (participant, round) => {
38         if (!participant || !participant.user_id) return null;
39         if (!round || !round.results || !round.results.length) return null;
40         return round.results.find(result => result.user_id === participant.user_id);
41 };
42
43 export const sortByResult = (participants, round) => {
44         if (!participants || !participants.length) return participants;
45         if (!round || !round.results || !round.results.length) return participants;
46         return participants.sort(compareResult(round));
47 };
48
49 export default {
50         compareResult,
51         compareUsername,
52         findResult,
53         sortByResult,
54 };