diff --git a/src/authTokens.json b/src/authTokens.json deleted file mode 100644 index 60e857b..0000000 --- a/src/authTokens.json +++ /dev/null @@ -1,4 +0,0 @@ -[ - "1234", - "5678" -] \ No newline at end of file diff --git a/src/hooks.server.ts b/src/hooks.server.ts deleted file mode 100644 index c18e2c2..0000000 --- a/src/hooks.server.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { redirect } from "@sveltejs/kit"; -import validAuthTokens from './authTokens.json'; - -export async function handle({ event, resolve }) { - const cookie = event.cookies.get('authToken'); - if (typeof cookie !== 'undefined') { - event.locals.authenticated = validAuthTokens.includes(cookie); - } - else { - event.locals.authenticated = false; - } - - - if (event.url.pathname.startsWith('/admin')) { - if (!event.locals.authenticated) { - throw redirect(303, '/401'); - } - } - - return await resolve(event); -} \ No newline at end of file diff --git a/src/lib/stores/admin-page/GameStore.ts b/src/lib/stores/admin-page/GameStore.ts index 4b277de..76e92b8 100644 --- a/src/lib/stores/admin-page/GameStore.ts +++ b/src/lib/stores/admin-page/GameStore.ts @@ -1,5 +1,5 @@ import { writable, type Writable } from "svelte/store"; -const AdminSelectedGameStore: Writable = writable(0); +const AdminSelectedGameStore: Writable = writable(1); export default AdminSelectedGameStore; \ No newline at end of file diff --git a/src/lib/stores/admin-page/GroupStore.ts b/src/lib/stores/admin-page/GroupStore.ts index 44a2f56..6ee3ff0 100644 --- a/src/lib/stores/admin-page/GroupStore.ts +++ b/src/lib/stores/admin-page/GroupStore.ts @@ -1,5 +1,5 @@ import { writable, type Writable } from "svelte/store"; -const AdminSelectedGroupStore: Writable<{ [key: number]: number }> = writable({}); +const AdminSelectedGroupStore: Writable<{ [key: number]: { [key: number]: boolean } }> = writable({}); export default AdminSelectedGroupStore; \ No newline at end of file diff --git a/src/lib/types/chellaris.ts b/src/lib/types/chellaris.ts index f28f6ef..eef408c 100644 --- a/src/lib/types/chellaris.ts +++ b/src/lib/types/chellaris.ts @@ -1,4 +1,4 @@ -import type { Ethic, Species } from './stellaris'; +import type { EmpireEthic, Ethic, Species } from './stellaris'; export type ChellarisInfo = { games: { [key: number]: ChellarisGame }, // Key is Game Id @@ -20,14 +20,15 @@ export type ChellarisGameGroup = { export type ChellarisEmpire = { id: number, - gestalt: boolean, - machine: boolean, group: number, - empire_portrait: number, // TODO replace with Enum? - empire_portrait_group: number, // TODO replace with Enum? + game: number, name: string, discord_user?: string, - ethics: { [key: number]: Ethic }, + gestalt: boolean, + machine: boolean, + portrait_id: number, // TODO replace with Enum? + portrait_group_id: number, // TODO replace with Enum? + ethics: { [key: number]: EmpireEthic }, } export type ChellarisGameInfo = { @@ -47,7 +48,7 @@ export const createChellarisInfo = (): ChellarisInfo => { export const createChellarisGame = (): ChellarisGame => { const newChellarisGame = { - id: 0, + id: 1, name: "", groups: [], empires: [], @@ -58,7 +59,7 @@ export const createChellarisGame = (): ChellarisGame => { export const createChellarisGameGroup = (): ChellarisGameGroup => { const newChellarisGameGroup = { - id: 0, + id: 1, name: "", }; @@ -67,23 +68,25 @@ export const createChellarisGameGroup = (): ChellarisGameGroup => { export const createChellarisEmpire = ( { - id, name, discord_user, group_id, gestalt, empire_portrait_id, empire_portrait_group_id + id, name, discord_user, group_id, game_id, gestalt, portrait_id, portrait_group_id }: { id: number, name: string, discord_user?: string, group_id: number, + game_id: number, gestalt: boolean, - empire_portrait_id: number, - empire_portrait_group_id: number, + portrait_id: number, + portrait_group_id: number, }): ChellarisEmpire => { const newChellarisEmpire = { id: id, + group: group_id, + game: game_id, gestalt: gestalt, machine: false, - group: group_id, - empire_portrait: empire_portrait_id, - empire_portrait_group: empire_portrait_group_id, + portrait_id: portrait_id, + portrait_group_id: portrait_group_id, ethics: [], discord_user: discord_user, name: name diff --git a/src/lib/types/stellaris.ts b/src/lib/types/stellaris.ts index 1b38988..1f4ac1a 100644 --- a/src/lib/types/stellaris.ts +++ b/src/lib/types/stellaris.ts @@ -38,7 +38,7 @@ export enum LegacyEthicsScale { export class EthicsDataLegacy { // Array index determines fanatic or regular, Index value represents number of occurences - private data: Array = [0] + private data: Array = [1] constructor(data: Array) { this.data[LegacyEthicsScale.normal] = data[0], @@ -66,14 +66,21 @@ export class EthicsDataLegacy { export type Ethic = { id: number, - displayName: string, + display: string, + machine: boolean, + fanatic?: boolean, +} + +export type EmpireEthic = { + ethic_id: number, + display: string, machine: boolean, fanatic?: boolean, } export type Species = { id: number, - displayName: string, + display: string, portraits: { [key: number]: Portrait }, // Key is Portrait Id } diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte index 8e83e9c..ff6c8ad 100644 --- a/src/routes/admin/+page.svelte +++ b/src/routes/admin/+page.svelte @@ -31,6 +31,9 @@ // Empire Variables let empireList: { [key: number]: ChellarisEmpire } = {}; + let empireData: ChellarisEmpire; + + let loadingEmpireData = true; const updateGameData = () => { fetch(apiBaseUrl + '/v3/game?game_id=' + $AdminSelectedGameStore, { @@ -39,15 +42,39 @@ 'x-api-key': $AuthTokenStore } }).then((res) => { - res.json().then((data) => { - console.log(data); + res.json().then((data: {groups: { [key: number]: ChellarisGameGroup}, empires: { [key: number]: ChellarisEmpire}}) => { groupList = data.groups; - empireList = data.empires; + empireList = {}; + Object.values(data.empires).forEach((empire: ChellarisEmpire) => { + if ($AdminSelectedGroupStore[$AdminSelectedGameStore] === undefined) { + $AdminSelectedGroupStore[$AdminSelectedGameStore] = {}; + } + if ($AdminSelectedGroupStore[$AdminSelectedGameStore][empire.group]) { + empireList[empire.id] = empire; + } + }); + loadingGameData = false; + loadEmpireData(); }); }); }; + const loadEmpireData = () => { + fetch(apiBaseUrl + '/v3/empire?game_id=' + $AdminSelectedGameStore + '&empire_id=' + $AdminSelectedEmpireStore[$AdminSelectedGameStore], { + headers: { + 'Content-Type': 'application/json', + 'x-api-key': $AuthTokenStore + } + }).then((res) => { + res.json().then((data: ChellarisEmpire) => { + empireData = data; + + loadingEmpireData = false; + }); + }); + } + $: { if (typeof localStorage !== 'undefined') { localStorage.setItem('adminGameSelection', JSON.stringify($AdminSelectedGameStore)); @@ -123,8 +150,12 @@ // Group Functions - const setActiveGroup = (group: number) => { - $AdminSelectedGroupStore[$AdminSelectedGameStore] = group; + const toggleActiveGroup = (group: number) => { + if ($AdminSelectedGroupStore[$AdminSelectedGameStore] === undefined) { + $AdminSelectedGroupStore[$AdminSelectedGameStore] = {}; + } + $AdminSelectedGroupStore[$AdminSelectedGameStore][group] = !$AdminSelectedGroupStore[$AdminSelectedGameStore][group]; + updateGameData(); }; const addGroup = () => { @@ -147,7 +178,7 @@ updateGameData(); addingNewGroup = false; newGroupName = ''; - response.json().then((result) => ($AdminSelectedGroupStore[$AdminSelectedGameStore] = result.id)); + response.json().then((result) => ($AdminSelectedGroupStore[$AdminSelectedGameStore][result.id] = true)); }); }; @@ -160,8 +191,8 @@ } }).then(() => { updateGameData(); - if ($AdminSelectedGroupStore[$AdminSelectedGameStore] == group_id) { - $AdminSelectedGroupStore[$AdminSelectedGameStore] = Object.values(groupList)[0].id; + if ($AdminSelectedGroupStore[$AdminSelectedGameStore][group_id]) { + $AdminSelectedGroupStore[$AdminSelectedGameStore][group_id] = false; } }); delete groupList[group_id]; @@ -171,6 +202,8 @@ // Empire Functions const setActiveEmpire = (empireId: number) => { + loadingEmpireData = true; + loadEmpireData(); $AdminSelectedEmpireStore[$AdminSelectedGameStore] = empireId; }; @@ -179,6 +212,23 @@ list2.push(list2.length); }; + const deleteEmpire = (empire: ChellarisEmpire) => { + fetch(apiBaseUrl + '/v3/empire?game_id=' + empire.game + '&group_id=' + empire.group + '&empire_id=' + empire.id, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + 'x-api-key': $AuthTokenStore + } + }).then(() => { + updateGameData(); + if ($AdminSelectedEmpireStore[$AdminSelectedGameStore] == empire.id) { + $AdminSelectedEmpireStore[$AdminSelectedGameStore] = Object.values(empireList)[0].id; + } + }); + delete empireList[empire.id]; + empireList = empireList; + }; + let list2 = new Array(); for (let i = 0; i < 100; i++) { list2.push(i); @@ -231,8 +281,8 @@ {#each Object.values(groupList) as group} + {:else} +
+
No Empires Present
+
{/each}