-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·193 lines (173 loc) · 8.65 KB
/
Copy pathapp.js
File metadata and controls
executable file
·193 lines (173 loc) · 8.65 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
const game = io.of('/game');
const games = [];
var nextAvailableGameRoom = (function(player) {
var sentences = [
"If you're part of the minority who truly wants to learn more, I'd love to help you with your journey. I welcome you to my website, \"for the minority has already proved themselves.\" This website is not just about being that smart student in class, but also about learning well on your own.",
"If you spend too much time thinking about a thing, you'll never get it done.",
"Failure will never overtake me if my determination to succeed is strong enough.",
"Unfortunately, only having a motive isn't enough. You can be fully determined and motivated to finish a project, but the execution of your project can be a big factor in the efficiency of execution during the process and the quality of your work in the end. As such, execution is one of the biggest factors that determine whether a project will go well...or not. If we glance back to the definition of this very topic, we find that the definition itself points us in the direction of making a specific plan, and calls a project \"a planned undertaking.\"",
"It may be possible to continue on with willpower alone, but the issue is that with pure willpower and a lack of motivation, the quality of work tends to deteoriate, and you start lacking a reason to produce quality work.",
"Motivation doesn't just come from responses in the future, but it can also come from the present, perhaps even on your journey to your goal. I don't particulary advise to spam your walls with motivational posters, but you should always have something that would help you keep moving forward. Your brain loves coming up with negative thoughts, so by keeping positive thoughts ubiquitous, you receive perhaps just a bit of extra motivation to keep you pushing forward.",
"Without a strict schedule, we often waste time deciding on what to do. And believe it or not, these times actually eat away at our willpower, because the act of deciding what to do next itself is something that takes thought and can often lead to wasted time. By keeping with you a path to reach your goals, and knowing what to do next, you eliminate the need to have to think about the next thing to do, and can focus on keeping your progress towards your goal consistent."
];
return function (player) {
let create = (player.wish == 'create');
function createGameRoom(player) {
let minPlayers = (create ? player.minPlayers : 1);
var gameRoom = {
id: (player.roomid ? player.roomid : io.engine.generateId()),
state: (create ? 'custom' : 'waiting'),
sentence: sentences[Math.floor(Math.random() * sentences.length)],
players: [],
start: function () {
game.to(this.id).emit('start game', this);
},
init: function () {
if (gameRoom.players.length >= gameRoom.minPlayers) {
this.countdown();
gameRoom.init = function(){};
} else {
game.to(this.id).emit('waiting for players', gameRoom.minPlayers - gameRoom.players.length);
}
},
minPlayers: parseInt(minPlayers, 10),
maxPlayers: 3,
countdown: (function () {
let count = 10;
return function countdown() {
if (gameRoom.state != 'dead') {
game.to(gameRoom.id).emit('countdown', count--);
if (count >= 0) {
if (count <= 3)
gameRoom.state = 'starting';
setTimeout(function() {
countdown();
}, 1000);
} else {
gameRoom.state = 'active';
gameRoom.start();
gameRoom.endCountdown();
}
}
}
})(),
initialTime: 100,
timeLeft: 100,
endCountdown: (function () {
return function countdown() {
if (gameRoom.state != 'dead') {
game.to(gameRoom.id).emit('end countdown', gameRoom.timeLeft--);
if (gameRoom.timeLeft >= 0) {
setTimeout(function() {
countdown();
}, 1000);
}
}
}
})(),
addPlayer: function(player) {
gameRoom.players.push(player);
if (gameRoom.players.length >= gameRoom.maxPlayers)
gameRoom.state = 'full';
}
};
games.push(gameRoom);
return gameRoom;
}
if (player.wish == 'join') {
let filterFunc;
if (player.roomid)
filterFunc = (gr => gr.id == player.roomid);
else
filterFunc = (gr => gr.state == 'waiting');
let nextAvailableRoom = games.find(filterFunc);
if (nextAvailableRoom !== undefined) {
return nextAvailableRoom;
} else if (player.roomid)
return false;
}
return createGameRoom(player);
}
})();
game.on('connection', function (socket) {
// Find player's game room
function getPlayer(player) {
let [gameIndex, playerIndex] = getPlayerIndices(player);
if (gameIndex !== false && playerIndex !== false)
return games[gameIndex].players[playerIndex];
return false;
}
function getPlayerIndices(player) {
let gameIndex, playerIndex;
gameIndex = games.findIndex(g => g.id == player.roomid);
if (gameIndex > -1) {
playerIndex = games[gameIndex].players.findIndex(p => p.id == player.id)
if (playerIndex > -1)
return [gameIndex, playerIndex];
}
return [false];
}
function removePlayer(player) {
let [gameIndex, playerIndex] = getPlayerIndices(player);
socket.leave(player.roomid);
if (gameIndex !== false && playerIndex !== false) {
games[gameIndex].players.splice(playerIndex, 1);
if (games[gameIndex].players.length == 0) {
games[gameIndex].state = 'dead';
games.splice(gameIndex, 1);
}
}
}
socket.on('join game', function(player, setPlayer) {
let playerExists = getPlayer(player);
if (playerExists === false) {
let gameRoom = nextAvailableGameRoom(player);
if (gameRoom === false) {
setPlayer(false, 'Room not found.');
} else {
socket.join(gameRoom.id);
player.roomid = gameRoom.id;
gameRoom.addPlayer(player);
gameRoom.init();
game.to(gameRoom.id).emit('init player', gameRoom);
setPlayer(JSON.stringify({roomid: gameRoom.id}));
}
} else {
setPlayer(false, 'You are already in a game.');
}
});
socket.on('race finished', function (player, setPlayer) {
progressUpdate(player, setPlayer);
});
socket.on('leave game', function (player) {
removePlayer(player);
});
function progressUpdate(player, setPlayer) {
if (player.roomid) {
let gameRoom = games.find(g => player.roomid == g.id);
if (gameRoom) {
let timeElapsed = gameRoom.initialTime - gameRoom.timeLeft;
let newWPM = Math.round(player.charIndex / 5 * 60 / timeElapsed);
setPlayer(JSON.stringify({wpm: newWPM}));
player.wpm = newWPM;
game.in(player.roomid).emit('progress update', player);
}
}
}
socket.on('progress update', progressUpdate);
socket.on('disconnect', function() {
});
});
io.on('connection', function(socket) {
});
http.listen(port, function() {
console.log(`listening on ${port}`);
});