From 9a5ba1178d04acc400a9e59e91d6694d17d06ee9 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Thu, 22 Jun 2023 18:38:51 +0200 Subject: [PATCH] merge gamemode and weapon into one reusable component --- .../{GamemodeChart.vue => PieChart.vue} | 34 ++-- src/components/WeaponChart.vue | 185 ------------------ src/views/PlayerView.vue | 17 +- 3 files changed, 30 insertions(+), 206 deletions(-) rename src/components/{GamemodeChart.vue => PieChart.vue} (81%) delete mode 100644 src/components/WeaponChart.vue diff --git a/src/components/GamemodeChart.vue b/src/components/PieChart.vue similarity index 81% rename from src/components/GamemodeChart.vue rename to src/components/PieChart.vue index 7a150fc..652d0eb 100644 --- a/src/components/GamemodeChart.vue +++ b/src/components/PieChart.vue @@ -11,37 +11,46 @@ import dataLabel from 'chartjs-plugin-datalabels' import { Weapon, Filter, useKillStore } from '@/stores/kill' import { Doughnut } from 'vue-chartjs' import { defineComponent, PropType, Ref, unref } from 'vue' -import gamemodes from '../stores/gamemodes.json' import LoadingBar from './LoadingBar.vue' ChartJS.register(ArcElement, Tooltip, Legend, dataLabel) export default defineComponent({ - name: 'GamemodeChart', + name: 'PieChart', props: { filters: Object as PropType, - playerHighlighted: String + playerHighlighted: String, + type: Object as PropType<'server' | 'player' | 'weapon' | 'map' | 'gamemode'> }, emits: ['highlightPlayer'], components: { Doughnut, LoadingBar }, + created () { + // eslint-disable-next-line no-extra-bind + import(`../stores/${this.type}s.json`).then((e => { this.locale = e }).bind(this)) + }, mounted () { this.refreshColors++ }, data: () => { return { store: useKillStore(), - refreshColors: 0 + refreshColors: 0, + locale: {} as {[key:string]:string} } }, computed: { progress () { const filter = new Filter(this.filters) - delete filter.gamemode - const data = this.store.getList('gamemodes', filter)?.value - if (!data) return this.store.fetch('gamemodes', filter).value.progress.value + if (this.type === undefined) return 0 + if (!(['server', 'player', 'weapon', 'map', 'gamemode'].includes(this.type))) return 0 + const type = this.type as 'server' | 'player' | 'weapon' | 'map' | 'gamemode' + + if (this.type !== undefined) delete filter[type] + const data = this.store.getList(`${type}s`, filter)?.value + if (!data) return this.store.fetch(`${type}s`, filter).value.progress.value return data.progress.value }, colors () { @@ -112,8 +121,7 @@ export default defineComponent({ datalabels: { formatter: (value, context) => { const weaponId = this.sortedList[context.dataIndex] - // if (!(weapons as {[key:string]:string})[weaponId]) console.log(weaponId) - return (gamemodes as { [key: string]: string })[weaponId] || weaponId + return this.locale[weaponId] || weaponId }, backgroundColor: (context) => { return context.dataset.backgroundColor(context, options) @@ -149,9 +157,11 @@ export default defineComponent({ }, values (): { [key: string]: Ref } { const filter = new Filter({ ...this.filters, player: this.playerHighlighted }) - delete filter.gamemode - const data = this.store.getList('gamemodes', filter)?.value.data - if (!data) return this.store.fetch('gamemodes', filter).value.data + if (this.type === undefined) return {} + if (!(['server', 'player', 'weapon', 'map', 'gamemode'].includes(this.type))) return {} + const type = this.type as 'server' | 'player' | 'weapon' | 'map' | 'gamemode' + const data = this.store.getList(`${type}s`, filter)?.value.data + if (!data) return this.store.fetch(`${type}s`, filter).value.data return data }, sortedList (): string[] { diff --git a/src/components/WeaponChart.vue b/src/components/WeaponChart.vue deleted file mode 100644 index 78796ca..0000000 --- a/src/components/WeaponChart.vue +++ /dev/null @@ -1,185 +0,0 @@ - - - - - diff --git a/src/views/PlayerView.vue b/src/views/PlayerView.vue index 446671c..4eda911 100644 --- a/src/views/PlayerView.vue +++ b/src/views/PlayerView.vue @@ -80,15 +80,16 @@ :playerHighlighted="playerHighlighted?.id" > - - - + + type="gamemode" + > @@ -96,8 +97,7 @@ import { Player, Weapon, Server, Kill, useKillStore, Filter } from '@/stores/kill' import PlayerList from '@/components/PlayerList.vue' import PlayerChart from '@/components/PlayerChart.vue' -import WeaponChart from '@/components/WeaponChart.vue' -import GamemodeChart from '@/components/GamemodeChart.vue' +import PieChart from '@/components/PieChart.vue' import { Ref, defineComponent, unref } from 'vue' import VueMultiselect from 'vue-multiselect' import 'vue-multiselect/dist/vue-multiselect.css' @@ -115,8 +115,7 @@ export default defineComponent({ VueMultiselect, PlayerList, PlayerChart, - WeaponChart, - GamemodeChart + PieChart }, data () {