Skip to content

Instantly share code, notes, and snippets.

@chrisvfritz
Created November 5, 2016 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrisvfritz/459a93673c5f2e362464125381e51515 to your computer and use it in GitHub Desktop.
Save chrisvfritz/459a93673c5f2e362464125381e51515 to your computer and use it in GitHub Desktop.
function getPlayers () {
var players = [
{ name: 'Alice', score: 99 },
{ name: 'Billy', score: 83 },
{ name: 'Cindy', score: 91 },
{ name: 'David', score: 96 },
{ name: 'Emily', score: 88 }
]
function randomIndex () {
return Math.floor(Math.random() * players.length)
}
var playerNum = 1
setInterval(function () {
players.forEach(function (player) {
player.score += Math.random() * 10
})
if (players.length > 3 && Math.random() < 0.05) {
players.splice(randomIndex(), 1)
}
if (Math.random() < 0.05) {
players.splice(randomIndex(), 0, {
name: 'Player ' + playerNum++,
score: players[randomIndex()].score
})
}
}, 200)
return players
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment