diff --git a/src/store/network/actions.ts b/src/store/network/actions.ts new file mode 100644 index 0000000..bbf4ba6 --- /dev/null +++ b/src/store/network/actions.ts @@ -0,0 +1,23 @@ +import { ActionTree } from "vuex"; +import { AppState } from "../types"; +import { NetworkState, StartServerOptions, ConnectOptions } from "./types"; +import { PeerServer, LocalClient, PeerClient } from "@/network"; + +const actions: ActionTree = { + startServer({ commit }, options: StartServerOptions) { + const local = new LocalClient(options.playerInfo); + const server = new PeerServer(options.roomInfo, local, options._customPeer); + commit("becomeServer", { local, server }); + }, + + connect({ commit }, options: ConnectOptions) { + const client = new PeerClient(options.playerInfo, options._customPeer); + commit("becomeClient", { peer: client }); + client.once("connected", () => { + commit("connected"); + }); + client.connect(options.serverID); + } +}; + +export default actions; diff --git a/src/store/network/getters.ts b/src/store/network/getters.ts index 54181d8..3840b22 100644 --- a/src/store/network/getters.ts +++ b/src/store/network/getters.ts @@ -4,14 +4,17 @@ 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; + switch (state.peerType) { + case "server": + return state.server.id; + case "client": + return state.peer.id; } + return null; + }, + + connectionType(state): "client" | "server" | "none" { + return state.peerType; } }; diff --git a/src/store/network/index.ts b/src/store/network/index.ts index 9e6952c..ed54c6c 100644 --- a/src/store/network/index.ts +++ b/src/store/network/index.ts @@ -2,21 +2,20 @@ import { NetworkState } from "./types"; import { AppState } from "../types"; import { Module } from "vuex"; +import actions from "./actions"; import getters from "./getters"; const namespaced = true; export const state: NetworkState = { - connecting: false, - connected: false, - name: "", + peerType: "none", chatLog: [] }; export const network: Module = { namespaced, state, - //actions, + actions, //mutations, getters }; diff --git a/src/store/network/mutations.ts b/src/store/network/mutations.ts new file mode 100644 index 0000000..b40d8b1 --- /dev/null +++ b/src/store/network/mutations.ts @@ -0,0 +1,29 @@ +import { MutationTree } from "vuex"; +import { NetworkState, ServerNetworkState, ClientNetworkState } from "./types"; +import { LocalClient, PeerServer, PeerClient } from "@/network"; + +const mutations: MutationTree = { + becomeServer(state, payload: { local: LocalClient; server: PeerServer }) { + state = { + ...state, + peerType: "server", + local: payload.local, + server: payload.server + }; + }, + + becomeClient(state, payload: { peer: PeerClient }) { + state = { + ...state, + connectionStatus: "connecting", + peerType: "client", + peer: payload.peer + }; + }, + + connected(state) { + (state as ClientNetworkState).connectionStatus = "connected"; + } +}; + +export default mutations; diff --git a/src/store/network/types.ts b/src/store/network/types.ts index 1fb298e..d5f813b 100644 --- a/src/store/network/types.ts +++ b/src/store/network/types.ts @@ -1,4 +1,11 @@ -import { PeerClient, PeerServer, LocalClient } from "@/network"; +import { + PeerClient, + PeerServer, + LocalClient, + RoomInfo, + PeerMetadata +} from "@/network"; +import Peer from "peerjs"; export interface ChatMessage { who: string; @@ -7,23 +14,21 @@ export interface ChatMessage { } export interface SharedNetworkState { - name: string; chatLog: ChatMessage[]; } export interface NoNetworkState extends SharedNetworkState { - connecting: boolean; - connected: false; + peerType: "none"; } export interface ClientNetworkState extends SharedNetworkState { - connected: true; peerType: "client"; + connectionStatus: "connecting" | "connected" | "error"; + connectionError?: Error; peer: PeerClient; } export interface ServerNetworkState extends SharedNetworkState { - connected: true; peerType: "server"; server: PeerServer; local: LocalClient; @@ -33,3 +38,19 @@ export type NetworkState = | NoNetworkState | ClientNetworkState | ServerNetworkState; + +export interface StartServerOptions { + roomInfo: RoomInfo; + playerInfo: PeerMetadata; + + // Testing utils + _customPeer?: Peer; +} + +export interface ConnectOptions { + serverID: string; + playerInfo: PeerMetadata; + + // Testing utils + _customPeer?: Peer; +}