extract tournament game creation, resign when idle for 20 seconds

This commit is contained in:
Thibault Duplessis 2012-09-17 23:09:13 +02:00
parent 7bcf20ad0c
commit bbc713c6bf
5 changed files with 81 additions and 44 deletions

View file

@ -1,7 +1,7 @@
package lila
package round
import game.{ DbGame, GameRepo, PovRef }
import game.{ DbGame, GameRepo, PovRef, Pov }
import scalaz.effects._
@ -24,17 +24,16 @@ final class Meddler(
)
} yield ()
def resign(pov: Pov): IO[Unit] = (finisher resign pov).fold(
err putStrLn(err.shows),
ioEvents for {
events ioEvents
_ io { socket.send(pov.game.id, events) }
} yield ()
)
def resign(povRef: PovRef): IO[Unit] = for {
povOption gameRepo pov povRef
_ povOption.fold(
pov (finisher resign pov).fold(
err putStrLn(err.shows),
ioEvents for {
events ioEvents
_ io { socket.send(pov.game.id, events) }
} yield ()
),
putStrLn("Cannot resign missing game " + povRef)
)
_ povOption.fold(resign, putStrLn("Cannot resign missing game " + povRef))
} yield ()
}

View file

@ -0,0 +1,60 @@
package lila
package tournament
import game.{ DbGame, DbPlayer, GameRepo, Pov }
import user.User
import round.Meddler
import scalaz.effects._
import play.api.libs.concurrent._
import play.api.Play.current
import akka.util.duration._
final class GameJoiner(
gameRepo: GameRepo,
roundMeddler: Meddler,
timelinePush: DbGame IO[Unit],
getUser: String IO[Option[User]]) {
def apply(tour: Started)(pairing: Pairing): IO[DbGame] = for {
user1 getUser(pairing.user1) map (_ err "No such user " + pairing)
user2 getUser(pairing.user2) map (_ err "No such user " + pairing)
variant = chess.Variant.Standard
game = DbGame(
game = chess.Game(
board = chess.Board init variant,
clock = tour.clock.chessClock.some
),
ai = None,
whitePlayer = DbPlayer.white withUser user1,
blackPlayer = DbPlayer.black withUser user2,
creatorColor = chess.Color.White,
mode = chess.Mode.Casual,
variant = variant
).withTournamentId(tour.id)
.withId(pairing.gameId)
.start
.startClock(2)
_ gameRepo insert game
_ gameRepo denormalizeStarted game
_ timelinePush(game)
_ scheduleIdleCheck(game.id)
} yield game
private def scheduleIdleCheck(gameId: String) = io {
Akka.system.scheduler.scheduleOnce(20 seconds)(idleCheck(gameId))
}
private def idleCheck(gameId: String) {
(for {
gameOption gameRepo game gameId
_ gameOption.fold(
game game.playerWhoDidNotMove.fold(
player roundMeddler resign Pov(game, player),
io()
),
io()
)
} yield ()).unsafePerformIO
}
}

View file

@ -7,14 +7,12 @@ import scalaz.effects._
import scalaz.{ NonEmptyList, Success, Failure }
import play.api.libs.json._
import game.{ DbGame, DbPlayer, GameRepo }
import game.DbGame
import user.User
final class TournamentApi(
repo: TournamentRepo,
gameRepo: GameRepo,
timelinePush: DbGame IO[Unit],
getUser: String IO[Option[User]],
joiner: GameJoiner,
socket: Socket,
siteSocket: site.Socket,
lobbyNotify: String IO[Unit],
@ -24,7 +22,7 @@ final class TournamentApi(
(tour addPairings pairings) |> { tour2
for {
_ repo saveIO tour2
games (pairings map makeGame(tour)).sequence
games (pairings map joiner(tour)).sequence
_ (games map socket.notifyPairing).sequence
} yield ()
}
@ -123,28 +121,4 @@ final class TournamentApi(
siteSocket.sendToFlag("tournament", message)
}
private val reloadSiteSocket = sendToSiteSocket(reloadMessage)
private def makeGame(tour: Started)(pairing: Pairing): IO[DbGame] = for {
user1 getUser(pairing.user1) map (_ err "No such user " + pairing)
user2 getUser(pairing.user2) map (_ err "No such user " + pairing)
variant = chess.Variant.Standard
game = DbGame(
game = chess.Game(
board = chess.Board init variant,
clock = tour.clock.chessClock.some
),
ai = None,
whitePlayer = DbPlayer.white withUser user1,
blackPlayer = DbPlayer.black withUser user2,
creatorColor = chess.Color.White,
mode = chess.Mode.Casual,
variant = variant
).withTournamentId(tour.id)
.withId(pairing.gameId)
.start
.startClock(2)
_ gameRepo insert game
_ gameRepo denormalizeStarted game
_ timelinePush(game)
} yield game
}

View file

@ -37,9 +37,7 @@ final class TournamentEnv(
lazy val api = new TournamentApi(
repo = repo,
gameRepo = gameRepo,
getUser = getUser,
timelinePush = timelinePush,
joiner = joiner,
socket = socket,
siteSocket = siteSocket,
lobbyNotify = lobbyNotify,
@ -74,4 +72,10 @@ final class TournamentEnv(
api = api,
hubMaster = hubMaster
)), name = ActorTournamentOrganizer)
private lazy val joiner = new GameJoiner(
gameRepo = gameRepo,
roundMeddler = roundMeddler,
timelinePush = timelinePush,
getUser = getUser)
}

2
todo
View file

@ -48,5 +48,5 @@ tournament ties
tournament detect leavers and withdraw them (started) (also use force resign)
-> or show current tournament on every page, with (join) and (redraw) buttons
tournament warmup games
tournament give users permanent tourney points
send lobby new forum posts html through websockets
un-ban IP mechanism (deleting cookies)