Compare commits
No commits in common. "feature/ready" and "master" have entirely different histories.
feature/re
...
master
7 changed files with 30 additions and 56 deletions
|
@ -1,11 +1,11 @@
|
||||||
import EventEmitter from "eventemitter3";
|
import EventEmitter from "eventemitter3";
|
||||||
import Vue from "vue";
|
import Vue from "vue";
|
||||||
|
|
||||||
import { NetworkMessage, PasswordResponse, PeerMetadata, RoomInfo, RoomPlayer } from "./types";
|
import { NetworkMessage, PasswordResponse, PeerMetadata, RoomInfo } from "./types";
|
||||||
|
|
||||||
export abstract class Client extends EventEmitter {
|
export abstract class Client extends EventEmitter {
|
||||||
public metadata: PeerMetadata;
|
public metadata: PeerMetadata;
|
||||||
public players!: RoomPlayer[];
|
public players!: string[];
|
||||||
public roomInfo!: RoomInfo;
|
public roomInfo!: RoomInfo;
|
||||||
|
|
||||||
public constructor(metadata: PeerMetadata) {
|
public constructor(metadata: PeerMetadata) {
|
||||||
|
@ -15,7 +15,6 @@ export abstract class Client extends EventEmitter {
|
||||||
|
|
||||||
protected _received(data: NetworkMessage) {
|
protected _received(data: NetworkMessage) {
|
||||||
this.emit("data", data);
|
this.emit("data", data);
|
||||||
console.log(data);
|
|
||||||
switch (data.kind) {
|
switch (data.kind) {
|
||||||
// Server is sending over player list and room info
|
// Server is sending over player list and room info
|
||||||
case "room-info":
|
case "room-info":
|
||||||
|
@ -34,12 +33,13 @@ export abstract class Client extends EventEmitter {
|
||||||
// This is because rename messages can be received during the initial
|
// This is because rename messages can be received during the initial
|
||||||
// handshake, to signal a forced name change before joining.
|
// handshake, to signal a forced name change before joining.
|
||||||
if (this.players) {
|
if (this.players) {
|
||||||
let idx = this.players.findIndex(player => player.name == data.oldname);
|
let idx = this.players.indexOf(data.oldname);
|
||||||
if (idx < 0) {
|
if (idx < 0) {
|
||||||
// Weird
|
// Weird
|
||||||
console.error(
|
console.warn(
|
||||||
`Someone (${data.oldname}) changed name but wasn't on the player list`
|
`Someone (${data.oldname}) changed name but wasn't on the player list`
|
||||||
);
|
);
|
||||||
|
this.players.push(data.newname);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Vue.set(this.players, idx, data.newname);
|
Vue.set(this.players, idx, data.newname);
|
||||||
|
@ -48,11 +48,11 @@ export abstract class Client extends EventEmitter {
|
||||||
break;
|
break;
|
||||||
// A new player joined the room (this includes us)
|
// A new player joined the room (this includes us)
|
||||||
case "player-joined":
|
case "player-joined":
|
||||||
this.players.push(data.player);
|
this.players.push(data.name);
|
||||||
this.emit("player-joined", data.player);
|
this.emit("player-joined", data.name);
|
||||||
break;
|
break;
|
||||||
case "player-left": {
|
case "player-left": {
|
||||||
let idx = this.players.findIndex(player => player.name == data.name);
|
let idx = this.players.indexOf(data.name);
|
||||||
if (idx < 0) {
|
if (idx < 0) {
|
||||||
// Weird
|
// Weird
|
||||||
console.warn(
|
console.warn(
|
||||||
|
|
|
@ -18,7 +18,6 @@ import {
|
||||||
Room,
|
Room,
|
||||||
RoomInfo,
|
RoomInfo,
|
||||||
RoomInfoMessage,
|
RoomInfoMessage,
|
||||||
RoomPlayer,
|
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
// Increment name, add number at the end if not present
|
// Increment name, add number at the end if not present
|
||||||
|
@ -57,7 +56,6 @@ export class PeerServer extends EventEmitter {
|
||||||
// Add local player to server
|
// Add local player to server
|
||||||
players[local.name] = {
|
players[local.name] = {
|
||||||
kind: "local",
|
kind: "local",
|
||||||
ready: false,
|
|
||||||
name: local.name,
|
name: local.name,
|
||||||
client: local
|
client: local
|
||||||
};
|
};
|
||||||
|
@ -68,7 +66,7 @@ export class PeerServer extends EventEmitter {
|
||||||
players
|
players
|
||||||
};
|
};
|
||||||
|
|
||||||
local.players = this.playerList;
|
local.players = Object.keys(this.players);
|
||||||
local.roomInfo = this.room.info;
|
local.roomInfo = this.room.info;
|
||||||
|
|
||||||
// Setup peer
|
// Setup peer
|
||||||
|
@ -91,7 +89,6 @@ export class PeerServer extends EventEmitter {
|
||||||
let player: NetworkPlayer = {
|
let player: NetworkPlayer = {
|
||||||
kind: "remote",
|
kind: "remote",
|
||||||
name: metadata.name,
|
name: metadata.name,
|
||||||
ready: false,
|
|
||||||
conn: conn
|
conn: conn
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -157,7 +154,7 @@ export class PeerServer extends EventEmitter {
|
||||||
|
|
||||||
// Hacky: Give player list before this player was added, so join
|
// Hacky: Give player list before this player was added, so join
|
||||||
// message doesn't mess things up later
|
// message doesn't mess things up later
|
||||||
const players = this.playerList;
|
const players = Object.keys(this.room.players);
|
||||||
|
|
||||||
// Add player to room
|
// Add player to room
|
||||||
this.room.players[playerName] = player;
|
this.room.players[playerName] = player;
|
||||||
|
@ -185,10 +182,7 @@ export class PeerServer extends EventEmitter {
|
||||||
// Notify other players
|
// Notify other players
|
||||||
this.broadcast<JoinMessage>({
|
this.broadcast<JoinMessage>({
|
||||||
kind: "player-joined",
|
kind: "player-joined",
|
||||||
player: {
|
name: playerName
|
||||||
name: player.name,
|
|
||||||
ready: player.ready
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,13 +276,6 @@ export class PeerServer extends EventEmitter {
|
||||||
public get id(): string {
|
public get id(): string {
|
||||||
return this.peer.id;
|
return this.peer.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get playerList(): RoomPlayer[] {
|
|
||||||
return Object.keys(this.room.players).map(playerName => ({
|
|
||||||
name: playerName,
|
|
||||||
ready: this.room.players[playerName].ready
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default PeerServer;
|
export default PeerServer;
|
||||||
|
|
|
@ -1,19 +1,15 @@
|
||||||
import { DataConnection } from "peerjs";
|
import { DataConnection } from "peerjs";
|
||||||
|
|
||||||
import LocalClient from "./LocalClient";
|
import LocalClient from "./LocalClient";
|
||||||
|
|
||||||
export interface RoomPlayer {
|
export interface LocalPlayer {
|
||||||
name: string;
|
|
||||||
ready: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface LocalPlayer extends RoomPlayer {
|
|
||||||
kind: "local";
|
kind: "local";
|
||||||
|
name: string;
|
||||||
client: LocalClient;
|
client: LocalClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NetworkPlayer extends RoomPlayer {
|
export interface NetworkPlayer {
|
||||||
kind: "remote";
|
kind: "remote";
|
||||||
|
name: string;
|
||||||
conn: DataConnection;
|
conn: DataConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,8 +70,7 @@ export type NetworkMessage =
|
||||||
| LeaveMessage
|
| LeaveMessage
|
||||||
| RenameMessage
|
| RenameMessage
|
||||||
| ChatMessage
|
| ChatMessage
|
||||||
| AckMessage
|
| AckMessage;
|
||||||
| ReadyMessage;
|
|
||||||
|
|
||||||
export interface PasswordRequest {
|
export interface PasswordRequest {
|
||||||
kind: "password-req";
|
kind: "password-req";
|
||||||
|
@ -89,7 +84,7 @@ export interface PasswordResponse {
|
||||||
export interface RoomInfoMessage {
|
export interface RoomInfoMessage {
|
||||||
kind: "room-info";
|
kind: "room-info";
|
||||||
room: RoomInfo;
|
room: RoomInfo;
|
||||||
players: RoomPlayer[];
|
players: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LeaveRequest {
|
export interface LeaveRequest {
|
||||||
|
@ -98,7 +93,7 @@ export interface LeaveRequest {
|
||||||
|
|
||||||
export interface JoinMessage {
|
export interface JoinMessage {
|
||||||
kind: "player-joined";
|
kind: "player-joined";
|
||||||
player: RoomPlayer;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LeaveMessage {
|
export interface LeaveMessage {
|
||||||
|
@ -128,8 +123,3 @@ export interface ErrorMessage {
|
||||||
kind: "error";
|
kind: "error";
|
||||||
error: string;
|
error: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ReadyMessage {
|
|
||||||
kind: "ready";
|
|
||||||
ready: boolean;
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Client, RoomPlayer } from "@/network";
|
import { Client } from "@/network";
|
||||||
import { GetterTree } from "vuex";
|
import { GetterTree } from "vuex";
|
||||||
|
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
|
@ -52,7 +52,7 @@ const getters: GetterTree<NetworkState, AppState> = {
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
players(state): RoomPlayer[] {
|
players(state): string[] {
|
||||||
return state.players;
|
return state.players;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { ChatMessage, LocalClient, PeerClient, PeerServer, RoomPlayer } from "@/network";
|
import { ChatMessage, LocalClient, PeerClient, PeerServer } from "@/network";
|
||||||
import Vue from "vue";
|
import Vue from "vue";
|
||||||
import { MutationTree } from "vuex";
|
import { MutationTree } from "vuex";
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import { ClientNetworkState, ConnectionStatus, NetworkState, ServerNetworkState
|
||||||
const mutations: MutationTree<NetworkState> = {
|
const mutations: MutationTree<NetworkState> = {
|
||||||
becomeServer(state, payload: { local: LocalClient; server: PeerServer }) {
|
becomeServer(state, payload: { local: LocalClient; server: PeerServer }) {
|
||||||
state.peerType = "server";
|
state.peerType = "server";
|
||||||
state.players = [{ name: payload.local.name, ready: true }];
|
state.players = [payload.local.name];
|
||||||
(state as ServerNetworkState).local = payload.local;
|
(state as ServerNetworkState).local = payload.local;
|
||||||
(state as ServerNetworkState).server = payload.server;
|
(state as ServerNetworkState).server = payload.server;
|
||||||
},
|
},
|
||||||
|
@ -36,7 +36,7 @@ const mutations: MutationTree<NetworkState> = {
|
||||||
state.serverID = id;
|
state.serverID = id;
|
||||||
},
|
},
|
||||||
|
|
||||||
playerListChanged(state, players: RoomPlayer[]) {
|
playerListChanged(state, players: string[]) {
|
||||||
Vue.set(state, "players", players);
|
Vue.set(state, "players", players);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { ChatMessage, LocalClient, PeerClient, PeerMetadata, PeerServer, RoomInfo, RoomPlayer } from "@/network";
|
import { ChatMessage, LocalClient, PeerClient, PeerMetadata, PeerServer, RoomInfo } from "@/network";
|
||||||
import Peer from "peerjs";
|
import Peer from "peerjs";
|
||||||
|
|
||||||
export type ConnectionStatus =
|
export type ConnectionStatus =
|
||||||
|
@ -10,7 +10,7 @@ export type ConnectionStatus =
|
||||||
export interface SharedNetworkState {
|
export interface SharedNetworkState {
|
||||||
chatLog: ChatMessage[];
|
chatLog: ChatMessage[];
|
||||||
serverID: string | null;
|
serverID: string | null;
|
||||||
players: RoomPlayer[];
|
players: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NoNetworkState extends SharedNetworkState {
|
export interface NoNetworkState extends SharedNetworkState {
|
||||||
|
|
|
@ -89,8 +89,8 @@
|
||||||
<section class="players">
|
<section class="players">
|
||||||
<header>Players</header>
|
<header>Players</header>
|
||||||
<ul>
|
<ul>
|
||||||
<li class="selectable" v-for="player in players" :key="player.name">
|
<li class="selectable" v-for="player in players" :key="player">
|
||||||
{{ player.name }}
|
{{ player }}
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</section>
|
</section>
|
||||||
|
@ -260,7 +260,7 @@ import { Component, Vue } from "vue-property-decorator";
|
||||||
import TopNav from "@/components/Navigation/TopNav.vue";
|
import TopNav from "@/components/Navigation/TopNav.vue";
|
||||||
import { StartServerOptions, ConnectOptions } from "@/store/network/types";
|
import { StartServerOptions, ConnectOptions } from "@/store/network/types";
|
||||||
import { Action, Getter } from "vuex-class";
|
import { Action, Getter } from "vuex-class";
|
||||||
import { Client, NetworkMessage, RoomPlayer } from "@/network";
|
import { Client, NetworkMessage } from "@/network";
|
||||||
|
|
||||||
const networkNS = { namespace: "network" };
|
const networkNS = { namespace: "network" };
|
||||||
|
|
||||||
|
@ -295,7 +295,7 @@ export default class Lobby extends Vue {
|
||||||
private sessionID!: string | null;
|
private sessionID!: string | null;
|
||||||
|
|
||||||
@Getter("players", networkNS)
|
@Getter("players", networkNS)
|
||||||
private players!: RoomPlayer[];
|
private players!: string[];
|
||||||
|
|
||||||
private data() {
|
private data() {
|
||||||
const playerName =
|
const playerName =
|
||||||
|
@ -368,10 +368,7 @@ export default class Lobby extends Vue {
|
||||||
}
|
}
|
||||||
|
|
||||||
private get nameAvailable(): boolean {
|
private get nameAvailable(): boolean {
|
||||||
return (
|
return this.wantedName != "" && !this.players.includes(this.wantedName);
|
||||||
this.wantedName != "" &&
|
|
||||||
!this.players.find(p => p.name == this.wantedName)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Reference in a new issue