Add basic network/draft vuex modules
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Hamcha 2019-09-17 11:45:27 +02:00
parent 412bb56b32
commit 4f7d095ec0
Signed by: hamcha
GPG Key ID: 44AD3571EB09A39E
8 changed files with 114 additions and 1 deletions

View File

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

View File

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

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;