alai-server/frontend/src/views/Chart.vue

172 lines
4.7 KiB
Vue
Raw Normal View History

2023-01-30 12:48:37 -03:00
<script setup>
2023-01-31 23:09:14 -03:00
import { ref, watch, onMounted } from 'vue';
2023-01-30 12:48:37 -03:00
import { useLayout } from '@/layout/composables/layout';
2023-02-01 23:51:02 -03:00
import auth from '../utils/Auth';
2023-02-12 16:34:03 -03:00
import axios from 'axios';
2023-01-30 12:48:37 -03:00
const { layoutConfig } = useLayout();
let documentStyle = getComputedStyle(document.documentElement);
let textColor = documentStyle.getPropertyValue('--text-color');
let textColorSecondary = documentStyle.getPropertyValue('--text-color-secondary');
let surfaceBorder = documentStyle.getPropertyValue('--surface-border');
2023-02-12 16:34:03 -03:00
const time_elapsed = ref([]);
2023-02-14 18:23:22 -03:00
const coins = ref([]);
2023-02-15 15:31:17 -03:00
const games = ref([]);
2023-02-16 23:11:58 -03:00
const ms = ref(true);
const selectedGame = ref();
2023-01-30 12:48:37 -03:00
const lineData = ref(null);
const lineOptions = ref(null);
const setColorOptions = () => {
documentStyle = getComputedStyle(document.documentElement);
textColor = documentStyle.getPropertyValue('--text-color');
textColorSecondary = documentStyle.getPropertyValue('--text-color-secondary');
surfaceBorder = documentStyle.getPropertyValue('--surface-border');
};
2023-03-01 23:34:29 -03:00
const url = new URL(window.location.href);
2023-03-01 23:55:56 -03:00
const api = (url.port == "5173") ? "http://localhost:3001" : "/api/v1";
2023-03-01 23:34:29 -03:00
2023-02-15 15:31:17 -03:00
async function getGames() {
try {
2023-03-01 23:34:29 -03:00
const response = await axios.get(api + `/game?limit=5000`);
2023-02-15 15:31:17 -03:00
games.value = response.data.map(game_id => {
return game_id.ID;
});
}
catch (error) {
console.error(error);
}
}
2023-02-16 23:11:58 -03:00
async function getFrames() {
2023-02-12 16:34:03 -03:00
try {
2023-02-16 23:11:58 -03:00
if (selectedGame.value === undefined) {
2023-02-15 15:31:17 -03:00
return;
}
2023-03-01 23:34:29 -03:00
const response = await axios.get(api + `/frame?limit=5000&game_id=` + selectedGame.value);
2023-02-16 23:11:58 -03:00
if (response.data[response.data.length - 1].elapsed_time >= 10000) {
ms.value = false;
}
else {
ms.value = true;
}
2023-02-14 18:23:22 -03:00
time_elapsed.value = response.data.map(frame => {
2023-02-16 23:11:58 -03:00
return (ms.value == true) ? (Math.round(frame.elapsed_time * 10) / 10).toFixed(1) : (Math.floor(frame.elapsed_time / 1000 * 10) / 10).toFixed(1);
2023-02-14 18:23:22 -03:00
});
coins.value = response.data.map(frame => {
return frame.coins;
});
2023-02-12 16:34:03 -03:00
}
catch (error) {
console.error(error);
}
2023-02-16 23:11:58 -03:00
lineData.value.labels = time_elapsed.value;
lineData.value.datasets[0].label = 'Game ' + selectedGame.value;
lineData.value.datasets[0].data = coins.value;
lineOptions.value.scales.x.title.text = (ms.value) ? "Time(ms)" : "Time(s)";
}
2023-02-12 16:34:03 -03:00
2023-02-16 23:11:58 -03:00
async function setChart() {
2023-01-30 12:48:37 -03:00
lineData.value = {
2023-02-14 18:23:22 -03:00
labels: time_elapsed.value,
2023-01-30 12:48:37 -03:00
datasets: [
{
2023-02-16 23:11:58 -03:00
label: 'Game ' + selectedGame.value,
2023-02-14 18:23:22 -03:00
data: coins.value,
2023-01-30 12:48:37 -03:00
fill: false,
backgroundColor: documentStyle.getPropertyValue('--primary-500'),
borderColor: documentStyle.getPropertyValue('--primary-500'),
tension: 0.4
}
]
};
lineOptions.value = {
2023-02-16 23:11:58 -03:00
reponsive: true,
2023-01-30 12:48:37 -03:00
plugins: {
legend: {
labels: {
fontColor: textColor
}
}
},
scales: {
x: {
2023-02-14 18:23:22 -03:00
title: {
display: true,
2023-02-16 23:11:58 -03:00
text: (ms.value) ? "Time(ms)" : "Time(s)"
2023-02-14 18:23:22 -03:00
},
2023-01-30 12:48:37 -03:00
ticks: {
color: textColorSecondary
},
grid: {
color: surfaceBorder,
drawBorder: false
}
},
y: {
2023-02-14 18:23:22 -03:00
title: {
display: true,
text: "Coins"
},
2023-01-30 12:48:37 -03:00
ticks: {
color: textColorSecondary
},
grid: {
color: surfaceBorder,
drawBorder: false
}
}
}
};
}
watch(
layoutConfig.theme,
() => {
setColorOptions();
},
{ immediate: true }
);
2023-01-31 23:09:14 -03:00
onMounted(() => {
2023-02-14 18:23:22 -03:00
setChart();
2023-02-16 23:11:58 -03:00
checkAuth();
2023-02-15 15:31:17 -03:00
getGames();
2023-01-31 23:09:14 -03:00
})
const checkAuth = () => {
auth.checkToken(true);
};
2023-01-30 12:48:37 -03:00
</script>
<template>
<div class="card">
<div class="grid p-fluid">
<div class="col-12 xl:col-2">
<span class="p-float-label">
2023-02-16 23:11:58 -03:00
<Dropdown v-model="selectedGame" :options="games" optionLabel="" optionValue="" @change="getFrames()" />
<label for="dropdown">Game session</label>
2023-01-30 12:48:37 -03:00
</span>
</div>
</div>
</div>
<div class="card">
<div class="grid p-fluid">
<div class="col-12 xl:col-6">
2023-02-16 23:11:58 -03:00
<Chart type="line" :data="lineData" :options="lineOptions"></Chart>
2023-01-30 12:48:37 -03:00
</div>
</div>
</div>
2023-02-16 23:11:58 -03:00
</template>
<style>
canvas {
width: 500px !important;
height: 310px !important;
}
</style>