X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=resources%2Fjs%2Fhelpers%2FResult.js;h=329116d245515ffa0f3603d599a620388443156e;hb=ac6921da72ff4b0beab9e5f1308788a55aae3ad9;hp=633294de1d051574c46f52fc3b64490ce7780125;hpb=edd0e97bfdc544114f30bf4c13a929631c44a555;p=alttp.git
diff --git a/resources/js/helpers/Result.js b/resources/js/helpers/Result.js
index 633294d..329116d 100644
--- a/resources/js/helpers/Result.js
+++ b/resources/js/helpers/Result.js
@@ -1,3 +1,29 @@
+import React from 'react';
+import Icon from '../components/common/Icon';
+
+export const compareResult = (a, b) => {
+ const a_placement = a && a.placement ? a.placement : 0;
+ const b_placement = b && b.placement ? b.placement : 0;
+ if (a_placement) {
+ if (b_placement) {
+ if (a_placement < b_placement) return -1;
+ if (b_placement < a_placement) return 1;
+ return compareUsername(a, b);
+ }
+ return -1;
+ }
+ if (b_placement) {
+ return 1;
+ }
+ return compareUsername(a, b);
+};
+
+export const compareUsername = (a, b) => {
+ const a_name = (a && a.user && a.user.username) || '';
+ const b_name = (b && b.user && b.user.username) || '';
+ return a_name.localeCompare(b_name);
+};
+
export const formatTime = result => {
const hours = `${Math.floor(result.time / 60 / 60)}`;
let minutes = `${Math.floor((result.time / 60) % 60)}`;
@@ -11,6 +37,48 @@ export const formatTime = result => {
return `${hours}:${minutes}:${seconds}`;
};
+export const getIcon = (result, maySee) => {
+ if (!result || !result.has_finished) {
+ return ;
+ }
+ if (result.forfeit && maySee) {
+ return ;
+ }
+ if (result.placement === 1 && maySee) {
+ return ;
+ }
+ if (result.placement === 2 && maySee) {
+ return ;
+ }
+ if (result.placement === 3 && maySee) {
+ return ;
+ }
+ return ;
+};
+
+export const getTime = (result, maySee) => {
+ if (!result || !maySee) {
+ return null;
+ }
+ if (result.time) {
+ return formatTime(result);
+ }
+ if (result.forfeit) {
+ return 'DNF';
+ }
+ return '?';
+};
+
+export const parseTime = str => {
+ if (!str) return null;
+ return `${str}`.trim().split(/[-.: ]+/).reduce((acc,time) => (60 * acc) + +time, 0);
+};
+
export default {
+ compareResult,
+ compareUsername,
formatTime,
+ getIcon,
+ getTime,
+ parseTime,
};