-
Notifications
You must be signed in to change notification settings - Fork 1
/
gm_server.js
175 lines (156 loc) · 4.63 KB
/
gm_server.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
const shocknet = require('../index.js');
// Setup variables
var exampleServer = new shocknet.Server;
const idIn = {
PLAYER_SYNC: 0,
MESSAGE: 1
}
const idOut = {
MESSAGE: 0,
PLAYER_READY: 1,
PLAYER_ADD: 2,
PLAYER_KICK: 3,
PLAYER_COLOR: 4,
PLAYER_MOVE: 5,
PLAYER_REMOVE: 6,
PLAYER_NAME: 7
}
var players = [];
// Player class
class Player {
constructor(connection) {
// Setup
this.x = 0;
this.y = 0;
this.color = {r: 0, g: 0, b: 0};
this.name = 'Player';
this.connection = connection;
// Register
var addPacket = new shocknet.Packet(idOut.PLAYER_ADD);
addPacket.add([this.connection.id, this.x, this.y]);
this.connection.broadcast(addPacket);
// Ready
var readyPacket = new shocknet.Packet(idOut.PLAYER_READY);
readyPacket.add([this.connection.id, this.x, this.y]);
this.connection.send(readyPacket);
// Set name and color
this.rename('Player #'+this.connection.id);
this.randomColor();
}
send(connection) {
// Add player
var addPacket = new shocknet.Packet(idOut.PLAYER_ADD);
addPacket.add([this.connection.id, this.x, this.y]);
connection.send(addPacket);
// Set color
var colorPacket = new shocknet.Packet(idOut.PLAYER_COLOR);
colorPacket.add([this.connection.id, this.color.r, this.color.g, this.color.b]);
exampleServer.broadcast(colorPacket);
// Set name
var namePacket = new shocknet.Packet(idOut.PLAYER_NAME);
namePacket.add([this.connection.id, this.name]);
exampleServer.broadcast(namePacket);
}
randomColor() {
// Create random RGB value
this.color.r = Math.floor(Math.random()*255);
this.color.g = Math.floor(Math.random()*255);
this.color.b = Math.floor(Math.random()*255);
// Recolor
var colorPacket = new shocknet.Packet(idOut.PLAYER_COLOR);
colorPacket.add([this.connection.id, this.color.r, this.color.g, this.color.b]);
exampleServer.broadcast(colorPacket);
}
rename(name) {
this.name = name;
// Rename
var namePacket = new shocknet.Packet(idOut.PLAYER_NAME);
namePacket.add([this.connection.id, this.name]);
exampleServer.broadcast(namePacket);
}
teleport(newX, newY) {
this.x = newX;
this.y = newY;
// Teleport
var teleportPacket = new shocknet.Packet(idOut.PLAYER_MOVE);
teleportPacket.add([this.connection.id, this.x, this.y]);
exampleServer.broadcast(teleportPacket);
}
kick(reason) {
var kickPacket = new shocknet.Packet(idOut.PLAYER_KICK);
kickPacket.add([this.connection.id, reason]);
this.connection.send(kickPacket);
this.connection.kick();
}
}
// Functions
function sendPlayers(connection) {
for (var i = 0; i < players.length; i++) {
var sendPlayer = players[i];
sendPlayer.send(connection);
}
}
function getPlayer(connection) {
for (var i = 0; i < players.length; i++) {
var searchPlayer = players[i];
if (searchPlayer.connection.id === connection.id) {
return searchPlayer;
}
}
}
// Server logic
exampleServer.on('ready', function() {
console.log('Server listening on port '+exampleServer.port);
});
exampleServer.on('packet', function(connection, packet) {
switch(packet.netId) {
case idIn.PLAYER_SYNC:
var syncPacket = new shocknet.Packet(idOut.PLAYER_MOVE);
// In the real world you'll want to validate the data
syncPacket.add([connection.id, packet.get(0), packet.get(1)]);
// connection.broadcast sends a packet to all connections EXCEPT the one is broadcasting from
connection.broadcast(syncPacket);
break;
case idIn.MESSAGE:
var player = getPlayer(connection);
if (packet.get(0) === 'kick') {
player.kick('oof');
}
var message = player.name+': '+packet.get(0);
var messagePacket = new shocknet.Packet(idOut.MESSAGE);
messagePacket.add(message);
connection.broadcast(messagePacket);
break;
}
});
exampleServer.on('connect', function(connection) {
console.log('Connection opened ('+connection.id+')');
// Send all other players to new connection
sendPlayers(connection);
// Create new player object
var newPlayer = new Player(connection);
players.push(newPlayer);
// Teleport
newPlayer.teleport(500, 600);
// Send welcome message
var helloPacket = new shocknet.Packet(idOut.MESSAGE);
helloPacket.add('Welcome to the Shocknet example!');
connection.send(helloPacket);
});
exampleServer.on('disconnect', function(connection) {
console.log('Connection closed ('+connection.id+')');
// Remove player from list
for (var i = 0; i < players.length; i++) {
var rm = players[i];
if (rm.connection.id === connection.id) {
players.splice(i, 1);
break;
}
}
// Tell connections to remove player
var removePacket = new shocknet.Packet(idOut.PLAYER_REMOVE);
removePacket.add(connection.id);
exampleServer.broadcast(removePacket);
});
// Start
exampleServer.listen(1000);