import { defineStore } from 'pinia' import { io, Socket } from 'socket.io-client' import type { Asset, Character, Notification, User, WorldSettings } from '@/types' import config from '@/config' import { useCookies } from '@vueuse/integrations/useCookies' export const useGameStore = defineStore('game', { state: () => { return { notifications: [] as Notification[], isAssetsLoaded: false, loadedAssets: [] as string[], token: '' as string | null, connection: null as Socket | null, user: null as User | null, character: null as Character | null, isPlayerDraggingCamera: false, world: { date: new Date(), isRainEnabled: false, isFogEnabled: false, fogDensity: 0.5 } as WorldSettings, gameSettings: { isCameraFollowingCharacter: false }, uiSettings: { isChatOpen: false, isCharacterProfileOpen: false, isGmPanelOpen: false, isPasswordResetOpen: false } } }, 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) }, setToken(token: string) { this.token = token }, setUser(user: User | null) { this.user = user }, setCharacter(character: Character | null) { this.character = character }, toggleGmPanel() { this.uiSettings.isGmPanelOpen = !this.uiSettings.isGmPanelOpen }, togglePlayerDraggingCamera() { this.isPlayerDraggingCamera = !this.isPlayerDraggingCamera }, setPlayerDraggingCamera(moving: boolean) { this.isPlayerDraggingCamera = moving }, toggleCameraFollowingCharacter() { this.gameSettings.isCameraFollowingCharacter = !this.gameSettings.isCameraFollowingCharacter }, setCameraFollowingCharacter(following: boolean) { this.gameSettings.isCameraFollowingCharacter = following }, toggleChat() { this.uiSettings.isChatOpen = !this.uiSettings.isChatOpen }, toggleCharacterProfile() { this.uiSettings.isCharacterProfileOpen = !this.uiSettings.isCharacterProfileOpen }, togglePasswordReset() { this.uiSettings.isPasswordResetOpen = !this.uiSettings.isPasswordResetOpen }, initConnection() { this.connection = io(config.server_endpoint, { secure: !config.development, withCredentials: true, transports: ['websocket'], reconnectionAttempts: 5 }) // #99 - If we can't connect, disconnect this.connection.on('connect_error', () => { this.disconnectSocket() }) // Let the server know the user is logged in this.connection.emit('login') // set user this.connection.on('logged_in', (user: User) => { this.setUser(user) }) // When we can't reconnect, disconnect this.connection.on('reconnect_failed', () => { this.disconnectSocket() }) }, disconnectSocket() { this.connection?.disconnect() useCookies().remove('token', { // for whole domain // @TODO : #190 // domain: window.location.hostname.split('.').slice(-2).join('.') }) this.isAssetsLoaded = false this.connection = null this.token = null this.user = null this.character = null this.uiSettings.isGmPanelOpen = false this.isPlayerDraggingCamera = false this.gameSettings.isCameraFollowingCharacter = false this.uiSettings.isChatOpen = false this.uiSettings.isCharacterProfileOpen = false this.uiSettings.isPasswordResetOpen = false this.world.date = new Date() this.world.isRainEnabled = false this.world.isFogEnabled = false this.world.fogDensity = 0.5 } } })