2023-08-13 03:24:50 +02:00
import ChellarisDataStore from '$lib/stores/ChellarisData' ;
import SelectedGameStore from '$lib/stores/GameFilter' ;
2023-08-24 05:04:27 +02:00
import SelectedGameGroupsStore , { type SelectedChellarisGroups } from "$lib/stores/GameGroupFilter" ;
2023-08-14 20:18:36 +02:00
import GraphsTabStore from '$lib/stores/GraphsTab' ;
2023-08-15 22:51:13 +02:00
import type { LayoutLoad } from "./$types" ;
2023-08-27 21:00:56 +02:00
import type { ChellarisInfo } from '../../lib/types/chellaris' ;
2023-09-07 22:04:51 +02:00
import { apiBaseUrl } from '$lib/components/consts' ;
2023-08-13 03:24:50 +02:00
2023-08-21 03:45:41 +02:00
export const load : LayoutLoad = async ( { fetch } ) = > {
2023-08-15 22:51:13 +02:00
let store : string | null ;
2023-08-24 05:04:27 +02:00
2023-08-13 03:24:50 +02:00
// Chellaris Data Code
2023-08-28 07:28:43 +02:00
const chellarisData : ChellarisInfo = await ( await fetch ( apiBaseUrl + '/v3/full_view_data' ) ) . json ( ) ;
2023-08-27 21:00:56 +02:00
2023-08-28 07:28:43 +02:00
ChellarisDataStore . set ( chellarisData ) ;
2023-08-13 03:24:50 +02:00
// Local Storage Code
2023-08-28 07:28:43 +02:00
let gameGroupSelections : { [ key : number ] : SelectedChellarisGroups } = { } ;
2023-08-24 05:04:27 +02:00
let gameSelection : number | undefined ;
2023-08-13 03:24:50 +02:00
if ( typeof localStorage !== 'undefined' ) {
2023-08-14 20:18:36 +02:00
// Tab Selection
store = localStorage . getItem ( 'graphsTab' ) ;
2023-08-24 05:04:27 +02:00
if ( typeof store === 'string' ) {
2023-08-14 20:18:36 +02:00
GraphsTabStore . set ( store ) ;
}
2023-08-24 05:04:27 +02:00
2023-08-13 03:24:50 +02:00
// Game Selection
2023-08-14 20:18:36 +02:00
store = localStorage . getItem ( 'gameSelection' ) ;
2023-08-13 03:24:50 +02:00
2023-08-24 05:04:27 +02:00
if ( typeof store === 'string' && store != "\"\"" ) {
gameSelection = JSON . parse ( store ) ;
}
if ( typeof gameSelection === 'undefined' ) {
2023-09-12 19:28:17 +02:00
gameSelection = Object . values ( chellarisData . games ) [ 0 ] . id ;
2023-08-14 20:18:36 +02:00
}
2023-08-13 03:24:50 +02:00
// Game Groups Selection
store = localStorage . getItem ( 'gameGroupSelection' ) ;
2023-08-24 05:04:27 +02:00
if ( typeof store === 'string' ) {
gameGroupSelections = JSON . parse ( store ) ;
if ( typeof gameGroupSelections [ gameSelection ] === 'undefined' ) {
// Default to all available groups
2023-09-12 19:35:20 +02:00
gameGroupSelections [ gameSelection ] = { gameId : gameSelection , selectedGroups : Object.fromEntries ( Object . values ( chellarisData . games [ gameSelection ] . groups ) . map ( ( group ) = > group . id ) . entries ( ) ) } ;
2023-08-24 05:04:27 +02:00
// Set Local Storage to default Values if not previously defined
localStorage . setItem ( 'gameGroupSelection' , JSON . stringify ( gameGroupSelections ) ) ;
}
else {
Object . keys ( gameGroupSelections ) . forEach (
gKey = > {
if ( gameGroupSelections [ + gKey ] == null ) {
delete gameGroupSelections [ + gKey ] ;
}
else {
Object . keys ( gameGroupSelections [ + gKey ] . selectedGroups ) . forEach (
key = > gameGroupSelections [ + gKey ] . selectedGroups [ + key ] != null || delete gameGroupSelections [ + gKey ] . selectedGroups [ + key ]
)
}
} ) ;
2023-08-13 03:24:50 +02:00
}
2023-08-14 20:18:36 +02:00
}
}
2023-08-24 05:04:27 +02:00
if ( typeof gameSelection === 'undefined' ) {
2023-09-12 19:28:17 +02:00
gameSelection = Object . values ( chellarisData . games ) [ 0 ] . id ;
2023-08-24 05:04:27 +02:00
}
SelectedGameStore . set ( gameSelection ) ;
if ( typeof gameGroupSelections [ gameSelection ] === 'undefined' ) {
// Default to all available groups
2023-09-12 19:35:20 +02:00
gameGroupSelections [ gameSelection ] = { gameId : gameSelection , selectedGroups : Object.fromEntries ( Object . values ( chellarisData . games [ gameSelection ] . groups ) . map ( ( group ) = > group . id ) . entries ( ) ) } ;
2023-08-24 05:04:27 +02:00
}
SelectedGameGroupsStore . set ( gameGroupSelections ) ;
gameGroupSelections = [ ] ; // TODO: actually assing a value
2023-08-15 22:51:13 +02:00
return { chellarisData }
2023-08-13 03:24:50 +02:00
}