2023-08-13 03:24:50 +02:00
import ChellarisDataStore from '$lib/stores/ChellarisData' ;
import SelectedGameStore from '$lib/stores/GameFilter' ;
import SelectedGameGroupsStore 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 { createChellarisInfo , type ChellarisGame , createChellarisGame , createChellarisGameGroup , createChellarisEmpire } from "$lib/types/chellaris" ;
import type { LayoutLoad } from "./$types" ;
2023-08-16 00:47:58 +02:00
import type { Ethic } from '../../lib/types/stellaris' ;
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-14 20:18:36 +02:00
const apiBaseUrl = 'https://www.chellaris.net/api/v2' ;
const chellarisData = createChellarisInfo ( ) ;
2023-08-15 22:51:13 +02:00
2023-08-14 20:18:36 +02:00
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 ) ;
}
} ) ;
2023-08-16 00:47:58 +02:00
const ethics : { id : number , name : string , machine_ethic : boolean } [ ] = await ( await fetch ( apiBaseUrl + '/ethics' ) ) . json ( ) ;
2023-08-15 22:51:41 +02:00
2023-08-16 00:47:58 +02:00
ethics . sort ( ( a , b ) = > ( a . id < b . id ? - 1 : 1 ) ) ;
2023-08-15 22:51:41 +02:00
ethics . forEach ( ethic = > {
2023-08-16 00:47:58 +02:00
const newEthic : Ethic = { displayName : ethic.name , machine : ethic.machine_ethic } ;
chellarisData . ethics . set ( ethic . id , newEthic ) ;
2023-08-15 22:51:41 +02:00
} ) ;
2023-08-16 00:47:58 +02:00
const empireEthics : {
empires_id : number ,
empires_group_id : number ,
empires_group_game_id : number ,
ethics_id : number ,
ethics_fanatic : boolean } [ ] = await ( await fetch ( apiBaseUrl + '/empire_ethics' ) ) . json ( ) ;
empireEthics . forEach ( empireEthic = > {
const gameData = chellarisData . games . get ( empireEthic . empires_group_game_id ) ;
const ethic = chellarisData . ethics . get ( empireEthic . ethics_id ) ;
if ( typeof gameData !== "undefined" && typeof ethic !== "undefined" ) {
const empireData = gameData . empires . get ( empireEthic . empires_id ) ;
if ( typeof empireData !== "undefined" ) {
const tmpEthic : Ethic = { machine : ethic.machine , displayName : ethic.displayName , fanatic : empireEthic.ethics_fanatic } ;
if ( tmpEthic . machine ) {
empireData . machine = true ;
}
empireData . ethics . set ( empireEthic . ethics_id , tmpEthic ) ;
}
}
2023-08-21 00:48:23 +02:00
} ) ;
const portraitGroups : {
id : number ,
name : string
} [ ] = await ( await fetch ( apiBaseUrl + '/portrait_groups' ) ) . json ( ) ;
portraitGroups . sort ( ( a , b ) = > ( a . id < b . id ? - 1 : 1 ) ) ;
portraitGroups . forEach ( portraitGroup = > {
const newPortraitGroup = { displayName : portraitGroup.name , portraits : new Map ( ) } ;
chellarisData . species . set ( portraitGroup . id , newPortraitGroup ) ;
} ) ;
const portraits : {
id : number ,
2023-08-21 03:45:41 +02:00
hires : string ,
lores : string ,
2023-08-21 00:48:23 +02:00
group_id : number
} [ ] = await ( await fetch ( apiBaseUrl + '/portraits' ) ) . json ( ) ;
portraits . sort ( ( a , b ) = > ( a . id < b . id ? - 1 : 1 ) ) ;
portraits . forEach ( portrait = > {
const portraitGroupData = chellarisData . species . get ( portrait . group_id ) ;
if ( typeof portraitGroupData !== "undefined" ) {
2023-08-21 03:45:41 +02:00
const newPortraitData = { hires : portrait.hires , lores : portrait.lores } ;
2023-08-21 00:48:23 +02:00
portraitGroupData . portraits . set ( portrait . id , newPortraitData ) ;
}
2023-08-16 00:47:58 +02:00
} )
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-21 03:45:41 +02:00
let selectedGame = 1 ;
if ( typeof store == 'string' && store != "\"\"" ) {
2023-08-14 20:18:36 +02:00
selectedGame = JSON . parse ( store ) ;
}
2023-08-21 03:45:41 +02:00
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-15 22:51:13 +02:00
return { chellarisData }
2023-08-13 03:24:50 +02:00
}