refactored code

This commit is contained in:
cnachtigall1991
2021-10-13 15:44:05 +02:00
parent 544ef95722
commit 5f3d0b981f
14 changed files with 250 additions and 156 deletions

19
src/utils/ApiRequests.js Normal file
View File

@@ -0,0 +1,19 @@
import axios from "axios";
export const GetUser = async (id) => {
try {
const res = await axios.get(`${process.env.VUE_APP_API_URL}/player/${id}`)
if (res.status === 200) {
return [200, res.data]
}
} catch (err) {
if (err.response) {
if (err.response.status === 404) {
return [404, 'Player not found']
}
} else {
console.log(err.response)
}
}
}

View File

@@ -28,7 +28,9 @@ export const LoadImage = (mapName) => {
let background = document.querySelector('.bg-img')
img.onload = function() {
if (background) {
background.src = img.src
}
}
img.onerror = function () {

View File

@@ -1,13 +1,13 @@
import router from "../router";
export const GoToMatch = (id) => {
router.push(`/match/${id}`)
router.push({name: 'Match', params: {match_id: id}})
}
export const GoToPlayer = (id) => {
router.push(`/player/${id}`)
router.push({name: 'Player', params: {id: id}})
}
export const GoToLink = (link) => {
router.push(`${link}`)
}
}

52
src/utils/Utils.js Normal file
View File

@@ -0,0 +1,52 @@
import axios from "axios";
export const GetWinLoss = (matchResult, teamId) => {
if (matchResult === teamId) {
return 'win'
} else if (matchResult === 0) {
return 'draw'
} else {
return 'loss'
}
}
export const TrackMe = async (id64, authcode, sharecode) => {
let statusError = ''
let status = 200
const shareCodeRegex = /^CSGO(?:-?[ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789]{5}){5}$/
const authCodeRegex = /^[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{5}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}$/
if (!shareCodeRegex.test(sharecode)) {
statusError = 'Is not a valid sharecode'
status = 418
return [status, statusError]
}
if (!authCodeRegex.test(authcode)) {
statusError = 'Is not a valid authcode'
status = 418
return [status, statusError]
}
try {
const res = await axios
.post(`${process.env.VUE_APP_API_URL}/player/trackme`, `id=${id64}&authcode=${authcode}&sharecode=${sharecode}`)
if (res.status === 202) {
statusError = 'Hurray!! Your Matches will now be tracked!'
status = res.status
}
} catch (err) {
if (err.response.status === 401) {
statusError = 'Data does not match player'
status = err.response.status
} else if (err.response.status === 400) {
statusError = 'Userinput was wrong'
status = err.response.status
} else {
console.log(`${err.response.status}: ${err.response.statusText}`)
}
}
return [status, statusError]
}

View File

@@ -1,13 +1,17 @@
import {FormatDate, FormatDuration, FormatFullDate, FormatFullDuration} from "./DateTime";
import {GoToMatch, GoToPlayer, GoToLink} from "./GoTo";
import {GoToLink, GoToMatch, GoToPlayer} from "./GoTo";
import {SaveLastVisitedToLocalStorage} from "./LocalStorage";
import {GetHLTV_1} from "./HLTV";
import {DisplayRank, LoadImage} from "./Display";
import {GetUser} from "./ApiRequests";
import {GetWinLoss, TrackMe} from "./Utils";
export {
FormatDate, FormatFullDuration, FormatFullDate, FormatDuration,
GoToMatch, GoToPlayer, GoToLink,
SaveLastVisitedToLocalStorage,
GetHLTV_1,
DisplayRank, LoadImage
}
FormatDate, FormatFullDuration, FormatFullDate, FormatDuration,
GoToMatch, GoToPlayer, GoToLink,
SaveLastVisitedToLocalStorage,
GetHLTV_1,
DisplayRank, LoadImage,
GetUser,
GetWinLoss, TrackMe
}