2013-05-18 19:18:41 -06:00
|
|
|
package lila.lobby
|
2013-04-09 06:36:11 -06:00
|
|
|
|
2013-07-19 16:17:00 -06:00
|
|
|
import akka.actor.ActorRef
|
2013-04-09 06:36:11 -06:00
|
|
|
import chess.{ Game ⇒ ChessGame, Board, Variant, Mode, Clock, Color ⇒ ChessColor }
|
2013-12-26 16:31:09 -07:00
|
|
|
import org.joda.time.DateTime
|
2013-12-20 12:55:27 -07:00
|
|
|
|
|
|
|
import actorApi.{ RemoveHook, BiteHook, JoinHook }
|
2013-04-09 06:36:11 -06:00
|
|
|
import lila.game.{ GameRepo, Game, Player, Pov, Progress }
|
2013-05-24 11:04:49 -06:00
|
|
|
import lila.user.{ User, UserRepo }
|
2013-05-18 19:18:41 -06:00
|
|
|
|
2013-12-29 04:38:07 -07:00
|
|
|
private[lobby] final class Biter(blocks: (String, String) ⇒ Fu[Boolean]) {
|
2013-04-09 06:36:11 -06:00
|
|
|
|
2013-12-26 16:31:09 -07:00
|
|
|
def apply(hook: Hook, userId: Option[String], uid: String): Fu[JoinHook] =
|
2013-06-08 19:34:44 -06:00
|
|
|
userId ?? UserRepo.byId flatMap { user ⇒
|
2013-12-26 16:31:09 -07:00
|
|
|
canJoin(hook, user) flatMap {
|
|
|
|
case true ⇒ join(hook, user, uid)
|
|
|
|
case false ⇒ fufail("%s cannot bite hook %s".format(userId, hook.id))
|
|
|
|
}
|
2013-04-09 06:36:11 -06:00
|
|
|
}
|
|
|
|
|
2013-12-26 16:31:09 -07:00
|
|
|
private def join(hook: Hook, userOption: Option[User], uid: String): Fu[JoinHook] = for {
|
2013-05-18 19:18:41 -06:00
|
|
|
ownerOption ← hook.userId ?? UserRepo.byId
|
2013-12-05 14:47:10 -07:00
|
|
|
creatorColor = hook.realColor.resolve
|
2013-04-09 06:36:11 -06:00
|
|
|
game = blame(
|
2013-12-05 14:47:10 -07:00
|
|
|
!creatorColor, userOption,
|
|
|
|
blame(creatorColor, ownerOption, makeGame(hook))
|
2013-04-09 06:36:11 -06:00
|
|
|
).start
|
2013-12-29 04:38:07 -07:00
|
|
|
_ ← GameRepo insertDenormalized game
|
2013-12-26 16:31:09 -07:00
|
|
|
} yield JoinHook(uid, hook, game, creatorColor)
|
2013-04-09 06:36:11 -06:00
|
|
|
|
2013-12-05 14:47:10 -07:00
|
|
|
def blame(color: ChessColor, userOption: Option[User], game: Game) =
|
|
|
|
userOption.fold(game)(user ⇒ game.updatePlayer(color, _ withUser user))
|
2013-04-09 06:36:11 -06:00
|
|
|
|
|
|
|
private def makeGame(hook: Hook) = Game.make(
|
|
|
|
game = ChessGame(
|
|
|
|
board = Board init hook.realVariant,
|
|
|
|
clock = hook.hasClock.fold(
|
|
|
|
hook.time |@| hook.increment apply { (limit, inc) ⇒
|
|
|
|
Clock(limit = limit, increment = inc)
|
|
|
|
},
|
|
|
|
none)
|
|
|
|
),
|
|
|
|
whitePlayer = Player.white,
|
|
|
|
blackPlayer = Player.black,
|
|
|
|
mode = hook.realMode,
|
|
|
|
variant = hook.realVariant,
|
|
|
|
source = lila.game.Source.Lobby,
|
|
|
|
pgnImport = None)
|
|
|
|
|
2013-12-26 16:31:09 -07:00
|
|
|
def canJoin(hook: Hook, user: Option[User]): Fu[Boolean] =
|
|
|
|
if (!hook.open) fuccess(false)
|
|
|
|
else hook.realMode.casual.fold(
|
|
|
|
user.isDefined || hook.allowAnon,
|
|
|
|
user ?? { u ⇒ hook.realRatingRange.fold(true)(_ contains u.rating) }
|
|
|
|
) ?? !{
|
|
|
|
(user |@| hook.userId).tupled ?? {
|
|
|
|
case (u, hookUserId) ⇒ blocks(hookUserId, u.id) >>| blocks(u.id, hookUserId)
|
2013-07-19 16:17:00 -06:00
|
|
|
}
|
2013-12-26 16:31:09 -07:00
|
|
|
}
|
2013-04-09 06:36:11 -06:00
|
|
|
}
|