-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
149 lines (123 loc) · 4.3 KB
/
server.js
File metadata and controls
149 lines (123 loc) · 4.3 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
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
136
137
138
139
140
141
142
143
144
145
146
147
//create server hello
const express = require("express");
const app = express();
const path = require("path");
const http = require("http");
const serv = http.Server(app);
const morgan = require("morgan");
app.use(morgan('dev'));
const PORT = process.env.PORT || 3000;
serv.listen(PORT, function(){
console.log("listening at "+PORT);
});
app.use(express.static(__dirname+"/public"));
app.get("/",function(req,res){
res.sendFile('index.html', {root: __dirname});
});
// app.get("/game",function(req,res){
// res.sendFile('game-page.html', {root: __dirname});
// });
if (process.env.NODE_ENV === 'production') {
// Exprees will serve up production assets
app.use(express.static('client/build'));
// Express serve up index.html file if it doesn't recognize route
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
app.use(express.json());
app.use(express.urlencoded());
const chatRouter = require('./routes/post');
app.use(chatRouter);
const createDB = require('./daos/db');
const { loginService } = require("./services/userServices");
createDB();
//player constructor
// const createDB = require('./daos/db');
// createDB();
//loading socket.io and binding to server
const io = require('socket.io')(serv);
var Player = function(name,id){
self = {
name: name,
score : 0,
id: id
}
return self;
}
//list of sockets
SOCKET_LIST = {};
//list of scores&players
SCORES_LIST = {};
io.sockets.on('connection', function(socket) {
console.log(socket.id + 'has joined the game.');
//create a player
var player = new Player(name = 'anonymous', socket.id);
//event thrown when user wants to play as guest
socket.on('username-submit', function(username) {
player.name = username;
console.log("hello" + username); // for debugging
io.emit('participant', '<i>' + player.name + ' joined the game...</i>');
})
SCORES_LIST[socket.id] = player;
SOCKET_LIST[socket.id] = socket;
//this event is thrown when user chooses to login
socket.on('login', function(nameAndPwd){
console.log(`login event!`);
console.log("database time");
player.name = nameAndPwd.name;
loginService(player.name, nameAndPwd.pwd, null);
io.emit('participant', '<i>' + player.name + ' joined the game...</i>');
//we send the event 'participant' and a message including the pseudoname
});
//event thrown when user wants to register
socket.on('register', function(nameAndPwd){
console.log(`login event!`);
console.log("database time");
player.name = nameAndPwd.name;
loginService(player.name, nameAndPwd.pwd, null);
io.emit('participant', '<i>' + player.name + ' joined the game...</i>');
//we send the event 'participant' and a message including the pseudoname
});
//listen for new score updates from user, then change player.score accordingly
socket.on('sendNewScore', function(score) {
player.score = score;
});
//chat feature
socket.on('submitted_message', function(message) {
io.emit('submitted_message', '<strong>' + player.name + ' : </strong>' + message);
console.log(message);
});
socket.on('disconnect', function() {
console.log(socket.id + 'has left the game.')
delete SCORES_LIST[socket.id];
delete SOCKET_LIST[socket.id];
io.emit('participant', '<i>' + player.name + ' left the game...</i>');
});
});
//emit score every 40 milliseconds
setInterval(function(){
// info about name and scores of everyone to be sent to every player
var pack = [];
for(var i in SCORES_LIST){
var score_player = SCORES_LIST[i];
pack.push({
name: score_player.name,
score: score_player.score
})
}
sortedPack = updateLeaderBoard(pack);
for(var j in SOCKET_LIST){
var socket = SOCKET_LIST[j];
socket.emit('updateScores', sortedPack);
}
},50);
function updateLeaderBoard(list) {
let newList = [];
list.sort(function(a, b){ return b.score - a.score });
for(let i = 0; i<list.length; i++){
newList[i] = (i+1) + " - " + list[i].name + " : " + list[i].score;
}
return newList;
}