Sync player list and player ID with vuex
Some checks failed
continuous-integration/drone/pr Build is failing
continuous-integration/drone/push Build is failing

This commit is contained in:
Hamcha 2019-10-07 12:23:17 +02:00
parent 6b34fe3dfa
commit 69f9cb6843
Signed by: hamcha
GPG key ID: 44AD3571EB09A39E
7 changed files with 57 additions and 12 deletions

View file

@ -23,6 +23,7 @@ export abstract class Client extends EventEmitter {
case "room-info": case "room-info":
this.roomInfo = data.room; this.roomInfo = data.room;
this.players = data.players; this.players = data.players;
this.emit("handshake");
break; break;
// Someone changed name (or was forced to) // Someone changed name (or was forced to)
case "rename": case "rename":

View file

@ -1,4 +1,4 @@
import { ActionTree } from "vuex"; import { ActionTree, Commit } from "vuex";
import { AppState } from "../types"; import { AppState } from "../types";
import { NetworkState, StartServerOptions, ConnectOptions } from "./types"; import { NetworkState, StartServerOptions, ConnectOptions } from "./types";
import { import {
@ -10,10 +10,23 @@ import {
ChatMessage ChatMessage
} from "@/network"; } from "@/network";
function bindClientEvents(commit: Commit, client: Client) {
client.on("handshake", () => {
commit("playerListChanged", client.players);
});
client.on("player-joined", () => commit("playerListChanged", client.players));
client.on("player-left", () => commit("playerListChanged", client.players));
client.on("rename", () => commit("playerListChanged", client.players));
}
const actions: ActionTree<NetworkState, AppState> = { const actions: ActionTree<NetworkState, AppState> = {
startServer({ commit }, options: StartServerOptions) { startServer({ commit }, options: StartServerOptions) {
const local = new LocalClient(options.playerInfo); const local = new LocalClient(options.playerInfo);
const server = new PeerServer(options.roomInfo, local, options._customPeer); const server = new PeerServer(options.roomInfo, local, options._customPeer);
server.once("open", id => {
commit("serverAssignedID", id);
});
bindClientEvents(commit, local);
commit("becomeServer", { local, server }); commit("becomeServer", { local, server });
}, },
@ -29,6 +42,7 @@ const actions: ActionTree<NetworkState, AppState> = {
client.on("error", err => { client.on("error", err => {
commit("connectionError", err); commit("connectionError", err);
}); });
bindClientEvents(commit, client);
client.connect(options.serverID); client.connect(options.serverID);
}, },

View file

@ -15,13 +15,7 @@ const getters: GetterTree<NetworkState, AppState> = {
}, },
sessionID(state): string | null { sessionID(state): string | null {
switch (state.peerType) { return state.serverID;
case "server":
return state.server.id;
case "client":
return state.serverID;
}
return null;
}, },
client(state): Client | null { client(state): Client | null {
@ -46,6 +40,10 @@ const getters: GetterTree<NetworkState, AppState> = {
return true; return true;
} }
return false; return false;
},
players(state): string[] {
return state.players;
} }
}; };

View file

@ -15,6 +15,7 @@ export const state: NetworkState = {
server: null, server: null,
local: null, local: null,
serverID: null, serverID: null,
players: [],
chatLog: [] chatLog: []
}; };

View file

@ -32,6 +32,14 @@ const mutations: MutationTree<NetworkState> = {
receivedChatMessage(state, message: ChatMessage) { receivedChatMessage(state, message: ChatMessage) {
state.chatLog.push(message); state.chatLog.push(message);
},
serverAssignedID(state, id: string) {
state.serverID = id;
},
playerListChanged(state, players: string[]) {
state.players = players;
} }
}; };

View file

@ -16,6 +16,8 @@ export type ConnectionStatus =
export interface SharedNetworkState { export interface SharedNetworkState {
chatLog: ChatMessage[]; chatLog: ChatMessage[];
serverID: string | null;
players: string[];
} }
export interface NoNetworkState extends SharedNetworkState { export interface NoNetworkState extends SharedNetworkState {
@ -25,7 +27,6 @@ export interface NoNetworkState extends SharedNetworkState {
peer: null; peer: null;
server: null; server: null;
local: null; local: null;
serverID: null;
} }
export interface ClientNetworkState extends SharedNetworkState { export interface ClientNetworkState extends SharedNetworkState {
@ -33,7 +34,6 @@ export interface ClientNetworkState extends SharedNetworkState {
connectionStatus: ConnectionStatus; connectionStatus: ConnectionStatus;
connectionError?: Error; connectionError?: Error;
peer: PeerClient; peer: PeerClient;
serverID: string;
} }
export interface ServerNetworkState extends SharedNetworkState { export interface ServerNetworkState extends SharedNetworkState {

View file

@ -80,7 +80,15 @@
</section> </section>
<section class="room" v-else> <section class="room" v-else>
<section class="info"> <section class="info">
Session ID: {{ sessionID }} Session ID: <span class="selectable">{{ sessionID }}</span>
</section>
<section class="players">
Players:
<ul>
<li class="selectable" v-for="player in players" :key="player">
{{ player }}
</li>
</ul>
</section> </section>
</section> </section>
</section> </section>
@ -160,10 +168,21 @@
} }
} }
.room {
padding: 10px 20px;
margin: 10px;
border: 1px solid rgba($white, 20%);
border-radius: 10px;
}
.only-mobile { .only-mobile {
display: none; display: none;
} }
.selectable {
user-select: all;
}
@media (max-width: 500px) { @media (max-width: 500px) {
.only-full { .only-full {
display: none; display: none;
@ -189,6 +208,7 @@ import { Component, Vue } from "vue-property-decorator";
import TopNav from "@/components/Navigation/TopNav.vue"; import TopNav from "@/components/Navigation/TopNav.vue";
import { StartServerOptions, ConnectOptions } from "@/store/network/types"; import { StartServerOptions, ConnectOptions } from "@/store/network/types";
import { Action, Getter } from "vuex-class"; import { Action, Getter } from "vuex-class";
import { Client } from "@/network";
@Component({ @Component({
components: { components: {
@ -211,9 +231,12 @@ export default class Lobby extends Vue {
@Getter("inRoom", { namespace: "network" }) @Getter("inRoom", { namespace: "network" })
private inRoom!: boolean; private inRoom!: boolean;
@Getter("sessionID", { namespace: "network"} ) @Getter("sessionID", { namespace: "network" })
private sessionID!: string | null; private sessionID!: string | null;
@Getter("players", { namespace: "network" })
private players!: string[];
private data() { private data() {
return { return {
playerName: playerName: