2023-08-13 03:24:50 +02:00
import type { PageLoad } from "../$types" ;
import ChellarisDataStore from '$lib/stores/ChellarisData' ;
import SelectedGameStore from '$lib/stores/GameFilter' ;
2023-08-14 22:31:16 +02:00
import { type ChellarisInfo , type ChellarisGame , type ChellarisGameGroup , createChellarisInfo , createChellarisGameGroup , createChellarisGame , createChellarisEmpire } from '../../lib/types/chellaris' ;
2023-08-13 03:24:50 +02:00
import SelectedGameGroupsStore from "$lib/stores/GameGroupFilter" ;
2023-08-14 20:18:36 +02:00
import GraphsTabStore from '$lib/stores/GraphsTab' ;
2023-08-13 03:24:50 +02:00
export const load : PageLoad = async ( ) = > {
2023-08-14 20:18:36 +02:00
const apiBaseUrl = 'https://www.chellaris.net/api/v2' ;
const chellarisData = createChellarisInfo ( ) ;
let store : string | null ;
2023-08-13 03:24:50 +02:00
// Chellaris Data Code
2023-08-14 20:18:36 +02:00
const games : { id : number , name : string } [ ] = await ( await fetch ( apiBaseUrl + '/games' ) ) . json ( ) ;
2023-08-13 03:24:50 +02:00
2023-08-14 20:18:36 +02:00
games . sort ( ( a , b ) = > ( a . name < b . name ? - 1 : 1 ) ) ;
games . forEach ( game = > {
const newGame : ChellarisGame = createChellarisGame ( ) ;
newGame . name = game . name ;
chellarisData . games . set ( game . id , newGame ) ;
2023-08-13 03:24:50 +02:00
} ) ;
2023-08-14 20:18:36 +02:00
const groups : { id : number , name : string , game_id : number } [ ] = await ( await fetch ( apiBaseUrl + '/game_groups' ) ) . json ( ) ;
2023-08-13 03:24:50 +02:00
2023-08-14 20:18:36 +02:00
groups . sort ( ( a , b ) = > ( a . name < b . name ? - 1 : 1 ) ) ;
groups . forEach ( group = > {
const gameData = chellarisData . games . get ( group . game_id ) ;
2023-08-13 03:24:50 +02:00
2023-08-14 20:18:36 +02:00
if ( typeof gameData !== "undefined" ) {
const newGroup = createChellarisGameGroup ( ) ;
newGroup . name = group . name ;
gameData . groups . set ( group . id , newGroup )
chellarisData . games . set ( group . game_id , gameData ) ;
}
} )
2023-08-13 03:24:50 +02:00
2023-08-14 22:31:16 +02:00
const empires : {
id : number ,
discord_user? : string ,
group_id : number ,
gestalt : boolean ,
empire_portrait_id : number ,
empire_portrait_group_id : number ,
group_game_id : number } [ ] = await ( await fetch ( apiBaseUrl + '/empires' ) ) . json ( ) ;
empires . sort ( ( a , b ) = > ( a . id < b . id ? - 1 : 1 ) ) ;
empires . forEach ( empire = > {
const gameData = chellarisData . games . get ( empire . group_game_id ) ;
if ( typeof gameData !== "undefined" ) {
const newEmpire = createChellarisEmpire ( empire ) ;
gameData . empires . set ( empire . id , newEmpire ) ;
}
} ) ;
console . log ( chellarisData ) ; //DEBUG
2023-08-14 20:18:36 +02:00
ChellarisDataStore . set ( chellarisData ) ;
2023-08-13 03:24:50 +02:00
// Local Storage Code
if ( typeof localStorage !== 'undefined' ) {
2023-08-14 20:18:36 +02:00
// Tab Selection
store = localStorage . getItem ( 'graphsTab' ) ;
if ( typeof store == 'string' ) {
GraphsTabStore . set ( store ) ;
}
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-14 20:18:36 +02:00
let selectedGame = 0 ;
if ( typeof store == 'string' ) {
selectedGame = JSON . parse ( store ) ;
SelectedGameStore . set ( selectedGame ) ;
}
2023-08-13 03:24:50 +02:00
// Game Groups Selection
store = localStorage . getItem ( 'gameGroupSelection' ) ;
2023-08-14 20:18:36 +02:00
if ( typeof store == 'string' ) {
2023-08-14 22:31:16 +02:00
let selectedGameGroups : Array < number > = [ ] ;
2023-08-14 20:18:36 +02:00
const gameGroupSelectionMap = new Map < number , Array < number > > ( JSON . parse ( store ) ) ;
const tmp = gameGroupSelectionMap . get ( selectedGame ) ;
2023-08-14 22:31:16 +02:00
2023-08-13 03:24:50 +02:00
if ( typeof tmp !== 'undefined' ) {
2023-08-14 22:31:16 +02:00
selectedGameGroups = tmp ;
2023-08-13 03:24:50 +02:00
} else {
2023-08-14 20:18:36 +02:00
const tmpGameData = chellarisData . games . get ( selectedGame ) ;
2023-08-13 03:24:50 +02:00
// If this fails an empty array is precisely what we want
if ( typeof tmpGameData !== "undefined" ) {
// Default to all available groups
selectedGameGroups = [ . . . tmpGameData . groups . keys ( ) ] ;
// Set Local Storage to default Values if not previously defined
localStorage . setItem ( 'gameGroupSelection' , JSON . stringify ( Array . from ( selectedGameGroups . entries ( ) ) ) ) ;
}
}
gameGroupSelectionMap . set ( selectedGame , selectedGameGroups ) ;
SelectedGameGroupsStore . set ( gameGroupSelectionMap ) ;
2023-08-14 20:18:36 +02:00
}
}
2023-08-13 03:24:50 +02:00
}