-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·113 lines (95 loc) · 2.57 KB
/
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
var express = require('express')
, http = require('http')
, app = express()
, server = http.createServer(app)
, io = require('socket.io').listen(server);
app.use(express.static(__dirname + '/'));
server.listen(3000, '0.0.0.0');
var serverLoopMilliseconds = 50
, clientid = ''
, users = Array()
, socketObjects = []
, playerObjects = []
, i = 0;
/**
* Server utility functions
*
*
*/
var checkGUID = function(object, destroy) {
for(var i = 0; i < socketObjects.length; i++) {
if (socketObjects[i].GUID == object.GUID) {
return i;
}
}
return false;
};
var checkAbstractObjectGUID = function(thisEntity, objectName) {
for(var i = 0; i < objectName.length; i++) {
if (objectName[i].GUID == thisEntity.GUID) {
return i;
}
}
return false;
};
// Update some socketObjects keys
var updateSocketObjectKeys = function(object, i) {
for (key in object) {
if (socketObjects[i]) {
if (socketObjects[i][key] == true && key === 'dead'){
return false;
}
else {
socketObjects[i][key] = object[key];
}
}
}
return true;
};
var updatePlayerObjectKeys = function(object, i) {
for (key in object) {
if (playerObjects[i]) {
playerObjects[i][key] = object[key];
}
}
return true;
};
io.sockets.on('connection', function (socket) {
// Creating player array
socket.emit('assignid',i);
i++
// socketObject data stored in server from Host client
socket.on('updateobjects', function (objects) {
socketObjects = objects;
});
// Updates an object sent from a slave client
socket.on('slaveupdateobjects', function (object) {
// Update socketObject Keys
var i = checkGUID(object);
if (i || i === 0) {
updateSocketObjectKeys(object,i);
}
// Send updated object to host
io.sockets.emit('updatehostobject', object);
});
// Adds an object sent from a slave client
socket.on('slaveaddobject', function (object) {
io.sockets.emit('addhostobject', object);
});
// playerObject socket functions
socket.on('playersaddobject', function (object) {
playerObjects.push(object);
});
socket.on('playersupdatedobject', function (object) {
var i = checkAbstractObjectGUID(object, playerObjects);
if (i || i === 0) {
updatePlayerObjectKeys(object,i);
}
});
// Updates player positions to client with serverLoopMilliseconds interval var
// Main server loop
setInterval(function(){
io.sockets.emit('updateplayerobjects',playerObjects);
io.sockets.emit('updateobjects',socketObjects);
}, serverLoopMilliseconds);
});