Skip to content

Commit

Permalink
api start clocks of AI game
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Feb 4, 2025
1 parent 0a6f8a7 commit c883cea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
31 changes: 20 additions & 11 deletions app/controllers/Challenge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,26 @@ final class Challenge(env: Env) extends LilaController(env):
def apiStartClocks(id: GameId) = Anon:
Found(env.round.proxyRepo.game(id)): game =>
val accepted = OAuthScope.select(_.Challenge.Write).into(EndpointScopes)
(Bearer.from(get("token1")), Bearer.from(get("token2")))
.mapN:
env.oAuth.server.authBoth(accepted, req)
.so:
_.flatMap:
case Left(e) => handleScopedFail(accepted, e)
case Right((u1, u2)) =>
if game.hasUserIds(u1.id, u2.id) then
env.round.roundApi.tell(game.id, lila.core.round.StartClock)
jsonOkResult
else notFoundJson()
def startNow =
env.round.roundApi.tell(game.id, lila.core.round.StartClock)
jsonOkResult
if game.hasAi
then
getAs[Bearer]("token1")
.soFu(env.oAuth.server.auth(_, accepted, req.some))
.mapz:
case Left(e) => handleScopedFail(accepted, e).some
case Right(a) if game.hasUserId(a.scoped.user.id) => startNow.some
case _ => none
.getOrElse(notFoundJson())
else
(getAs[Bearer]("token1"), getAs[Bearer]("token2"))
.mapN(env.oAuth.server.authBoth(accepted, req))
.so:
_.map:
case Left(e) => handleScopedFail(accepted, e)
case Right((u1, u2)) if game.hasUserIds(u1.id, u2.id) => startNow
case _ => notFoundJson()

def toFriend(id: ChallengeId) = AuthBody { ctx ?=> _ ?=>
Found(api.byId(id)): c =>
Expand Down
7 changes: 3 additions & 4 deletions modules/oauth/src/main/OAuthServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ final class OAuthServer(
else fuccess(OAuthScope.Access(OAuthScope.Scoped(u, at.scopes), at.tokenId))
}
}
.dmap(Right.apply)
.recover { case e: AuthError =>
Left(e)
}
.dmap(Right(_))
.recover:
case e: AuthError => Left(e)

def authBoth(scopes: EndpointScopes, req: RequestHeader)(
token1: Bearer,
Expand Down

0 comments on commit c883cea

Please sign in to comment.