Integrate draft/network stack via Vuex #26

Merged
hamcha merged 5 commits from feature/vuex into master 2019-09-17 13:22:43 +00:00
8 changed files with 114 additions and 1 deletions
Showing only changes of commit 4f7d095ec0 - Show all commits

View file

@ -34,6 +34,10 @@ export class PeerClient extends Client {
return this.metadata.name; return this.metadata.name;
} }
public get id(): string {
return this.peer.id;
}
public send<T extends NetworkMessage>(data: T) { public send<T extends NetworkMessage>(data: T) {
if (!this.connection) { if (!this.connection) {
throw new Error("Client is not connected to a server"); throw new Error("Client is not connected to a server");

View file

@ -234,6 +234,10 @@ export class PeerServer extends EventEmitter {
this.send<T>(player, message); this.send<T>(player, message);
} }
} }
public get id(): string {
return this.peer.id;
}
} }
export default PeerServer; export default PeerServer;

17
src/store/draft/index.ts Normal file
View file

@ -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<DraftState, AppState> = {
namespaced,
state
//actions,
//mutations,
//getters,
};

7
src/store/draft/types.ts Normal file
View file

@ -0,0 +1,7 @@
import { Session } from "@/mlpccg/draft";
import { Card } from "@/mlpccg";
export interface DraftState {
session?: Session;
picks: Card[];
}

View file

@ -8,6 +8,9 @@ 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 { draft } from "./draft";
const store: StoreOptions<AppState> = { const store: StoreOptions<AppState> = {
state: { state: {
loaded: false, loaded: false,
@ -17,7 +20,10 @@ const store: StoreOptions<AppState> = {
actions, actions,
mutations, mutations,
getters, getters,
modules: {} modules: {
network,
draft
}
}; };
export default new Vuex.Store<AppState>(store); export default new Vuex.Store<AppState>(store);

View file

@ -0,0 +1,18 @@
import { GetterTree } from "vuex";
import { AppState } from "../types";
import { NetworkState } from "./types";
const getters: GetterTree<NetworkState, AppState> = {
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;

View file

@ -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<NetworkState, AppState> = {
namespaced,
state,
//actions,
//mutations,
getters
};

View file

@ -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;