Compare commits
No commits in common. "4da4e0687100544909c439688783502459a03890" and "4f7d095ec0a98bb14cf29674a38596098680e96d" have entirely different histories.
4da4e06871
...
4f7d095ec0
7 changed files with 19 additions and 98 deletions
|
@ -15,5 +15,3 @@ export const draft: Module<DraftState, AppState> = {
|
||||||
//mutations,
|
//mutations,
|
||||||
//getters,
|
//getters,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default draft;
|
|
||||||
|
|
|
@ -8,8 +8,8 @@ import actions from "./actions";
|
||||||
import mutations from "./mutations";
|
import mutations from "./mutations";
|
||||||
import getters from "./getters";
|
import getters from "./getters";
|
||||||
|
|
||||||
import network from "./network";
|
import { network } from "./network";
|
||||||
import draft from "./draft";
|
import { draft } from "./draft";
|
||||||
|
|
||||||
const store: StoreOptions<AppState> = {
|
const store: StoreOptions<AppState> = {
|
||||||
state: {
|
state: {
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
import { ActionTree } from "vuex";
|
|
||||||
import { AppState } from "../types";
|
|
||||||
import { NetworkState, StartServerOptions, ConnectOptions } from "./types";
|
|
||||||
import { PeerServer, LocalClient, PeerClient } from "@/network";
|
|
||||||
|
|
||||||
const actions: ActionTree<NetworkState, AppState> = {
|
|
||||||
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;
|
|
|
@ -4,17 +4,14 @@ import { NetworkState } from "./types";
|
||||||
|
|
||||||
const getters: GetterTree<NetworkState, AppState> = {
|
const getters: GetterTree<NetworkState, AppState> = {
|
||||||
peerID(state): string | null {
|
peerID(state): string | null {
|
||||||
switch (state.peerType) {
|
if (!state.connected) {
|
||||||
case "server":
|
return null;
|
||||||
return state.server.id;
|
}
|
||||||
case "client":
|
if (state.peerType == "server") {
|
||||||
return state.peer.id;
|
return state.server.id;
|
||||||
|
} else {
|
||||||
|
return state.peer.id;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
},
|
|
||||||
|
|
||||||
connectionType(state): "client" | "server" | "none" {
|
|
||||||
return state.peerType;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,22 +2,21 @@ import { NetworkState } from "./types";
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
import { Module } from "vuex";
|
import { Module } from "vuex";
|
||||||
|
|
||||||
import actions from "./actions";
|
|
||||||
import getters from "./getters";
|
import getters from "./getters";
|
||||||
|
|
||||||
const namespaced = true;
|
const namespaced = true;
|
||||||
|
|
||||||
export const state: NetworkState = {
|
export const state: NetworkState = {
|
||||||
peerType: "none",
|
connecting: false,
|
||||||
|
connected: false,
|
||||||
|
name: "",
|
||||||
chatLog: []
|
chatLog: []
|
||||||
};
|
};
|
||||||
|
|
||||||
export const network: Module<NetworkState, AppState> = {
|
export const network: Module<NetworkState, AppState> = {
|
||||||
namespaced,
|
namespaced,
|
||||||
state,
|
state,
|
||||||
actions,
|
//actions,
|
||||||
//mutations,
|
//mutations,
|
||||||
getters
|
getters
|
||||||
};
|
};
|
||||||
|
|
||||||
export default network;
|
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
import { MutationTree } from "vuex";
|
|
||||||
import { NetworkState, ServerNetworkState, ClientNetworkState } from "./types";
|
|
||||||
import { LocalClient, PeerServer, PeerClient } from "@/network";
|
|
||||||
|
|
||||||
const mutations: MutationTree<NetworkState> = {
|
|
||||||
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;
|
|
|
@ -1,11 +1,4 @@
|
||||||
import {
|
import { PeerClient, PeerServer, LocalClient } from "@/network";
|
||||||
PeerClient,
|
|
||||||
PeerServer,
|
|
||||||
LocalClient,
|
|
||||||
RoomInfo,
|
|
||||||
PeerMetadata
|
|
||||||
} from "@/network";
|
|
||||||
import Peer from "peerjs";
|
|
||||||
|
|
||||||
export interface ChatMessage {
|
export interface ChatMessage {
|
||||||
who: string;
|
who: string;
|
||||||
|
@ -14,21 +7,23 @@ export interface ChatMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SharedNetworkState {
|
export interface SharedNetworkState {
|
||||||
|
name: string;
|
||||||
chatLog: ChatMessage[];
|
chatLog: ChatMessage[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NoNetworkState extends SharedNetworkState {
|
export interface NoNetworkState extends SharedNetworkState {
|
||||||
peerType: "none";
|
connecting: boolean;
|
||||||
|
connected: false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ClientNetworkState extends SharedNetworkState {
|
export interface ClientNetworkState extends SharedNetworkState {
|
||||||
|
connected: true;
|
||||||
peerType: "client";
|
peerType: "client";
|
||||||
connectionStatus: "connecting" | "connected" | "error";
|
|
||||||
connectionError?: Error;
|
|
||||||
peer: PeerClient;
|
peer: PeerClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ServerNetworkState extends SharedNetworkState {
|
export interface ServerNetworkState extends SharedNetworkState {
|
||||||
|
connected: true;
|
||||||
peerType: "server";
|
peerType: "server";
|
||||||
server: PeerServer;
|
server: PeerServer;
|
||||||
local: LocalClient;
|
local: LocalClient;
|
||||||
|
@ -38,19 +33,3 @@ export type NetworkState =
|
||||||
| NoNetworkState
|
| NoNetworkState
|
||||||
| ClientNetworkState
|
| ClientNetworkState
|
||||||
| ServerNetworkState;
|
| ServerNetworkState;
|
||||||
|
|
||||||
export interface StartServerOptions {
|
|
||||||
roomInfo: RoomInfo;
|
|
||||||
playerInfo: PeerMetadata;
|
|
||||||
|
|
||||||
// Testing utils
|
|
||||||
_customPeer?: Peer;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ConnectOptions {
|
|
||||||
serverID: string;
|
|
||||||
playerInfo: PeerMetadata;
|
|
||||||
|
|
||||||
// Testing utils
|
|
||||||
_customPeer?: Peer;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue