From d0ecfde2b00d0ee7e7d1aa1ba24bb9311c7f212d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=AEittaG=20ordnasselA?= Date: Mon, 10 Jun 2019 18:15:29 +0200 Subject: [PATCH] Add room creation --- webclient/src/components/RoomList.vue | 4 ++++ webclient/src/components/RoomLog.vue | 23 ++++++++++++++++++-- webclient/src/store/room.ts | 14 +++++++++++-- webclient/src/store/server.ts | 30 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/webclient/src/components/RoomList.vue b/webclient/src/components/RoomList.vue index a793f42..cd8d715 100644 --- a/webclient/src/components/RoomList.vue +++ b/webclient/src/components/RoomList.vue @@ -1,5 +1,8 @@ diff --git a/webclient/src/store/room.ts b/webclient/src/store/room.ts index e255189..a8236a7 100644 --- a/webclient/src/store/room.ts +++ b/webclient/src/store/room.ts @@ -1,4 +1,4 @@ -import { Module, MutationTree, ActionTree } from "vuex"; +import { Module, MutationTree, ActionTree, GetterTree } from "vuex"; import { AppState } from "@/store/index"; import RoomClient from "@/roomclient"; @@ -75,9 +75,19 @@ const actions: ActionTree = { } }; +const getters: GetterTree = { + users(state: RoomState): string[] { + if (!state.room) { + return []; + } + return [...state.room.players, ...state.room.spectators]; + } +}; + export const room: Module = { namespaced, state, mutations, - actions + actions, + getters }; diff --git a/webclient/src/store/server.ts b/webclient/src/store/server.ts index 8309977..f7ee254 100644 --- a/webclient/src/store/server.ts +++ b/webclient/src/store/server.ts @@ -96,6 +96,36 @@ const actions: ActionTree = { } 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); + } } };