lila/app/controllers/Racer.scala

76 lines
2.1 KiB
Scala
Raw Normal View History

2021-03-03 12:11:22 -07:00
package controllers
import play.api.mvc._
import views._
import lila.api.Context
import lila.app._
import lila.common.HTTPRequest
2021-03-04 10:01:53 -07:00
import lila.racer.RacerPlayer
2021-03-03 12:11:22 -07:00
import lila.racer.RacerRace
final class Racer(env: Env)(implicit mat: akka.stream.Materializer) extends LilaController(env) {
def home =
Open { implicit ctx =>
NoBot {
2021-03-05 11:18:48 -07:00
Ok(html.racer.home).fuccess
2021-03-03 12:11:22 -07:00
}
}
2021-03-04 02:20:02 -07:00
def create =
2021-03-04 10:01:53 -07:00
WithPlayerId { implicit ctx => playerId =>
2021-03-14 04:48:38 -06:00
env.racer.api.createAndJoin(playerId) map { raceId =>
Redirect(routes.Racer.show(raceId.value))
2021-03-03 12:11:22 -07:00
}
}
def show(id: String) =
2021-03-04 10:01:53 -07:00
WithPlayerId { implicit ctx => playerId =>
env.racer.api.get(RacerRace.Id(id)) match {
case None => Redirect(routes.Racer.home).fuccess
2021-03-14 12:39:52 -06:00
case Some(r) =>
val race = r.isLobby.??(env.racer.api.join(r.id, playerId)) | r
2021-03-05 11:18:48 -07:00
val player = race.player(playerId) | RacerPlayer.make(playerId)
2021-03-04 10:01:53 -07:00
Ok(
html.racer.show(
race,
2021-03-05 11:18:48 -07:00
env.racer.json.data(race, player),
env.storm.json.pref(ctx.pref)
2021-03-04 10:01:53 -07:00
)
2021-03-14 12:39:52 -06:00
).fuccess dmap NoCache
2021-03-04 10:01:53 -07:00
}
}
2021-03-13 09:45:57 -07:00
def rematch(id: String) =
WithPlayerId { implicit ctx => playerId =>
env.racer.api.get(RacerRace.Id(id)) match {
case None => Redirect(routes.Racer.home).fuccess
case Some(race) =>
env.racer.api.rematch(race, playerId) map { rematchId =>
Redirect(routes.Racer.show(rematchId.value))
}
}
}
2021-03-14 04:48:38 -06:00
def lobby =
WithPlayerId { implicit ctx => playerId =>
env.racer.lobby.join(playerId) map { raceId =>
Redirect(routes.Racer.show(raceId.value))
}
}
2021-03-04 10:01:53 -07:00
private def WithPlayerId(f: Context => RacerPlayer.Id => Fu[Result]): Action[Unit] =
2021-03-03 12:11:22 -07:00
Open { implicit ctx =>
NoBot {
2021-03-04 10:01:53 -07:00
HTTPRequest sid ctx.req map { env.racer.api.playerId(_, ctx.me) } match {
case Some(id) => f(ctx)(id)
2021-03-05 11:18:48 -07:00
case None =>
env.lilaCookie.ensureAndGet(ctx.req) { sid =>
f(ctx)(env.racer.api.playerId(sid, none))
}
2021-03-03 12:11:22 -07:00
}
}
}
}