-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
171 lines (169 loc) · 4.48 KB
/
Copy pathclient.js
File metadata and controls
171 lines (169 loc) · 4.48 KB
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
const now = require("performance-now");
const _ = require("underscore");
module.exports = function() {
const client = this;
//ovject will be added at runtime
this.init = function() {
//send connection handshake packet
try {
client.socket.write(packet.build(["HELLO", now().toString()]));
} catch (e) {
console.log(e);
}
console.log("client init");
};
//client methods
this.enterRoom = function(selectedRoom) {
try {
global.maps.get(selectedRoom).clients.map(otherClient => {
otherClient.socket.write(
packet.build([
"ENTER",
client.user.username,
client.user.pos_x,
client.user.pos_y
])
);
return true;
});
return maps.get(selectedRoom).clients.push(client);
} catch (e) {
return e;
console.log(e);
}
};
this.broadcastRoom = function(packetData) {
try {
return global.maps
.get(client.user.currentRoom)
.clients.map(otherClients => {
if (otherClients.user.username != client.user.username) {
otherClients.socket.write(packetData);
}
return true;
});
} catch (e) {
return e;
console.log(e);
}
};
this.getTagged = function() {
try {
return global.maps.get(client.user.currentRoom).clients.find(Clients => {
if (Clients.playingTag === "TRUE") {
if (Clients.tagBoss) {
return Clients.user.username;
}
}
});
} catch (e) {
return e;
}
};
this.setTagged = function(username) {
try {
return global.maps.get(client.user.currentRoom).clients.map(Clients => {
if (Clients.playingTag === "TRUE") {
if (Clients.user.username === username) {
Clients.tagBoss = true;
//console.log(Clients.user);
return Clients.user.username;
} else {
Clients.tagBoss = false;
}
}
return true;
});
} catch (e) {
return e;
}
};
this.setTaggedImmune = function(username) {
try {
return global.maps.get(client.user.currentRoom).clients.map(Clients => {
if (Clients.playingTag === "TRUE") {
if (Clients.user.username === username) {
if (Clients.tagImmune) {
Clients.tagImmune = false;
} else {
Clients.tagImmune = true;
}
console.log("immune status", username, Clients.tagImmune);
}
}
return true;
});
} catch (e) {
return e;
console.log(e);
}
};
this.broadcastEveryone = function(packetData) {
try {
return global.maps
.get(client.user.currentRoom)
.clients.map(otherClients => {
otherClients.socket.write(packetData);
return true;
});
} catch (e) {
return e;
console.log(e);
}
};
this.endTag = function() {
try {
return global.maps.get(client.user.currentRoom).clients.map(clients => {
clients.playingTag = "FALSE";
clients.tagBoss = false;
return true;
});
} catch (e) {
return e;
}
};
//socket stuff
this.error = function(err) {
client.end();
console.log("socket error" + err);
};
this.end = async function() {
try {
client.user.save();
client.endTag();
client.broadcastRoom(packet.build(["DISCONNECT", client.user.username]));
client.broadcastRoom(
packet.build([
"MSG",
(client.user.username + " : disconnected").toString()
])
);
let mapToEdit = global.maps.get(client.user.currentRoom);
let filtered = mapToEdit.clients.filter(otherClients => {
return otherClients.user.username !== client.user.username;
});
mapToEdit.clients = filtered;
global.maps.set(client.user.currentRoom, mapToEdit);
} catch (e) {
console.log(e.message);
try {
let mapToEdit = global.maps.get(client.user.currentRoom);
let filtered = mapToEdit.clients.filter(otherClients => {
return otherClients.user.username !== client.user.username;
});
mapToEdit.clients = filtered;
global.maps.set(client.user.currentRoom, mapToEdit);
} catch (e) {
console.log("144", e);
}
}
console.log("socket closed");
};
this.data = function(data) {
try {
packet.parse(client, data);
} catch (e) {
console.log(e);
}
};
};