-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
75 lines (57 loc) · 2 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
'use strict';
let logger = require('./includes/Logger');
let GameHandler = require('./includes/GameHandler');
let https = require('https');
let fs = require('fs');
let express = require('express');
let app = express();
let privateKey = fs.readFileSync( 'cert.key' );
let certificate = fs.readFileSync( 'cert.pem');
let server = https.createServer({
key: privateKey,
cert: certificate
}, app);
let io = require('socket.io').listen(server);
server.listen(8000);
app.use(express.static(__dirname + '/public/'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
let gameHandler = new GameHandler(io);
io.sockets.on('connection', (socket) => {
logger.info('Client(' + socket.id + ') connected');
socket.on('searchingForGame', () => {
logger.info('Client(' + socket.id + ') is searching for a game');
gameHandler.playerSearchingForGame(socket.id);
});
socket.on('disconnect', () => {
logger.info('Client(' + socket.id + ') disconnected');
gameHandler.closeMatch(socket.id, true);
});
socket.on('chatMessage', (message) => {
if (!message || message.length === 0) {
return;
}
io.emit('newChatMessage', message);
logger.debug('Client(' + socket.id + ') send this chat message: ' + message);
});
socket.on('getRandomGameField', () => {
if (gameHandler.isThisPlayerInAnyMatch(socket.id)) {
gameHandler.getMatch(socket.id).generateNewGameFieldForPlayer(socket.id);
logger.debug('Client(' + socket.id + ') got send a new game field');
}
});
socket.on('playerIsReady', () => {
if (gameHandler.isThisPlayerInAnyMatch(socket.id)) {
logger.debug('Client(' + socket.id + ') is ready to play');
gameHandler.getMatch(socket.id).playerIsReady(socket.id);
}
});
socket.on('clickOnOpponentGameField', (data) => {
if (gameHandler.isThisPlayerInAnyMatch(socket.id)) {
if (gameHandler.getMatch(socket.id).clickOnOpponentGameField(socket.id, data)) {
gameHandler.closeMatch(socket.id, false);
}
}
});
});