API Updates

This commit is contained in:
Neshura 2023-09-07 22:04:51 +02:00
parent 5499916e10
commit 00579db8ce
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
12 changed files with 197 additions and 94 deletions

View file

@ -1,4 +0,0 @@
[
"1234",
"5678"
]

View file

@ -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);
}

View file

@ -1,5 +1,5 @@
import { writable, type Writable } from "svelte/store";
const AdminSelectedGameStore: Writable<number> = writable(0);
const AdminSelectedGameStore: Writable<number> = writable(1);
export default AdminSelectedGameStore;

View file

@ -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;

View file

@ -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

View file

@ -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<number> = [0]
private data: Array<number> = [1]
constructor(data: Array<number>) {
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
}

View file

@ -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}
<button
class="list-card"
class:active={group.id == $AdminSelectedGroupStore[$AdminSelectedGameStore] ? 'active' : ''}
on:click={() => setActiveGroup(group.id)}
class:active={$AdminSelectedGroupStore[$AdminSelectedGameStore] ? $AdminSelectedGroupStore[$AdminSelectedGameStore][group.id] ? 'active' : '' : ''}
on:click={() => toggleActiveGroup(group.id)}
>
<div class="card-content">{group.name}</div>
{#if group.name !== 'N/A'}
@ -289,7 +339,16 @@
<div class="card-content" class:active={empire.id == $AdminSelectedEmpireStore[$AdminSelectedGameStore] ? 'active' : ''}>
{empire.discord_user}
</div>
<button class="delete-box" on:click={() => deleteEmpire(empire)}>
<svg class="checkmark" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path fill="none" d="M0 0 24 24 M24 0 0 24" />
</svg>
</button>
</button>
{:else}
<div class="list-card">
<div class="card-content">No Empires Present</div>
</div>
{/each}
<button class="list-card" on:click={addEmpire}>
<div class="card-content button">+</div>
@ -299,7 +358,11 @@
</div>
</List>
<List area="details" listTitle="Empire Details">
<EmpireDetails id={$AdminSelectedEmpireStore[$AdminSelectedGameStore]} />
{#if loadingEmpireData}
Loading...
{:else}
<EmpireDetails empire={empireData} />
{/if}
</List>
</div>

View file

@ -1,10 +1,29 @@
import { apiBaseUrl } from "$lib/components/consts";
import AdminSelectedEmpireStore from "$lib/stores/admin-page/EmpireStore";
import AdminSelectedGameStore from "$lib/stores/admin-page/GameStore";
import AuthTokenStore from "$lib/stores/AuthTokenStore";
import type { ChellarisGameInfo } from "$lib/types/chellaris";
import { redirect } from "@sveltejs/kit";
import AdminSelectedGroupStore from '../../lib/stores/admin-page/GroupStore';
export async function load ({ fetch }) {
let authToken = "";
AuthTokenStore.subscribe(token => {
authToken = token;
});
const auth = await (await fetch(apiBaseUrl + "/v3/auth",{
headers: {
'Content-Type': 'application/json',
'x-api-key': authToken
}
})).json();
if (!auth.admin && !auth.moderator) {
throw redirect(303, '/401');
}
const gameList: { [key: number]: ChellarisGameInfo } = await (await fetch(apiBaseUrl + "/v3/games")).json();
let store: string | null;

View file

@ -1,11 +1,49 @@
<script lang="ts">
export let id: number;
import type { ChellarisEmpire } from '$lib/types/chellaris';
export let empire: ChellarisEmpire;
</script>
<div>
{id}
{#if empire}
<div>
ID: {empire.id}
</div>
<div>
Group ID: {empire.group}
</div>
<div>
Game ID: {empire.game}
</div>
<div>
Gestalt: {empire.gestalt}
</div>
<div>
Machine: {empire.machine}
</div>
<div>
Portrait ID: {empire.portrait_id}
</div>
<div>
Portrait Group ID: {empire.portrait_group_id}
</div>
<div>
Name: {empire.name}
</div>
<div>
Discord User: {empire.discord_user}
</div>
<div>
{#if empire.ethics}
{#each Object.values(empire.ethics) as ethic}
{ethic.ethic_id},
{/each}
{/if}
</div>
{:else}
No Empire Selected
{/if}
</div>
<style>
</style>
</style>

View file

@ -2,14 +2,12 @@ import ChellarisDataStore from '$lib/stores/ChellarisData';
import SelectedGameStore from '$lib/stores/GameFilter';
import SelectedGameGroupsStore, { type SelectedChellarisGroups } from "$lib/stores/GameGroupFilter";
import GraphsTabStore from '$lib/stores/GraphsTab';
import { createChellarisInfo, type ChellarisGame, createChellarisGame, createChellarisGameGroup, createChellarisEmpire } from "$lib/types/chellaris";
import type { LayoutLoad } from "./$types";
import type { Ethic } from '../../lib/types/stellaris';
import type { ChellarisInfo } from '../../lib/types/chellaris';
import { apiBaseUrl } from '$lib/components/consts';
export const load: LayoutLoad = async ({ fetch }) => {
let store: string | null;
const apiBaseUrl = 'https://wip.chellaris.net/api';
// Chellaris Data Code
const chellarisData: ChellarisInfo = await (await fetch(apiBaseUrl + '/v3/full_view_data')).json();
@ -35,7 +33,7 @@ export const load: LayoutLoad = async ({ fetch }) => {
}
if (typeof gameSelection === 'undefined') {
gameSelection = chellarisData.games[0].id;
gameSelection = chellarisData.games[1].id;
}
// Game Groups Selection
@ -68,7 +66,7 @@ export const load: LayoutLoad = async ({ fetch }) => {
}
if (typeof gameSelection === 'undefined') {
gameSelection = chellarisData.games[0].id;
gameSelection = chellarisData.games[1].id;
}
SelectedGameStore.set(gameSelection);

View file

@ -32,7 +32,7 @@
[key: number]: {
id: number;
machine: boolean;
displayName: string;
display: string;
regular: number;
fanatic: number;
};
@ -40,8 +40,6 @@
takenPortraits: Array<Array<[number, Array<number>]>>; // <SpeciesGroup, <Portrait, <SumTaken, <GroupsTaken>>>>
};
$: {
console.log('Reran'); // DEBUG
let newPageData: {
init: boolean;
empireCount: number;
@ -50,7 +48,7 @@
[key: number]: {
id: number;
machine: boolean;
displayName: string;
display: string;
regular: number;
fanatic: number;
};
@ -70,13 +68,13 @@
const newEthicsData: {
id: number;
machine: boolean;
displayName: string;
display: string;
regular: number;
fanatic: number;
} = {
id: ethic.id,
machine: ethic.machine,
displayName: ethic.displayName,
display: ethic.display,
regular: 0,
fanatic: 0
};
@ -84,6 +82,7 @@
});
Object.values(selectedGame.empires).forEach((empire) => {
console.log(empire); //DEBUG
if (empire.group in selectedGameGroupData.selectedGroups) {
newPageData.empireCount = newPageData.empireCount + 1;
@ -103,28 +102,28 @@
}
Object.values(empire.ethics).forEach((ethic) => {
const tmpEthicPageData = newPageData.ethicsData[ethic.id];
const tmpEthicPageData = newPageData.ethicsData[ethic.ethic_id];
if (typeof tmpEthicPageData !== 'undefined') {
tmpEthicPageData.displayName = ethic.displayName;
tmpEthicPageData.display = ethic.display;
if (!ethic.fanatic) {
tmpEthicPageData.regular = tmpEthicPageData.regular + 1;
} else {
tmpEthicPageData.fanatic = tmpEthicPageData.fanatic + 1;
}
newPageData.ethicsData[ethic.id] = tmpEthicPageData;
newPageData.ethicsData[ethic.ethic_id] = tmpEthicPageData;
} else {
let newEthicsData: {
id: number;
machine: boolean;
displayName: string;
display: string;
regular: number;
fanatic: number;
} = {
id: ethic.id,
id: ethic.ethic_id,
machine: ethic.machine,
displayName: ethic.displayName,
display: ethic.display,
regular: 0,
fanatic: 0
};
@ -135,27 +134,27 @@
newEthicsData.fanatic = 1;
}
newPageData.ethicsData[ethic.id] = newEthicsData;
newPageData.ethicsData[ethic.ethic_id] = newEthicsData;
}
});
if (typeof newPageData.takenPortraits[empire.empire_portrait_group] === 'undefined') {
newPageData.takenPortraits[empire.empire_portrait_group] = [];
if (typeof newPageData.takenPortraits[empire.portrait_group_id] === 'undefined') {
newPageData.takenPortraits[empire.portrait_group_id] = [];
}
const portraitGroupData = newPageData.takenPortraits[empire.empire_portrait_group];
const portraitGroupData = newPageData.takenPortraits[empire.portrait_group_id];
if (typeof portraitGroupData !== 'undefined') {
if (typeof portraitGroupData[empire.empire_portrait] === 'undefined') {
portraitGroupData[empire.empire_portrait] = [0, []];
if (typeof portraitGroupData[empire.portrait_id] === 'undefined') {
portraitGroupData[empire.portrait_id] = [0, []];
}
let portraitData = portraitGroupData[empire.empire_portrait];
let portraitData = portraitGroupData[empire.portrait_id];
if (typeof portraitData !== 'undefined') {
portraitData[0] = portraitData[0] + 1;
portraitData[1].push(empire.group);
portraitGroupData[empire.empire_portrait] = portraitData;
newPageData.takenPortraits[empire.empire_portrait_group] = portraitGroupData;
portraitGroupData[empire.portrait_id] = portraitData;
newPageData.takenPortraits[empire.portrait_group_id] = portraitGroupData;
}
}
}
@ -178,6 +177,7 @@
}
pageData = newPageData;
console.log(pageData);
}
// Save Tab to Store
@ -212,7 +212,7 @@
{#each Object.values(pageData.ethicsData) as ethicData}
{#if ethicData && !ethicData.machine}
<tr>
<td class="table-label">{ethicData.displayName}</td>
<td class="table-label">{ethicData.display}</td>
<td>{ethicData.regular}</td>
<td>{ethicData.fanatic}</td>
</tr>
@ -232,7 +232,7 @@
{#each Object.values(pageData.ethicsData) as ethicData}
{#if ethicData && ethicData.machine}
<tr>
<td class="table-label">{ethicData.displayName}</td>
<td class="table-label">{ethicData.display}</td>
<td>{ethicData.regular}</td>
</tr>
{/if}
@ -252,12 +252,12 @@
{#each Object.values($ChellarisDataStore.species) ?? false as portraitGroup}
{#if portraitGroup}
<tr>
<td>{portraitGroup.displayName}</td>
<td>{portraitGroup.display}</td>
{#each Object.values(portraitGroup.portraits) ?? false as portrait}
{#if portrait}
<td
class="image-box"
style="--color-hovered-portrait: {groupPortraitLimit - pageData.takenPortraits[portraitGroup.id][portrait.id][0] == 0
style="--color-hovered-portrait: {groupPortraitLimit - pageData.takenPortraits[portraitGroup.id][portrait.id][0] <= 0
? 'var(--color-warn-2)'
: groupPortraitLimit - pageData.takenPortraits[portraitGroup.id][portrait.id][0] != groupPortraitLimit
? 'var(--color-warn-1)'
@ -267,7 +267,7 @@
id={portraitGroup.id + '-' + portrait.id}
type="image"
src={'../' + portrait.lores}
alt={portraitGroup.displayName + '-' + portrait.id}
alt={portraitGroup.display + '-' + portrait.id}
/>
<label for={portraitGroup.id + '-' + portrait.id}>{pageData.takenPortraits[portraitGroup.id][portrait.id][0]}/{groupPortraitLimit}</label>
</td>

View file

@ -1,14 +1,14 @@
import { apiBaseUrl } from "$lib/components/consts";
import type { PageLoad } from "../$types";
export const load: PageLoad = async ({ fetch }) => {
const apiBaseUrl = 'https://www.chellaris.net/api/v1';
const popsRet: { speciesArray: Array<number> } = await (await fetch(apiBaseUrl + '/species')).json();
const popsRet: { speciesArray: Array<number> } = await (await fetch(apiBaseUrl + '/v1/species')).json();
const popsData: Array<number> = popsRet.speciesArray;
const ethicsRet: { sheetData: Array<Array<number>> } = await (await fetch(apiBaseUrl + '/ethics')).json();
const ethicsRet: { sheetData: Array<Array<number>> } = await (await fetch(apiBaseUrl + '/v1/ethics')).json();
const ethicsData: Array<Array<number>> = ethicsRet.sheetData;
const empireRet: { empireCount: number } = await (await fetch(apiBaseUrl + '/empires')).json();
const empireRet: { empireCount: number } = await (await fetch(apiBaseUrl + '/v1/empires')).json();
const empireData: number = empireRet.empireCount;
return { popsData, ethicsData, empireData }