lila/modules/tournament/src/main/TeamBattle.scala

72 lines
1.6 KiB
Scala
Raw Normal View History

2019-10-02 04:20:44 -06:00
package lila.tournament
2019-10-02 10:50:09 -06:00
import play.api.data._
2019-12-08 09:58:50 -07:00
import lila.hub.LightTeam.TeamID
2019-10-04 09:28:51 -06:00
import lila.user.User
2019-10-02 04:20:44 -06:00
case class TeamBattle(
2019-12-08 09:58:50 -07:00
teams: Set[TeamID],
2019-10-05 13:52:28 -06:00
nbLeaders: Int
2019-10-02 10:50:09 -06:00
) {
2019-12-13 07:30:20 -07:00
def hasEnoughTeams = teams.size > 1
2019-10-03 02:41:36 -06:00
lazy val sortedTeamIds = teams.toList.sorted
2019-10-02 10:50:09 -06:00
}
2019-10-02 04:20:44 -06:00
object TeamBattle {
2019-12-08 09:58:50 -07:00
def init(teamId: TeamID) = TeamBattle(Set(teamId), 5)
2019-10-04 11:12:26 -06:00
case class TeamVs(teams: chess.Color.Map[TeamID])
2019-10-04 09:28:51 -06:00
case class RankedTeam(
rank: Int,
2019-12-08 09:58:50 -07:00
teamId: TeamID,
2019-10-05 13:52:28 -06:00
leaders: List[TeamLeader]
2019-10-04 09:28:51 -06:00
) {
2019-10-05 13:52:28 -06:00
def magicScore = leaders.foldLeft(0)(_ + _.magicScore)
2019-12-13 07:30:20 -07:00
def score = leaders.foldLeft(0)(_ + _.score)
2019-10-04 09:28:51 -06:00
}
2019-10-05 13:52:28 -06:00
case class TeamLeader(userId: User.ID, magicScore: Int) {
2019-10-03 11:49:44 -06:00
def score: Int = magicScore / 10000
}
2019-10-05 13:52:28 -06:00
case class TeamInfo(
2019-12-08 09:58:50 -07:00
teamId: TeamID,
2019-10-05 13:52:28 -06:00
nbPlayers: Int,
avgRating: Int,
avgPerf: Int,
avgScore: Int,
topPlayers: List[Player]
)
2019-10-02 04:20:44 -06:00
object DataForm {
import play.api.data.Forms._
2019-10-02 10:50:09 -06:00
val fields = mapping(
2019-12-13 07:30:20 -07:00
"teams" -> nonEmptyText,
2019-12-16 10:08:36 -07:00
"nbLeaders" -> number(min = 1, max = 20)
2019-10-02 04:20:44 -06:00
)(Setup.apply)(Setup.unapply)
2019-10-03 02:54:02 -06:00
.verifying("We need at least 2 teams", s => s.potentialTeamIds.size > 1)
2019-12-13 07:30:20 -07:00
.verifying(
"In this version of team battles, no more than 10 teams can be allowed.",
s => s.potentialTeamIds.size <= 10
)
2019-10-02 04:20:44 -06:00
2019-12-13 07:30:20 -07:00
def edit(teams: List[String], nbLeaders: Int) =
Form(fields) fill
Setup(s"${teams mkString "\n"}\n", nbLeaders)
2019-10-04 11:12:26 -06:00
def empty = Form(fields)
2019-10-02 04:20:44 -06:00
case class Setup(
2019-10-04 11:12:26 -06:00
teams: String,
2019-10-05 13:52:28 -06:00
nbLeaders: Int
2019-10-02 04:20:44 -06:00
) {
2019-12-08 09:58:50 -07:00
def potentialTeamIds: Set[TeamID] =
2019-12-02 17:42:57 -07:00
teams.linesIterator.map(_.takeWhile(' ' !=)).filter(_.nonEmpty).toSet
2019-10-02 04:20:44 -06:00
}
}
}