Skip to content

Instantly share code, notes, and snippets.

@mondaychen
Created July 11, 2019 06:56
Show Gist options
  • Save mondaychen/e7040d0c88178cf86522338c9ca210ce to your computer and use it in GitHub Desktop.
Save mondaychen/e7040d0c88178cf86522338c9ca210ce to your computer and use it in GitHub Desktop.
const lodash = require('lodash');
const { random } = lodash;
const online = []
const offline = []
const afk = []
function addPlayer(set, fixPower) {
set.push({
power: fixPower || random(1, 1000, true),
games: 0,
hasPack: false
})
}
for (let i = 0; i < 1000; i++) {
addPlayer(online);
}
for (let i = 0; i < 99000; i++) {
addPlayer(offline);
}
function afterGame(player) {
if (!player.hasPack) {
return
}
const chanceToQuit = 20+player.games
if (random(0, 100) > chanceToQuit) {
online.splice(online.indexOf(player), 1)
const nextPlayer = random(0, offline.length - 1)
online.push(offline[nextPlayer])
offline.splice(nextPlayer, 1)
if (random(0,2) > 0) {
offline.push(player)
} else {
afk.push(player)
}
}
}
function battle() {
const player1 = online[random(0, online.length - 1)]
const player2 = online[random(0, online.length - 1)]
if (player1.power > player2.power) {
player2.power = player1.power
player1.hasPack = true
} else {
player1.power = player2.power
player2.hasPack = true
}
player1.games++
player2.games++
afterGame(player1)
afterGame(player2)
}
let best = 0
online.forEach(p => {
if (p.power > best) {
best = p.power
}
})
offline.forEach(p => {
if (p.power > best) {
best = p.power
}
})
let count100 = 0
let count90 = 0
let count60 = 0
function stats(arr, name) {
console.log(`${name} length: ${arr.length}`)
arr.forEach(player => {
if (!player) {
return
}
if (player.power >= best - 0.0001) {
count100+=1
}
if (player.power >= 990) {
count90+=1
}
if (player.power >= 600) {
count60+=1
}
})
}
stats(online, 'online')
stats(offline, 'offline')
console.log(count100, count90, count60)
for (let i = 0; i < 5000000; i++) {
battle()
if (offline.length === 0) {
console.log('battles: ', i)
break
}
}
// console.log(online, offline, afk)
count100 = 0
count90 = 0
count60 = 0
stats(online, 'online')
// stats(offline, 'offline')
stats(afk, 'afk')
console.log(count100, count90, count60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment