forked from satellite-game/Satellite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·196 lines (161 loc) · 5.64 KB
/
Copy pathapp.js
File metadata and controls
executable file
·196 lines (161 loc) · 5.64 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// creating global parameters and start
// listening to 'port', we are creating an express
// server and then we are binding it with socket.io
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var port = 1337;
var path = require('path');
var url = 'http://localhost:' + port + '/';
/* We can access nodejitsu enviroment variables from process.env */
/* Note: the SUBDOMAIN variable will always be defined for a nodejitsu app */
if(process.env.SUBDOMAIN){
url = 'http://' + process.env.SUBDOMAIN + '.jit.su/';
}
// listening to port...
server.listen(port);
console.log("Express server listening on port " + port);
console.log(url);
// log it all
io.set('log level', 2);
// give the client access to all client files
app.use(express.static(path.join(__dirname, '/build/client')));
// serving the main applicaion file (index.html)
// when a client makes a request to the app root
// http://localhost:1337
app.get('/', function (req, res) {
res.sendfile(path.join(__dirname, '/build/client/index.html'));
});
// Holds players
var players = {};
var mapItems = [
{ type: 'Alien Space Station', pos: [0, 1000], rot: 0, hp: 100 },
{ type: 'Human Space Station', pos: [0, -1000], rot: Math.PI*2, hp: 100 }
];
var mapSize = 1600;
var maxSpeed = 200; // units per second
function getRandomCoord() {
return Math.random()*mapSize - mapSize/2;
}
function getRandomPosition() {
return [getRandomCoord(), 140, getRandomCoord()];
}
function getTime() {
return (new Date()).getTime();
}
io.sockets.on('connection', function (socket) {
var address = socket.handshake.address;
var ip = socket.handshake.address.address;
console.log("New connection from " + address.address + ":" + address.port);
// Setup message handlers
socket.on('join', function(message) {
console.dir(message);
if (players[message.name] !== undefined && ip === players[message.name].ip) {
console.warn('Error: '+message.name+' tried to join twice!');
return;
}
if (!message.name) {
console.error('Error: Cannot join, player name was null!');
socket.emit('failed');
return false;
}
console.log('Player joined: '+message.name);
// Send list of players
socket.emit('player list', players);
// Send the map to the players
socket.emit('map', mapItems);
var pos = getRandomPosition();
socket.set('name', message.name, function() {
// Store client info
players[message.name] = {
name: message.name,
pos: message.pos,
rot: message.rot,
aVeloc: message.aVeloc,
lVeloc: message.lVeloc,
lastMove: getTime(),
ip: ip
};
var packet = {
name: message.name,
pos: message.pos,
rot: [0, 0, 0],
aVeloc: [0, 0, 0],
lVeloc: [0, 0, 0],
interp: false // Not really necessary here, we're telling the client itself to move
};
socket.emit('move', packet);
// Notify players of new challenger
socket.broadcast.emit('join', {
name: message.name,
pos: message.pos,
rot: message.rot,
aVeloc: message.aVeloc,
lVeloc: message.lVeloc
});
});
});
socket.on('disconnect', function() {
socket.get('name', function (err, name) {
console.log(name+' dropped');
// Remove from client list
delete players[name];
// Notify players
socket.broadcast.emit('leave', {
name: name
});
});
});
socket.on('move', function(message) {
socket.get('name', function (err, name) {
if (players[name]) {
var player = players[name];
// Update position
player.pos = message.pos;
player.rot = message.rot;
player.aVeloc = message.aVeloc;
player.lVeloc = message.lVeloc;
player.lastMove = message.time;
// Notify players
socket.broadcast.emit('move', {
name: name,
pos: message.pos,
rot: message.rot,
aVeloc: message.aVeloc,
lVeloc: message.lVeloc,
interp: true
});
}
else {
socket.emit('failed');
}
});
});
socket.on('hit', function(message) {
socket.broadcast.emit('hit', {
otherPlayerName: message.otherPlayerName,
yourName: message.yourName
});
socket.emit('hit', {
otherPlayerName: message.otherPlayerName,
yourName: message.yourName
});
});
socket.on('killed', function(message) {
socket.broadcast.emit('killed', {
killed: message.you,
killer: message.killer
});
});
socket.on('fire', function(message) {
socket.get('name', function (err, name) {
socket.broadcast.emit('fire', {
name: message.name,
position: message.position,
rotation: message.rotation,
initialVelocity: message.initialVelocity
});
});
});
});