-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
133 lines (106 loc) · 3.25 KB
/
game.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
const connection = require('./connection')
const io = connection.io
const solve_letters = require('./solver')
const vowels = 'aeiouy'
const consonants = 'bcdfghjklmnpqrstvwxz'
let letters = []
let availableWords = []
let validatedPlayerWords = []
let allPlayerWords = []
let status = false
let amountOfBots = 0
let roundTime = 50
let currentRoundSecond = 50
io.on('connection', socket => {
io.emit('players', io.engine.clientsCount + amountOfBots)
socket.emit('updateGameStatus', status)
// if someone join a game after letters push but before game start
if (currentRoundSecond < 6) {
socket.emit('currentRoundSecond', roundTime - currentRoundSecond)
} else {
socket.emit('currentRoundSecond', currentRoundSecond)
}
socket.on('disconnect', reason => {
io.emit('players', io.engine.clientsCount + amountOfBots)
})
socket.on('pushWord', word => {
allPlayerWords.push(word)
if (availableWords.indexOf(word) !== -1) {
validatedPlayerWords.push(word)
}
})
})
const resetVariables = () => {
letters = []
availableWords = []
validatedPlayerWords = []
allPlayerWords = []
}
const runBots = () => {
amountOfBots = Math.floor(Math.random() * 7) + 4
const botWords = availableWords.filter(word => word.length > 2 && word.length < 5)
io.emit('players', io.engine.clientsCount + amountOfBots)
for (let i = 1; i <= amountOfBots; i++) {
const randomBotWord = botWords[Math.floor(Math.random() * botWords.length)]
allPlayerWords.push(randomBotWord)
validatedPlayerWords.push(randomBotWord)
}
}
const generateNewLetters = () => {
const numberOfVowels = Math.floor(Math.random() * 3) + 3
const numberOfConsonants = 9 - numberOfVowels
for (let i = 0; i < numberOfVowels; i++) {
letters.push(vowels.charAt(Math.floor(Math.random() * vowels.length)))
}
for (let i = 0; i < numberOfConsonants; i++) {
letters.push(consonants.charAt(Math.floor(Math.random() * consonants.length)))
}
letters.sort(function () {
return .5 - Math.random();
})
availableWords = solve_letters(letters.join(''));
// rerun random letters to make sure we have big variety of words
if (!availableWords.length || Array.isArray(availableWords) && availableWords[0] && availableWords[0].length < 8) {
letters = []
availableWords = []
generateNewLetters()
}
}
const gameOn = () => {
resetVariables()
generateNewLetters()
runBots()
currentRoundSecond = 50
// send letters before starting the game to make some time for animation
io.emit('updateLetters', letters, availableWords)
setTimeout(() => {
io.emit('updateGameStatus', true)
}, 5000)
// pause game after 30s
setTimeout(() => {
pauseGame()
}, 35000)
}
const pauseGame = () => {
io.emit('updateGameStatus', false)
currentRoundSecond = 15
// send round results
setTimeout(function () {
validatedPlayerWords.sort(function (a, b) {
return b.length - a.length;
})
io.emit('roundResults', validatedPlayerWords, allPlayerWords)
}, 3000)
// start game after 15s after pause
setTimeout(() => {
gameOn()
}, 15000)
}
setInterval(() => {
if (currentRoundSecond > 0) {
currentRoundSecond--
} else {
currentRoundSecond = roundTime
}
}, 1000)
module.exports = gameOn