Skip to content

Commit

Permalink
serve different flavours of challenge to website, lichobile, and API/…
Browse files Browse the repository at this point in the history
…mobile
  • Loading branch information
ornicar committed Jun 20, 2024
1 parent 03216e0 commit b58a6da
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
31 changes: 19 additions & 12 deletions app/controllers/Challenge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ final class Challenge(
else if isForMe(c) then Direction.In.some
else none
direction.so: dir =>
val json = env.challenge.jsonView(dir.some)(c)
for
fullId <- c.accepted.so(env.round.proxyRepo.game(c.gameId).map2(c.fullIdOf(_, dir)))
socketVersion <- ctx.isMobileOauth.so(env.challenge.version(c.id).dmap(some))
yield JsonOk(json.add("fullId", fullId).add("socketVersion", socketVersion))
json = env.challenge.jsonView.apiAndMobile(c, socketVersion, dir.some, fullId)
yield JsonOk(json)
}

protected[controllers] def showId(id: ChallengeId)(using Context): Fu[Result] =
Expand All @@ -69,7 +69,7 @@ final class Challenge(
if mine then Direction.Out.some
else if isForMe(c) then Direction.In.some
else none
val json = env.challenge.jsonView.show(c, version, direction)
val json = env.challenge.jsonView.websiteAndLichobile(c, version, direction)
negotiate(
html =
val color = get("color").flatMap(Color.fromName)
Expand Down Expand Up @@ -310,15 +310,22 @@ final class Challenge(
JsonBadRequest:
jsonError(lila.challenge.ChallengeDenied.translated(denied))
case _ =>
env.challenge.api.create(challenge).map {
env.challenge.api.create(challenge).flatMap {
if _ then
val json = env.challenge.jsonView
.show(challenge, SocketVersion(0), lila.challenge.Direction.Out.some)
if config.keepAliveStream then
jsOptToNdJson:
ndJson.addKeepAlive(env.challenge.keepAliveStream(challenge, json))
else JsonOk(json)
else JsonBadRequest(jsonError("Challenge not created"))
ctx.isMobileOauth
.so(env.challenge.version(challenge.id).dmap(some))
.map: socketVersion =>
val json = env.challenge.jsonView
.apiAndMobile(
challenge,
socketVersion,
lila.challenge.Direction.Out.some
)
if config.keepAliveStream then
jsOptToNdJson:
ndJson.addKeepAlive(env.challenge.keepAliveStream(challenge, json))
else JsonOk(json)
else JsonBadRequest(jsonError("Challenge not created")).toFuccess
}
yield res
}
Expand Down Expand Up @@ -358,7 +365,7 @@ final class Challenge(
.map: challenge =>
JsonOk:
val url = s"${env.net.baseUrl}/${challenge.id}"
env.challenge.jsonView.show(challenge, SocketVersion(0), none) ++ Json.obj(
env.challenge.jsonView.apiAndMobile(challenge, none, none) ++ Json.obj(
"urlWhite" -> s"$url?color=white",
"urlBlack" -> s"$url?color=black"
)
Expand Down
19 changes: 16 additions & 3 deletions modules/challenge/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import lila.game.JsonView.given
import lila.core.i18n.I18nKey as trans
import lila.core.socket.{ SocketVersion, userLag }
import lila.core.i18n.{ Translate, JsDump }
import lila.core.id.GameFullId

final class JsonView(
baseUrl: lila.core.config.BaseUrl,
Expand Down Expand Up @@ -45,14 +46,26 @@ final class JsonView(
r.key -> JsString(r.trans.txt()))
)

def show(challenge: Challenge, socketVersion: SocketVersion, direction: Option[Direction])(using
Translate
) =
def websiteAndLichobile(
challenge: Challenge,
socketVersion: SocketVersion,
direction: Option[Direction]
)(using Translate) =
Json.obj(
"challenge" -> apply(direction)(challenge),
"socketVersion" -> socketVersion
)

def apiAndMobile(
challenge: Challenge,
socketVersion: Option[SocketVersion],
direction: Option[Direction],
fullId: Option[GameFullId] = none
)(using Translate) =
apply(direction)(challenge)
.add("socketVersion" -> socketVersion)
.add("fullId" -> fullId)

private given OWrites[Challenge.Open] = Json.writes

def apply(direction: Option[Direction])(c: Challenge)(using Translate): JsObject =
Expand Down

0 comments on commit b58a6da

Please sign in to comment.