-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
337 lines (324 loc) · 10.4 KB
/
app.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
var physics = require('./server/serverPhis.js');
var socketProxy = require('./server/socketProxy.js');
var config = require('./config').server;
var clientConfig = require('./config').client;
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var bodyParser = require('body-parser');
var THREE = require('three');
var CANNON = require('cannon');
var mysql = require('mysql');
var mongoose = require('mongoose');
var _istackid = 0;
var debugItem, itemName, cubeItem;
var User = {};
var groundItems = [];
var connection = config.enableMysql ? mysql.createConnection({
host: config.mysql.hostname,
user: config.mysql.username,
password: config.mysql.password,
database: config.mysql.database
}) : null;
mongoose.connect('mongodb://localhost/game');
var db = mongoose.connection;
Object.size = function(obj) {
var size = 0,
key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
Object.allFalse = function(obj) {
for (var i in obj) {
if (i === 'angle') continue;
if (obj[i] === true) return false;
}
return true;
}
var dbUserSchema = mongoose.Schema({
name: String,
position: {
x: {type : Number, default : 0},
y: {type : Number, default : 0},
z: {type : Number, default : 0}
},
health: {type : Number, default : 100},
keyConfig: {
forward: {type : Number, default : 0},
back: {type : Number, default : 0},
left: {type : Number, default : 0},
right: {type : Number, default : 0},
up: {type : Number, default : 0},
down: {type : Number, default : 0},
reset: {type : Number, default : 0},
chat: {type : Number, default : 0},
inventory: {type : Number, default : 0}
},
items: mongoose.Schema.Types.Mixed
});
var dbUser = db.model('User', dbUserSchema);
var Item = function(name, id, onUse, onSecondary) {
this.id = id || Math.floor(Math.random() * 1000);
this.name = name;
this._onUse = onUse;
this._onSecondary = onSecondary;
this.use = function() {
return this._onUse();
}
this.secondary = function() {
return this._onSecondary();
}
this.clone = function() {
return new Item(this.name, this.id, this._onUse, this._onSecondary);
}
return this;
}
var ItemStack = function(item, ammount) {
this.id = _istackid++;
this.ammount = ammount || 1;
this.item = item;
this.name = item.name;
this._unclonedItem = item;
this.addItem = function(num) {
num = num || 1;
this.ammount += num;
return this;
}
this.removeItem = function(num) {
num = num || 1;
this.ammount -= num;
return this;
}
this.use = function() {
return this.item.use();
}
this.secondary = function() {
return this.item.secondary();
}
this.getAmmount = function() {
return this.ammount;
}
this.clone = function() {
return new ItemStack(this._unclonedItem, this.ammount);
}
return this;
}
exports.start = function(port) {
physics.initCannon();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.all('/config.json', function(req, res) {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(clientConfig));
});
app.use(express.static(__dirname + '/'));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
debugItem = new Item("debugItem");
cubeItem = new Item("cubeItem");
io.on('connection', function(socket) {
var userName = "";
var keysBeingPressed = false;
socket.emit('user count', Object.size(User));
socket.on('getKeyConfig', function(name) {
dbUser.findOne({'name':name},function(err,data) {
if(err) return console.err(err);
io.emit('keyConfig',{name:name,config:data.keyConfig})
});
});
socket.on('create user', function(user) {
for (var i in User) {
if (i == user.name) {
console.warn('user ' + user.name + ' is in use');
socket.emit('error', 'user exists');
return;
}
}
socket /*.broadcast.to(socket.id)*/ .emit('user created');
console.info(user.name + " joined ");
userName = user.name;
User[userName] = new THREE.Object3D();
if (config.enableMysql) {
connection.query('SELECT * FROM ' + config.mysql.table, function(err, rows, fields) {
for (var i = 0; i < rows.length; i++) {
if (rows[i].Username == user.cookie && rows[i].Password == user.hashedPassword) {
userName = user.cookie;
User[userName].posSaved = true;
break;
}
}
dbUser.findOne({name:userName},function(err,data){
if(err) return console.error(err);
console.log(data);
if(User[userName].posSaved) {
if(!data) {
User[userName].db = new dbUser({name:userName});
} else {
User[userName].db = data;
}
User[userName].keyConfig = JSON.parse(user.keyConfig);
User[userName].phisObj.position.copy(User[userName].db.position);
User[userName].position.copy(User[userName].db.position);
}
});
});
}
User[userName].sid = socket.id;
User[userName].model = user.model;
User[userName].items = {};
User[userName].health = 100;
User[userName].directionalForce = new CANNON.Vec3(0, 0, 0);
User[userName].jumpForce = new CANNON.Vec3(0, 10, 0);
User[userName].key = {
forward: false,
left: false,
back: false,
right: false,
q: false,
e: false,
down: false,
up: false,
angle: 0
}
for (var i in User) {
if (typeof User[i].position === "undefined") continue;
socket.emit('user joined', {
name: i,
model: User[i].model,
position: User[i].position.toArray(),
rotation: User[i].rotation.toArray()
});
}
physics.capsuleColider(1, 4, User[userName]);
physics.world.addBody(User[userName].phisObj);
socket.broadcast.emit('user joined', {
name: user.name,
model: user.model,
position: User[user.name].position.toArray(),
rotation: User[user.name].rotation.toArray()
});
socket.broadcast.emit('chat message', user.name + " Joined!");
});
socket.on('keys pressed', function(keys) {
if (typeof User[userName] !== "undefined") {
User[userName].keysBeingPressed = true;
User[userName].key = keys;
User[userName].keysBeingPressed = Object.allFalse(keys);
}
});
socket.on('chat message', function(message) {
if (message.substring(0, 1) === "/") {
if (message.substring(1, 10) === "debug_rot") {
console.info(User[userName].rotation);
}
if (message.substring(1, 10) === "debug_pos") {
console.info(User[userName].position);
}
if (message.substring(1, 5) === "kick") {
//NOOP
}
return;
}
console.info("(chat) " + userName + " : " + message);
socket.broadcast.emit('chat message', userName + " : " + message);
socket.emit('chat message', userName + " : " + message);
});
socket.on('disconnect', function() {
if (typeof User[userName] !== "undefined") {
// if(User[userName].posSaved) {
// }
if(User[userName].posSaved) {
console.info("saving userData");
User[userName].db.position = { x :User[userName].position.x, y: User[userName].position.y, z : User[userName].position.z};
User[userName].db.keyConfig = User[userName].keyConfig;
User[userName].db.items = User[userName].items;
User[userName].db.save();
}
physics.world.removeBody(User[userName].phisObj);
delete User[userName];
}
socket.broadcast.emit('user left', userName);
socket.broadcast.emit('chat message', userName + " left");
console.info(userName + " left");
});
socket.on('latencyCheck', function(clientTime) {
socket.emit('latencyCheck', clientTime);
});
});
http.listen(port, function() {
addGroundItem(debugItem, new CANNON.Vec3(Math.floor(Math.random() * 10), -2.5, Math.floor(Math.random() * 10)));
addGroundItem(cubeItem, new CANNON.Vec3(Math.floor(Math.random() * 10), -2.5, Math.floor(Math.random() * 10)));
console.info('listening on ' + port);
});
var mainLoop = function() {
physics.updatePhysics(User);
for (var i in User) {
if (typeof User[i].phisObj === "undefined") continue;
if (User[i].keysBeingPressed) {
User[i].phisObj.velocity.x *= 0.75;
User[i].phisObj.velocity.z *= 0.75;
} else {
User[i].directionalForce.setZero();
if (User[i].key.forward) User[i].directionalForce.add(-Math.sin(User[i].key.angle), 0, -Math.cos(User[i].key.angle));
if (User[i].key.back) User[i].directionalForce.add(Math.sin(User[i].key.angle), 0, Math.cos(User[i].key.angle));
if (User[i].key.left) User[i].directionalForce.add(-Math.sin(User[i].key.angle + Math.PI / 2), 0, -Math.cos(User[i].key.angle + Math.PI / 2));
if (User[i].key.right) User[i].directionalForce.add(Math.sin(User[i].key.angle + Math.PI / 2), 0, Math.cos(User[i].key.angle + Math.PI / 2));
if (User[i].key.up && User[i].canJump) {
User[i].phisObj.applyImpulse(User[i].jumpForce, User[i].phisObj.position);
User[i].canJump = false;
}
if (User[i].key.down) User[i].directionalForce.add(0, -0.25, 0);
User[i].directionalForce.normalize();
User[i].phisObj.applyImpulse(User[i].directionalForce, User[i].phisObj.position);
}
for (var j = 0; j < groundItems.length; j++) {
if (groundItems[j].position.distanceTo(User[i].position) <= 1) {
itemName = groundItems[j].name;
if (typeof User[i].items[itemName] === "undefined") {
User[i].items[groundItems[j].name] = 1;
} else {
User[i].items[itemName]++;
}
io.emit('itemRemove', {
id: groundItems[j].id,
user: i,
itemName: groundItems[j].name
});
groundItems[j].removeItem();
if (groundItems[j].ammount <= 0) groundItems.splice(j, 1);
switch (itemName) {
case "debugItem":
addGroundItem(debugItem, new CANNON.Vec3(Math.floor(-Math.random() * 10), -2.5, -Math.floor(Math.random() * 10)));
break;
case "cubeItem":
addGroundItem(cubeItem, new CANNON.Vec3(Math.floor(Math.random() * 10), -2.5, Math.floor(Math.random() * 10)));
}
}
}
io.emit('itemHeld', {
name: i,
items: User[i].items
});
if (typeof User[i] !== "undefined") {
socketProxy.sendSyncPhisUpdate(io, i, User[i].phisObj.position.toArray(), User[i].phisObj.velocity.toArray(), User[i].phisObj.quaternion.toArray());
}
}
for (var k = 0; k < groundItems.length; k++) {
io.emit('item', {
name: groundItems[k].name,
position: groundItems[k].position,
id: groundItems[k].id
});
}
}
var addGroundItem = function(item, location) {
groundItems.push(new ItemStack(item));
groundItems[groundItems.length - 1].position = location;
return groundItems.length - 1;
}
var interval = setInterval(mainLoop, 1000 / 60);
}