opt-cc/server/tools/util.js

66 lines
1.7 KiB
JavaScript

'use strict';
const isSteamUUID = (input) => {
const steamUIDPattern = new RegExp('[0-9]{17}');
return steamUIDPattern.test(input);
};
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) => {
let i = 0;
let count = arr.length;
let matchFound = false;
for (; i < count; i++) {
if (arr[i].name === item.name && arr[i].fraction === item.fraction) {
matchFound = true;
break;
}
}
return matchFound;
};
const timeStringToDecimal = (timeString) => {
const timeArray = timeString.split(':');
const hour = parseInt(timeArray[0]);
const sek = parseInt(timeArray[1]) * 60 + parseInt(timeArray[2]);
return hour + (sek / 3600);
};
const decimalToTimeString = (decimal) => {
const hours = parseInt(decimal.toString().split('.')[0]);
const minutesFloat = ((decimal % 1) * 3600) / 60;
const minutes = parseInt(minutesFloat.toString().split('.')[0]);
const seconds = Math.round((minutesFloat - parseInt(minutes)) * 60);
return (hours < 10 ? '0' + hours : hours) + ':' +
(minutes < 10 ? '0' + minutes : minutes) + ':' +
(seconds < 10 ? '0' + seconds : seconds);
};
const getFileBuffer = (file) => {
if (file.encoding === 'base64') {
return Buffer.from(file.buffer.toString(), file.encoding);
}
return Buffer.from(file.buffer);
};
exports.isSteamUUID = isSteamUUID;
exports.sortCollection = sortCollectionBy;
exports.playerArrayContains = playerArrayContains;
exports.timeStringToDecimal = timeStringToDecimal;
exports.decimalToTimeString = decimalToTimeString;
exports.getFileBuffer = getFileBuffer;