diff --git a/modules/tournament/src/main/PlayerRepo.scala b/modules/tournament/src/main/PlayerRepo.scala index b0169d4eeb..037a77df1e 100644 --- a/modules/tournament/src/main/PlayerRepo.scala +++ b/modules/tournament/src/main/PlayerRepo.scala @@ -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 diff --git a/modules/tournament/src/main/TournamentApi.scala b/modules/tournament/src/main/TournamentApi.scala index f5a4ecfa6f..071a1963bf 100644 --- a/modules/tournament/src/main/TournamentApi.scala +++ b/modules/tournament/src/main/TournamentApi.scala @@ -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 =>