This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
136 lines (111 loc) · 3.46 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Newton Labs
* App Entry file
* 5/02/2017
*/
import socket from 'socket.io';
import express from 'express';
import UUID from 'uuid';
import http from 'http';
const port = process.env.PORT || 4004;
const app = express();
const server = http.createServer(app);
const debug = false;
server.listen(port);
//Log something so we know that it succeeded.
console.log('Express -> listening on ' + port );
//main index.html file
app.get( '/', ( req, res ) => {
if (debug) {
console.log('loading %s', __dirname + '/index.html');
}
res.sendFile( '/client/public/index.html' , { root:__dirname });
});
//send assets, files, images, css, javascript
app.get( '/*' , ( req, res, next ) => {
//This is the current file they have requested
var file = req.params[0];
//show requested files.
if (debug) {
console.log('Express :: file sent : ' + file);
}
//send files
res.sendFile( __dirname + '/' + file );
});
const io = socket.listen(server);
const maxCons = 4;
const game_server = require('./server/server.js');
//let playerNumber = undefined;
//Socket.io will make connections
io.sockets.on('connection', (client) => {
//Generate a new UUID
//and store this on their socket/connection
client.userid = UUID();
console.log('\t socket.io:: player ' + client.userid + ' connected');
//tell player he is connected with id
client.on('createGame', () => {
const { game_id, player_number } = game_server.createGame(client.userid);
client.game_id = game_id;
client.emit('onconnected', {
player: player_number,
player_id: client.userid,
game_id
});
})
client.on('joinGame', (data) => {
const { game_id, player_number } = game_server.findGame(client.userid, data.id_server);
client.game_id = game_id;
client.emit('onconnected', {
player: player_number,
player_id: client.userid,
game_id
});
})
//when player moves on team select
client.on('teamselect', (data) => {
game_server.updatePlayerPosition(data.player, client.userid);
client.emit('updatePosition', {
players: game_server.getPlayersPosition(client.game_id, client.userid)
});
});
client.on('playerUpdate', (data) => {
game_server.updatePlayerPosition(data.player, client.userid);
client.emit('receiveUpdate', {
players: game_server.getPlayersFullPosition(client.game_id, client.userid)
});
});
client.on('getPlayerType', () => {
client.emit('sendPlayerType', {
type: game_server.getPlayerType(client.game_id, client.userid)
})
})
client.on('requestState', () => {
client.emit('getNewState', {
players: game_server.sendNewState(client.game_id)
})
});
client.on('prepareState', () => {
game_server.prepareNewState(client.game_id);
});
client.on('addObject', (data) => {
game_server.addNewObject(client.game_id, data.bulletOrCoin);
client.emit('broadCastObject', {
objects: game_server.broadCastObjects(client.game_id)
})
});
client.on('requestObjects', () => {
client.emit('updateBrodcast', {
objects: game_server.broadCastObjects(client.game_id)
})
})
client.on('removeObject', (data) => {
client.emit('removeBroadcast', {
object_id: game_server.removeObject(client.game_id, data.objectId)
});
})
// Add a disconnect listener
client.on('disconnect', () => {
console.log('client disconnected ' + client.userid);
game_server.endGame(client.userid);
});
}); //client.on