Skip to content

Commit

Permalink
API - publish gameFinish event for #857 (#862)
Browse files Browse the repository at this point in the history
* API - publish gameFinish event for #857

* add game source to gameStart bot/board event

* send more game preview data in event stream gameStart & gameFinish

* add `status` and `winner` fields to the event stream game objects

* Fix AI name typo

---------

Co-authored-by: Thibault Duplessis <t@lichess.org>
  • Loading branch information
ddugovic and ornicar authored Sep 17, 2024
1 parent 8bf8cc4 commit 931487f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 44 deletions.
42 changes: 31 additions & 11 deletions modules/api/src/main/EventStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import org.joda.time.DateTime

import lila.challenge.Challenge
import lila.common.Bus
import lila.game.actorApi.StartGame
import lila.game.Game
import lila.user.{ User, UserRepo }
import lila.game.actorApi.{ FinishGame, StartGame }
import lila.game.{ Game, Pov }
import lila.user.{ LightUserApi, User, UserRepo }

final class EventStream(
challengeJsonView: lila.challenge.JsonView,
challengeMaker: lila.challenge.ChallengeMaker,
onlineApiUsers: lila.bot.OnlineApiUsers,
userRepo: UserRepo
userRepo: UserRepo,
gameJsonView: lila.game.JsonView,
lightUserApi: LightUserApi
)(implicit
ec: scala.concurrent.ExecutionContext,
system: ActorSystem
Expand All @@ -37,7 +39,7 @@ final class EventStream(
Bus.publish(PoisonPill, s"eventStreamFor:${me.id}")

blueprint mapMaterializedValue { queue =>
gamesInProgress map toJson map some foreach queue.offer
gamesInProgress map { gameJson(_, "gameStart", me) } foreach queue.offer
challenges map toJson map some foreach queue.offer

val actor = system.actorOf(Props(mkActor(me, queue)))
Expand All @@ -53,6 +55,7 @@ final class EventStream(

val classifiers = List(
s"userStartGame:${me.id}",
s"userFinishGame:${me.id}",
s"rematchFor:${me.id}",
s"eventStreamFor:${me.id}",
"challenge"
Expand Down Expand Up @@ -95,7 +98,9 @@ final class EventStream(
}
.unit

case StartGame(game) => queue.offer(toJson(game).some).unit
case StartGame(game) => queue.offer(gameJson(game, "gameStart", me)).unit

case FinishGame(game, _, _) => queue.offer(gameJson(game, "gameFinish", me)).unit

case lila.challenge.Event.Create(c) if c.destUserId has me.id => queue.offer(toJson(c).some).unit

Expand All @@ -109,14 +114,29 @@ final class EventStream(
}
}

private def toJson(game: Game) =
Json.obj(
"type" -> "gameStart",
"game" -> Json.obj("id" -> game.id)
)
private def gameJson(game: Game, tpe: String, me: User) =
Pov(game, me) map { pov =>
Json.obj(
"type" -> tpe,
"game" -> {
gameJsonView
.ownerPreview(pov)(lightUserApi.sync)
.add("source" -> game.source.map(_.name)) ++ compatJson(
bot = me.isBot && Game.isBotCompatible(game),
board = Game.isBoardCompatible(game)
) ++ Json.obj(
"id" -> game.id // API BC
)
}
)
}

private def toJson(c: Challenge) =
Json.obj(
"type" -> "challenge",
"challenge" -> challengeJsonView(none)(c)(lila.i18n.defaultLang)
)

private def compatJson(bot: Boolean, board: Boolean) =
Json.obj("compat" -> Json.obj("bot" -> bot, "board" -> board))
}
33 changes: 3 additions & 30 deletions modules/api/src/main/LobbyApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package lila.api

import play.api.libs.json.{ JsArray, JsObject, Json }

import lila.common.Json._
import lila.game.Pov
import lila.lobby.SeekApi

final class LobbyApi(
lightUserApi: lila.user.LightUserApi,
seekApi: SeekApi,
gameProxyRepo: lila.round.GameProxyRepo
gameProxyRepo: lila.round.GameProxyRepo,
gameJson: lila.game.JsonView
)(implicit ec: scala.concurrent.ExecutionContext) {

def apply(implicit ctx: Context): Fu[(JsObject, List[Pov])] =
Expand All @@ -33,32 +33,5 @@ final class LobbyApi(
}
}

def nowPlaying(pov: Pov) =
Json
.obj(
"fullId" -> pov.fullId,
"gameId" -> pov.gameId,
"sfen" -> pov.game.situation.toSfen,
"color" -> pov.color.name,
"lastMove" -> ~pov.game.lastUsiStr,
"variant" -> Json.obj(
"key" -> pov.game.variant.key,
"name" -> pov.game.variant.name
),
"speed" -> pov.game.speed.key,
"perf" -> lila.game.PerfPicker.key(pov.game),
"rated" -> pov.game.rated,
"hasMoved" -> pov.hasMoved,
"opponent" -> Json
.obj(
"id" -> pov.opponent.userId,
"username" -> lila.game.Namer
.playerTextBlocking(pov.opponent, withRating = false)(lightUserApi.sync)
)
.add("rating" -> pov.opponent.rating)
.add("ai" -> pov.opponent.aiLevel)
.add("aiCode" -> pov.opponent.aiCode),
"isMyTurn" -> pov.isMyTurn
)
.add("secondsLeft" -> pov.remainingSeconds)
def nowPlaying(pov: Pov) = gameJson.ownerPreview(pov)(lightUserApi.sync)
}
36 changes: 35 additions & 1 deletion modules/game/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import play.api.libs.json._

import shogi.format.forsyth.Sfen
import shogi.{ Clock, Color }
import lila.common.Json.jodaWrites
import lila.common.Json._
import lila.common.LightUser

final class JsonView(rematches: Rematches) {

Expand Down Expand Up @@ -35,6 +36,39 @@ final class JsonView(rematches: Rematches) {
.add("check" -> game.situation.check)
.add("rematch" -> rematches.of(game.id))
.add("postGameStudy" -> game.postGameStudy)

def ownerPreview(pov: Pov)(lightUserSync: LightUser.GetterSync) =
Json
.obj(
"fullId" -> pov.fullId,
"gameId" -> pov.gameId,
"sfen" -> pov.game.situation.toSfen,
"color" -> pov.color.name,
"lastMove" -> ~pov.game.lastUsiStr,
"source" -> pov.game.source,
"status" -> pov.game.status,
"variant" -> Json.obj(
"key" -> pov.game.variant.key,
"name" -> pov.game.variant.name
),
"speed" -> pov.game.speed.key,
"perf" -> lila.game.PerfPicker.key(pov.game),
"rated" -> pov.game.rated,
"hasMoved" -> pov.hasMoved,
"opponent" -> Json
.obj(
"id" -> pov.opponent.userId,
"username" -> lila.game.Namer
.playerTextBlocking(pov.opponent, withRating = false)(lightUserSync)
)
.add("rating" -> pov.opponent.rating)
.add("ai" -> pov.opponent.aiLevel)
.add("aiCode" -> pov.opponent.aiCode),
"isMyTurn" -> pov.isMyTurn
)
.add("secondsLeft" -> pov.remainingSeconds)
.add("tournamentId" -> pov.game.tournamentId)
.add("winner" -> pov.game.winnerColor)
}

object JsonView {
Expand Down
2 changes: 1 addition & 1 deletion modules/game/src/main/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object Namer {
}
}
) { ec =>
s"${ec.engine.fullName} level ${ec.level}}" // rework to use i18n?
s"${ec.engine.fullName} level ${ec.level}" // rework to use i18n?
}

def gameVsTextBlocking(game: Game, withRatings: Boolean = false)(implicit
Expand Down
6 changes: 5 additions & 1 deletion modules/round/src/main/Finisher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ final private class Finisher(
message foreach { messenger.system(game, _) }
gameRepo game game.id foreach { newGame =>
newGame foreach proxy.setFinishedGame
Bus.publish(finish.copy(game = newGame | game), "finishGame")
val newFinish = finish.copy(game = newGame | game)
Bus.publish(newFinish, "finishGame")
game.userIds.foreach { userId =>
Bus.publish(newFinish, s"userFinishGame:$userId")
}
}
List(lila.game.Event.EndData(game, ratingDiffs))
}
Expand Down

0 comments on commit 931487f

Please sign in to comment.