Integrate draft/network stack via Vuex #26
8 changed files with 114 additions and 1 deletions
|
@ -34,6 +34,10 @@ export class PeerClient extends Client {
|
|||
return this.metadata.name;
|
||||
}
|
||||
|
||||
public get id(): string {
|
||||
return this.peer.id;
|
||||
}
|
||||
|
||||
public send<T extends NetworkMessage>(data: T) {
|
||||
if (!this.connection) {
|
||||
throw new Error("Client is not connected to a server");
|
||||
|
|
|
@ -234,6 +234,10 @@ export class PeerServer extends EventEmitter {
|
|||
this.send<T>(player, message);
|
||||
}
|
||||
}
|
||||
|
||||
public get id(): string {
|
||||
return this.peer.id;
|
||||
}
|
||||
}
|
||||
|
||||
export default PeerServer;
|
||||
|
|
17
src/store/draft/index.ts
Normal file
17
src/store/draft/index.ts
Normal 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
7
src/store/draft/types.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { Session } from "@/mlpccg/draft";
|
||||
import { Card } from "@/mlpccg";
|
||||
|
||||
export interface DraftState {
|
||||
session?: Session;
|
||||
picks: Card[];
|
||||
}
|
|
@ -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<AppState> = {
|
||||
state: {
|
||||
loaded: false,
|
||||
|
@ -17,7 +20,10 @@ const store: StoreOptions<AppState> = {
|
|||
actions,
|
||||
mutations,
|
||||
getters,
|
||||
modules: {}
|
||||
modules: {
|
||||
network,
|
||||
draft
|
||||
}
|
||||
};
|
||||
|
||||
export default new Vuex.Store<AppState>(store);
|
||||
|
|
18
src/store/network/getters.ts
Normal file
18
src/store/network/getters.ts
Normal 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;
|
22
src/store/network/index.ts
Normal file
22
src/store/network/index.ts
Normal 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
|
||||
};
|
35
src/store/network/types.ts
Normal file
35
src/store/network/types.ts
Normal 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;
|
Loading…
Reference in a new issue