Files
csgowtf/src/utils/Utils.js
vikingowl c105289971
All checks were successful
CSGOWTF/csgowtf/pipeline/head This commit looks good
added player metadata
2021-10-24 15:54:05 +02:00

102 lines
2.1 KiB
JavaScript

export const setTitle = (title) => {
document.title = `${title} | csgoWTF`
}
export const closeNav = (navSelector) => {
const nav = document.getElementById(navSelector)
if (nav)
if (nav.classList.contains('show'))
nav.classList.remove('show')
}
export const GetWinLoss = (matchResult, teamId) => {
if (matchResult === teamId) {
return 'win'
} else if (matchResult === 0) {
return 'draw'
} else {
return 'loss'
}
}
export const truncate = (str, len, ending) => {
if (len == null)
len = 100
if (ending == null)
ending = '..'
if (str.length > len)
return str.substring(0, len - ending.length) + ending
else
return str
}
export const checkStatEmpty = (stat) => {
if (stat)
return stat
return 0
}
export const FixMapName = (map) => {
return map.split('_')[1].replace(/^\w/, c => c.toUpperCase());
}
export const getPlayerArr = (stats, team, color) => {
let arr = []
for (let i = (team - 1) * 5; i < team * 5; i++) {
arr.push({
value: truncate(stats[i].player.name, 12),
textStyle: {
color: color ? getComputedStyle(document.documentElement).getPropertyValue(`--csgo-${stats[i].color}`) : 'white'
}
})
}
arr.reverse()
return arr
}
export const constructAvatarUrl = (hash, size) => {
const base = 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars'
const imgSize = size ? `_${size}` : ''
if (hash) {
const hashDir = hash.substring(0, 2)
return `${base}/${hashDir}/${hash}${imgSize}.jpg`
}
}
export const GetAvgRank = (stats) => {
let count = 0
let fullRank = 0
stats.map(player => {
if (player.rank?.old) {
fullRank += player.rank?.old
count += 1
}
})
return count === 0 ? 0 : Math.floor(fullRank / count)
}
export const sortObjectValue = (obj, direction = 'asc') => {
const sortable = []
for (let key in obj) {
sortable.push([key, obj[key]])}
if (direction === 'asc') {
sortable.sort((a, b) => {
return a[1] - b[1]
})
}
if (direction === 'desc') {
sortable.sort((a, b) => {
return b[1] - a[1]
})
}
return sortable
}