My 13th reason (2.0)

This commit is contained in:
2024-09-20 16:34:17 +02:00
parent b9bb55cf49
commit be4c201d81
21 changed files with 129 additions and 131 deletions

View File

@ -1,38 +0,0 @@
import { defineStore } from 'pinia'
import { type Asset } from '@/types'
import config from '@/config'
export const useAssetStore = defineStore('assets', {
state: () => ({
assets: [] as Asset[]
}),
actions: {
setAssets(assets: Asset[]) {
this.assets = assets
},
async fetchAssets() {
return fetch(config.server_endpoint + '/assets')
.then((response) => response.json())
.then((assets) => {
this.setAssets(assets)
return true
})
.catch((error) => {
console.error('Error fetching assets:', error)
return false
})
},
async fetchAssetsByZoneId(zoneId: number) {
return fetch(config.server_endpoint + '/assets/' + zoneId)
.then((response) => response.json())
.then((assets) => {
this.setAssets(assets)
return true
})
.catch((error) => {
console.error('Error fetching assets:', error)
return false
})
}
}
})

View File

@ -1,11 +1,13 @@
import { defineStore } from 'pinia'
import { io, Socket } from 'socket.io-client'
import type { Character, User } from '@/types'
import type { Asset, Character, Notification, User } from '@/types'
import config from '@/config'
import { useCookies } from '@vueuse/integrations/useCookies'
export const useGameStore = defineStore('game', {
state: () => ({
notifications: [] as Notification[],
assets: [] as Asset[],
token: '' as string | null,
connection: null as Socket | null,
user: null as User | null,
@ -15,7 +17,70 @@ export const useGameStore = defineStore('game', {
isChatOpen: false,
isUserPanelOpen: false
}),
getters: {
getNotifications: (state: any) => state.notifications,
getAssetByKey: (state) => {
return (key: string) => state.assets.find((asset) => asset.key === key)
}
},
actions: {
addNotification(notification: Notification) {
if (!notification.id) {
notification.id = Math.random().toString(16)
}
this.notifications.push(notification)
},
removeNotification(id: string) {
this.notifications = this.notifications.filter((notification: Notification) => notification.id !== id)
},
setAssets(assets: Asset[]) {
this.assets = assets
},
addAsset(asset: Asset) {
this.assets.push(asset)
},
addAssets(assets: Asset[]) {
this.assets = this.assets.concat(assets)
},
async fetchSpriteAssets() {
return fetch(config.server_endpoint + '/assets/sprites')
.then((response) => response.json())
.then((assets) => {
// Only add the sprites that are not already in the store
this.addAssets(assets.filter((asset: Asset) => !this.getAssetByKey(asset.key)))
return true
})
.catch((error) => {
console.error('Error fetching assets:', error)
return false
})
},
async fetchZoneAssets(zoneId: number) {
return fetch(config.server_endpoint + '/assets/zone/' + zoneId)
.then((response) => response.json())
.then((assets) => {
// Only add the zones that are not already in the store
this.addAssets(assets.filter((asset: Asset) => !this.getAssetByKey(asset.key)))
return true
})
.catch((error) => {
console.error('Error fetching assets:', error)
return false
})
},
async fetchAllAssets() {
return fetch(config.server_endpoint + '/assets')
.then((response) => response.json())
.then((assets) => {
// Only add the zones that are not already in the store
this.addAssets(assets.filter((asset: Asset) => !this.getAssetByKey(asset.key)))
return true
})
.catch((error) => {
console.error('Error fetching assets:', error)
return false
})
},
setToken(token: string) {
this.token = token
},
@ -74,6 +139,7 @@ export const useGameStore = defineStore('game', {
domain: window.location.hostname.split('.').slice(-2).join('.')
})
this.assets = []
this.connection = null
this.token = null
this.user = null

View File

@ -1,22 +0,0 @@
import { defineStore, type StoreDefinition } from 'pinia'
import type { Notification } from '@/types'
export const useNotificationStore: StoreDefinition = defineStore('notifications', {
state: () => ({
notifications: [] as Notification[]
}),
getters: {
getNotifications: (state: any) => state.notifications
},
actions: {
addNotification(notification: Notification) {
if (!notification.id) {
notification.id = Math.random().toString(16)
}
this.notifications.push(notification)
},
removeNotification(id: string) {
this.notifications = this.notifications.filter((notification: Notification) => notification.id !== id)
}
}
})