refactor to utils + bug-fixes

This commit is contained in:
cnachtigall1991
2021-10-07 19:04:34 +02:00
parent 0619989748
commit 9794235339
12 changed files with 189 additions and 134 deletions

View File

@@ -1,96 +1,13 @@
import {DateTime, Duration} from "luxon/build/es6/luxon";
import router from '../router'
import {FormatDate, FormatDuration, FormatFullDate, FormatFullDuration} from "./DateTime";
import {GoToMatch, GoToPlayer} from "./GoTo";
import {SaveLastVisitedToLocalStorage} from "./LocalStorage";
import {GetHLTV_1} from "./HLTV";
import {DisplayRank} from "./Display";
export const FormatDuration = (d) => {
const duration = Duration.fromObject({hours: 0, minutes: 0, seconds: d}).normalize().toObject()
if (duration.hours > 1)
return `${duration.hours} h ${duration.minutes} min`
else if (duration.hours < 1)
return `${duration.minutes} min`
}
export const FormatFullDuration = (d) => {
const duration = Duration.fromObject({hours: 0, minutes: 0, seconds: d}).normalize()
if (duration.hours > 1)
return duration.toFormat('hh:mm:ss')
else if (duration.hours < 1)
return duration.toFormat('mm:ss')
}
export const FormatDate = (date) => {
const matchDate = DateTime.fromISO(date)
const diff = DateTime.now().diff(matchDate)
if (diff.as('days') > 10)
return matchDate.toLocaleString({weekday: 'short', day: '2-digit', month: '2-digit', year: 'numeric'})
else if (diff.as('days') < 1)
if (diff.as('hours') < 1)
return Math.floor(diff.as('minutes')) + ' minutes ago'
else
return Math.floor(diff.as('hours')) + ' hours ago'
else
return Math.floor(diff.as('days')) + ' days ago'
}
export const FormatFullDate = (date) => {
const matchDate = DateTime.fromISO(date)
return matchDate.toLocaleString({
weekday: 'short',
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
})
}
export const GoToMatch = (id) => {
router.push(`/match/${id}`)
}
export const GoToPlayer = (id) => {
router.push(`/player/${id}`)
}
export const GetHLTV_1 = (kills = 0, rounds, deaths = 0, k2 = 0, k3 = 0, k4 = 0, k5 = 0) => {
const k1 = kills - k2 - k3 - k4 - k5
const Weight_KPR = 0.679 // weight kills per round
const Weight_SPR = 0.317 // weight survived rounds per round
const Weight_RMK = 1.277 // weight value calculated from rounds with multiple kills (1k + 4*2k + 9*3k + 16*4k + 25*5k)
const KillRating = kills / rounds / Weight_KPR
const SurvivalRating = (rounds - deaths) / rounds / Weight_SPR
const RoundsWithMultipleKillsRating = (k1 + 4 * k2 + 9 * k3 + 16 * k4 + 25 * k5) / rounds / Weight_RMK
return ((KillRating + 0.7 * SurvivalRating + RoundsWithMultipleKillsRating) / 2.7).toFixed(2)
}
export const SaveLastVisitedToLocalStorage = (data) => {
let a = JSON.parse(localStorage.getItem('recent-visited')) || [];
if (a.length === 0) {
a.push(data);
} else if (a.length === 9) {
if (a.find(p => p.steamid64 === data.steamid64)) {
a.shift()
a.splice(a.findIndex(i => i.steamid64 === data.steamid64), 1)
a.push(data)
} else if (!a.find(p => p.steamid64 === data.steamid64)) {
a.shift()
a.push(data)
}
} else if (a.length > 0 && a.length < 9) {
if (a.find(p => p.steamid64 === data.steamid64)) {
a.splice(a.findIndex(i => i.steamid64 === data.steamid64), 1)
a.push(data)
} else if (!a.find(p => p.steamid64 === data.steamid64)) {
a.push(data)
}
}
localStorage.setItem('recent-visited', JSON.stringify(a));
export {
FormatDate, FormatFullDuration, FormatFullDate, FormatDuration,
GoToMatch, GoToPlayer,
SaveLastVisitedToLocalStorage,
GetHLTV_1,
DisplayRank
}