From 4f7d095ec0a98bb14cf29674a38596098680e96d Mon Sep 17 00:00:00 2001 From: Hamcha Date: Tue, 17 Sep 2019 11:45:27 +0200 Subject: [PATCH] Add basic network/draft vuex modules --- src/network/PeerClient.ts | 4 ++++ src/network/PeerServer.ts | 4 ++++ src/store/draft/index.ts | 17 +++++++++++++++++ src/store/draft/types.ts | 7 +++++++ src/store/index.ts | 8 +++++++- src/store/network/getters.ts | 18 ++++++++++++++++++ src/store/network/index.ts | 22 ++++++++++++++++++++++ src/store/network/types.ts | 35 +++++++++++++++++++++++++++++++++++ 8 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/store/draft/index.ts create mode 100644 src/store/draft/types.ts create mode 100644 src/store/network/getters.ts create mode 100644 src/store/network/index.ts create mode 100644 src/store/network/types.ts diff --git a/src/network/PeerClient.ts b/src/network/PeerClient.ts index b728dab..f956b6c 100644 --- a/src/network/PeerClient.ts +++ b/src/network/PeerClient.ts @@ -34,6 +34,10 @@ export class PeerClient extends Client { return this.metadata.name; } + public get id(): string { + return this.peer.id; + } + public send(data: T) { if (!this.connection) { throw new Error("Client is not connected to a server"); diff --git a/src/network/PeerServer.ts b/src/network/PeerServer.ts index bfadda2..e88ffbf 100644 --- a/src/network/PeerServer.ts +++ b/src/network/PeerServer.ts @@ -234,6 +234,10 @@ export class PeerServer extends EventEmitter { this.send(player, message); } } + + public get id(): string { + return this.peer.id; + } } export default PeerServer; diff --git a/src/store/draft/index.ts b/src/store/draft/index.ts new file mode 100644 index 0000000..f1e4f8f --- /dev/null +++ b/src/store/draft/index.ts @@ -0,0 +1,17 @@ +import { DraftState } from "./types"; +import { AppState } from "../types"; +import { Module } from "vuex"; + +const namespaced = true; + +export const state: DraftState = { + picks: [] +}; + +export const draft: Module = { + namespaced, + state + //actions, + //mutations, + //getters, +}; diff --git a/src/store/draft/types.ts b/src/store/draft/types.ts new file mode 100644 index 0000000..3a25e1b --- /dev/null +++ b/src/store/draft/types.ts @@ -0,0 +1,7 @@ +import { Session } from "@/mlpccg/draft"; +import { Card } from "@/mlpccg"; + +export interface DraftState { + session?: Session; + picks: Card[]; +} diff --git a/src/store/index.ts b/src/store/index.ts index 4909137..7cb6150 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -8,6 +8,9 @@ import actions from "./actions"; import mutations from "./mutations"; import getters from "./getters"; +import { network } from "./network"; +import { draft } from "./draft"; + const store: StoreOptions = { state: { loaded: false, @@ -17,7 +20,10 @@ const store: StoreOptions = { actions, mutations, getters, - modules: {} + modules: { + network, + draft + } }; export default new Vuex.Store(store); diff --git a/src/store/network/getters.ts b/src/store/network/getters.ts new file mode 100644 index 0000000..54181d8 --- /dev/null +++ b/src/store/network/getters.ts @@ -0,0 +1,18 @@ +import { GetterTree } from "vuex"; +import { AppState } from "../types"; +import { NetworkState } from "./types"; + +const getters: GetterTree = { + peerID(state): string | null { + if (!state.connected) { + return null; + } + if (state.peerType == "server") { + return state.server.id; + } else { + return state.peer.id; + } + } +}; + +export default getters; diff --git a/src/store/network/index.ts b/src/store/network/index.ts new file mode 100644 index 0000000..6b421a4 --- /dev/null +++ b/src/store/network/index.ts @@ -0,0 +1,22 @@ +import { NetworkState } from "./types"; +import { AppState } from "../types"; +import { Module } from "vuex"; + +import getters from "./getters"; + +const namespaced = true; + +export const state: NetworkState = { + connecting: false, + connected: false, + name: "", + chatLog: [] +}; + +export const network: Module = { + namespaced, + state, + //actions, + //mutations, + getters +}; diff --git a/src/store/network/types.ts b/src/store/network/types.ts new file mode 100644 index 0000000..1fb298e --- /dev/null +++ b/src/store/network/types.ts @@ -0,0 +1,35 @@ +import { PeerClient, PeerServer, LocalClient } from "@/network"; + +export interface ChatMessage { + who: string; + to: string; + message: string; +} + +export interface SharedNetworkState { + name: string; + chatLog: ChatMessage[]; +} + +export interface NoNetworkState extends SharedNetworkState { + connecting: boolean; + connected: false; +} + +export interface ClientNetworkState extends SharedNetworkState { + connected: true; + peerType: "client"; + peer: PeerClient; +} + +export interface ServerNetworkState extends SharedNetworkState { + connected: true; + peerType: "server"; + server: PeerServer; + local: LocalClient; +} + +export type NetworkState = + | NoNetworkState + | ClientNetworkState + | ServerNetworkState;