Created
September 23, 2011 20:51
-
-
Save bkyrlach/1238407 to your computer and use it in GitHub Desktop.
Ladder simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//A new approach... doesn't work yet. | |
import akka.stm._ | |
import java.util.concurrent.CyclicBarrier | |
import scala.actors.Actor | |
import scala.actors.Actor._ | |
class Player(val id:Int, val matches:Ref[Int], val rating:Ref[Int]) { | |
override def toString():String = { | |
"Player: " + id + " has played " + matches.get + " and has a mmr of " + rating.get | |
} | |
} | |
object Stats { | |
def main(args:Array[String]):Unit = { | |
val r = new java.util.Random | |
val players = Ref((1 to 10000).map { x => new Player(x, Ref(0), Ref(50)) }) | |
def findMatch():Option[(Player, Player)] = { | |
atomic { | |
val stillSearching = players.get.filterNot(_.matches.get == 10000) | |
val p = stillSearching.drop(r.nextInt(stillSearching.size)).headOption | |
p match { | |
case None => None | |
case Some(p:Player) => { | |
val o = stillSearching.drop(r.nextInt(stillSearching.filterNot(_ == p).filter(o => p.rating.get > o.rating.get - 5 && p.rating.get < o.rating.get + 5 ).size)).headOption | |
o match { | |
case None => None | |
case Some(o:Player) => { | |
players.alter(_.filterNot(x => x == p || x == o)) | |
println(players) | |
Some(o,p) | |
} | |
} | |
} | |
} | |
} | |
} | |
lazy val matches:Stream[(Player, Player)] = Stream.cons(findMatch().getOrElse(null), matches) | |
matches.foreach { players => | |
Battler ! Battle(players._1,players._2) | |
} | |
} | |
} | |
case class Battle(p1:Player, p2:Player) | |
object Battler extends Actor { | |
def act = { | |
loop { | |
react { | |
case Battle(p1:Player, p2:Player) => { | |
//println(p1 + " -> " + p2) | |
} | |
case _ => | |
} | |
} | |
} | |
start | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment