I was trying to recreate a popular gambling game called crash (like on the website roobet.com/crash). I needed to build a backend server so the current multiplier could be sent to all the players. And I've came up with this code.
function set_result() {
if (!initialized) {
init()
initialized = true
crashed = false
}
curr_time = new Date().getTime()
curr_point = Math.pow(e, (curr_time - start_time) / 1000).toFixed(2)
if (curr_point === crash_point) {
curr_point = 0
crashed = true
initialized = false
}
}
setInterval(set_result, 1)
io.on('connection', async socket => {
async function send_result() {
if (crashed) {
socket.emit('message', {crash: `Crashed at ${crash_point}`})
return
}
socket.emit('message', {crash: curr_point, time: (curr_time - start_time) / 1000})
}
setInterval(() => send_result(), 100)
})
It works perfectly. The result is getting calculated in the background and is being sent to all the connected players. But after every game in crash, there is a few second delay where the players can place their bets. To do that i need to make the set_result() function sleep after curr_point is equal to crash_point. How can i make the set_result() function sleep without stopping the whole server?