64 lines
1.3 KiB
TypeScript
64 lines
1.3 KiB
TypeScript
import { Module, ActionTree, MutationTree, GetterTree } from "vuex";
|
|
import { AppState } from "@/store/index";
|
|
|
|
const namespaced: boolean = true;
|
|
|
|
export interface ServerState {
|
|
server: string;
|
|
connecting: boolean;
|
|
connected: boolean;
|
|
rooms: Object | null;
|
|
connectionError: string;
|
|
}
|
|
|
|
const state: ServerState = {
|
|
connecting: false,
|
|
connected: false,
|
|
server: "",
|
|
rooms: null,
|
|
connectionError: ""
|
|
};
|
|
|
|
const mutations: MutationTree<ServerState> = {
|
|
beginConnection(state: ServerState) {
|
|
state.connected = false;
|
|
state.connectionError = "";
|
|
state.connecting = true;
|
|
},
|
|
|
|
connectionDone(
|
|
state: ServerState,
|
|
payload: { addr: string; rooms: Object }
|
|
) {
|
|
state.connected = true;
|
|
state.server = payload.addr;
|
|
state.rooms = payload.rooms;
|
|
},
|
|
|
|
connectionFailed(state: ServerState, err: Error) {
|
|
state.connecting = false;
|
|
state.connectionError = err.message;
|
|
}
|
|
};
|
|
|
|
const actions: ActionTree<ServerState, AppState> = {
|
|
connect: async ({ commit }, addr: string) => {
|
|
commit("beginConnection");
|
|
// Get room list
|
|
try {
|
|
let req = await fetch(`${addr}/api/lobby/room/list`);
|
|
let data = await req.json();
|
|
commit("connectionDone", { addr, rooms: data });
|
|
} catch (err) {
|
|
commit("connectionFailed", err);
|
|
}
|
|
}
|
|
};
|
|
|
|
export const server: Module<ServerState, AppState> = {
|
|
namespaced,
|
|
state,
|
|
actions,
|
|
mutations
|
|
};
|