Initial structure
This commit is contained in:
parent
ecb354cf6e
commit
874bd18f11
14 changed files with 157 additions and 1 deletions
5
src/api/config.json
Normal file
5
src/api/config.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"schema": "http",
|
||||
"url": "localhost",
|
||||
"port": 3012
|
||||
}
|
||||
12
src/api/game/game.ts
Normal file
12
src/api/game/game.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { callApi, HTTP } from "../tools";
|
||||
import { IGameTimeResponse } from "./types";
|
||||
|
||||
async function fetchTimeApi(): Promise<IGameTimeResponse> {
|
||||
const data = await callApi(HTTP.GET, "game/time");
|
||||
|
||||
return {
|
||||
time: data,
|
||||
};
|
||||
}
|
||||
|
||||
export { fetchTimeApi };
|
||||
5
src/api/game/types.ts
Normal file
5
src/api/game/types.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
interface IGameTimeResponse {
|
||||
time: number;
|
||||
}
|
||||
|
||||
export { type IGameTimeResponse };
|
||||
41
src/api/tools.ts
Normal file
41
src/api/tools.ts
Normal file
|
|
@ -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 };
|
||||
13
src/services/game/game.ts
Normal file
13
src/services/game/game.ts
Normal file
|
|
@ -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 };
|
||||
7
src/store/actions.ts
Normal file
7
src/store/actions.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { GameAction, GameActionTypes } from "./game";
|
||||
|
||||
type ActionTypes = GameActionTypes;
|
||||
|
||||
type Action = GameAction;
|
||||
|
||||
export { type Action, type ActionTypes };
|
||||
10
src/store/game/actions.ts
Normal file
10
src/store/game/actions.ts
Normal file
|
|
@ -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 };
|
||||
17
src/store/game/game.ts
Normal file
17
src/store/game/game.ts
Normal file
|
|
@ -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 };
|
||||
3
src/store/game/index.ts
Normal file
3
src/store/game/index.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export * from "./game";
|
||||
export * from "./actions";
|
||||
export * from "./types";
|
||||
5
src/store/game/types.ts
Normal file
5
src/store/game/types.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
interface IGameState {
|
||||
time: number;
|
||||
}
|
||||
|
||||
export { type IGameState };
|
||||
11
src/store/reducers.ts
Normal file
11
src/store/reducers.ts
Normal file
|
|
@ -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 };
|
||||
20
src/store/state.ts
Normal file
20
src/store/state.ts
Normal file
|
|
@ -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<IState>({
|
||||
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 };
|
||||
7
src/store/types.ts
Normal file
7
src/store/types.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { IGameState } from "./game";
|
||||
|
||||
interface IState {
|
||||
game: IGameState;
|
||||
}
|
||||
|
||||
export { type IState };
|
||||
Loading…
Add table
Add a link
Reference in a new issue