157 lines
4.7 KiB
JavaScript
157 lines
4.7 KiB
JavaScript
|
require('babel-polyfill');
|
||
|
const express = require('express'),
|
||
|
app = express(),
|
||
|
server = require('http').createServer(app),
|
||
|
io = require('socket.io')(server, {
|
||
|
// wsEngine: 'ws'
|
||
|
}),
|
||
|
exphbs = require('express-handlebars'),
|
||
|
qrCode = require('branded-qr-code');
|
||
|
|
||
|
/** CONSTANTS */
|
||
|
const HOST = 'http://192.168.0.105:8080/client/';
|
||
|
|
||
|
const players = [
|
||
|
{
|
||
|
type: 'mario',
|
||
|
id: guid(),
|
||
|
points: 0
|
||
|
},
|
||
|
{
|
||
|
type: 'luigi',
|
||
|
id: guid(),
|
||
|
points: 0
|
||
|
},
|
||
|
]
|
||
|
console.log(players);
|
||
|
|
||
|
|
||
|
|
||
|
app.set('views', 'src/views');
|
||
|
app.engine('.hbs', exphbs({
|
||
|
extname: '.hbs',
|
||
|
}));
|
||
|
app.set('view engine', '.hbs');
|
||
|
|
||
|
server.listen(8080, function () {
|
||
|
console.log('Listening on *:' + this.address().port);
|
||
|
});
|
||
|
|
||
|
|
||
|
app.get('/assets*', (req, res) => {
|
||
|
if (req.originalUrl.includes('qr.png')) {
|
||
|
const isMario = req.originalUrl.includes('mario');
|
||
|
qrCode.generate({
|
||
|
text: HOST + players[isMario ? 0 : 1].id,
|
||
|
path: __dirname + '/assets/' + (isMario ? 'mario.png' : 'luigi.png'),
|
||
|
ratio: 4,
|
||
|
opt: {
|
||
|
errorCorrectionLevel: 'H',
|
||
|
width: 500,
|
||
|
},
|
||
|
}).then(qr => {
|
||
|
res.send(qr);
|
||
|
});
|
||
|
|
||
|
}
|
||
|
else if (!req.originalUrl.includes('.map'))
|
||
|
res.sendFile(__dirname + req.originalUrl.split('?')[0]);
|
||
|
});
|
||
|
|
||
|
app.get(['/', '/index.html'], (req, res) => {
|
||
|
res.render('server', {
|
||
|
layout: false,
|
||
|
marioURL: HOST + players[0].id,
|
||
|
luigiURL: HOST + players[1].id,
|
||
|
});
|
||
|
});
|
||
|
|
||
|
app.get('/client/:id', (req, res) => {
|
||
|
console.log(req.params.id);
|
||
|
if (req.params.id == players[0].id || req.params.id == players[1].id)
|
||
|
res.render('client', {
|
||
|
layout: false,
|
||
|
id: getPlayerFromId(req.params.id).id,
|
||
|
type: getPlayerFromId(req.params.id).type,
|
||
|
});
|
||
|
else
|
||
|
res.render('client', { layout: false }); //TODO change to error
|
||
|
});
|
||
|
|
||
|
const monitorNSP = io.of('/monitor');
|
||
|
const clientNSP = io.of('/client');
|
||
|
|
||
|
clientNSP.on('connection', socket => {
|
||
|
let id = socket.request.headers.referer.split('/').slice(-1)[0];
|
||
|
if (!getPlayerFromId(id)) { console.error('Wrong ID'); return; }
|
||
|
console.log('Client connected.');
|
||
|
monitorNSP.emit('clientConnect', getPlayerFromId(id).type);
|
||
|
|
||
|
// monitorNSP.emit('updateGameState', 1);
|
||
|
|
||
|
socket.on('moveStart_left', () => {
|
||
|
let pl = getPlayerFromId(socket.request.headers.referer.split('/').slice(-1)[0]);
|
||
|
console.log('moveStart_left: ' + pl.type);
|
||
|
monitorNSP.emit('moveStart_left', pl.type);
|
||
|
});
|
||
|
socket.on('moveStart_right', () => {
|
||
|
let pl = getPlayerFromId(socket.request.headers.referer.split('/').slice(-1)[0]);
|
||
|
console.log('moveStart_right: ' + pl.type);
|
||
|
monitorNSP.emit('moveStart_right', pl.type);
|
||
|
});
|
||
|
socket.on('moveStart_jump', () => {
|
||
|
let pl = getPlayerFromId(socket.request.headers.referer.split('/').slice(-1)[0]);
|
||
|
console.log('moveStart_jump: ' + pl.type);
|
||
|
monitorNSP.emit('moveStart_jump', pl.type)
|
||
|
});
|
||
|
socket.on('moveStart_crouch', () => {
|
||
|
let pl = getPlayerFromId(socket.request.headers.referer.split('/').slice(-1)[0]);
|
||
|
console.log('moveStart_crouch: ' + pl.type);
|
||
|
});
|
||
|
socket.on('moveEnd_left', () => {
|
||
|
let pl = getPlayerFromId(socket.request.headers.referer.split('/').slice(-1)[0]);
|
||
|
console.log('moveEnd_left: ' + pl.type);
|
||
|
monitorNSP.emit('moveEnd_left', pl.type);
|
||
|
});
|
||
|
socket.on('moveEnd_right', () => {
|
||
|
let pl = getPlayerFromId(socket.request.headers.referer.split('/').slice(-1)[0]);
|
||
|
console.log('moveEnd_right: ' + pl.type);
|
||
|
monitorNSP.emit('moveEnd_right', pl.type);
|
||
|
});
|
||
|
socket.on('moveEnd_jump', () => {
|
||
|
let pl = getPlayerFromId(socket.request.headers.referer.split('/').slice(-1)[0]);
|
||
|
console.log('moveEnd_jump: ' + pl.type);
|
||
|
});
|
||
|
socket.on('moveEnd_crouch', () => {
|
||
|
let pl = getPlayerFromId(socket.request.headers.referer.split('/').slice(-1)[0]);
|
||
|
console.log('moveEnd_crouch: ' + pl.type);
|
||
|
});
|
||
|
|
||
|
|
||
|
socket.on('disconnect', () => {
|
||
|
console.log('Client disconnected.');
|
||
|
monitorNSP.emit('clientDisconnect', getPlayerFromId(id).type);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
|
||
|
monitorNSP.on('connection', socket => {
|
||
|
console.log('Monitor connected.');
|
||
|
|
||
|
socket.on('disconnect', () => {
|
||
|
console.log('Monitor disconnected.');
|
||
|
});
|
||
|
});
|
||
|
|
||
|
function guid() {
|
||
|
function s4() {
|
||
|
return Math.floor((1 + Math.random()) * 0x10000)
|
||
|
.toString(16)
|
||
|
.substring(1);
|
||
|
}
|
||
|
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
||
|
}
|
||
|
|
||
|
function getPlayerFromId(id) {
|
||
|
return players.filter(el => el.id == id)[0];
|
||
|
}
|