2019-07-28 21:33:48 +00:00
|
|
|
var express = require('express');
|
|
|
|
var app = express();
|
2019-07-28 01:26:48 +00:00
|
|
|
var http = require('http').createServer(app);
|
|
|
|
var io = require('socket.io')(http);
|
2019-07-28 21:33:48 +00:00
|
|
|
var path = require('path');
|
2019-07-28 01:26:48 +00:00
|
|
|
|
2019-07-28 21:33:48 +00:00
|
|
|
var publicpath = path.join(__dirname, '/views');
|
|
|
|
app.use('/', express.static(publicpath));
|
2019-07-28 01:26:48 +00:00
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.sendFile(__dirname + '/views/index.html');
|
|
|
|
});
|
|
|
|
|
2019-08-02 13:58:34 +00:00
|
|
|
var buffer = {};
|
|
|
|
var users = {};
|
|
|
|
var targetcount = 0;
|
|
|
|
|
2019-07-28 01:26:48 +00:00
|
|
|
io.on('connection', (socket) => {
|
|
|
|
console.log('Nutzer verbunden!');
|
2019-07-28 21:33:48 +00:00
|
|
|
userConnected(socket.id);
|
|
|
|
|
2019-08-02 13:58:34 +00:00
|
|
|
|
2019-07-28 01:26:48 +00:00
|
|
|
socket.on('disconnect', () => {
|
2019-07-28 21:33:48 +00:00
|
|
|
console.log(socket.id, 'Nutzer verbindung unterbrochen');
|
|
|
|
socket.broadcast.emit('cursor_remove', { id: socket.id });
|
|
|
|
userDisconnected(socket.id);
|
2019-07-28 01:26:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('cursor_pos', function (data) {
|
|
|
|
if (buffer[data.id] == null) {
|
|
|
|
buffer[data.id] = data.pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.pos.x != buffer[data.id].x) {
|
|
|
|
socket.broadcast.emit('cursor_draw', { pos: data.pos, id: data.id });
|
|
|
|
buffer[data.id] = data.pos;
|
|
|
|
}
|
|
|
|
});
|
2019-07-28 21:33:48 +00:00
|
|
|
|
|
|
|
socket.on('nickname', function (data) {
|
|
|
|
socket.broadcast.emit('nickname_set', { name: data, id: socket.id });
|
|
|
|
});
|
2019-08-02 13:58:34 +00:00
|
|
|
|
|
|
|
socket.on('remove_target', function (data) {
|
|
|
|
socket.broadcast.emit('remove_target', data);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('test', function () {
|
|
|
|
createTarget();
|
|
|
|
});
|
|
|
|
|
|
|
|
function createTarget() {
|
|
|
|
var pos = {};
|
|
|
|
pos.x = getRandomArbitrary(10, 720);
|
|
|
|
pos.y = getRandomArbitrary(10, 720);
|
|
|
|
function getRandomArbitrary(min, max) {
|
|
|
|
return Math.random() * (max - min) + min;
|
|
|
|
}
|
|
|
|
spawnTarget(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
function spawnTarget(pos) {
|
|
|
|
targetcount++;
|
|
|
|
socket.emit('create_target', { id: targetcount, pos: pos });
|
|
|
|
socket.broadcast.emit('create_target', { id: targetcount, pos: pos });
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-07-28 01:26:48 +00:00
|
|
|
});
|
|
|
|
|
2019-08-02 13:58:34 +00:00
|
|
|
|
|
|
|
|
2019-07-28 21:33:48 +00:00
|
|
|
function userConnected(id) {
|
|
|
|
users[id] = {
|
|
|
|
Name: "unnamed",
|
|
|
|
Points: 0
|
|
|
|
};
|
|
|
|
console.log("Add " + id);
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
function userDisconnected(id) {
|
|
|
|
delete users[id];
|
|
|
|
console.log("Remove " + id);
|
|
|
|
};
|
|
|
|
|
2019-07-28 01:26:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
http.listen(1234, () => {
|
|
|
|
console.log("up and run on port 1234");
|
|
|
|
});
|