persist games in fixed intervals and on shutdown

This commit is contained in:
Zutatensuppe 2020-12-04 00:04:47 +01:00
parent 5160008c89
commit 996822b046
5 changed files with 88 additions and 36 deletions

View file

@ -7,6 +7,19 @@ export const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1
// get one random item from the given array
export const choice = (array) => array[randomInt(0, array.length - 1)]
export const throttle = (fn, delay) => {
let canCall = true
return (...args) => {
if (canCall) {
fn.apply(null, args)
canCall = false
setTimeout(() => {
canCall = true
}, delay)
}
}
}
// return a shuffled (shallow) copy of the given array
export const shuffle = (array) => {
let arr = array.slice()
@ -37,6 +50,7 @@ export default {
uniqId,
randomInt,
choice,
throttle,
shuffle,
timestamp,
}