Created
November 18, 2013 01:05
-
-
Save tobz/7520757 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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