more on round actor messages
parent
53cf280c22
commit
21c948ca7e
|
@ -150,7 +150,7 @@ private[controllers] trait LilaController
|
|||
fua flatMap { _.fold(notFound(ctx))(a ⇒ op(a)) }
|
||||
|
||||
protected def notFound(implicit ctx: Context): Fu[Result] =
|
||||
ctx.req.headers get "X-Requested-With" ?? ("XMLHttpRequest" ==) fold (
|
||||
(ctx.req.headers get "X-Requested-With") ?? ("XMLHttpRequest" ==) fold (
|
||||
NotFound("resource not found").fuccess,
|
||||
Lobby handleNotFound ctx
|
||||
)
|
||||
|
|
|
@ -3,8 +3,9 @@ package controllers
|
|||
import lila.app._
|
||||
import views._
|
||||
import lila.user.{ Context, UserRepo }
|
||||
import lila.game.{ Pov, GameRepo, Game ⇒ GameModel }
|
||||
import lila.game.{ Pov, PlayerRef, GameRepo, Game ⇒ GameModel }
|
||||
import lila.round.{ RoomRepo, WatcherRoomRepo }
|
||||
import lila.round.actorApi.round._
|
||||
import lila.socket.actorApi.{ Forward, GetVersion }
|
||||
import lila.tournament.{ TournamentRepo, Tournament ⇒ Tourney }
|
||||
|
||||
|
@ -92,36 +93,32 @@ object Round extends LilaController with TheftPrevention with RoundEventPerforme
|
|||
pov, v, roomHtml, bookmarkers, analysed, tour))
|
||||
}
|
||||
|
||||
private def hand = env.hand
|
||||
def abort(fullId: String) = performAndRedirect(fullId, hand.abort)
|
||||
def resign(fullId: String) = performAndRedirect(fullId, hand.resign)
|
||||
def resignForce(fullId: String) = performAndRedirect(fullId, hand.resignForce)
|
||||
def drawClaim(fullId: String) = performAndRedirect(fullId, hand.drawClaim)
|
||||
def drawAccept(fullId: String) = performAndRedirect(fullId, hand.drawAccept)
|
||||
def drawOffer(fullId: String) = performAndRedirect(fullId, hand.drawOffer)
|
||||
def drawCancel(fullId: String) = performAndRedirect(fullId, hand.drawCancel)
|
||||
def drawDecline(fullId: String) = performAndRedirect(fullId, hand.drawDecline)
|
||||
def abort(fullId: String) = performAndRedirect(fullId, Abort(_))
|
||||
def resign(fullId: String) = performAndRedirect(fullId, Resign(_))
|
||||
def resignForce(fullId: String) = performAndRedirect(fullId, ResignForce(_))
|
||||
def drawClaim(fullId: String) = performAndRedirect(fullId, DrawClaim(_))
|
||||
def drawAccept(fullId: String) = performAndRedirect(fullId, DrawAccept(_))
|
||||
def drawOffer(fullId: String) = performAndRedirect(fullId, DrawOffer(_))
|
||||
def drawCancel(fullId: String) = performAndRedirect(fullId, DrawCancel(_))
|
||||
def drawDecline(fullId: String) = performAndRedirect(fullId, DrawDecline(_))
|
||||
|
||||
def rematch(fullId: String) = Open { implicit ctx ⇒
|
||||
Env.setup.rematcher offerOrAccept fullId fold (
|
||||
err ⇒ {
|
||||
logwarn(err.getMessage)
|
||||
Redirect(routes.Round.player(fullId))
|
||||
}, {
|
||||
Env.setup.rematcher offerOrAccept PlayerRef(fullId) fold (
|
||||
_ ⇒ Redirect(routes.Round.player(fullId)), {
|
||||
case (nextFullId, events) ⇒ {
|
||||
performEvents(fullId)(events)
|
||||
sendEvents(fullId)(events)
|
||||
Redirect(routes.Round.player(nextFullId))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
def rematchCancel(fullId: String) = performAndRedirect(fullId, hand.rematchCancel)
|
||||
def rematchDecline(fullId: String) = performAndRedirect(fullId, hand.rematchDecline)
|
||||
def rematchCancel(fullId: String) = performAndRedirect(fullId, RematchCancel(_))
|
||||
def rematchDecline(fullId: String) = performAndRedirect(fullId, RematchDecline(_))
|
||||
|
||||
def takebackAccept(fullId: String) = performAndRedirect(fullId, hand.takebackAccept)
|
||||
def takebackOffer(fullId: String) = performAndRedirect(fullId, hand.takebackOffer)
|
||||
def takebackCancel(fullId: String) = performAndRedirect(fullId, hand.takebackCancel)
|
||||
def takebackDecline(fullId: String) = performAndRedirect(fullId, hand.takebackDecline)
|
||||
def takebackAccept(fullId: String) = performAndRedirect(fullId, TakebackAccept(_))
|
||||
def takebackOffer(fullId: String) = performAndRedirect(fullId, TakebackOffer(_))
|
||||
def takebackCancel(fullId: String) = performAndRedirect(fullId, TakebackCancel(_))
|
||||
def takebackDecline(fullId: String) = performAndRedirect(fullId, TakebackDecline(_))
|
||||
|
||||
def tableWatcher(gameId: String, color: String) = Open { implicit ctx ⇒
|
||||
OptionOk(GameRepo.pov(gameId, color)) { html.round.table.watch(_) }
|
||||
|
|
|
@ -3,28 +3,30 @@ package controllers
|
|||
import lila.app._
|
||||
import lila.game.Event
|
||||
import lila.socket.actorApi.Forward
|
||||
import lila.game.Game.takeGameId
|
||||
import lila.hub.actorApi.map.Ask
|
||||
import lila.game.Game.{ takeGameId, takePlayerId }
|
||||
import makeTimeout.large
|
||||
|
||||
import akka.pattern.ask
|
||||
import play.api.mvc._, Results._
|
||||
|
||||
trait RoundEventPerformer {
|
||||
|
||||
protected type FuEvents = Fu[List[Event]]
|
||||
|
||||
protected def performAndRedirect(fullId: String, op: String ⇒ FuEvents) =
|
||||
protected def performAndRedirect(fullId: String, makeMessage: String ⇒ Any) =
|
||||
Action {
|
||||
perform(fullId, op)
|
||||
Redirect(routes.Round.player(fullId))
|
||||
Async {
|
||||
perform(fullId, makeMessage) inject Redirect(routes.Round.player(fullId))
|
||||
}
|
||||
}
|
||||
|
||||
protected def perform(fullId: String, op: String ⇒ FuEvents) {
|
||||
op(fullId) effectFold (
|
||||
err ⇒ logwarn("[round] fail to perform on game %s\n%s".format(fullId, err)),
|
||||
performEvents(fullId)
|
||||
)
|
||||
}
|
||||
protected def perform(fullId: String, makeMessage: String ⇒ Any): Fu[List[Event]] =
|
||||
Env.round.roundMap ?
|
||||
Ask(takeGameId(fullId), makeMessage(takePlayerId(fullId))) mapTo
|
||||
manifest[List[Event]] logFailure
|
||||
"[round] fail to perform on game %s".format(fullId) addEffect
|
||||
{ sendEvents(fullId) _ }
|
||||
|
||||
protected def performEvents(fullId: String)(events: List[Event]) {
|
||||
Env.round.socketHub ! Forward(takeGameId(fullId), events)
|
||||
protected def sendEvents(gameId: String)(events: List[Event]) {
|
||||
Env.round.socketHub ! Forward(gameId, events)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ object Setup extends LilaController with TheftPrevention with RoundEventPerforme
|
|||
Redirect(routes.Round.watcher(id, game.creatorColor.name)),
|
||||
_ map {
|
||||
case (p, events) ⇒ {
|
||||
performEvents(p.gameId)(events)
|
||||
sendEvents(p.gameId)(events)
|
||||
Redirect(routes.Round.player(p.fullId))
|
||||
}
|
||||
})
|
||||
|
|
|
@ -148,6 +148,8 @@ trait WithPlay extends Zeros { self: PackageObject ⇒
|
|||
case e ⇒ throw e
|
||||
})
|
||||
|
||||
def addEffect(effect: A => Unit) = fua ~ (_ foreach effect)
|
||||
|
||||
def thenPp: Fu[A] = fua ~ {
|
||||
_.effectFold(
|
||||
e ⇒ logwarn("[failure] " + e),
|
||||
|
|
|
@ -379,6 +379,7 @@ object Game {
|
|||
def abandonedDate = DateTime.now - 10.days
|
||||
|
||||
def takeGameId(fullId: String) = fullId take gameIdSize
|
||||
def takePlayerId(fullId: String) = fullId drop gameIdSize
|
||||
|
||||
def make(
|
||||
game: ChessGame,
|
||||
|
|
|
@ -43,5 +43,5 @@ case class PlayerRef(gameId: String, playerId: String)
|
|||
|
||||
object PlayerRef {
|
||||
|
||||
def apply(fullId: String): PlayerRef = PlayerRef(fullId take Game.gameIdSize, fullId drop Game.gameIdSize)
|
||||
def apply(fullId: String): PlayerRef = PlayerRef(Game takeGameId fullId, Game takePlayerId fullId)
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ private[importer] final class Importer(
|
|||
case Result(Status.Draw, _) ⇒ finisher drawForce game
|
||||
case Result(Status.Resign, Some(color)) ⇒ roundMap ? Ask(
|
||||
game.id,
|
||||
Resign(game.player(!color).id, false)
|
||||
Resign(game.player(!color).id)
|
||||
)
|
||||
case _ ⇒ funit
|
||||
}).void
|
||||
|
|
|
@ -71,11 +71,11 @@ private[round] final class Round(
|
|||
}
|
||||
}
|
||||
|
||||
case Abort(playerId) ⇒ sender ! blocking(playerId)(finisher.abort)
|
||||
case Abort(playerId) ⇒ sender ! blocking(playerId)(finisher.abort)
|
||||
|
||||
case Resign(playerId, false) ⇒ sender ! blocking(playerId)(finisher.resign)
|
||||
case Resign(playerId) ⇒ sender ! blocking(playerId)(finisher.resign)
|
||||
|
||||
case Resign(playerId, true) ⇒ sender ! blocking(playerId) { pov ⇒
|
||||
case ResignForce(playerId) ⇒ sender ! blocking(playerId) { pov ⇒
|
||||
socketHub ? IsGone(pov.game.id, !pov.color) flatMap {
|
||||
case true ⇒ finisher resignForce pov
|
||||
case _ ⇒ funit
|
||||
|
|
|
@ -91,7 +91,7 @@ private[round] final class SocketHandler(
|
|||
token: String,
|
||||
ctx: Context): Fu[JsSocketHandler] =
|
||||
GameRepo.pov(fullId) flatMap {
|
||||
_ ?? { join(_, Some(fullId take Game.gameIdSize), version, uid, token, ctx) }
|
||||
_ ?? { join(_, Some(Game takeGameId fullId), version, uid, token, ctx) }
|
||||
}
|
||||
|
||||
private def join(
|
||||
|
|
|
@ -77,7 +77,8 @@ package round {
|
|||
case class PlayResult(events: Events, fen: String, lastMove: Option[String])
|
||||
|
||||
case class Abort(playerId: String)
|
||||
case class Resign(playerId: String, force: Boolean)
|
||||
case class Resign(playerId: String)
|
||||
case class ResignForce(playerId: String)
|
||||
case class DrawClaim(playerId: String)
|
||||
case class DrawAccept(playerId: String)
|
||||
case class DrawOffer(playerId: String)
|
||||
|
|
Loading…
Reference in New Issue