152 lines
3.9 KiB
JavaScript
152 lines
3.9 KiB
JavaScript
|
var app = require('express')();
|
||
|
var http = require('http').Server(app);
|
||
|
var io = require('socket.io')(http);
|
||
|
|
||
|
app.get('/', function(req, res){
|
||
|
res.sendFile(__dirname + '/index.html');
|
||
|
});
|
||
|
|
||
|
var sockets = [];
|
||
|
var gameState = 0;
|
||
|
var width, height;
|
||
|
|
||
|
/** Game-Config **/
|
||
|
var cube_width = 50,
|
||
|
cube_height = 200,
|
||
|
speed = 7;
|
||
|
|
||
|
/** ########## **/
|
||
|
/** CONNECTION **/
|
||
|
/** ########## **/
|
||
|
io.on('connection', function(socket){
|
||
|
var id = sockets.length + 1;
|
||
|
if(sockets.length == 1)
|
||
|
if(sockets[0].id == 2) id = 1;
|
||
|
var clientkey = sockets.push(new Client(id, socket)) - 1;
|
||
|
console.log('Client connected. ID=' + id);
|
||
|
|
||
|
/** ######### **/
|
||
|
/** HANDSHAKE **/
|
||
|
/** ######### **/
|
||
|
socket.emit('handshake', id, gameState, cube_width, cube_height, speed, function(w, h){ // Callback function for 1st Client to set screen size //
|
||
|
sockets[clientkey].width = w;
|
||
|
sockets[clientkey].height = h;
|
||
|
if(sockets.length > 1 && gameState == 0) setUp(socket);
|
||
|
});
|
||
|
|
||
|
if(gameState > 0) socket.emit('size', width, height);
|
||
|
/** #### **/
|
||
|
/** Ping **/
|
||
|
/** ### **/
|
||
|
socket.on('appping', function(fn){
|
||
|
fn(Date.now());
|
||
|
});
|
||
|
|
||
|
/** ######## **/
|
||
|
/** KEYPRESS **/
|
||
|
/** ######## **/
|
||
|
/** TODO!!!! */
|
||
|
socket.on('keydown', function(id, or){
|
||
|
console.log('Keypress'+ id + or);
|
||
|
socket.broadcast.emit('setOrr', id, or);
|
||
|
});
|
||
|
socket.on('keyup', function(id, or){
|
||
|
console.log('Keypress'+ id + or);
|
||
|
socket.broadcast.emit('setOrr', id, or);
|
||
|
});
|
||
|
|
||
|
/** ########## **/
|
||
|
/** DISCONNECT **/
|
||
|
/** ########## **/
|
||
|
socket.on('disconnect', function(){
|
||
|
var index;
|
||
|
sockets.forEach(function(sock, ind){
|
||
|
if (sock.socket == socket){
|
||
|
index = ind;
|
||
|
return;
|
||
|
}
|
||
|
});
|
||
|
var oldid = sockets[index].id;
|
||
|
sockets.splice(index, 1);
|
||
|
console.log('Client disconnected. ID=' + (oldid+1));
|
||
|
if(sockets.length == 1){
|
||
|
sockets[0].socket.emit('test', 'player_left'); //TODO win-event
|
||
|
console.log('Only one Player left. Sending win-event');
|
||
|
// Debug Restart:
|
||
|
gameState = 0;
|
||
|
broadcastGameState(socket);
|
||
|
return;
|
||
|
}
|
||
|
else if(sockets.length == 0){
|
||
|
console.log('All Players left. Reseting lobby...');
|
||
|
gameState = 0;
|
||
|
id = 0;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var key = 0;
|
||
|
if(sockets[0].id == 1 || sockets[0].id == 2) key = 1;
|
||
|
switch(oldid){
|
||
|
case 1 : sockets[key].id = 1; sockets[key].socket.emit('handshake', 1); console.log('New Player 1 selected.'); setUp(); break;
|
||
|
case 2 : sockets[key].id = 2; sockets[key].socket.emit('handshake', 2); console.log('New Player 2 selected.'); setUp(); break;
|
||
|
default : console.log('Spectator left. Nothing need to select new Players.'); break;
|
||
|
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
|
||
|
|
||
|
http.listen(3000, function(){
|
||
|
console.log('listening on *:3000');
|
||
|
});
|
||
|
|
||
|
function setUp(socket){
|
||
|
gameState = 1;
|
||
|
broadcastGameState(socket);
|
||
|
console.log('Starting SetUp...');
|
||
|
/** Select smalest screensize and set for all clients **/
|
||
|
sockets.forEach(function(sock){
|
||
|
broadcastGameState(socket);
|
||
|
if(sock.id == 1)
|
||
|
sockets.forEach(function(sock2){
|
||
|
if(sock2.id == 2){
|
||
|
if(sock.width < sock2.width)
|
||
|
width = sock.width;
|
||
|
else
|
||
|
width = sock2.width;
|
||
|
if(sock.height < sock2.height)
|
||
|
height = sock.height;
|
||
|
else
|
||
|
height = sock2.height;
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
socket.emit('size', width, height);
|
||
|
socket.broadcast.emit('size', width, height);
|
||
|
var i = 5;
|
||
|
var interval = setInterval(function(){
|
||
|
i--;
|
||
|
socket.emit('countdown', i);
|
||
|
socket.broadcast.emit('countdown', i);
|
||
|
if(i == 0){
|
||
|
clearInterval(interval);
|
||
|
gameState = 2;
|
||
|
broadcastGameState(socket);
|
||
|
socket.emit('start');
|
||
|
socket.broadcast.emit('start');
|
||
|
}
|
||
|
}, 1000);
|
||
|
}
|
||
|
|
||
|
function broadcastGameState(socket){
|
||
|
socket.emit('gamestate', gameState);
|
||
|
socket.broadcast.emit('gamestate', gameState);
|
||
|
}
|
||
|
|
||
|
function Client(id, socket){
|
||
|
this.id = id;
|
||
|
this.socket = socket;
|
||
|
this.width = 0;
|
||
|
this.height = 0;
|
||
|
}
|