-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
sockets.js
178 lines (155 loc) · 5.07 KB
/
sockets.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const log = require("simple-console-logger").getLogger("LiveMap Sockets");
const WS = require("ws");
const SocketController = (access) => {
const hook = (wss) => {
wss.broadcast = (data) => {
wss.clients.forEach(function each(client) {
if (client.readyState === WS.OPEN) {
client.send(data);
}
});
};
wss.on("connection", function connection(ws, req) { //ws, res
log.trace("Access is: %s", access);
if (access !== "*") { // We don't want to accept all requests
if (req.headers.origin !== access) {
ws.close();
log.warn("Someone tried connecting from an invalid origin: %s", req.headers.origin);
}
}
log.debug("connection made from", req.headers.origin);
});
websocketServer = wss;
};
let playerData = {};
let websocketServer = null;
const makeSurePlayerExists = (id) => {
if (playerData[id] === undefined){
playerData[id] = {};
}
};
const SendPlayerData = () => {
if (websocketServer == null) return;
let payload = [];
for (let identifier in playerData){
payload.push(playerData[identifier]);
}
websocketServer.broadcast(JSON.stringify({
type: "playerData",
payload: payload
}));
};
const checkInputs = (identifier, key, data) => {
if (!identifier) {
log.debug("Empty identifier: ", identifier);
log.warn("Identifier cannot be null or empty when adding player data");
return false;
}
if (!key) {
log.warn("Key for the data cannot be null or empty when adding player data");
return false;
}
if (!data) {
log.warn("Cannot add no data to player");
return false;
}
return true;
};
const validBlip = (blip) => {
if (blip["sprite"] === undefined || blip["sprite"] === null) {
log.debug("Blip didn't have sprite: %o", blip);
log.warn("Blip has no sprite...");
return false;
}
if (blip["pos"] === undefined || blip["pos"] === null) {
log.debug("Blip didn't have pos: %o", blip);
log.warn("Blip has no position...");
return false;
}
if (typeof (blip.pos) !== "object") {
log.warn("Blip position must be an object");
return false;
}
if (blip.pos["x"] === undefined || blip.pos["y"] === undefined || blip.pos["z"] === undefined) {
log.debug("Invalid position: %o", blip.pos);
log.warn("Blip position invalid");
return false;
}
if (typeof (blip.pos.x) !== "number" || typeof (blip.pos.y) !== "number" || typeof (blip.pos.z) !== "number") {
log.warn("Blip pos must be numbers");
log.debug("Invalid position: %o", blip.pos);
return false;
}
return true;
};
// The old, internal events... I can now just call them so... Make them functions!
const AddPlayerData = (identifier, key, data) => {
UpdatePlayerData(identifier, key, data); // It'll get added in here anyways...
};
const UpdatePlayerData = (identifier, key, data) => {
if (!checkInputs(identifier, key, data)) {
return;
}
makeSurePlayerExists(identifier);
playerData[identifier][key] = data;
};
const RemovePlayerData = (identifier, key) => {
if(!checkInputs(identifier, key, "s")) {
return;
}
makeSurePlayerExists(identifier);
if (playerData[identifier][key] !== undefined) {
delete playerData[identifier][key];
}
};
const RemovePlayer = (identifier) => {
if (!identifier) {
log.warn("Cannot remove player with no identifier");
return;
}
delete playerData[identifier];
websocketServer.broadcast(JSON.stringify({
type: "playerLeft",
payload: identifier
}));
};
const AddBlip = (blip) => {
if(!validBlip(blip)) {
return;
}
websocketServer.broadcast(JSON.stringify({
type: "addBlip",
payload: blip
}));
};
const RemoveBlip = (blip) => {
if(!validBlip(blip)) {
return;
}
websocketServer.broadcast(JSON.stringify({
type: "removeBlip",
payload: blip
}));
};
const UpdateBlip = (blip) => {
if(!validBlip(blip)) {
return;
}
websocketServer.broadcast(JSON.stringify({
type: "updateBlip",
payload: blip
}));
};
return {
hook,
SendPlayerData,
AddPlayerData,
UpdatePlayerData,
RemovePlayerData,
RemovePlayer,
AddBlip,
UpdateBlip,
RemoveBlip,
};
};
module.exports = SocketController;