From 874bd18f11ddcd896861d66f9a64e75bd8dcae58f9d6cfae4d4ef39e87f8e5d1 Mon Sep 17 00:00:00 2001 From: aslan Date: Tue, 30 Dec 2025 16:57:07 +0100 Subject: [PATCH] Initial structure --- package.json | 2 +- src/api/config.json | 5 +++++ src/api/game/game.ts | 12 ++++++++++++ src/api/game/types.ts | 5 +++++ src/api/tools.ts | 41 +++++++++++++++++++++++++++++++++++++++ src/services/game/game.ts | 13 +++++++++++++ src/store/actions.ts | 7 +++++++ src/store/game/actions.ts | 10 ++++++++++ src/store/game/game.ts | 17 ++++++++++++++++ src/store/game/index.ts | 3 +++ src/store/game/types.ts | 5 +++++ src/store/reducers.ts | 11 +++++++++++ src/store/state.ts | 20 +++++++++++++++++++ src/store/types.ts | 7 +++++++ 14 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/api/config.json create mode 100644 src/api/game/game.ts create mode 100644 src/api/game/types.ts create mode 100644 src/api/tools.ts create mode 100644 src/services/game/game.ts create mode 100644 src/store/actions.ts create mode 100644 src/store/game/actions.ts create mode 100644 src/store/game/game.ts create mode 100644 src/store/game/index.ts create mode 100644 src/store/game/types.ts create mode 100644 src/store/reducers.ts create mode 100644 src/store/state.ts create mode 100644 src/store/types.ts diff --git a/package.json b/package.json index ffa7df8..ecf1e13 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "vite-template-solid", + "name": "pulsar-web", "version": "0.1.0", "description": "", "type": "module", diff --git a/src/api/config.json b/src/api/config.json new file mode 100644 index 0000000..336a8c3 --- /dev/null +++ b/src/api/config.json @@ -0,0 +1,5 @@ +{ + "schema": "http", + "url": "localhost", + "port": 3012 +} diff --git a/src/api/game/game.ts b/src/api/game/game.ts new file mode 100644 index 0000000..4256270 --- /dev/null +++ b/src/api/game/game.ts @@ -0,0 +1,12 @@ +import { callApi, HTTP } from "../tools"; +import { IGameTimeResponse } from "./types"; + +async function fetchTimeApi(): Promise { + const data = await callApi(HTTP.GET, "game/time"); + + return { + time: data, + }; +} + +export { fetchTimeApi }; diff --git a/src/api/game/types.ts b/src/api/game/types.ts new file mode 100644 index 0000000..29b2b91 --- /dev/null +++ b/src/api/game/types.ts @@ -0,0 +1,5 @@ +interface IGameTimeResponse { + time: number; +} + +export { type IGameTimeResponse }; diff --git a/src/api/tools.ts b/src/api/tools.ts new file mode 100644 index 0000000..c02c772 --- /dev/null +++ b/src/api/tools.ts @@ -0,0 +1,41 @@ +import config from "./config.json"; + +enum HTTP { + GET, + POST, +} + +async function callApi(type: HTTP, path: string, body?: any) { + let response: Response; + switch (type) { + case HTTP.GET: + response = await fetch( + `${config.schema}://${config.url}:${config.port}/${path}`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }, + ); + break; + case HTTP.POST: + response = await fetch( + `${config.schema}://${config.url}:${config.port}/${path}`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(body ?? {}), + }, + ); + break; + } + + const data = await response.json(); + + return data; +} + +export { callApi, HTTP }; diff --git a/src/services/game/game.ts b/src/services/game/game.ts new file mode 100644 index 0000000..467733f --- /dev/null +++ b/src/services/game/game.ts @@ -0,0 +1,13 @@ +import { fetchTimeApi } from "../../api/game/game"; +import { dispatch } from "../../store/state"; + +async function fetchTime() { + const data = await fetchTimeApi(); + + dispatch({ + type: "FETCH_TIME_FINISH", + payload: data.time, + }); +} + +export { fetchTime }; diff --git a/src/store/actions.ts b/src/store/actions.ts new file mode 100644 index 0000000..edd7909 --- /dev/null +++ b/src/store/actions.ts @@ -0,0 +1,7 @@ +import { GameAction, GameActionTypes } from "./game"; + +type ActionTypes = GameActionTypes; + +type Action = GameAction; + +export { type Action, type ActionTypes }; diff --git a/src/store/game/actions.ts b/src/store/game/actions.ts new file mode 100644 index 0000000..7f276e9 --- /dev/null +++ b/src/store/game/actions.ts @@ -0,0 +1,10 @@ +type FETCH_TIME_START = "FETCH_TIME_START"; +type FETCH_TIME_FINISH = "FETCH_TIME_FINISH"; + +type GameActionTypes = FETCH_TIME_START | FETCH_TIME_FINISH; + +type GameAction = + | { type: FETCH_TIME_START } + | { type: FETCH_TIME_FINISH; payload: number }; + +export { type GameActionTypes, type GameAction }; diff --git a/src/store/game/game.ts b/src/store/game/game.ts new file mode 100644 index 0000000..712e304 --- /dev/null +++ b/src/store/game/game.ts @@ -0,0 +1,17 @@ +import { fetchTime } from "../../services/game/game"; +import { GameAction } from "./actions"; +import { IGameState } from "./types"; + +function gameReducer(state: IGameState, action: GameAction): IGameState { + switch (action.type) { + case "FETCH_TIME_START": + fetchTime(); + return { ...state }; + case "FETCH_TIME_FINISH": + return { ...state, time: action.payload }; + default: + return state; + } +} + +export { gameReducer }; diff --git a/src/store/game/index.ts b/src/store/game/index.ts new file mode 100644 index 0000000..09e9caa --- /dev/null +++ b/src/store/game/index.ts @@ -0,0 +1,3 @@ +export * from "./game"; +export * from "./actions"; +export * from "./types"; diff --git a/src/store/game/types.ts b/src/store/game/types.ts new file mode 100644 index 0000000..0777613 --- /dev/null +++ b/src/store/game/types.ts @@ -0,0 +1,5 @@ +interface IGameState { + time: number; +} + +export { type IGameState }; diff --git a/src/store/reducers.ts b/src/store/reducers.ts new file mode 100644 index 0000000..991f069 --- /dev/null +++ b/src/store/reducers.ts @@ -0,0 +1,11 @@ +import { IState } from "./types"; +import { Action } from "./actions"; +import { GameAction, gameReducer } from "./game"; + +function reducer(state: IState, action: Action): IState { + return { + game: gameReducer(state.game, action as GameAction), + }; +} + +export { reducer }; diff --git a/src/store/state.ts b/src/store/state.ts new file mode 100644 index 0000000..f09b65c --- /dev/null +++ b/src/store/state.ts @@ -0,0 +1,20 @@ +import { createStore } from "solid-js/store"; +import { IState } from "./types"; +import { Action } from "./actions"; +import { reducer } from "./reducers"; + +const [state, setState] = createStore({ + game: { + time: 0, + }, +}); + +function dispatch(action: Action) { + setState(reducer(state, action)); +} + +async function dispatchAsync(action: Action) { + setState(reducer(state, action)); +} + +export { state, setState, dispatch, dispatchAsync }; diff --git a/src/store/types.ts b/src/store/types.ts new file mode 100644 index 0000000..2953136 --- /dev/null +++ b/src/store/types.ts @@ -0,0 +1,7 @@ +import { IGameState } from "./game"; + +interface IState { + game: IGameState; +} + +export { type IState };