From 5e8bdd7d4b1dede2703be6b8eed62a25c9d97806 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Thu, 23 Mar 2023 13:00:57 +0100 Subject: [PATCH] Change data loading to centralized store --- src/components/PlayerChart.vue | 41 +++++++++------ src/components/PlayerList.vue | 45 ++++++++++------ src/main.ts | 4 ++ src/store/index.ts | 83 +++++++++++++++++++++++++---- src/views/PlayerView.vue | 96 +++++++++++++--------------------- 5 files changed, 168 insertions(+), 101 deletions(-) diff --git a/src/components/PlayerChart.vue b/src/components/PlayerChart.vue index b227f23..ce347e7 100644 --- a/src/components/PlayerChart.vue +++ b/src/components/PlayerChart.vue @@ -14,6 +14,8 @@ import { Legend } from 'chart.js' import annotationPlugin from 'chartjs-plugin-annotation' +import { Store, useStore } from 'vuex' +import { Player, Weapon } from '@/store/index' import { Scatter } from 'vue-chartjs' import { defineComponent } from 'vue' @@ -23,7 +25,7 @@ ChartJS.register(LinearScale, PointElement, LineElement, Tooltip, Legend, annota export default defineComponent({ name: 'PlayerChart', props: { - players: Object + filters: Object }, components: { Scatter @@ -37,15 +39,24 @@ export default defineComponent({ labels: [] as string[], borderColor: '#f87979', backgroundColor: '#f87979', + pointRadius: [1], + pointStyle: ['dot'], data: [] as {x:number, y:number}[] } ] } - if (!this.$props.players) { + if (!this.players) { return chartData } - chartData.datasets[0].labels = Object.keys(this.$props.players) - chartData.datasets[0].data = Object.values(this.$props.players).map((e:any) => ({ x: e.deaths, y: e.kills })) + chartData.datasets[0].labels = this.playerIdList + chartData.datasets[0].data = this.playerIdList.map(e => { + if (!this.players) { + return { x: 0, y: 0 } + } + return { x: this.players[e].deaths, y: this.players[e].kills } + }) + chartData.datasets[0].pointRadius = [1].fill(1, 0, chartData.datasets[0].labels.length) + chartData.datasets[0].pointStyle = ['dot'].fill('dot', 0, chartData.datasets[0].labels.length) return chartData }, chartOptions () { @@ -56,10 +67,10 @@ export default defineComponent({ callbacks: { label: (ctx:any) => { let label:string - if (!this.$props.players) { + if (!this.players) { label = ctx.dataset.labels[ctx.dataIndex] } else { - label = this.$props.players[ctx.dataset.labels[ctx.dataIndex]].username + label = this.players[ctx.dataset.labels[ctx.dataIndex]].username } label += ' (' + ctx.parsed.y + ' kills ' + ctx.parsed.x + ' deaths)' return label @@ -69,11 +80,11 @@ export default defineComponent({ annotation: {} } } - if (!this.$props.players) { + if (!this.players) { return options } - const maxX = Math.max(...Object.values(this.$props.players).map(e => e.deaths)) - const maxY = Math.max(...Object.values(this.$props.players).map(e => e.kills)) + const maxX = Math.max(...Object.values(this.players).map(e => e.deaths)) + const maxY = Math.max(...Object.values(this.players).map(e => e.kills)) let endX, endY if (maxY > maxX) { endX = maxX @@ -97,16 +108,14 @@ export default defineComponent({ } } return options - } - }, - /* watch: { - players: function (newval: any, oldval: any): void { + }, + players ():{[key:string]:Player} { return this.store.getters.getPlayerList(this.filters) }, + playerIdList ():string[] { return Object.keys(this.players) } - } - }, */ + }, data () { return { - + store: useStore() } } }) diff --git a/src/components/PlayerList.vue b/src/components/PlayerList.vue index 1f1cf60..9617735 100644 --- a/src/components/PlayerList.vue +++ b/src/components/PlayerList.vue @@ -9,7 +9,7 @@ average kill distance
- {{ $props.players[playerId].username }} + {{ players[playerId].username }} {{ players[playerId].kills }} {{ players[playerId].deaths }} {{ Math.round(players[playerId].kills / players[playerId].deaths * 100) / 100 }} @@ -21,27 +21,37 @@