From 5dfe47a3e86d0d71b17873ffcded615a9e9ca8de Mon Sep 17 00:00:00 2001 From: Vortrex <3858226+VortrexFTW@users.noreply.github.com> Date: Sun, 9 Jan 2022 17:19:53 -0600 Subject: [PATCH] Organize a lot of utils --- scripts/client/native/connected.js | 72 ++ scripts/server/accent.js | 18 + scripts/server/animation.js | 20 +- scripts/server/anticheat.js | 40 ++ scripts/server/business.js | 19 +- scripts/server/clan.js | 40 +- scripts/server/command.js | 42 ++ scripts/server/house.js | 19 +- scripts/server/item.js | 21 +- scripts/server/job.js | 42 +- scripts/server/locale.js | 15 + scripts/server/messaging.js | 28 + scripts/server/native/connected.js | 65 ++ scripts/server/utilities.js | 1063 ---------------------------- scripts/server/vehicle.js | 6 + scripts/shared/utilities.js | 464 ++++++++++++ 16 files changed, 905 insertions(+), 1069 deletions(-) diff --git a/scripts/client/native/connected.js b/scripts/client/native/connected.js index 8215fa01..ac2e2b90 100644 --- a/scripts/client/native/connected.js +++ b/scripts/client/native/connected.js @@ -67,4 +67,76 @@ function sendNetworkEventToServer(eventName, ...args) { triggerNetworkEvent.apply(null, eventName, args); } +// =========================================================================== + +function getElementId(element) { + return element.id; +} + +// =========================================================================== + +function getClientFromIndex(index) { + let clients = getClients(); + for(let i in clients) { + if(clients[i].index == index) { + return clients[i]; + } + } +} + +// =========================================================================== + +function getVehiclesInRange(position, distance) { + return getElementsByType(ELEMENT_VEHICLE).filter(x => x.player && x.position.distance(position) <= distance); +} + +// =========================================================================== + +function getClientsInRange(position, distance) { + return getPlayersInRange(position, distance); +} + +// =========================================================================== + +function getCiviliansInRange(position, distance) { + return getElementsByType(ELEMENT_PED).filter(x => !x.isType(ELEMENT_PLAYER) && getElementPosition(x).position.distance(position) <= distance); +} + +// =========================================================================== + +function getPlayersInRange(position, distance) { + return getClients().filter(x => getPlayerPosition(x).distance(position) <= distance); +} + +// =========================================================================== + +function getElementsByTypeInRange(elementType, position, distance) { + return getElementsByType(elementType).filter(x => getElementPosition(x).position.distance(position) <= distance); +} + +// =========================================================================== + +function getClosestCivilian(position) { + return getElementsByType(ELEMENT_PED).reduce((i, j) => ((i.position.distance(position) <= j.position.distance(position)) ? i : j)); +} + +// =========================================================================== + +function is2dPositionOnScreen(pos2d) { + return pos2d.x >= 0 && pos2d.y >= 0 && pos2d.x <= game.width && pos2d.y <= game.height; +} + +// =========================================================================== + +function getVehiclesInRange(position, range) { + let vehicles = getElementsByType(ELEMENT_VEHICLE); + let inRangeVehicles = []; + for(let i in vehicles) { + if(getDistance(position, vehicles[i].position) <= range) { + inRangeVehicles.push(vehicles[i]); + } + } + return inRangeVehicles; +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/server/accent.js b/scripts/server/accent.js index 404c14b0..8ddafb52 100644 --- a/scripts/server/accent.js +++ b/scripts/server/accent.js @@ -70,4 +70,22 @@ function listAccentsCommand(command, params, client) { } } +// =========================================================================== + +function getAccentFromParams(params) { + if(isNaN(params)) { + for(let i in getGlobalConfig().accents) { + if(toLowerCase(getGlobalConfig().accents[i]).indexOf(toLowerCase(params)) != -1) { + return i; + } + } + } else { + if(typeof getGlobalConfig().accents[params] != "undefined") { + return toInteger(params); + } + } + + return false; +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/server/animation.js b/scripts/server/animation.js index 2a367db5..583f6935 100644 --- a/scripts/server/animation.js +++ b/scripts/server/animation.js @@ -75,7 +75,7 @@ function showAnimationListCommand(command, params, client) { let chunkedList = splitArrayIntoChunks(animList, 10); - messagePlayerInfo(client, makeChatSectionHeader(getLocaleString(client, "HeaderAnimationsList"))); + messagePlayerInfo(client, makeChatBoxSectionHeader(getLocaleString(client, "HeaderAnimationsList"))); for(let i in chunkedList) { messagePlayerNormal(client, chunkedList[i].join(", ")); @@ -138,4 +138,22 @@ function makePlayerStopAnimation(client) { getPlayerData(client).animationForced = false; } +// =========================================================================== + +function getAnimationFromParams(params) { + if(isNaN(params)) { + for(let i in getGameData().animations[getServerGame()]) { + if(toLowerCase(getGameData().animations[getServerGame()][i][0]).indexOf(toLowerCase(params)) != -1) { + return i; + } + } + } else { + if(typeof getGameData().animations[getServerGame()][params] != "undefined") { + return toInteger(params); + } + } + + return false; +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/server/anticheat.js b/scripts/server/anticheat.js index bebfe6bf..e82b5079 100644 --- a/scripts/server/anticheat.js +++ b/scripts/server/anticheat.js @@ -248,4 +248,44 @@ function isPlayerExemptFromAntiCheat(client) { return false; } +// =========================================================================== + +function canPlayerUsePoliceJob(client) { + if(getPlayerData(client).accountData.flags.moderation & getServerBitFlags().moderationFlags.policeBanned) { + return false; + } + + return true; +} + +// =========================================================================== + +function canClientUseFireJob(client) { + if(getPlayerData(client).accountData.flags.moderation & getServerBitFlags().moderationFlags.fireBanned) { + return false; + } + + return true; +} + +// =========================================================================== + +function canClientUseAmmunations(client) { + if(getPlayerData(client).accountData.flags.moderation & getServerBitFlags().moderationFlags.AmmuBanned) { + return false; + } + + return true; +} + +// =========================================================================== + +function canClientUseGuns(client) { + if(getPlayerData(client).accountData.flags.moderation & getServerBitFlags().moderationFlags.GunBanned) { + return false; + } + + return true; +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/server/business.js b/scripts/server/business.js index 45dfe3f8..0d921f53 100644 --- a/scripts/server/business.js +++ b/scripts/server/business.js @@ -517,7 +517,7 @@ function lockUnlockBusinessCommand(command, params, client) { setEntityData(getBusinessData(businessId).entrancePickup, "vrr.label.locked", getBusinessData(businessId).locked, true); getBusinessData(businessId).needsSaved = true; - messagePlayerSuccess(client, `${getLockedUnlockedEmojiFromBool((getBusinessData(businessId).locked))} Business {businessBlue}${getBusinessData(businessId).name} {MAINCOLOUR}${getLockedUnlockedTextFromBool((getBusinessData(businessId).locked))}!`); + messagePlayerSuccess(client, `${getLockedUnlockedEmojiFromBool((getBusinessData(businessId).locked))} Business {businessBlue}${getBusinessData(businessId).name} {MAINCOLOUR}${getLockedUnlockedFromBool((getBusinessData(businessId).locked))}!`); } // =========================================================================== @@ -2126,3 +2126,20 @@ function deleteBusinessPickups(business) { // =========================================================================== +function getBusinessFromParams(params) { + if(isNaN(params)) { + for(let i in getServerData().businesses) { + if(toLowerCase(getServerData().businesses[i].name).indexOf(toLowerCase(params)) != -1) { + return i; + } + } + } else { + if(typeof getServerData().businesses[params] != "undefined") { + return toInteger(params); + } + } + return false; +} + +// =========================================================================== + diff --git a/scripts/server/clan.js b/scripts/server/clan.js index 0938b0d3..0e58a962 100644 --- a/scripts/server/clan.js +++ b/scripts/server/clan.js @@ -1215,4 +1215,42 @@ function showClanFlagListCommand(command, params, client) { messagePlayerInfo(client, chunkedList[i].join(", ")); } } -*/ \ No newline at end of file +*/ + +function getClanFromParams(params) { + if(isNaN(params)) { + for(let i in getServerData().clans) { + if(toLowerCase(getServerData().clans[i].name).indexOf(toLowerCase(params)) != -1) { + return i; + } + } + } else { + if(typeof getServerData().clans[params] != "undefined") { + return toInteger(params); + } + } + + return false; +} + +// =========================================================================== + +function getClanRankFromParams(clanId, params) { + if(isNaN(params)) { + for(let i in getClanData(clanId).ranks) { + if((toLowerCase(getClanData(clanId).ranks[i].name).indexOf(toLowerCase(params)) != -1)) { + return i; + } + } + } else { + for(let i in getClanData(clanId).ranks) { + if(getClanData(clanId).ranks[i].level == toInteger(params)) { + return i; + } + } + } + + return false; +} + +// =========================================================================== \ No newline at end of file diff --git a/scripts/server/command.js b/scripts/server/command.js index 5c81bb46..1815ec37 100644 --- a/scripts/server/command.js +++ b/scripts/server/command.js @@ -765,4 +765,46 @@ function getCommandAliasesNames(command) { return commandAliases; } +// =========================================================================== + +function areParamsEmpty(params) { + if(!params || params == "" || params.length == 0 || typeof params == "undefined") { + return true; + } + + return false; +} + +// =========================================================================== + +function getParamsCount(params, delimiter = " ") { + return params.split(delimiter).length; +} + +// =========================================================================== + +function areThereEnoughParams(params, requiredAmount, delimiter = " ") { + return (params.split(delimiter).length >= requiredAmount); +} + +// =========================================================================== + +function getParam(params, delimiter, index) { + return params.split(delimiter)[index]; +} + +// =========================================================================== + +function getCommandFromParams(params) { + for(let i in serverCommands) { + for(let j in serverCommands[i]) { + if(toLowerCase(serverCommands[i][j].command).indexOf(toLowerCase(params)) != -1) { + return serverCommands[i][j]; + } + } + } + + return false; +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/server/house.js b/scripts/server/house.js index 969e9fd0..b690dc9c 100644 --- a/scripts/server/house.js +++ b/scripts/server/house.js @@ -125,7 +125,7 @@ function lockUnlockHouseCommand(command, params, client) { setEntityData(getHouseData(houseId).entrancePickup, "vrr.label.locked", getHouseData(houseId).locked, true); getHouseData(houseId).needsSaved = true; - messagePlayerSuccess(client, `House {houseGreen}${getHouseData(houseId).description} {MAINCOLOUR}${getLockedUnlockedTextFromBool((getHouseData(houseId).locked))}!`); + messagePlayerSuccess(client, `House {houseGreen}${getHouseData(houseId).description} {MAINCOLOUR}${getLockedUnlockedFromBool((getHouseData(houseId).locked))}!`); } // =========================================================================== @@ -1380,4 +1380,21 @@ function canPlayerManageHouse(client, houseId) { return false; } +// =========================================================================== + +function getHouseFromParams(params) { + if(isNaN(params)) { + for(let i in getServerData().houses) { + if(toLowerCase(getServerData().houses[i].description).indexOf(toLowerCase(params)) != -1) { + return i; + } + } + } else { + if(typeof getServerData().houses[params] != "undefined") { + return toInteger(params); + } + } + return false; +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/server/item.js b/scripts/server/item.js index aadad5b8..c6a2c074 100644 --- a/scripts/server/item.js +++ b/scripts/server/item.js @@ -2020,4 +2020,23 @@ function createGroundPlant(itemId) { groundPlantCache.push(itemId); groundItemCache.push(itemId); -} \ No newline at end of file +} + +// =========================================================================== + +function getItemTypeFromParams(params) { + if(isNaN(params)) { + for(let i in getServerData().itemTypes) { + if(toLowerCase(getServerData().itemTypes[i].name).indexOf(toLowerCase(params)) != -1) { + return i; + } + } + } else { + if(typeof getServerData().itemTypes[params] != "undefined") { + return toInteger(params); + } + } + return false; +} + +// =========================================================================== \ No newline at end of file diff --git a/scripts/server/job.js b/scripts/server/job.js index ecb2396c..7668ee65 100644 --- a/scripts/server/job.js +++ b/scripts/server/job.js @@ -2083,4 +2083,44 @@ function setAllJobIndexes() { } } } -} \ No newline at end of file +} + +// =========================================================================== + +function getJobFromParams(params) { + if(isNaN(params)) { + for(let i in getServerData().jobs) { + if(toLowerCase(getServerData().jobs[i].name).indexOf(toLowerCase(params)) != -1) { + return i; + } + } + } else { + if(typeof getServerData().jobs[params] != "undefined") { + return params; + } + } + + return false; +} + +// =========================================================================== + +function getClosestJobLocation(position) { + let closestJobLocation = false; + for(let i in getServerData().jobs) { + for(let j in getServerData().jobs[i].locations) { + if(!closestJobLocation || getServerData().jobs[i].locations[j].position.distance(position) < closestJobLocation.position.distance(position)) { + closestJobLocation = getServerData().jobs[i].locations[j]; + } + } + } + return closestJobLocation; +} + +// =========================================================================== + +function getJobPointsInRange(position, distance) { + return getServerData().jobs[getServerGame()].filter(x => x.position.distance(position) <= distance); +} + +// =========================================================================== \ No newline at end of file diff --git a/scripts/server/locale.js b/scripts/server/locale.js index 59dfce4d..4ae80449 100644 --- a/scripts/server/locale.js +++ b/scripts/server/locale.js @@ -68,4 +68,19 @@ function getLocaleStrings() { return getServerData().localeStrings; } +// =========================================================================== + +function getLocaleNameFromParams(params) { + let locales = getLocales(); + if(isNaN(params)) { + for(let i in locales) { + if(toLowerCase(i).indexOf(toLowerCase(params)) != -1) { + return locales[i]; + } + } + } + + return false; +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/server/messaging.js b/scripts/server/messaging.js index 373e8bc8..790cf72b 100644 --- a/scripts/server/messaging.js +++ b/scripts/server/messaging.js @@ -211,4 +211,32 @@ function messagePlayerTimedRandomTip(client, message) { } } +// =========================================================================== + +function makeChatBoxSectionHeader(name) { + let resultString = `== ${name} `; + let endFiller = fillStringWithCharacter("=", getGlobalConfig().chatSectionHeaderLength-resultString.length); + return `${resultString} ${endFiller}`; +} + +// =========================================================================== + +function clearChatBox(client) { + //game.messages.clear(); + for(let i = 0; i <= 20; i++) { + messageClient(" ", client, COLOUR_WHITE); + } +} + +// =========================================================================== + +function replaceEmojiIntoString(message) { + for(let i in emojiReplaceString) { + while(message.indexOf(emojiReplaceString[i][0]) != -1) { + message = message.replace(emojiReplaceString[i][0], emojiReplaceString[i][1]); + } + } + return message; +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/server/native/connected.js b/scripts/server/native/connected.js index 218dcced..1ce9a32c 100644 --- a/scripts/server/native/connected.js +++ b/scripts/server/native/connected.js @@ -898,4 +898,69 @@ function addNetworkEventHandler(eventName, handlerFunction) { addNetworkHandler(eventName, handlerFunction); } +// =========================================================================== + +function getElementId(element) { + return element.id; +} + +// =========================================================================== + +function getClientFromIndex(index) { + let clients = getClients(); + for(let i in clients) { + if(clients[i].index == index) { + return clients[i]; + } + } +} + +// =========================================================================== + +function getClientsInRange(position, distance) { + return getPlayersInRange(position, distance); +} + +// =========================================================================== + +function getCiviliansInRange(position, distance) { + return getElementsByTypeInRange(ELEMENT_PED, position, distance).filter(x => !x.isType(ELEMENT_PLAYER)); +} + +// =========================================================================== + +function getPlayersInRange(position, distance) { + return getClients().filter(x => getDistance(position, getPlayerPosition(x)) <= distance); +} + +// =========================================================================== + +function getElementsByTypeInRange(elementType, position, distance) { + return getElementsByType(elementType).filter(x => getDistance(position, getElementPosition(x)) <= distance); +} + +// =========================================================================== + +function getClosestCivilian(position) { + return getClosestElementByType(ELEMENT_PED, position).filter(ped => !ped.isType(ELEMENT_PLAYER)); +} + +// =========================================================================== + +function getVehiclesInRange(position, range) { + return getElementsByTypeInRange(ELEMENT_VEHICLE, position, range); +} + +// =========================================================================== + +function getClosestVehicle(position) { + return getClosestElementByType(ELEMENT_VEHICLE, position); +} + +// =========================================================================== + +function getClosestElementByType(elementType, position) { + return getElementsByType(elementType).reduce((i, j) => (getDistance(position, getElementPosition(i)) <= getDistance(position, getElementPosition(j))) ? i : j); +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/server/utilities.js b/scripts/server/utilities.js index 34352b41..dca6eca1 100644 --- a/scripts/server/utilities.js +++ b/scripts/server/utilities.js @@ -23,420 +23,6 @@ let disconnectReasons = [ // =========================================================================== -let weekDays = [ - "Sunday", - "Monday", - "Tuesday", - "Wednesday", - "Thursday", - "Friday", - "Saturday" -]; - -// =========================================================================== - -let months = [ - "January", - "February", - "March", - "April", - "May", - "June", - "July", - "August", - "September", - "October", - "November", - "December" -]; - -// =========================================================================== - -let cardinalDirections = [ - "North", - "Northeast", - "East", - "Southeast", - "South", - "Southwest", - "West", - "Northwest", - "Unknown" -]; - -// =========================================================================== - -function getCardinalDirectionName(cardinalDirectionId) { - let cardinalDirections = ["North", "Northeast", "East", "Southeast", "South", "Southwest", "West", "Northwest", "Unknown" ]; - return cardinalDirections[cardinalDirectionId]; -} - -// =========================================================================== - -function getWeekDayName(weekdayId) { - let weekdayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; - return weekdayNames[weekdayId]; -} - -// =========================================================================== - -function getMonthName(monthId) { - let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; - return monthNames[monthId]; -} - -// =========================================================================== - -function openAllGarages() { - -} - -// =========================================================================== - -function closeAllGarages() { - -} - -// =========================================================================== - -function replaceEmojiIntoString(message) { - for(let i in emojiReplaceString) { - message = message.replace(emojiReplaceString[i][0], emojiReplaceString[i][1]); - } - return message; -} - -// =========================================================================== - -function makeReadableTime(hour, minute) { - let hourStr = toString(hour); - let minuteStr = toString(minute); - let meridianStr = "AM"; - - if(hour < 10) { - hourStr = "0" + toString(hour); - meridianStr = "AM"; - } - - if(hour > 11) { - let actualHour = hour-12; - if(actualHour < 10) { - hourStr = "0" + toString(hour-12); - } else { - hourStr = toString(hour-12); - } - meridianStr = "PM"; - } - - if(minute < 10) { - minuteStr = "0" + toString(minute); - } - - return hourStr + ":" + minuteStr + " " + meridianStr; -} - -// =========================================================================== - -function getClosestVehicle(position) { - //let vehicles = getServerData().vehicles; - //let closest = 0; - //for(let i in vehicles) { - // if(vehicles[i] != null) { - // if(vehicles[closest] == null || getDistance(getVehiclePosition(vehicles[i].vehicle), position) < getDistance(getVehiclePosition(vehicles[closest].vehicle), position)) { - // closest = i; - // } - // } - //} - //return vehicles[closest].vehicle; - return getClosestElementByType(ELEMENT_VEHICLE, position); -} - -// =========================================================================== - -function getClosestElementByType(elementType, position) { - return getElementsByType(elementType).reduce((i, j) => ((i.position.distance(position) <= j.position.distance(position)) ? i : j)); -} - -// =========================================================================== - -function getClosestJobLocation(position) { - let closestJobLocation = false; - for(let i in getServerData().jobs) { - for(let j in getServerData().jobs[i].locations) { - if(!closestJobLocation || getServerData().jobs[i].locations[j].position.distance(position) < closestJobLocation.position.distance(position)) { - closestJobLocation = getServerData().jobs[i].locations[j]; - } - } - } - return closestJobLocation; -} - -// =========================================================================== - -function getJobIndex(jobData) { - return getServerData().jobs.indexOf(jobData); -} - -// =========================================================================== - -function getJobPointsInRange(position, distance) { - return getServerData().jobs[getServerGame()].filter(x => x.position.distance(position) <= distance); -} - -// =========================================================================== - -function getWeaponName(weapon) { - return weaponNames[getServerGame()][weapon]; -} - -// =========================================================================== - -function isParamsInvalid(params) { - if(params == null) { - return true; - } - - if(params == "") { - return true; - } - - if(params.length == 0) { - return true; - } - - return false; -} - -// =========================================================================== - -function doesWordStartWithVowel(word) { - switch(toLowerCase(word.substr(0,1))) { - case "a": - case "e": - case "i": - case "o": - case "u": - return true; - - default: - return false; - } - - return false; -} - -// =========================================================================== - -function replaceEmojiIntoString(message) { - for(let i in emojiReplaceString) { - while(message.indexOf(emojiReplaceString[i][0]) != -1) { - message = message.replace(emojiReplaceString[i][0], emojiReplaceString[i][1]); - } - } - return message; -} - -// =========================================================================== - -function getClientFromName(clientName) { - let clients = getClients(); - for(let i in clients) { - if(toLowerCase(clients[i].name).indexOf(toLowerCase(clientName)) != -1) { - return clients[i]; - } - - let charName = `${getPlayerCurrentSubAccount(clients[i]).firstName} ${getPlayerCurrentSubAccount(clients[i]).lastName}`; - if(toLowerCase(charName).indexOf(toLowerCase(clientName)) != -1) { - return clients[i]; - } - } - - return false; -} - -// =========================================================================== - -function getClientFromPlayer(player) { - let clients = getClients(); - for(let i in clients) { - if(clients[i].player == player) { - return clients[i]; - } - } - - return false; -} - -// =========================================================================== - -function killDeathRatio(kills, deaths) { - if(deaths == 0 || kills == 0) { - return 0.0; - } - return Float((iKills*100/iDeaths) * 0.01); -} - -// =========================================================================== - -function getCardinalDirection(pos1, pos2) { - let a = pos1.x - pos2.x; - let b = pos1.y - pos2.y; - let c = pos1.z - pos2.z; - - let x = Math.abs(a); - let y = Math.abs(b); - let z = Math.abs(c); - - let no = 0; - let ne = 1; - let ea = 2; - let se = 3; - let so = 4; - let sw = 5; - let we = 6; - let nw = 7; - let na = 8; - - if(b < 0 && a < 0){ - if(x < (y/2)){ - return no; - } else if(y < (x/2)){ - return ea; - } else { - return ne; - } - } else if(b < 0 && a >= 0){ - if(x < (y/2)){ - return no; - } else if(y < (x/2)){ - return we; - } else { - return nw; - } - } else if(b >= 0 && a >= 0){ - if(x < (y/2)){ - return so; - } else if(y < (x/2)){ - return we; - } else { - return sw; - } - } else if(b >= 0 && a < 0){ - if(x < (y/2)){ - return so; - } else if(y < (x/2)){ - return ea; - } else { - return se; - } - } else { - return na; - } - return na; -} - -// =========================================================================== - -function getTimeDifferenceDisplay(timeStamp2, timeStamp1) { - timeStamp1 = timeStamp1 * 1000; - timeStamp2 = timeStamp2 * 1000; - if(isNaN(timeStamp1) || isNaN(timeStamp2)) { - return "Unknown"; - } - - let millisecondDiff = timeStamp2 - timeStamp1; - - let days = Math.floor(millisecondDiff / 1000 / 60 / (60 * 24)); - let diffDate = new Date(millisecondDiff); - - return `${days} days, ${diffDate.getHours()} hours, ${diffDate.getMinutes()} minutes`; -} - -// =========================================================================== - -function getVehiclesInRange(position, range) { - let vehicles = getElementsByType(ELEMENT_VEHICLE); - let inRangeVehicles = []; - for(let i in vehicles) { - if(getDistance(position, vehicles[i].position) <= range) { - inRangeVehicles.push(vehicles[i]); - } - } - return inRangeVehicles; -} - -// =========================================================================== - -function getFileData(filePath) { - let file = openFile(filePath, false); - if(!file) { - return false; - } - let fileData = file.readBytes(file.length); - file.close(); - return fileData; -} - -// =========================================================================== - -function setFileData(filePath, fileData) { - let file = openFile(filePath, true); - if(!file) { - return false; - } - file.writeBytes(fileData, fileData.length); - file.close(); - return true; -} - -// =========================================================================== - -function is2dPositionOnScreen(pos2d) { - return pos2d.x >= 0 && pos2d.y >= 0 && pos2d.x <= game.width && pos2d.y <= game.height; -} - -// =========================================================================== - -function breakText(text, maxLength) { - let lines = []; - let j = Math.floor(text.length / maxLength); - - for(let i = 0; i < j; i++) { - lines.push(text.substr(i*maxLength,maxLength)); - } - - let line = text.substr(j*maxLength, text.length % maxLength); - if(line.length > 0) { - lines.push(line); - } - - return lines; -} - -// =========================================================================== - -function getSpeedFromVelocity(vel) { - return Math.sqrt(vel.x*vel.x + vel.y*vel.y + vel.z*vel.z); -} - -// =========================================================================== - -function isValidSkin(skin, game = VRR_GAME_GTA_III) { - if(game == VRR_GAME_GTA_III) { - return true; - } else if(game == VRR_GAME_GTA_VC) { - switch(skin) { - case 111: - return false; - - default: - return true; - } - } -} - -// =========================================================================== - function getPositionArea(position) { if(typeof position == "Vec3") { position = vec3ToVec2(position); @@ -480,130 +66,6 @@ function getPlayerData(client) { // =========================================================================== -function createAllPoliceStationBlips() { - if(getGameConfig().blipSprites[getServerGame()].policeStation != -1) { - for(let i in getServerData().policeStations[getServerGame()]) { - getServerData().policeStations[getServerGame()][i].blip = createBlip(getGameConfig().blipSprites[getServerGame()].policeStation, getServerData().policeStations[getServerGame()][i].position); - } - } -} - -// =========================================================================== - -function createAllFireStationBlips() { - if(getGameConfig().blipSprites[getServerGame()].fireStation != -1) { - for(let i in getServerData().fireStations[getServerGame()]) { - getServerData().fireStations[getServerGame()][i].blip = createBlip(getGameConfig().blipSprites[getServerGame()].fireStation, getServerData().fireStations[getServerGame()][i].position); - } - } -} - -// =========================================================================== - -function createAllHospitalBlips() { - if(getGameConfig().blipSprites[getServerGame()].hospital != -1) { - for(let i in getServerData().hospitals[getServerGame()]) { - getServerData().hospitals[getServerGame()][i].blip = createBlip(getGameConfig().blipSprites[getServerGame()].hospital, getServerData().hospitals[getServerGame()][i].position); - } - } -} - -// =========================================================================== - -function createAllAmmunationBlips() { - if(getGameConfig().blipSprites[getServerGame()].ammunation != -1) { - for(let i in getServerData().ammunations[getServerGame()]) { - getServerData().ammunations[getServerGame()][i].blip = createBlip(getGameConfig().blipSprites[getServerGame()].ammunation, getServerData().ammunations[getServerGame()][i].position); - } - } -} - -// =========================================================================== - -function createAllPayAndSprayBlips() { - if(getGameConfig().blipSprites[getServerGame()].payAndSpray != -1) { - for(let i in getServerData().payAndSprays[getServerGame()]) { - getServerData().payAndSprays[getServerGame()][i].blip = createBlip(getGameConfig().blipSprites[getServerGame()].payAndSpray, getServerData().payAndSprays[getServerGame()][i].position); - } - } -} - -// =========================================================================== - -function createAllFuelStationBlips() { - if(getGameConfig().blipSprites[getServerGame()].fuelStation != -1) { - for(let i in getServerData().fuelStations[getServerGame()]) { - getServerData().fuelStations[getServerGame()][i].blip = createBlip(getGameConfig().blipSprites[getServerGame()].fuelStation, getServerData().fuelStations[getServerGame()][i].position, 2, getColourByName("burntOrange")); - } - } -} - -// =========================================================================== - -function getPickupOwnerType(pickup) { - return pickup.getData("vrr.ownerType"); -} - -// =========================================================================== - -function getPickupOwnerId(pickup) { - return pickup.getData("vrr.ownerId"); -} - -// =========================================================================== - -function canPlayerUsePoliceJob(client) { - if(getPlayerData(client).accountData.flags.moderation & getServerBitFlags().moderationFlags.policeBanned) { - return false; - } - - return true; -} - -// =========================================================================== - -function canClientUseFireJob(client) { - if(getPlayerData(client).accountData.flags.moderation & getServerBitFlags().moderationFlags.fireBanned) { - return false; - } - - return true; -} - -// =========================================================================== - -function canClientUseAmmunations(client) { - if(getPlayerData(client).accountData.flags.moderation & getServerBitFlags().moderationFlags.AmmuBanned) { - return false; - } - - return true; -} - -// =========================================================================== - -function canClientUseGuns(client) { - if(getPlayerData(client).accountData.flags.moderation & getServerBitFlags().moderationFlags.GunBanned) { - return false; - } - - return true; -} - -// =========================================================================== - -function sendAllBlips(client) { - sendAllPoliceStationBlips(client); - sendAllFireStationBlips(client); - sendAllHospitalBlips(client); - sendAllPayAndSprayBlips(client); - sendAllAmmunationBlips(client); - sendAllFuelStationBlips(client); - sendAllJobBlips(client); -} - -// =========================================================================== - function initAllClients() { getClients().forEach(function(client) { initClient(client); @@ -612,36 +74,6 @@ function initAllClients() { // =========================================================================== -function getYesNoFromBool(boolVal) { - return (boolVal) ? "Yes" : "No"; -} - -// =========================================================================== - -function getOnOffFromBool(boolVal) { - return (boolVal) ? "On" : "Off"; -} - -// =========================================================================== - -function getEnabledDisabledFromBool(boolVal) { - return (boolVal) ? "Enabled" : "Disabled"; -} - -// =========================================================================== - -function getLockedUnlockedFromBool(boolVal) { - return (boolVal) ? "Locked" : "Unlocked"; -} - -// =========================================================================== - -function getOpenedClosedFromBool(boolVal) { - return (boolVal) ? "Opened" : "Closed"; -} - -// =========================================================================== - function updateServerRules() { if(isTimeSupported()) { server.setRule("Time", makeReadableTime(getServerConfig().hour, getServerConfig().minute)); @@ -658,53 +90,6 @@ function updateServerRules() { // =========================================================================== -function getAccentFromParams(params) { - if(isNaN(params)) { - for(let i in getGlobalConfig().accents) { - if(toLowerCase(getGlobalConfig().accents[i]).indexOf(toLowerCase(params)) != -1) { - return i; - } - } - } else { - if(typeof getGlobalConfig().accents[params] != "undefined") { - return toInteger(params); - } - } - - return false; -} - -// =========================================================================== - -function getCommandFromParams(params) { - for(let i in serverCommands) { - for(let j in serverCommands[i]) { - if(toLowerCase(serverCommands[i][j].command).indexOf(toLowerCase(params)) != -1) { - return serverCommands[i][j]; - } - } - } - - return false; -} - -// =========================================================================== - -function getLocaleNameFromParams(params) { - let locales = getLocales(); - if(isNaN(params)) { - for(let i in locales) { - if(toLowerCase(i).indexOf(toLowerCase(params)) != -1) { - return locales[i]; - } - } - } - - return false; -} - -// =========================================================================== - function getWeatherFromParams(params) { if(isNaN(params)) { for(let i in getGameData().weatherNames[getServerGame()]) { @@ -741,158 +126,6 @@ function getFightStyleFromParams(params) { // =========================================================================== -function getAnimationFromParams(params) { - if(isNaN(params)) { - for(let i in getGameData().animations[getServerGame()]) { - if(toLowerCase(getGameData().animations[getServerGame()][i][0]).indexOf(toLowerCase(params)) != -1) { - return i; - } - } - } else { - if(typeof getGameData().animations[getServerGame()][params] != "undefined") { - return toInteger(params); - } - } - - return false; -} - -// =========================================================================== - -function getClanFromParams(params) { - if(isNaN(params)) { - for(let i in getServerData().clans) { - if(toLowerCase(getServerData().clans[i].name).indexOf(toLowerCase(params)) != -1) { - return i; - } - } - } else { - if(typeof getServerData().clans[params] != "undefined") { - return toInteger(params); - } - } - - return false; -} - -// =========================================================================== - -function getClanRankFromParams(clanId, params) { - if(isNaN(params)) { - for(let i in getClanData(clanId).ranks) { - if((toLowerCase(getClanData(clanId).ranks[i].name).indexOf(toLowerCase(params)) != -1)) { - return i; - } - } - } else { - for(let i in getClanData(clanId).ranks) { - if(getClanData(clanId).ranks[i].level == toInteger(params)) { - return i; - } - } - } - - return false; -} - -// =========================================================================== - -function getJobFromParams(params) { - if(isNaN(params)) { - for(let i in getServerData().jobs) { - if(toLowerCase(getServerData().jobs[i].name).indexOf(toLowerCase(params)) != -1) { - return i; - } - } - } else { - if(typeof getServerData().jobs[params] != "undefined") { - return params; - } - } - - return false; -} - -// =========================================================================== - -function getBusinessFromParams(params) { - if(isNaN(params)) { - for(let i in getServerData().businesses) { - if(toLowerCase(getServerData().businesses[i].name).indexOf(toLowerCase(params)) != -1) { - return i; - } - } - } else { - if(typeof getServerData().businesses[params] != "undefined") { - return toInteger(params); - } - } - return false; -} - -// =========================================================================== - -function getGameLocationFromParams(params) { - if(isNaN(params)) { - for(let i in getGameData().locations[getServerGame()]) { - if(toLowerCase(getGameData().locations[getServerGame()][i][0]).indexOf(toLowerCase(params)) != -1) { - return i; - } - } - } else { - if(typeof getGameData().locations[getServerGame()][params] != "undefined") { - return toInteger(params); - } - } - return false; -} - -// =========================================================================== - -function getHouseFromParams(params) { - if(isNaN(params)) { - for(let i in getServerData().houses) { - if(toLowerCase(getServerData().houses[i].description).indexOf(toLowerCase(params)) != -1) { - return i; - } - } - } else { - if(typeof getServerData().houses[params] != "undefined") { - return toInteger(params); - } - } - return false; -} - -// =========================================================================== - -function getItemTypeFromParams(params) { - if(isNaN(params)) { - for(let i in getServerData().itemTypes) { - if(toLowerCase(getServerData().itemTypes[i].name).indexOf(toLowerCase(params)) != -1) { - return i; - } - } - } else { - if(typeof getServerData().itemTypes[params] != "undefined") { - return toInteger(params); - } - } - return false; -} - - -// =========================================================================== - -function clearChatBox(client) { - //game.messages.clear(); - for(let i = 0; i <= 20; i++) { - messageClient(" ", client, COLOUR_WHITE); - } -} - -// =========================================================================== - function getClosestHospital(position) { let closest = 0; for(let i in getGameData().hospitals[getServerGame()]) { @@ -946,18 +179,6 @@ function isPlayerSpawned(client) { // =========================================================================== -function getLockedUnlockedTextFromBool(boolVal) { - return (boolVal) ? "locked" : "unlocked"; -} - -// =========================================================================== - -function getLockedUnlockedEmojiFromBool(boolVal) { - return (boolVal) ? "🔒" : "🔓"; -} - -// =========================================================================== - function getPlayerIsland(client) { return getIsland(getPlayerPosition(client)); } @@ -976,21 +197,6 @@ function isAtPayAndSpray(position) { // =========================================================================== -async function waitUntil(condition) { - return new Promise((resolve) => { - let interval = setInterval(() => { - if (!condition()) { - return - } - - clearInterval(interval); - resolve(); - }, 1); - }); -} - -// =========================================================================== - function resetClientStuff(client) { logToConsole(LOG_DEBUG, `[VRR.Utilities] Resetting client data for ${getPlayerDisplayForConsole(client)}`); @@ -1028,57 +234,6 @@ function getPlayerFromCharacterId(subAccountId) { // =========================================================================== -function doesWordStartWithVowel(word) { - switch(word.substr(0,1).toLowerCase()) { - case "a": - case "e": - case "i": - case "o": - case "u": - return true; - - default: - return false; - } - - return false; -} - -// =========================================================================== - -function getProperDeterminerForName(word) { - switch(word.substr(0,1).toLowerCase()) { - case "a": - case "e": - case "i": - case "o": - return "an"; - - default: - return "a"; - } -} - -// =========================================================================== - -function getPluralForm(name) { - return name; -} - -// =========================================================================== - -function removeHexColoursFromString(str) { - let matchRegex = /#([a-f0-9]{3}|[a-f0-9]{4}(?:[a-f0-9]{2}){0,2})\b/gi; - let matchedHexes = str.match(matchRegex); - for(let i in matchHex) { - str.replace(matchedHexes, `{${i}}`); - } - - return [str, matchedHexes]; -} - -// =========================================================================== - function checkPlayerPedStates() { let clients = getClients(); for(let i in clients) { @@ -1116,54 +271,6 @@ function showCharacterSelectCameraToPlayer(client) { // =========================================================================== -function generateRandomString(length) { - var result = ''; - var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - var charactersLength = characters.length; - for ( var i = 0; i < length; i++ ) { - result += characters.charAt(Math.floor(Math.random() * charactersLength)); - } - return result; -} - -// =========================================================================== - -function getVehiclesInRange(position, distance) { - return getElementsByType(ELEMENT_VEHICLE).filter(x => x.player && x.position.distance(position) <= distance); -} - -// =========================================================================== - -function getClientsInRange(position, distance) { - return getPlayersInRange(position, distance); -} - -// =========================================================================== - -function getCiviliansInRange(position, distance) { - return getElementsByType(ELEMENT_PED).filter(x => !x.isType(ELEMENT_PLAYER) && getElementPosition(x).position.distance(position) <= distance); -} - -// =========================================================================== - -function getPlayersInRange(position, distance) { - return getClients().filter(x => getPlayerPosition(x).distance(position) <= distance); -} - -// =========================================================================== - -function getElementsByTypeInRange(elementType, position, distance) { - return getElementsByType(elementType).filter(x => getElementPosition(x).position.distance(position) <= distance); -} - -// =========================================================================== - -function getClosestCivilian(position) { - return getElementsByType(ELEMENT_PED).reduce((i, j) => ((i.position.distance(position) <= j.position.distance(position)) ? i : j)); -} - -// =========================================================================== - function getClosestPlayer(position, exemptClient) { //let clients = getClients(); //let closest = 0; @@ -1188,49 +295,12 @@ function isPlayerMuted(client) { // =========================================================================== -function getCurrentUnixTimestamp() { - return new Date().getTime()/1000; -} - -// =========================================================================== - -function msToTime(duration) { - let milliseconds = Math.floor(toInteger((duration % 1000) / 100)); - let seconds = Math.floor(toInteger((duration / 1000) % 60)); - let minutes = Math.floor(toInteger((duration / (1000 * 60)) % 60)); - let hours = Math.floor(toInteger((duration / (1000 * 60 * 60)) % 24)); - let days = Math.floor(toInteger((duration / (1000 * 60 * 60 * 24)) % 365)); - - //hours = (hours < 10) ? "0" + hours : hours; - //minutes = (minutes < 10) ? "0" + minutes : minutes; - //seconds = (seconds < 10) ? "0" + seconds : seconds; - - if (days !== 0) { - return `${days} days, ${hours} hours, ${minutes} minutes`; - } else { - return `${hours} hours, ${minutes} minutes`; - } -} - -// =========================================================================== - function isSamePlayer(client1, client2) { return (client1 == client2); } // =========================================================================== -function getClientFromIndex(index) { - let clients = getClients(); - for(let i in clients) { - if(clients[i].index == index) { - return clients[i]; - } - } -} - -// =========================================================================== - function getConsoleClient() { let clients = getClients(); for(let i in clients) { @@ -1265,16 +335,6 @@ function getPlayerFromParams(params) { return false; } -// =========================================================================== - -function getArrayOfElementId(elements) { - let tempArray = []; - for(let i in elements) { - tempArray.push(elements[i].id); - } - - return tempArray; -} // =========================================================================== @@ -1285,93 +345,6 @@ function getSyncerFromId(syncerId) { // =========================================================================== -function arrayBufferToString(arrayBuffer) { - return String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)); -} - -// =========================================================================== - -function vec3ToVec2(pos) { - return toVector2(pos[0], pos[1]); -} - -// =========================================================================== - -function vec2ToVec3(pos, z) { - return toVector3(pos[0], pos[1], z); -} - -// =========================================================================== - -function degToRad(deg) { - return deg * Math.PI / 180; -} - -// =========================================================================== - -function radToDeg(rad) { - return rad * 180 / Math.PI; -} - -// =========================================================================== - -function getHeadingFromPosToPos(pos1, pos2) { - let x = pos2.x-pos1.x; - let y = pos2.y-pos1.y; - let rad = Math.atan2(y, x); - let deg = radToDeg(rad); - deg -= 90; - deg = deg % 360; - return degToRad(deg); -} - -// =========================================================================== - -function getAngleInCircleFromCenter(center, total, current) { - let gap = 360 / total; - let deg = Math.floor(gap*current); - - if(deg <= 0) { - deg = 1; - } else { - if(deg >= 360) { - deg = 359; - } - } - - return degToRad(deg); -} - -// =========================================================================== - -function areParamsEmpty(params) { - if(!params || params == "" || params.length == 0 || typeof params == "undefined") { - return true; - } - - return false; -} - -// =========================================================================== - -function getParamsCount(params, delimiter = " ") { - return params.split(delimiter).length; -} - -// =========================================================================== - -function areThereEnoughParams(params, requiredAmount, delimiter = " ") { - return (params.split(delimiter).length >= requiredAmount); -} - -// =========================================================================== - -function getParams(params, delimiter, index) { - return params.split(delimiter)[index]; -} - -// =========================================================================== - function isConsole(client) { if(client == null) { return false; @@ -1434,12 +407,6 @@ function fixCharacterName(name) { // =========================================================================== -function addPositiveNegativeSymbol(value) { - return (value >= 0) ? `+${value}` : `${value}`; -} - -// =========================================================================== - function getCurrentTimeStampWithTimeZone(timeZone) { let date = new Date(); @@ -1454,30 +421,12 @@ function getCurrentTimeStampWithTimeZone(timeZone) { // =========================================================================== -function getElementId(element) { - return element.id; -} - -// =========================================================================== - function getClientFromSyncerId(syncerId) { return getClients().filter(c => c.index == syncerId)[0]; } // =========================================================================== -function fixAngle(angle) { - angle = radToDeg(angle); - if(angle < 0) - { - angle = Math.abs(angle); - angle = ((180-angle+1)+180); - } - return degToRad(angle); -} - -// =========================================================================== - async function triggerWebHook(webHookURL, payloadData) { return new Promise(resolve => { //console.warn(webHookURL); @@ -1493,11 +442,7 @@ async function triggerWebHook(webHookURL, payloadData) { }); } -// =========================================================================== -function getVehicleTrunkPosition(vehicle) { - return getPosBehindPos(getVehiclePosition(vehicle), getVehicleHeading(vehicle), getGlobalConfig().vehicleTrunkRearDistance); -} // =========================================================================== @@ -1531,12 +476,4 @@ function clearTemporaryPeds() { } } -// =========================================================================== - -function makeChatSectionHeader(name) { - let resultString = `== ${name} `; - let endFiller = fillStringWithCharacter("=", getGlobalConfig().chatSectionHeaderLength-resultString.length); - return `${resultString} ${endFiller}`; -} - // =========================================================================== \ No newline at end of file diff --git a/scripts/server/vehicle.js b/scripts/server/vehicle.js index 1560c1f9..d74e0d3e 100644 --- a/scripts/server/vehicle.js +++ b/scripts/server/vehicle.js @@ -1496,4 +1496,10 @@ function getClosestTaxi(position) { .reduce((i, j) => ((i.position.distance(position) <= j.position.distance(position)) ? i : j)); } +// =========================================================================== + +function getVehicleTrunkPosition(vehicle) { + return getPosBehindPos(getVehiclePosition(vehicle), getVehicleHeading(vehicle), getGlobalConfig().vehicleTrunkRearDistance); +} + // =========================================================================== \ No newline at end of file diff --git a/scripts/shared/utilities.js b/scripts/shared/utilities.js index 00eb2350..6773c17d 100644 --- a/scripts/shared/utilities.js +++ b/scripts/shared/utilities.js @@ -127,6 +127,49 @@ let bindableKeys = { // =========================================================================== +let weekDays = [ + "Sunday", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday" +]; + +// =========================================================================== + +let months = [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December" +]; + +// =========================================================================== + +let cardinalDirections = [ + "North", + "Northeast", + "East", + "Southeast", + "South", + "Southwest", + "West", + "Northwest", + "Unknown" +]; + +// =========================================================================== + function makeLargeNumberReadable(num) { return new Number(num).toLocaleString("en-US"); } @@ -877,4 +920,425 @@ function boolToInt(boolVal) { return (boolVal) ? 1 : 0; } +// =========================================================================== + +function fixAngle(angle) { + angle = radToDeg(angle); + if(angle < 0) + { + angle = Math.abs(angle); + angle = ((180-angle+1)+180); + } + return degToRad(angle); +} + +// =========================================================================== + +function addPositiveNegativeSymbol(value) { + return (value >= 0) ? `+${value}` : `${value}`; +} + +// =========================================================================== + +function arrayBufferToString(arrayBuffer) { + return String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)); +} + +// =========================================================================== + +function vec3ToVec2(pos) { + return toVector2(pos[0], pos[1]); +} + +// =========================================================================== + +function vec2ToVec3(pos, z) { + return toVector3(pos[0], pos[1], z); +} + +// =========================================================================== + +function degToRad(deg) { + return deg * Math.PI / 180; +} + +// =========================================================================== + +function radToDeg(rad) { + return rad * 180 / Math.PI; +} + +// =========================================================================== + +function getHeadingFromPosToPos(pos1, pos2) { + let x = pos2.x-pos1.x; + let y = pos2.y-pos1.y; + let rad = Math.atan2(y, x); + let deg = radToDeg(rad); + deg -= 90; + deg = deg % 360; + return degToRad(deg); +} + +// =========================================================================== + +function getAngleInCircleFromCenter(center, total, current) { + let gap = 360 / total; + let deg = Math.floor(gap*current); + + if(deg <= 0) { + deg = 1; + } else { + if(deg >= 360) { + deg = 359; + } + } + + return degToRad(deg); +} + +// =========================================================================== + +function getArrayOfElementId(elements) { + let tempArray = []; + for(let i in elements) { + tempArray.push(elements[i].id); + } + + return tempArray; +} + +// =========================================================================== + +function getCurrentUnixTimestamp() { + return new Date().getTime()/1000; +} + +// =========================================================================== + +function msToTime(duration) { + let milliseconds = Math.floor(toInteger((duration % 1000) / 100)); + let seconds = Math.floor(toInteger((duration / 1000) % 60)); + let minutes = Math.floor(toInteger((duration / (1000 * 60)) % 60)); + let hours = Math.floor(toInteger((duration / (1000 * 60 * 60)) % 24)); + let days = Math.floor(toInteger((duration / (1000 * 60 * 60 * 24)) % 365)); + + //hours = (hours < 10) ? "0" + hours : hours; + //minutes = (minutes < 10) ? "0" + minutes : minutes; + //seconds = (seconds < 10) ? "0" + seconds : seconds; + + if (days !== 0) { + return `${days} days, ${hours} hours, ${minutes} minutes`; + } else { + return `${hours} hours, ${minutes} minutes`; + } +} + +// =========================================================================== + +function generateRandomString(length, characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") { + var result = ''; + var charactersLength = characters.length; + for ( var i = 0; i < length; i++ ) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +} + +// =========================================================================== + +function doesWordStartWithVowel(word) { + switch(word.substr(0,1).toLowerCase()) { + case "a": + case "e": + case "i": + case "o": + case "u": + return true; + + default: + return false; + } + + return false; +} + +// =========================================================================== + +function getProperDeterminerForName(word) { + switch(word.substr(0,1).toLowerCase()) { + case "a": + case "e": + case "i": + case "o": + return "an"; + + default: + return "a"; + } +} + +// =========================================================================== + +function getPluralForm(name) { + return name; +} + +// =========================================================================== + +function removeHexColoursFromString(str) { + let matchRegex = /#([a-f0-9]{3}|[a-f0-9]{4}(?:[a-f0-9]{2}){0,2})\b/gi; + let matchedHexes = str.match(matchRegex); + for(let i in matchHex) { + str.replace(matchedHexes, `{${i}}`); + } + + return [str, matchedHexes]; +} + +// =========================================================================== + +async function waitUntil(condition) { + return new Promise((resolve) => { + let interval = setInterval(() => { + if (!condition()) { + return + } + + clearInterval(interval); + resolve(); + }, 1); + }); +} + +// =========================================================================== + +function getGameLocationFromParams(params) { + if(isNaN(params)) { + for(let i in getGameData().locations[getServerGame()]) { + if(toLowerCase(getGameData().locations[getServerGame()][i][0]).indexOf(toLowerCase(params)) != -1) { + return i; + } + } + } else { + if(typeof getGameData().locations[getServerGame()][params] != "undefined") { + return toInteger(params); + } + } + return false; +} + +// =========================================================================== + +function getYesNoFromBool(boolVal) { + return (boolVal) ? "Yes" : "No"; +} + +// =========================================================================== + +function getOnOffFromBool(boolVal) { + return (boolVal) ? "On" : "Off"; +} + +// =========================================================================== + +function getEnabledDisabledFromBool(boolVal) { + return (boolVal) ? "Enabled" : "Disabled"; +} + +// =========================================================================== + +function getLockedUnlockedFromBool(boolVal) { + return (boolVal) ? "Locked" : "Unlocked"; +} + +// =========================================================================== + +function getOpenedClosedFromBool(boolVal) { + return (boolVal) ? "Opened" : "Closed"; +} + +// =========================================================================== + +function breakText(text, maxLength) { + let lines = []; + let j = Math.floor(text.length / maxLength); + + for(let i = 0; i < j; i++) { + lines.push(text.substr(i*maxLength,maxLength)); + } + + let line = text.substr(j*maxLength, text.length % maxLength); + if(line.length > 0) { + lines.push(line); + } + + return lines; +} + +// =========================================================================== + +function getSpeedFromVelocity(vel) { + return Math.sqrt(vel.x*vel.x + vel.y*vel.y + vel.z*vel.z); +} + +// =========================================================================== + +function getCardinalDirection(pos1, pos2) { + let a = pos1.x - pos2.x; + let b = pos1.y - pos2.y; + let c = pos1.z - pos2.z; + + let x = Math.abs(a); + let y = Math.abs(b); + let z = Math.abs(c); + + let no = 0; + let ne = 1; + let ea = 2; + let se = 3; + let so = 4; + let sw = 5; + let we = 6; + let nw = 7; + let na = 8; + + if(b < 0 && a < 0){ + if(x < (y/2)){ + return no; + } else if(y < (x/2)){ + return ea; + } else { + return ne; + } + } else if(b < 0 && a >= 0){ + if(x < (y/2)){ + return no; + } else if(y < (x/2)){ + return we; + } else { + return nw; + } + } else if(b >= 0 && a >= 0){ + if(x < (y/2)){ + return so; + } else if(y < (x/2)){ + return we; + } else { + return sw; + } + } else if(b >= 0 && a < 0){ + if(x < (y/2)){ + return so; + } else if(y < (x/2)){ + return ea; + } else { + return se; + } + } else { + return na; + } + return na; +} + +// =========================================================================== + +function getTimeDifferenceDisplay(timeStamp2, timeStamp1) { + timeStamp1 = timeStamp1 * 1000; + timeStamp2 = timeStamp2 * 1000; + if(isNaN(timeStamp1) || isNaN(timeStamp2)) { + return "Unknown"; + } + + let millisecondDiff = timeStamp2 - timeStamp1; + + let days = Math.floor(millisecondDiff / 1000 / 60 / (60 * 24)); + let diffDate = new Date(millisecondDiff); + + return `${days} days, ${diffDate.getHours()} hours, ${diffDate.getMinutes()} minutes`; +} + +// =========================================================================== + +function doesWordStartWithVowel(word) { + switch(toLowerCase(word.substr(0,1))) { + case "a": + case "e": + case "i": + case "o": + case "u": + return true; + + default: + return false; + } + + return false; +} + +// =========================================================================== + +function replaceEmojiIntoString(message) { + for(let i in emojiReplaceString) { + message = message.replace(emojiReplaceString[i][0], emojiReplaceString[i][1]); + } + return message; +} + +// =========================================================================== + +function makeReadableTime(hour, minute) { + let hourStr = toString(hour); + let minuteStr = toString(minute); + let meridianStr = "AM"; + + if(hour < 10) { + hourStr = "0" + toString(hour); + meridianStr = "AM"; + } + + if(hour > 11) { + let actualHour = hour-12; + if(actualHour < 10) { + hourStr = "0" + toString(hour-12); + } else { + hourStr = toString(hour-12); + } + meridianStr = "PM"; + } + + if(minute < 10) { + minuteStr = "0" + toString(minute); + } + + return hourStr + ":" + minuteStr + " " + meridianStr; +} + +// =========================================================================== + +function getCardinalDirectionName(cardinalDirectionId) { + let cardinalDirections = ["North", "Northeast", "East", "Southeast", "South", "Southwest", "West", "Northwest", "Unknown" ]; + return cardinalDirections[cardinalDirectionId]; +} + +// =========================================================================== + +function getWeekDayName(weekdayId) { + let weekdayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; + return weekdayNames[weekdayId]; +} + +// =========================================================================== + +function getMonthName(monthId) { + let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; + return monthNames[monthId]; +} + +// =========================================================================== + +function getLockedUnlockedEmojiFromBool(boolVal) { + return (boolVal) ? "🔒" : "🔓"; +} + // =========================================================================== \ No newline at end of file