Skip to content

Instantly share code, notes, and snippets.

@tobz
Created November 18, 2013 01:05
Show Gist options
  • Save tobz/7520757 to your computer and use it in GitHub Desktop.
Save tobz/7520757 to your computer and use it in GitHub Desktop.
class Arbiter extends Actor {
var occupants:List[String] = List()
def addOccupant(who: String) = {
occupants = occupants ::: List(who)
}
def removeOccupant(who: String) = {
val (b, a) = occupants span (x => x != who)
occupants = (b ::: a.drop(1))
}
def receive = {
case EnterRoom(who) =>
{
addOccupant(who) // Add the person to the room list.
sender ! Message(s"Hello there, $who!") // Send a greeting to the person.
}
case IntroduceRoom =>
{
var message = occupants.foldLeft("")((r,c) => r + (if (r == "") "" else if (occupants.last == c) " and " else ", ") + c)
//var message = occupants.mkString(", ")
sender ! Message(s"Everyone, meet: " + message)
}
case LeaveRoom(who) =>
{
sender ! Message(s"Bye, $who!") // Send a farewell to the person.
removeOccupant(who) // Remove the person from the room list.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment