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 { 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-24 05:04:27 +02:00
2023-08-14 20:18:36 +02:00
2023-08-13 03:24:50 +02:00
// Chellaris Data Code
2023-08-24 05:04:27 +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 ( ) ;
2023-08-24 05:04:27 +02:00
newGame . id = game . id ;
2023-08-14 20:18:36 +02:00
newGame . name = game . name ;
2023-08-24 05:04:27 +02:00
chellarisData . games [ game . id ] = newGame ;
2023-08-13 03:24:50 +02:00
} ) ;
2023-08-24 05:04:27 +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 = > {
2023-08-24 05:04:27 +02:00
const gameData = chellarisData . games [ 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 ( ) ;
2023-08-24 05:04:27 +02:00
newGroup . id = group . id ;
2023-08-14 20:18:36 +02:00
newGroup . name = group . name ;
2023-08-24 05:04:27 +02:00
chellarisData . games [ group . game_id ] . groups [ group . id ] = newGroup ;
2023-08-14 20:18:36 +02:00
}
} )
2023-08-13 03:24:50 +02:00
2023-08-14 22:31:16 +02:00
const empires : {
2023-08-24 05:04:27 +02:00
id : number ,
discord_user? : string ,
2023-08-14 22:31:16 +02:00
group_id : number ,
gestalt : boolean ,
2023-08-24 05:04:27 +02:00
empire_portrait_id : number ,
empire_portrait_group_id : number ,
group_game_id : number
} [ ] = await ( await fetch ( apiBaseUrl + '/empires' ) ) . json ( ) ;
2023-08-14 22:31:16 +02:00
empires . sort ( ( a , b ) = > ( a . id < b . id ? - 1 : 1 ) ) ;
empires . forEach ( empire = > {
2023-08-24 05:04:27 +02:00
const gameData = chellarisData . games [ empire . group_game_id ] ;
2023-08-14 22:31:16 +02:00
if ( typeof gameData !== "undefined" ) {
const newEmpire = createChellarisEmpire ( empire ) ;
2023-08-24 05:04:27 +02:00
chellarisData . games [ empire . group_game_id ] . empires [ empire . id ] = newEmpire ;
2023-08-14 22:31:16 +02:00
}
} ) ;
2023-08-24 05:04:27 +02:00
const ethics : { id : number , name : string , machine_ethic : boolean } [ ] = await ( await fetch ( apiBaseUrl + '/ethics' ) ) . json ( ) ;
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-24 05:04:27 +02:00
const newEthic : Ethic = { id : ethic.id , displayName : ethic.name , machine : ethic.machine_ethic } ;
2023-08-16 00:47:58 +02:00
2023-08-24 05:04:27 +02:00
chellarisData . ethics [ ethic . id ] = newEthic ;
} ) ;
2023-08-15 22:51:41 +02:00
2023-08-16 00:47:58 +02:00
const empireEthics : {
2023-08-24 05:04:27 +02:00
empires_id : number ,
empires_group_id : number ,
empires_group_game_id : number ,
2023-08-16 00:47:58 +02:00
ethics_id : number ,
2023-08-24 05:04:27 +02:00
ethics_fanatic : boolean
} [ ] = await ( await fetch ( apiBaseUrl + '/empire_ethics' ) ) . json ( ) ;
2023-08-16 00:47:58 +02:00
empireEthics . forEach ( empireEthic = > {
2023-08-24 05:04:27 +02:00
const gameData = chellarisData . games [ empireEthic . empires_group_game_id ] ;
const ethic = chellarisData . ethics [ empireEthic . ethics_id ] ;
2023-08-16 00:47:58 +02:00
if ( typeof gameData !== "undefined" && typeof ethic !== "undefined" ) {
2023-08-24 05:04:27 +02:00
const empireData = gameData . empires [ empireEthic . empires_id ] ;
2023-08-16 00:47:58 +02:00
if ( typeof empireData !== "undefined" ) {
2023-08-24 05:04:27 +02:00
const tmpEthic : Ethic = { id : ethic.id , machine : ethic.machine , displayName : ethic.displayName , fanatic : empireEthic.ethics_fanatic } ;
2023-08-16 00:47:58 +02:00
if ( tmpEthic . machine ) {
empireData . machine = true ;
}
2023-08-24 05:04:27 +02:00
empireData . ethics [ empireEthic . ethics_id ] = tmpEthic ;
2023-08-16 00:47:58 +02:00
}
}
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 = > {
2023-08-24 05:04:27 +02:00
const newPortraitGroup = { id : portraitGroup.id , displayName : portraitGroup.name , portraits : [ ] } ;
chellarisData . species [ portraitGroup . id ] = newPortraitGroup ;
2023-08-21 00:48:23 +02:00
} ) ;
2023-08-24 05:04:27 +02:00
const portraits : {
id : number ,
2023-08-21 03:45:41 +02:00
hires : string ,
lores : string ,
2023-08-24 05:04:27 +02:00
group_id : number
2023-08-21 00:48:23 +02:00
} [ ] = await ( await fetch ( apiBaseUrl + '/portraits' ) ) . json ( ) ;
portraits . sort ( ( a , b ) = > ( a . id < b . id ? - 1 : 1 ) ) ;
portraits . forEach ( portrait = > {
2023-08-24 05:04:27 +02:00
const portraitGroupData = chellarisData . species [ portrait . group_id ] ;
2023-08-21 00:48:23 +02:00
if ( typeof portraitGroupData !== "undefined" ) {
2023-08-24 05:04:27 +02:00
const newPortraitData = { id : portrait.id , hires : portrait.hires , lores : portrait.lores } ;
2023-08-21 00:48:23 +02:00
2023-08-24 05:04:27 +02:00
portraitGroupData . portraits [ portrait . id ] = newPortraitData ;
2023-08-21 00:48:23 +02:00
}
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
2023-08-24 05:04:27 +02:00
let gameGroupSelections : Array < SelectedChellarisGroups > = [ ] ;
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' ) {
gameSelection = 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
gameGroupSelections [ gameSelection ] = { gameId : gameSelection , selectedGroups : chellarisData.games [ gameSelection ] . groups . map ( group = > group . id ) } ;
// 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' ) {
gameSelection = chellarisData . games [ 0 ] . id ;
}
SelectedGameStore . set ( gameSelection ) ;
if ( typeof gameGroupSelections [ gameSelection ] === 'undefined' ) {
// Default to all available groups
gameGroupSelections [ gameSelection ] = { gameId : gameSelection , selectedGroups : chellarisData.games [ gameSelection ] . groups . map ( group = > group . id ) } ;
}
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
}