diff --git a/app/page.tsx b/app/page.tsx index fc30591..40d41ca 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,12 +1,14 @@ -'use client'; import '@/app/globals.css'; +import { Game, GameGroup } from '@/types/stellaris'; +import { GameView } from '@/components/gui/game-view'; +import { generateUrl } from '@/components/server/fetchers'; + +export default async function Home() { + const games = await fetch(generateUrl('/games')).then((res) => res.json()) -export default function Home() { return ( - - Empire Overview goes here evenutally - + ) } diff --git a/components/gui/game-group-select.tsx b/components/gui/game-group-select.tsx new file mode 100644 index 0000000..c48986a --- /dev/null +++ b/components/gui/game-group-select.tsx @@ -0,0 +1,85 @@ +import { Game, GameGroup } from "@/types/stellaris"; +import { Dropdown } from "@nextui-org/react"; +import { Dispatch, Key, SetStateAction, Suspense } from "react"; +import { fetchGameGroups } from "@/components/server/fetchers"; + +type SelectionType = "all" | Set; + +export const GameGroupSelect = ( + props: { + game: Game, + groups: [GameGroup[], Dispatch>>], + currentGroups: [GameGroup[], Dispatch>>] + }) => { + const currentGame = props.game; + const [gameGroups, setGameGroups] = props.groups; + const [currentGameGroups, setCurrentGameGroups] = props.currentGroups; + + const changeSelection = (keys: SelectionType) => { + if (keys != "all") { + let newSelection: GameGroup[] = []; + keys.forEach(key => { + gameGroups.forEach(group => { + if (key == group.name) { + newSelection.push(group); + } + }) + }); + setCurrentGameGroups(newSelection) + } + } + + return ( + <> + + + + + + {currentGameGroups.map(elem => elem.name).join(", ")} + + elem.name)} + onSelectionChange={changeSelection} + items={gameGroups} + > + {gameGroups.map((item) => ( + + {item.name} + + ))} + + + > + ) +} + +const DataHandler = async ( + props: { + game: Game, + groups: [GameGroup[], Dispatch>>], + currentGroups: [GameGroup[], Dispatch>>] +}) => { + if (props) { + const currentGame = props.game; + const [gameGroups, setGameGroups] = props.groups; + const [currentGameGroups, setCurrentGameGroups] = props.currentGroups; + + if (!gameGroups[0] || gameGroups[0].game_id != currentGame.id) { + setGameGroups(await fetchGameGroups(currentGame)); + } + else if (!currentGameGroups[0] || currentGameGroups[0].game_id != gameGroups[0].game_id) { + setCurrentGameGroups(gameGroups); + } + + } + + return <>> +} \ No newline at end of file diff --git a/components/gui/game-select.tsx b/components/gui/game-select.tsx new file mode 100644 index 0000000..74c38d8 --- /dev/null +++ b/components/gui/game-select.tsx @@ -0,0 +1,43 @@ +import { Game } from "@/types/stellaris"; +import { Dropdown } from "@nextui-org/react"; +import { Dispatch, Key, SetStateAction, useState } from "react"; + +type SelectionType = "all" | Set; + +export const GameSelect = (props: { games: Game[], currentGame: Game | undefined, setCurrentGame: Dispatch> }) => { + const changeSelection = (keys: SelectionType) => { + if (keys != "all") { + props.games.forEach(game => { + if (game.name == keys.keys().next().value) [ + props.setCurrentGame(game) + ] + }) + } + } + + return ( + + + {props.currentGame ? props.currentGame.name : "-"} + + + {props.games.map((item) => ( + + {item.name} + + ))} + + + ) +} \ No newline at end of file diff --git a/components/gui/game-view.tsx b/components/gui/game-view.tsx new file mode 100644 index 0000000..fb4c1dd --- /dev/null +++ b/components/gui/game-view.tsx @@ -0,0 +1,22 @@ +'use client'; +import { Game } from "@/types/stellaris"; +import { GameSelect } from "./game-select"; +import { useState } from 'react'; +import { GameGroupSelect } from "./game-group-select"; +import { SSRProvider } from "react-bootstrap"; + +export const GameView = (props: { games: Game[] }) => { + const [currentGame, setCurrentGame] = useState(props.games[0]); + + const [gameGroups, setGameGroups] = useState([{id: 0, name: "", game_id: 0}]); + const [currentGameGroups, setCurrentGameGroups] = useState(gameGroups); + + return ( + <> + + + + + > + ) +} \ No newline at end of file diff --git a/components/server/fetchers.ts b/components/server/fetchers.ts new file mode 100644 index 0000000..443112f --- /dev/null +++ b/components/server/fetchers.ts @@ -0,0 +1,27 @@ +import { Game } from "@/types/stellaris" + +type Parameter = { + key: string, + val: string | number +} + +const baseUrl = 'http://stellaris.neshweb.net/api/v2' + +export const generateUrl = (url: string, params?: Parameter[]) => { + let query: string = baseUrl + url; + if (params && params.length > 0) { + query = query + '?'; + params.forEach((param, idx) => { + query = query + param.key + '=' + param.val; + if (idx + 1 != params.length) { + query = query + '&'; + } + }); + } + return query; +} + +export const fetchGameGroups = async (game: Game) => { + const gameGroups = await fetch(generateUrl('/game_groups', [{key: "game_id", val: game.id}])).then((res) => res.json()) + return gameGroups +} \ No newline at end of file diff --git a/types/stellaris.ts b/types/stellaris.ts index fcbaa95..b2a04de 100644 --- a/types/stellaris.ts +++ b/types/stellaris.ts @@ -64,3 +64,14 @@ export enum Species { Toxoid = 11, Machine = 12, } + +export type Game = { + id: number, + name: string +} + +export type GameGroup = { + id: number, + game_id: number, + name: string +}
Empire Overview goes here evenutally