Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
151 lines (133 sloc) 4.56 KB
package controllers
import java.time.Instant
import com.actionfps.accumulation.{Clan, GameAxisAccumulator}
import com.actionfps.stats.Clanstats.Implicits._
import controllers.ClansController._
import lib.RenderGeneric.PageData.scalaTagsFragToHtml
import lib.RenderGeneric._
import lib._
import play.api.routing.sird._
import scalatags.Text.all._
import views.ClanRankings
import views.rendergame.MixedGame
import scala.async.Async._
import scala.concurrent.ExecutionContext
final class ClansController(
clansProvider: ClansProvider,
clanDataProvider: ClanDataProvider[Clan,
GameAxisAccumulator.ClanwarPlayer])(
implicit executionContext: ExecutionContext) {
import shapeless._
import syntax.std.tuple._
def stablePaths: RenderGeneric.PartialRenderer[PageData] = {
case GET(p"/clans/") =>
() =>
async {
PageData(
Titled("ActionFPS Clans") ::
Contents(views.Clans(await(clansProvider.clans))) :: HNil
)
}
case GET(p"/rankings/") =>
() =>
async {
PageData {
val stats = await(clanDataProvider.clanstats)
.shiftedElo(Instant.now())
.onlyRanked
Titled("Clan Rankings") ::
Contents(ClanRankings.renderWithBottom(stats)) :: HNil
}
}
case GET(p"/clanwars/") =>
() =>
async {
PageData {
Titled("Clanwars") :: Contents(
frag(
views.clanwar.Clanwar.renderClanwars(
renderGame = g =>
raw(
views.rendergame.Render
.renderMixedGame(MixedGame.fromJsonGame(g))
.body),
clanwars = await(clanDataProvider.clanwarsWithMeta)
.sortBy(_.id.firstGame.id)
.reverse
.take(NumberOfClanwarsToShow)
),
navLink
)) :: HNil
}
}
}
def conditionalPaths: RenderGeneric.PartialRenderer[Option[PageData]] = {
case GET(p"/clan/" ? q"id=$id") =>
() =>
async {
await(clansProvider.clans).find(_.id == id) match {
case Some(clan) =>
Some {
PageData {
Titled(s"${clan.fullName}") :: Contents(
views.Clan(
clan,
await(clanDataProvider.clanwarsWithMeta)
.filter(_.teams.teams.exists(_.clanId.id == id))
.sortBy(_.id.firstGame.id)
.reverse
.take(NumberOfClanwarsToShowInClanPage),
await(clanDataProvider.clanstats)
.shiftedElo(Instant.now())
.clans
.find(_._1.id == id)
.map(_._2)
)) :: HNil
}
}
case None =>
None
}
}
case GET(p"/clanwar/" ? q"id=$id") =>
() =>
async {
await(clanDataProvider.clanwarsWithMeta)
.find(_.id.firstGame.id == id) match {
case Some(clanwar) =>
Some {
val title = Titled(
s"${clanwar.teams.teams.map(_.clanId.fullName).mkString(" and ")} - Clanwar")
val htmlFrags = {
val gameView = views.clanwar.Clanwar.renderAsGame(
clanwarMeta = clanwar,
showPlayers = true,
showGames = true,
clanwarHtmlPath = WebTemplateRender.wwwLocation.resolve(
views.clanwar.Clanwar.ClanwarHtmlFile),
renderGame = g =>
raw(
views.rendergame.Render
.renderMixedGame(MixedGame.fromJsonGame(g))
.body)
)
frag(gameView, navLink)
}
PageData(title :: Contents(htmlFrags) :: HNil)
}
}
}
}
}
object ClansController {
val NumberOfClanwarsToShow = 50
val NumberOfClanwarsToShowInClanPage = 15
private val navLink: Frag = tag("nav")(
attr("id") := "bottom-links",
a(
href := "https://github.com/ScalaWilliam/ActionFPS/blob/master/clans/clanwars/pure-clanwar/src/main/scala/com/actionfps/clans/Clanwar.scala",
"View Clanwar Logic"
)
)
private val ClansReferenceKey = "af.reference.clans"
}