refactored code
This commit is contained in:
@@ -44,6 +44,17 @@
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<div v-if="data.statusErrorCode === 200" class="alert alert-success" role="alert">
|
||||
{{ data.statusError }}
|
||||
</div>
|
||||
<div v-if="data.statusErrorCode === 418" class="alert alert-warning" role="alert">
|
||||
{{ data.statusError }}
|
||||
</div>
|
||||
<div v-if="data.statusErrorCode !== 0 && data.statusErrorCode !== 200 && data.statusErrorCode !== 418"
|
||||
class="alert alert-danger"
|
||||
role="alert">
|
||||
{{ data.statusError }}
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="!data.playerDetails.tracked" class="dropdown trackme-btn">
|
||||
<button
|
||||
@@ -79,7 +90,8 @@
|
||||
</div>
|
||||
|
||||
<!-- Submit button -->
|
||||
<button class="btn btn-outline-warning border-2" type="submit" @click.prevent="TrackMe">
|
||||
<button class="btn btn-outline-warning border-2" type="submit"
|
||||
@click.prevent="TrackPlayer">
|
||||
TrackMe
|
||||
</button>
|
||||
</form>
|
||||
@@ -189,8 +201,8 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {onBeforeMount, reactive, watch} from "vue";
|
||||
import axios from "axios";
|
||||
import {onMounted, reactive} from "vue";
|
||||
import router from "../router";
|
||||
import {useStore} from "vuex";
|
||||
import {
|
||||
DisplayRank,
|
||||
@@ -198,16 +210,21 @@ import {
|
||||
FormatDuration,
|
||||
FormatFullDate,
|
||||
FormatFullDuration,
|
||||
GetHLTV_1, GoToLink,
|
||||
GetHLTV_1,
|
||||
GetUser,
|
||||
GetWinLoss,
|
||||
GoToLink,
|
||||
GoToMatch,
|
||||
LoadImage,
|
||||
SaveLastVisitedToLocalStorage
|
||||
SaveLastVisitedToLocalStorage,
|
||||
TrackMe
|
||||
} from "../utils";
|
||||
|
||||
export default {
|
||||
name: 'Player',
|
||||
props: ['id'],
|
||||
setup(props) {
|
||||
// Variables
|
||||
const store = useStore()
|
||||
|
||||
const data = reactive({
|
||||
@@ -216,8 +233,10 @@ export default {
|
||||
sharecode: ''
|
||||
},
|
||||
playerDetails: {},
|
||||
tracked: false,
|
||||
matches: [],
|
||||
statusError: '',
|
||||
statusErrorCode: 0,
|
||||
match_stats: {
|
||||
loss: 0,
|
||||
win: 0,
|
||||
@@ -225,83 +244,62 @@ export default {
|
||||
}
|
||||
})
|
||||
|
||||
// Functions
|
||||
const TrackMe = async () => {
|
||||
const shareCodeRegex = /^CSGO(?:-?[ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789]{5}){5}$/
|
||||
const authCodeRegex = /^[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{5}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}$/
|
||||
const GetPlayer = async () => {
|
||||
if (router.currentRoute.value.params.id) {
|
||||
const [res, resData] = await GetUser(router.currentRoute.value.params.id)
|
||||
|
||||
if (!shareCodeRegex.test(data.userData.sharecode))
|
||||
data.statusError = 'Is not a valid sharecode'
|
||||
if (!authCodeRegex.test(data.userData.authcode))
|
||||
data.statusError = 'Is not a valid authcode'
|
||||
|
||||
const res = await axios
|
||||
.post(`${process.env.VUE_APP_API_URL}/player/trackme`, `id=${store.state.id64}&authcode=${data.userData.authcode}&sharecode=${data.userData.sharecode}`)
|
||||
|
||||
if (res.status === 401) {
|
||||
data.statusError = 'Data does not match player'
|
||||
} else if (res.status === 400) {
|
||||
data.statusError = 'Userinput was wrong'
|
||||
} else if (res.status === 200) {
|
||||
data.statusError = ''
|
||||
} else {
|
||||
console.log(`${res.status}: ${res.statusText}`)
|
||||
}
|
||||
}
|
||||
|
||||
const GetUser = () => {
|
||||
axios
|
||||
.get(`${process.env.VUE_APP_API_URL}/player/${props.id}`)
|
||||
.then((response) => {
|
||||
data.playerDetails = response.data
|
||||
data.matches = response.data.matches
|
||||
data.match_stats.loss = response.data.match_stats.loss ? response.data.match_stats.loss : 0
|
||||
data.match_stats.win = response.data.match_stats.win ? response.data.match_stats.win : 0
|
||||
data.match_stats.tie = response.data.match_stats.tie ? response.data.match_stats.tie : 0
|
||||
store.state.id64 = response.data.steamid64
|
||||
if (res === 200 && resData) {
|
||||
data.playerDetails = resData
|
||||
data.tracked = resData.tracked
|
||||
data.matches = data.playerDetails.matches
|
||||
data.match_stats.loss = data.playerDetails.match_stats.loss || 0
|
||||
data.match_stats.win = data.playerDetails.match_stats.win || 0
|
||||
data.match_stats.tie = data.playerDetails.match_stats.tie || 0
|
||||
|
||||
if (data.matches)
|
||||
LoadImage(data.matches[0].map ? data.matches[0].map : 'random')
|
||||
|
||||
// console.log(response.data)
|
||||
let player = {
|
||||
'steamid64': data.playerDetails.steamid64,
|
||||
'vanity_url': data.playerDetails.vanity_url || '',
|
||||
'name': data.playerDetails.name,
|
||||
'avatar': data.playerDetails.avatar
|
||||
}
|
||||
SaveLastVisitedToLocalStorage(player)
|
||||
|
||||
let player = {
|
||||
'steamid64': response.data.steamid64,
|
||||
'name': response.data.name,
|
||||
'avatar': response.data.avatar
|
||||
}
|
||||
SaveLastVisitedToLocalStorage(player)
|
||||
document.title = `${data.playerDetails.name} | csgoWTF`
|
||||
|
||||
document.title = `${response.data.name} | csgoWTF`
|
||||
})
|
||||
.catch((e) => {
|
||||
if (e.response.status === 404) {
|
||||
GoToLink('/')
|
||||
}
|
||||
// console.log(e.response.status, e.response.statusText);
|
||||
});
|
||||
}
|
||||
|
||||
const GetWinLoss = (matchResult, teamId) => {
|
||||
if (matchResult === teamId) {
|
||||
return 'win'
|
||||
} else if (matchResult === 0) {
|
||||
return 'draw'
|
||||
console.log(data.playerDetails)
|
||||
}
|
||||
} else {
|
||||
return 'loss'
|
||||
GoToLink('/')
|
||||
}
|
||||
}
|
||||
|
||||
watch(() => props.id, GetUser)
|
||||
const TrackPlayer = async () => {
|
||||
[data.statusErrorCode, data.statusError] = await TrackMe(data.playerDetails.steamid64, data.userData.authcode, data.userData.sharecode)
|
||||
|
||||
onBeforeMount(() => {
|
||||
GetUser()
|
||||
})
|
||||
// setTimeout(() => {
|
||||
if (data.statusErrorCode === 202) {
|
||||
data.statusErrorCode = 0
|
||||
data.statusError = ''
|
||||
data.tracked = true
|
||||
}
|
||||
// }, 2000)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
setTimeout(() => {
|
||||
GetPlayer()
|
||||
}, 200)
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
data,
|
||||
store,
|
||||
props,
|
||||
GetUser,
|
||||
TrackPlayer,
|
||||
TrackMe,
|
||||
GetWinLoss,
|
||||
FormatDate,
|
||||
@@ -544,4 +542,4 @@ table {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user