opt-cc/server/tools/util.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-03-12 09:26:44 +01:00
'use strict';
2017-05-10 11:04:06 +02:00
const isSteamUUID = (input) => {
2018-03-12 09:26:44 +01:00
const steamUIDPattern = new RegExp('[0-9]{17}');
return steamUIDPattern.test(input);
};
2017-05-10 11:04:06 +02:00
const sortCollectionBy = (collection, key) => {
collection.sort((a, b) => {
a = a[key].toLowerCase();
b = b[key].toLowerCase();
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
return collection;
};
const playerArrayContains = (arr, item) => {
2018-03-12 09:26:44 +01:00
let i = 0;
let count = arr.length;
let matchFound = false;
2017-10-20 23:42:41 +02:00
2017-10-24 20:21:06 +02:00
for (; i < count; i++) {
if (arr[i].name === item.name && arr[i].fraction === item.fraction) {
2017-10-20 23:42:41 +02:00
matchFound = true;
break;
}
}
return matchFound;
};
2017-10-24 20:21:06 +02:00
const timeStringToDecimal = (timeString) => {
const timeArray = timeString.split(':');
const hour = parseInt(timeArray[0]);
const sek = parseInt(timeArray[1]) * 60 + parseInt(timeArray[2]);
2017-10-26 19:12:54 +02:00
return hour + (sek / 3600);
2017-10-24 20:21:06 +02:00
};
const decimalToTimeString = (decimal) => {
2018-03-12 09:26:44 +01:00
const hours = parseInt(decimal.toString().split('.')[0]);
2017-10-24 20:21:06 +02:00
const minutesFloat = ((decimal % 1) * 3600) / 60;
2018-03-12 09:26:44 +01:00
const minutes = parseInt(minutesFloat.toString().split('.')[0]);
2017-10-24 20:21:06 +02:00
const seconds = Math.round((minutesFloat - parseInt(minutes)) * 60);
return (hours < 10 ? '0' + hours : hours) + ':' +
(minutes < 10 ? '0' + minutes : minutes) + ':' +
(seconds < 10 ? '0' + seconds : seconds);
};
exports.isSteamUUID = isSteamUUID;
2017-05-10 11:04:06 +02:00
exports.sortCollection = sortCollectionBy;
exports.playerArrayContains = playerArrayContains;
2017-10-24 20:21:06 +02:00
exports.timeStringToDecimal = timeStringToDecimal;
exports.decimalToTimeString = decimalToTimeString;