This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Broadcaster.js
156 lines (132 loc) · 3.8 KB
/
Broadcaster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
const Assert = require('assert-js');
const Connection = require('./Network/Connection');
const Area = require('./../Engine/Map/Area');
const Player = require('./../Engine/Player');
class Broadcaster
{
constructor()
{
this._connections = new Map();
}
/**
* @param {Connection} connection
*/
addConnection(connection)
{
Assert.instanceOf(connection, Connection);
this._connections.set(connection.id, connection);
}
/**
* @param {string} playerId
* @returns {Connection}
*/
getConnection(playerId)
{
Assert.string(playerId);
for (let connection of this._connections.values()) {
if (connection.hasPlayerId && connection.playerId === playerId) {
return connection;
}
}
throw `Can't find connection for player with id ${playerId}`;
}
/**
* @param {string} id
*/
removeConnection(id)
{
Assert.string(id);
this._connections.delete(id);
}
/**
* @param {Player[]} [players]
* @param {function} messageFactory
*/
sendToPlayers(players = [], messageFactory)
{
Assert.containsOnly(players, Player);
Assert.isFunction(messageFactory);
for (let player of players) {
for (let connection of this._connections.values()) {
if (connection.playerId === player.id) {
connection.send(messageFactory(connection));
break;
}
}
}
}
/**
* @param {Player} player
* @param {function} messageFactory
*/
sendToPlayer(player, messageFactory)
{
Assert.instanceOf(player, Player);
Assert.isFunction(messageFactory);
for (let connection of this._connections.values()) {
if (connection.playerId === player.id) {
connection.send(messageFactory(connection));
return ;
}
}
throw `Connection for player ${player.id} was not found.`;
}
/**
* Send message only to players visible by player with id playerId
*
* @param {Area} area;
* @param {string} playerId
* @param {function} messageFactory
*/
sendToPlayersInRange(area, playerId, messageFactory)
{
let visiblePlayers = area.visiblePlayersFor(playerId);
for (let connection of this._connections.values()) {
if (!connection.hasPlayerId) {
continue;
}
if (connection.playerId === playerId) {
continue;
}
for (let player of visiblePlayers) {
if (connection.playerId === player.id) {
connection.send(messageFactory(connection));
}
}
}
}
/**
* Send message to all connected clients with logged players except currentConnection
*
* @param {Connection} currentConnection
* @param {function} messageFactory
*/
sendToOtherConnectedClients(currentConnection, messageFactory)
{
for (let connection of this._connections.values()) {
if (connection.id === currentConnection.id) {
continue;
}
if (!connection.hasPlayerId) {
continue;
}
connection.send(messageFactory(connection));
}
}
/**
* Send message to all connected clients
*
* @param {function} messageFactory
*/
sendToAllConnectedClients(messageFactory)
{
for (let connection of this._connections.values()) {
if (!connection.hasPlayerId) {
continue;
}
connection.send(messageFactory(connection));
}
}
}
module.exports = Broadcaster;