import "firebase/auth" ; import "firebase/database" ; import { AlchemyColor , AlchemyData } from "~/composables/Alchemy" ; import { Character , useCharacters } from "~/composables/Characters" ; import { Stamps } from "./composables/Stamps" ; import { StatueInfo , StatueName , Statues } from "./composables/Statues" ; import { Task , Timer } from "~/composables/Tasks" ; import { createGlobalState , useStorage } from "@vueuse/core" ; import { ref } from "vue" ; import { useToast } from "vue-toastification" ; import { version } from "../package.json" ; import firebase from "firebase/app" ; const StorageKey = "idleon-companion" ; export const DefaultState = { alchemy : { goals : { Orange : [ ] , Green : [ ] , Purple : [ ] , Yellow : [ ] , } , upgrades : { Orange : [ ] , Green : [ ] , Purple : [ ] , Yellow : [ ] , } , vials : { } , } as AlchemyData , cards : { } as Record < string , number > , chars : [ ] as Character [ ] , checklist : { } as Record < string , boolean > , stamps : { } as Record < string , number > , starSigns : { } as Record < string , boolean > , statues : { } as Record < StatueName , StatueInfo > , tasks : { dailyReset : "12:00" , tasks : Array < Task > ( ) , timers : Array < Timer > ( ) , } , version : "0.2.0" , } ; export const useState = createGlobalState ( ( ) => useStorage ( StorageKey , DefaultState ) ) ; export function versionControl ( ) { const state = useState ( ) ; const savedVersion = localStorage . getItem ( "version" ) ; if ( savedVersion !== null ) { if ( savedVersion < "0.1.1" ) { localStorage . removeItem ( "tasks" ) ; } if ( savedVersion < "0.2.0" ) { for ( const k of [ "alchemy" , "cards" , "chars" , "checklist" , "tasks" , ] as const ) { const value = localStorage . getItem ( k ) ; if ( value !== null ) { state . value [ k ] = JSON . parse ( value ) ; } } for ( const k of Object . keys ( localStorage ) ) { if ( ! [ StorageKey , "iconify" ] . includes ( k ) ) { localStorage . removeItem ( k ) ; } } } } if ( state . value . version < "0.2.2" ) { for ( const key in state . value . chars ) { if ( ! state . value . chars [ key ] . constellations ) { state . value . chars [ key ] . constellations = { } ; } if ( ! state . value . chars [ key ] . starSigns ) { state . value . chars [ key ] . starSigns = { } ; } } if ( ! state . value . starSigns ) { state . value . starSigns = { } ; } } if ( state . value . version < "0.2.3" ) { const newSkills = [ "Trapping" , "Construction" , "Worship" ] as const ; for ( const key in state . value . chars ) { for ( const s of newSkills ) { if ( ! ( s in state . value . chars [ key ] . skills ) ) { state . value . chars [ key ] . skills [ s ] = 0 ; } } } } if ( state . value . version < "0.2.4" ) { const colors = [ "Orange" , "Green" , "Purple" , "Yellow" ] ; for ( const k of colors ) { const amount = 15 ; if ( ! state . value . alchemy . goals ) { state . value . alchemy . goals = { Green : [ ] , Orange : [ ] , Purple : [ ] , Yellow : [ ] , } ; } for ( let i = 0 ; i < amount ; i ++ ) { state . value . alchemy . upgrades [ k ] [ i ] = state . value . alchemy . upgrades [ k ] [ i ] ?? 0 ; state . value . alchemy . goals [ k ] [ i ] = state . value . alchemy . goals [ k ] [ i ] ?? state . value . alchemy . upgrades [ k ] [ i ] + 1 ; } } } if ( state . value . version < "0.3.0" ) { if ( ! state . value . statues ) { state . value . statues = { } as Record < StatueName , StatueInfo > ; } for ( const statue of Object . keys ( Statues ) as Array < StatueName > ) { if ( ! state . value . statues [ statue ] ) { state . value . statues [ statue ] = { golden : false , level : 0 , progress : 0 , } ; } } for ( const index in state . value . chars ) { delete ( state . value . chars [ index ] as Character & { statues : any ; } ) . statues ; } if ( ! state . value . stamps ) { state . value . stamps = { } ; for ( const stamp of Object . values ( Stamps ) ) { state . value . stamps [ stamp . name ] = 0 ; } } state . value . tasks = { dailyReset : "00hr 00min 00sec" , tasks : [ ] , timers : [ ] , } ; } if ( state . value . version < "0.3.1" ) { for ( const key in state . value . chars ) { if ( "subclass" in state . value . chars [ key ] ) { state . value . chars [ key ] . class = ( state . value . chars [ key ] as Character & { subclass : any ; } ) . subclass ?? state . value . chars [ key ] . class ; delete ( state . value . chars [ key ] as Character & { subclass : any ; } ) . subclass ; } } for ( const key in state . value . cards ) { if ( key . includes ( "_" ) ) { state . value . cards [ key . replace ( /_/g , " " ) ] = state . value . cards [ key ] ; delete state . value . cards [ key ] ; } } } state . value . version = version ; } const firebaseConfig = { apiKey : "AIzaSyDP9fu1062i82w64K9LgKHFFMDgPtUj6k4" , appId : "1:693976777179:web:cc00d02a3bd8752ec327fe" , authDomain : "idleon-companion.firebaseapp.com" , databaseURL : "https://idleon-companion-default-rtdb.firebaseio.com" , measurementId : "G-3W9H9KERK0" , messagingSenderId : "693976777179" , projectId : "idleon-companion" , storageBucket : "idleon-companion.appspot.com" , } ; type UserState = firebase . User | null ; export const firebaseApp = firebase . initializeApp ( firebaseConfig ) ; const auth = firebaseApp . auth ( ) ; const db = firebaseApp . database ( ) ; const user = ref ( null as UserState ) ; export enum DbRef { Builds = "/builds" , Users = "/users" } export const useAuth = ( ) => { user . value = auth . currentUser ; const toast = useToast ( ) ; const state = useState ( ) ; const { createCharactersFromData } = useCharacters ( ) ; const loadCloud = ( ) => { if ( user . value === null ) { toast . error ( "You are not logged in!" ) ; return null ; } return db . ref ( `${ DbRef . Users }/${ user . value . uid }` ) . once ( "value" ) . then ( ( snapshot ) => { if ( snapshot . exists ( ) ) { const data = JSON . parse ( snapshot . val ( ) ) ; state . value = data ; versionControl ( ) ; createCharactersFromData ( state . value . chars ) ; toast . success ( "Cloud data loaded!" ) ; } else { toast . error ( "No data found on the cloud." ) ; } } ) ; } ; const saveCloud = ( ) => { if ( user . value === null ) { toast . error ( "You are not logged in!" ) ; return null ; } toast . success ( "Data saved to the cloud." ) ; return db . ref ( `${ DbRef . Users }/${ user . value . uid }` ) . set ( JSON . stringify ( state . value ) ) ; } ; return { auth , loadCloud , saveCloud , user } ; } ; export const useDB = ( ) => { return { db , DbRef , } ; } ;