Start implementing player timeout

This commit is contained in:
Thibault Duplessis 2012-05-09 23:26:59 +02:00
parent c815cb4934
commit add24d54d0
5 changed files with 50 additions and 15 deletions

View file

@ -41,7 +41,8 @@ final class SystemEnv(application: Application) {
lazy val gameHubMaster = Akka.system.actorOf(Props(new game.HubMaster(
makeHistory = gameHistory,
uidTimeout = getMilliseconds("game.uid.timeout"),
hubTimeout = getMilliseconds("game.hub.timeout")
hubTimeout = getMilliseconds("game.hub.timeout"),
playerTimeout = getMilliseconds("game.player.timeout")
)), name = "game_hub_master")
lazy val gameSocket = new game.Socket(

View file

@ -16,15 +16,23 @@ final class Hub(
gameId: String,
history: History,
uidTimeout: Int,
hubTimeout: Int) extends HubActor[Member](uidTimeout) {
hubTimeout: Int,
playerTimeout: Int) extends HubActor[Member](uidTimeout) {
var lastPingTime = nowMillis
// when the players have been seen online for the last time
var whiteTime = nowMillis
var blackTime = nowMillis
def receiveSpecific = {
case Ping(uid) {
ping(uid)
lastPingTime = nowMillis
ownerOf(uid) foreach { o
playerTime(o.color, lastPingTime)
}
}
case Broom {
@ -32,16 +40,19 @@ final class Hub(
if (lastPingTime < (nowMillis - hubTimeout)) {
context.parent ! CloseGame(gameId)
}
Color.all foreach { c
if (playerIsGone(c)) notifyOwner(!c, "gone", JsNull)
}
}
case GetGameVersion(_) sender ! history.version
case IsConnectedOnGame(_, color) sender ! member(color).isDefined
case IsConnectedOnGame(_, color) sender ! ownerOf(color).isDefined
case Join(uid, username, version, color, owner) {
val msgs = {
history since version filter (_.visible(color, owner)) map (_.js)
}:+ makeEvent("crowd", owner.fold(
} :+ makeEvent("crowd", owner.fold(
crowdEvent,
crowdEvent.incWatchers
).data)
@ -66,12 +77,12 @@ final class Hub(
}
}
private def crowdEvent = CrowdEvent(
white = member(White).isDefined,
black = member(Black).isDefined,
def crowdEvent = CrowdEvent(
white = ownerOf(White).isDefined,
black = ownerOf(Black).isDefined,
watchers = members.values count (_.watcher))
private def applyEvents(events: List[Event]) {
def applyEvents(events: List[Event]) {
events match {
case Nil
case single :: Nil notify(single)
@ -79,12 +90,12 @@ final class Hub(
}
}
private def notify(e: Event) {
def notify(e: Event) {
val vevent = history += e
members.values filter vevent.visible foreach (_.channel push vevent.js)
}
private def notify(events: List[Event]) {
def notify(events: List[Event]) {
val vevents = events map history.+=
members.values foreach { member
member.channel push JsObject(Seq(
@ -94,9 +105,29 @@ final class Hub(
}
}
private def makeEvent(t: String, data: JsValue): JsObject =
def notifyOwner(color: Color, t: String, data: JsValue) {
ownerOf(color) foreach { m
m.channel push makeEvent(t, data)
}
}
def makeEvent(t: String, data: JsValue): JsObject =
JsObject(Seq("t" -> JsString(t), "d" -> data))
private def member(color: Color): Option[Member] =
def ownerOf(color: Color): Option[Member] =
members.values find { m m.owner && m.color == color }
def ownerOf(uid: String): Option[Member] =
member(uid) filter (_.owner)
def playerTime(color: Color): Double = color.fold(
whiteTime,
blackTime)
def playerTime(color: Color, time: Double) {
color.fold(whiteTime = time, blackTime = time)
}
def playerIsGone(color: Color) =
playerTime(color) < (nowMillis - playerTimeout)
}

View file

@ -18,7 +18,8 @@ import play.api.Play.current
final class HubMaster(
makeHistory: () History,
uidTimeout: Int,
hubTimeout: Int) extends Actor {
hubTimeout: Int,
playerTimeout: Int) extends Actor {
implicit val timeout = Timeout(1 second)
val log = Logging(context.system, this)
@ -70,6 +71,7 @@ final class HubMaster(
gameId = gameId,
history = makeHistory(),
uidTimeout = uidTimeout,
hubTimeout = hubTimeout
hubTimeout = hubTimeout,
playerTimeout = playerTimeout
)), name = "game_hub_" + gameId)
}

View file

@ -26,6 +26,7 @@ game {
message.lifetime = 30 seconds
uid.timeout = 10 seconds
hub.timeout = 2 minutes
player.timeout = 1 minute
}
site {
uid.timeout = 10 seconds

2
todo
View file

@ -2,6 +2,6 @@ ping nbm
extract chess
chat should autoscroll only when at bottom
back button to game -> old status
bad visibility of online indicators during game
force resign
finish flagged game
static typed conf (also passable!)