package com.jdbernard.teammaker public class WeightedChooser extends PlayerChooser { int hardLimit = 5 public Player choose(def players) { // make a proper list players = players.collect { it } def choices = [] def threshold = getThreshold(players) if (log.isTraceEnabled()) { log.trace("Choosing a player weighted by games sat.") log.trace("Players: $players") log.trace("Threshold: $threshold") } // add players, ignoring those past the hard limit players.each { player -> if (player.gamesSat >= threshold) { choices << player player.gamesSat.times { choices << player } } } return chooseRandomly(choices) } public float getOdds(def players, Player player) { def threshold = getThreshold(players) def playerGames = player.gamesSat >= threshold ? player.gamesSat + 1 : 0 def totalGames = players.sum { it.gamesSat >= threshold ? it.gamesSat + 1 : 0 } if (log.isTraceEnabled()) log.trace("Odds for $player: $playerGames / $totalGames = " + ((float) playerGames / (float) totalGames)) return (float) playerGames / (float) totalGames } protected int getThreshold(def players) { int highest = (players.max { it.gamesSat }).gamesSat // everyone has 0 games sat, no threshold if (highest == 0) return 0; // if any players have sat at least one game, exclude those that haven't return Math.max(highest - hardLimit, 1) } }