138 lines
3.2 KiB
TypeScript
138 lines
3.2 KiB
TypeScript
import { Module, ActionTree, MutationTree, GetterTree } from "vuex";
|
|
import { AppState } from "@/store/index";
|
|
import { Room } from "./room";
|
|
import RoomClient from "@/roomclient";
|
|
|
|
const namespaced: boolean = true;
|
|
|
|
export interface ServerState {
|
|
server: string;
|
|
joining: boolean;
|
|
connecting: boolean;
|
|
connected: boolean;
|
|
rooms: Room[] | null;
|
|
connectionError: string;
|
|
joinError: string;
|
|
}
|
|
|
|
const state: ServerState = {
|
|
connecting: false,
|
|
joining: false,
|
|
connected: false,
|
|
server: "",
|
|
rooms: null,
|
|
connectionError: "",
|
|
joinError: ""
|
|
};
|
|
|
|
const mutations: MutationTree<ServerState> = {
|
|
beginConnection(state: ServerState) {
|
|
state.connected = false;
|
|
state.connectionError = "";
|
|
state.connecting = true;
|
|
},
|
|
|
|
beginJoin(state: ServerState) {
|
|
state.joining = true;
|
|
},
|
|
|
|
connectionDone(
|
|
state: ServerState,
|
|
payload: { addr: string; rooms: Room[] }
|
|
) {
|
|
state.connected = true;
|
|
state.server = payload.addr;
|
|
state.rooms = payload.rooms;
|
|
},
|
|
|
|
connectionFailed(state: ServerState, err: Error) {
|
|
state.connecting = false;
|
|
state.connectionError = err.message;
|
|
},
|
|
|
|
joinFailed(state: ServerState, err: Error) {
|
|
state.joining = false;
|
|
state.joinError = err.message;
|
|
}
|
|
};
|
|
|
|
const actions: ActionTree<ServerState, AppState> = {
|
|
async connect({ 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.rooms });
|
|
} catch (err) {
|
|
commit("connectionFailed", err);
|
|
}
|
|
},
|
|
|
|
async joinRoom(
|
|
{ state, commit, dispatch, rootState },
|
|
{ roomid, as_spectator }
|
|
) {
|
|
commit("beginJoin");
|
|
// Try joining room
|
|
try {
|
|
// Ask lobby server for permission
|
|
let req = await fetch(`${state.server}/api/lobby/room/join`, {
|
|
method: "POST",
|
|
headers: {
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({
|
|
room_id: roomid,
|
|
player_name: rootState.playerName,
|
|
as_player: !as_spectator
|
|
})
|
|
});
|
|
let data = await req.json();
|
|
// We haz permission, let's contact the room server
|
|
let ws = await RoomClient.connect(data);
|
|
dispatch("room/setClient", ws, { root: true });
|
|
} catch (err) {
|
|
commit("joinFailed", err);
|
|
}
|
|
},
|
|
|
|
async createRoom({ state, commit, dispatch, rootState }) {
|
|
commit("beginJoin");
|
|
// Try creating room
|
|
try {
|
|
// Ask lobby server for permission
|
|
let req = await fetch(`${state.server}/api/lobby/room/create`, {
|
|
method: "POST",
|
|
headers: {
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({
|
|
game_id: "webclient",
|
|
player_name: rootState.playerName,
|
|
name: `${rootState.playerName}'s room`,
|
|
allow_spectators: true,
|
|
max_players: 32,
|
|
max_spectators: 32,
|
|
tags: ["test"]
|
|
})
|
|
});
|
|
let data = await req.json();
|
|
// We haz permission, let's contact the room server
|
|
let ws = await RoomClient.connect(data);
|
|
dispatch("room/setClient", ws, { root: true });
|
|
} catch (err) {
|
|
commit("joinFailed", err);
|
|
}
|
|
}
|
|
};
|
|
|
|
export const server: Module<ServerState, AppState> = {
|
|
namespaced,
|
|
state,
|
|
actions,
|
|
mutations
|
|
};
|