Skip to content

Commit

Permalink
show players' teams in team battle games
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Feb 10, 2020
1 parent b069aa1 commit 6669020
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 20 deletions.
11 changes: 4 additions & 7 deletions app/controllers/Round.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import lila.app._
import lila.chat.Chat
import lila.common.HTTPRequest
import lila.game.{ Pov, Game => GameModel, PgnDump }
import lila.tournament.{ TourMiniView, Tournament => Tour }
import lila.tournament.{ Tournament => Tour }
import lila.user.{ User => UserModel }
import views._

Expand All @@ -30,7 +30,7 @@ final class Round(
else
PreventTheft(pov) {
pov.game.playableByAi ?? env.fishnet.player(pov.game)
myTour(pov.game.tournamentId, true) flatMap {
env.tournament.api.miniView(pov.game, true) flatMap {
tour =>
gameC.preloadUsers(pov.game) zip
(pov.game.simulId ?? env.simul.repo.find) zip
Expand Down Expand Up @@ -155,7 +155,7 @@ final class Round(
html = {
if (pov.game.replayable) analyseC.replay(pov, userTv = userTv)
else if (HTTPRequest.isHuman(ctx.req))
myTour(pov.game.tournamentId, false) zip
env.tournament.api.miniView(pov.game, false) zip
(pov.game.simulId ?? env.simul.repo.find) zip
getWatcherChat(pov.game) zip
(ctx.noBlind ?? env.game.crosstableApi.withMatchup(pov.game)) zip
Expand Down Expand Up @@ -200,9 +200,6 @@ final class Round(
) map { NoCache(_) }
}

private def myTour(tourId: Option[String], withTop: Boolean): Fu[Option[TourMiniView]] =
tourId ?? { env.tournament.api.miniView(_, withTop) }

private[controllers] def getWatcherChat(
game: GameModel
)(implicit ctx: Context): Fu[Option[lila.chat.UserChat.Mine]] = {
Expand Down Expand Up @@ -258,7 +255,7 @@ final class Round(

def sides(gameId: String, color: String) = Open { implicit ctx =>
OptionFuResult(proxyPov(gameId, color)) { pov =>
(pov.game.tournamentId ?? env.tournament.tournamentRepo.byId) zip
env.tournament.api.withTeamVs(pov.game) zip
(pov.game.simulId ?? env.simul.repo.find) zip
env.game.gameRepo.initialFen(pov.game) zip
env.game.crosstableApi.withMatchup(pov.game) zip
Expand Down
2 changes: 1 addition & 1 deletion app/templating/TeamHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait TeamHelper { self: HasEnv =>

def teamIdToName(id: String): Frag = StringFrag(env.team.getTeamName(id).getOrElse(id))

def teamLink(id: String, withIcon: Boolean = true): Frag =
def teamLink(id: String, withIcon: Boolean = true) =
a(
href := routes.Team.show(id),
dataIcon := withIcon.option("f"),
Expand Down
2 changes: 1 addition & 1 deletion app/views/game/bits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object bits {
def sides(
pov: Pov,
initialFen: Option[chess.format.FEN],
tour: Option[lila.tournament.Tournament],
tour: Option[lila.tournament.TourAndTeamVs],
cross: Option[lila.game.Crosstable.WithMatchup],
simul: Option[lila.simul.Simul],
userTv: Option[lila.user.User] = None,
Expand Down
15 changes: 9 additions & 6 deletions app/views/game/side.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object side {
def apply(
pov: lila.game.Pov,
initialFen: Option[chess.format.FEN],
tour: Option[lila.tournament.Tournament],
tour: Option[lila.tournament.TourAndTeamVs],
simul: Option[lila.simul.Simul],
userTv: Option[lila.user.User] = None,
bookmarked: Boolean
Expand All @@ -28,7 +28,7 @@ object side {
def meta(
pov: lila.game.Pov,
initialFen: Option[chess.format.FEN],
tour: Option[lila.tournament.Tournament],
tour: Option[lila.tournament.TourAndTeamVs],
simul: Option[lila.simul.Simul],
userTv: Option[lila.user.User] = None,
bookmarked: Boolean
Expand Down Expand Up @@ -88,8 +88,11 @@ object side {
),
div(cls := "game__meta__players")(
game.players.map { p =>
div(cls := s"player color-icon is ${p.color.name} text")(
playerLink(p, withOnline = false, withDiff = true, withBerserk = true)
frag(
div(cls := s"player color-icon is ${p.color.name} text")(
playerLink(p, withOnline = false, withDiff = true, withBerserk = true)
),
tour.flatMap(_.teamVs).map(_.teams(p.color)) map { teamLink(_, false)(cls := "team") }
)
}
)
Expand Down Expand Up @@ -124,8 +127,8 @@ object side {
},
tour.map { t =>
st.section(cls := "game__tournament")(
a(cls := "text", dataIcon := "g", href := routes.Tournament.show(t.id))(t.fullName),
div(cls := "clock", dataTime := t.secondsToFinish)(div(cls := "time")(t.clockStatus))
a(cls := "text", dataIcon := "g", href := routes.Tournament.show(t.tour.id))(t.tour.fullName),
div(cls := "clock", dataTime := t.tour.secondsToFinish)(div(cls := "time")(t.tour.clockStatus))
)
} orElse {
game.tournamentId map { tourId =>
Expand Down
2 changes: 1 addition & 1 deletion app/views/round/bits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ object bits {
)(implicit ctx: Context) = views.html.game.side(
pov,
(data \ "game" \ "initialFen").asOpt[String].map(chess.format.FEN),
tour.map(_.tour),
tour.map(_.tourAndTeamVs),
simul = simul,
userTv = userTv,
bookmarked = bookmarked
Expand Down
10 changes: 10 additions & 0 deletions modules/tournament/src/main/PlayerRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ final class PlayerRepo(coll: Coll)(implicit ec: scala.concurrent.ExecutionContex
}
}

def teamVs(tourId: Tournament.ID, game: lila.game.Game): Fu[Option[TeamBattle.TeamVs]] =
game.twoUserIds ?? {
case (w, b) =>
teamsOfPlayers(tourId, List(w, b)).dmap(_.toMap) map { m =>
(m.get(w) |@| m.get(b)).tupled ?? {
case (wt, bt) => TeamBattle.TeamVs(chess.Color.Map(wt, bt)).some
}
}
}

def countActive(tourId: Tournament.ID): Fu[Int] =
coll.countSel(selectTour(tourId) ++ selectActive)

Expand Down
2 changes: 2 additions & 0 deletions modules/tournament/src/main/TeamBattle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ object TeamBattle {

def init(teamId: TeamID) = TeamBattle(Set(teamId), 5)

case class TeamVs(teams: chess.Color.Map[TeamID])

case class RankedTeam(
rank: Int,
teamId: TeamID,
Expand Down
18 changes: 15 additions & 3 deletions modules/tournament/src/main/TournamentApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,22 @@ final class TournamentApi(
def tournamentTop(tourId: Tournament.ID): Fu[TournamentTop] =
tournamentTopCache get tourId

def miniView(tourId: Tournament.ID, withTop: Boolean): Fu[Option[TourMiniView]] =
tournamentRepo byId tourId flatMap {
def miniView(game: Game, withTop: Boolean): Fu[Option[TourMiniView]] =
withTeamVs(game) flatMap {
_ ?? {
case TourAndTeamVs(tour, teamVs) =>
withTop ?? { tournamentTop(tour.id) map some } map {
TourMiniView(tour, _, teamVs).some
}
}
}

def withTeamVs(game: Game): Fu[Option[TourAndTeamVs]] =
game.tournamentId ?? tournamentRepo.byId flatMap {
_ ?? { tour =>
withTop ?? { tournamentTop(tour.id) map some } map { TourMiniView(tour, _).some }
(tour.isTeamBattle ?? playerRepo.teamVs(tour.id, game)) map {
TourAndTeamVs(tour, _).some
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion modules/tournament/src/main/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ final class LeaderboardRepo(val coll: lila.db.dsl.Coll)

case class TournamentTop(value: List[Player]) extends AnyVal

case class TourMiniView(tour: Tournament, top: Option[TournamentTop])
case class TourMiniView(
tour: Tournament,
top: Option[TournamentTop],
teamVs: Option[TeamBattle.TeamVs]
) {
def tourAndTeamVs = TourAndTeamVs(tour, teamVs)
}

case class TourAndTeamVs(tour: Tournament, teamVs: Option[TeamBattle.TeamVs])

case class MyInfo(rank: Int, withdraw: Boolean, gameId: Option[lila.game.Game.ID]) {
def page = {
Expand Down
3 changes: 3 additions & 0 deletions ui/round/css/_side.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

.game__meta {
flex: 0 0 auto;
&__players .team {
margin-left: 1.7em;
}
}
}

Expand Down

0 comments on commit 6669020

Please sign in to comment.