-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (79 loc) · 2.43 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Cheat-Cooperate
* @author Björn Schmidt
* @created 2020-04-27
*/
// Server constants
const VERSION = '1.0.0';
const APPNAME = 'Cheat-Cooperate';
// Dependencies
var express = require('express');
var http = require('http');
var path = require('path');
var socketIO = require('socket.io');
var constants = require('./src/server/constants');
var communication = require('./src/server/communication');
var app = express();
var server = http.Server(app);
var io = socketIO(server);
app.set('port', 5000);
app.use('/static', express.static(__dirname + '/src/client'));
app.use('/res', express.static(__dirname + '/src/resources'));
// Routing
app.get('/', function(request, response) {
response.sendFile(path.join(__dirname, 'src/index.html'));
});
// Starts the server.
server.listen(5000, function() {
console.log('Running '+APPNAME+' v'+VERSION);
console.log('Starting server on port 5000');
});
//------------------------------------------------------------------
// Add the WebSocket handlers
io.on('connection', (socket) => {
console.log('A user connected');
socket.on('create', (roomId) => {
let createdServerSuccessfully = communication.createServer(socket, roomId);
if(createdServerSuccessfully) {
communication.playerJoined(io, socket, roomId);
}
});
socket.on('join', (roomId) => {
let joined = communication.playerJoined(io, socket, roomId);
if(joined) {
socket.emit('joined_game', {id: roomId});
} else {
socket.emit('join_error', 'Game with given ID does not exist or is locked.');
}
});
socket.on('disconnect', () => {
communication.playerDisconnected(io, socket);
});
socket.on('cooperate', (cooperates) => {
try {
communication.sendDecision(io, socket, cooperates);
} catch(aEx) {
console.log('The game has encountered an exception in "communication.sendDecision(...)');
console.log(aEx);
socket.emit('crash');
}
});
socket.on('replay', () => {
try {
communication.replay(io, socket);
} catch(aEx) {
console.log('The game has encountered an exception in "communication.replay(...)');
console.log(aEx);
socket.emit('crash');
}
});
socket.on('vote', () => {
try {
communication.sendStartVote(io, socket);
} catch(aEx) {
console.log('The game has encountered an exception in "communication.sendStartVote(...)');
console.log(aEx);
socket.emit('crash');
}
});
});