more tournament join optimization

pull/9856/head
Thibault Duplessis 2021-09-21 21:29:20 +02:00
parent 95fff08b50
commit b6f28e46f1
2 changed files with 17 additions and 10 deletions

View File

@ -206,12 +206,18 @@ final class PlayerRepo(coll: Coll)(implicit ec: scala.concurrent.ExecutionContex
coll.update.one(selectId(player._id), player).void
}
def join(tourId: Tournament.ID, user: User, perfType: PerfType, team: Option[TeamID]) =
find(tourId, user.id) flatMap {
def join(
tourId: Tournament.ID,
user: User,
perfType: PerfType,
team: Option[TeamID],
prev: Option[Player]
) =
prev match {
case Some(p) if p.withdraw => coll.update.one(selectId(p._id), $unset("w"))
case Some(_) => funit
case None => coll.insert.one(Player.make(tourId, user, perfType, team))
} void
}
def withdraw(tourId: Tournament.ID, userId: User.ID) =
coll.update.one(selectTourUser(tourId, userId), $set("w" -> true)).void

View File

@ -289,25 +289,26 @@ final class TournamentApi(
promise: Option[Promise[Tournament.JoinResult]]
): Funit =
Sequencing(tourId)(tournamentRepo.enterableById) { tour =>
playerRepo.exists(tour.id, me.id) flatMap { playerExists =>
playerRepo.find(tour.id, me.id) flatMap { prevPlayer =>
import Tournament.JoinResult
val fuResult: Fu[JoinResult] =
if (!playerExists && tour.password.exists(p => !password.has(p))) fuccess(JoinResult.WrongEntryCode)
if (prevPlayer.isEmpty && tour.password.exists(p => !password.has(p)))
fuccess(JoinResult.WrongEntryCode)
else
getVerdicts(tour, me.some, getUserTeamIds, playerExists) flatMap { verdicts =>
getVerdicts(tour, me.some, getUserTeamIds, prevPlayer.isDefined) flatMap { verdicts =>
if (!verdicts.accepted) fuccess(JoinResult.Verdicts)
else if (!pause.canJoin(me.id, tour)) fuccess(JoinResult.Paused)
else {
def proceedWithTeam(team: Option[String]): Fu[JoinResult] =
playerRepo.join(tour.id, me, tour.perfType, team) >>
playerRepo.join(tour.id, me, tour.perfType, team, prevPlayer) >>
updateNbPlayers(tour.id) >>- {
socket.reload(tour.id)
publish()
} inject JoinResult.Ok
withTeamId match {
case None if tour.isTeamBattle && playerExists => proceedWithTeam(none)
case None if tour.isTeamBattle => fuccess(JoinResult.MissingTeam)
case None => proceedWithTeam(none)
case None if tour.isTeamBattle && prevPlayer.isDefined => proceedWithTeam(none)
case None if tour.isTeamBattle => fuccess(JoinResult.MissingTeam)
case None => proceedWithTeam(none)
case Some(team) =>
tour.teamBattle match {
case Some(battle) if battle.teams contains team =>