mlpcardgame/src/store/network/actions.ts

30 lines
979 B
TypeScript

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.on("connected", () => {
commit("connected");
});
client.on("disconnected", () => {
commit("disconnected");
});
client.on("error", err => {
commit("connectionError", err);
});
client.connect(options.serverID);
}
};
export default actions;