-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
103 lines (82 loc) · 3.06 KB
/
app.js
File metadata and controls
103 lines (82 loc) · 3.06 KB
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
/////////////////////////////////////
// Bounty Hunter Game Server
// 2019
// COM2027 - Group 15
/////////////////////////////////////
//set port to either the deployment port or 3000 for development
const PORT = process.env.PORT || 3000;
//include express, http server and socket.io
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
if(process.env.NODE_ENV == 'development'){
require('dotenv').config()
}
//include middleware
const checkAuth = require('./middleware/check-auth');
//include models
const Player = require('./models/player');
const Game = require('./models/game');
// INITAL SERVER SETUP
/////////////////////////////////////////////////////////////////////////
//On the root route, serve the index page which is just a HTML page containing the logo
app.get('/', function(req, res){
res.sendFile(__dirname + '/public/index.html');
});
//Set express to use static files so we can load the logo
app.use(express.static('public'));
//Start listening on the port and serving web pages.
http.listen(PORT, function(){
console.log('Game server listening on *:' + PORT);
});
/////////////////////////////////////////////////////////////////////////
//set up socket io to use some middleware so only authenticated users can connect.
io.use(checkAuth);
// SOCKET IO GAME STUFF STARTS HERE
/////////////////////////////////////////////////////////////////////////
//when the user is connected and authenticated
io.on('connection', function(socket){
//log the user to the server console
console.log(socket.player.user.firstName + ' ' + socket.player.user.lastName + ' connected');
//when a user tries to create a game
socket.on('createGame', (data) => {
//get the data
let players = data.players;
let photo = data.photo;
//try to make the game
try{
//create new game object giving the players who are invited
let game = new Game(players);
//log to console attempting to make game
console.log(socket.player.user.firstName + " " + socket.player.user.lastName + " is trying to create game: " + game.id )
//set the photo for the user
socket.player.photo = photo;
//create the game
game.create(socket);
//set the game for the user
// socket.player.game = game;
}catch(err){
//if the game cannot be created then log the error to the user and console
socket.emit('gameNotCreated', {message: err.message});
console.log(err.message);
}
});
//when a user tries to leave a game
socket.on('leaveGame', () => {
//leave the game
socket.player.game.leave(socket);
});
//when a user tries to join a game
socket.on('joinGame', (data) => {
//get the data
let game_id = data.id;
let photo = data.photo;
//join the game
Game.join(socket, game_id, photo);
});
socket.on('disconnect', function(){
console.log(socket.player.user.firstName + ' ' + socket.player.user.lastName + ' disconnected')
});
});
/////////////////////////////////////////////////////////////////////////