-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
160 lines (130 loc) · 5.05 KB
/
server.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const Game = require('./config/game').Game;
const Player = require('./config/game').Player;
const games = require('./confic/games');
const server = app.listen(6789, function(){
console.log('listening on port 6789');
})
app.use(bodyParser.json())
app.use(express.static(__dirname + "/public/dist/public"));
const io = require('socket.io')(server)
const gameArray = [];
io.on('connection', socket => {
socket.on('addGame', data => {
let newPlayer = new Player({
name: data.hostname,
color: games.randomColor(),
wpm: 0,
playerId: games.randomPlayerId()
});
newPlayer.save(function(err){
if(err) console.log(err.message);
});
let newGame = new Game({
id: data.id,
title: data.gametitle,
hostname: data.hostname,
players: [newPlayer],
leader: newPlayer,
secondsRemaining: 60,
beingPlayed: false
});
newGame.save(function(err){
if(err) console.log(err.message);
});
gameArray.push(newGame) //////////////////////
socket.join(data.id);
socket.emit('addedGame', {id: data.id, playerId: newPlayer.playerId})
io.emit('gameAdded', {data: newGame})///////////////
});
socket.on('lookForGames', async () => {
const findall = await games.findAllGames();
socket.emit('gameAdded', {data: findall}) // re-emits all games to ViewGamesComponent
});
socket.on('newPlayer', data => {
let newPlayer = new Player({
playerId: games.randomPlayerId(),
name: data.username,
color: games.randomColor(),
wpm: 0
})
newPlayer.save(function(err){
if(err) {
console.log(err.message);
}
})
games.addPlayerToGame(newPlayer, data.id);
console.log("DOUBLE CHECK", newPlayer)
socket.emit('joinOK', {id: data.id, playerId: newPlayer.playerId});
// io.to(data.id).emit('playerJoined', findGameById(data.id));
// Re-emit new array of players to players waiting
games.findGameById(data.id, function(err, game){
if(err){
console.log(err.message);
} else {
io.to(data.id).emit('playerJoined', game);
}
})
})
socket.on('listenForNewPlayers', id => { //when the player gets to the wait screen
socket.join(id)
games.findGameById(id, function(err, gameCallback){
if(err){
console.log(err.message);
} else {
socket.emit('playerJoined', gameCallback);
}
});
});
socket.on('beginWasClicked', id => {
io.to(id).emit('playerClickedCountdown')
})
socket.on('gameHasStarted', id => {
games.updateBeingPlayed(id);
let seconds = 60;
let interval = setInterval( () => {
console.log('seconds:::', seconds)
if(seconds <= 0){
io.to(id).emit('gameHasEnded');
clearInterval(interval);
} else {
seconds--;
games.findLeader(id, function(err, game){
if(err){
console.log(err.message);
} else {
console.log("GAMEjjj ", game);
io.to(id).emit('updateData', {time: seconds, leader: leader})
};
});
};
}, 1000);
});
//TODO:: findgameAndUpdate here
socket.on('playerUpdate', data => {
let game = games.findGameById(data.id, function(err, game){
if(err){
console.log(err.message);
} else {
game.players.map(function(player){
if(player.playerId == data.playerId){
player.wpm = data.wpm;
}
})
}
}) ///////////////////////
// for (let i = 0; i < game.players.length; i++){
// if(game.players[i].playerId == data.playerId){
// game.players[i].wpm = data.wpm;
// }
// }
})
socket.on('playerTypeIndex', data => {
socket.broadcast.to(data.id).emit('otherPlayerIndex', {index: data.index, color: 'purple'})
})
})
app.all('*', function(req, res){
res.sendFile(__dirname + '/public/dist/public/index.html');
})