From ac48dae13d143f78976aaa7c61da16ce6e34b29f Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Sun, 23 Sep 2012 22:38:04 +0200 Subject: [PATCH] send featured game through websockets --- app/controllers/Game.scala | 6 ------ app/game/Featured.scala | 9 ++++----- app/game/GameHelper.scala | 11 +++++++++++ app/lobby/Hub.scala | 8 +++----- app/socket/actorApi.scala | 2 +- app/views/game/featuredJs.scala.html | 13 +++---------- app/views/game/featuredJsNoCtx.scala.html | 3 +++ app/views/lobby/home.scala.html | 6 +++++- conf/routes | 1 - public/javascripts/opening.js | 15 +++------------ public/stylesheets/opening.css | 2 +- todo | 1 - 12 files changed, 34 insertions(+), 43 deletions(-) create mode 100644 app/views/game/featuredJsNoCtx.scala.html diff --git a/app/controllers/Game.scala b/app/controllers/Game.scala index 9456e94cc8..12d2c675d4 100644 --- a/app/controllers/Game.scala +++ b/app/controllers/Game.scala @@ -48,10 +48,4 @@ object Game extends LilaController with BaseGame { Ok(html.game.analysed(analysePaginator games page, makeListMenu)) } } - - def featuredJs(id: String) = Open { implicit ctx ⇒ - IOk(gameRepo game id map { gameOption => - html.game.featuredJs(gameOption) - }) - } } diff --git a/app/game/Featured.scala b/app/game/Featured.scala index 74b4c21267..db5fea3abf 100644 --- a/app/game/Featured.scala +++ b/app/game/Featured.scala @@ -35,12 +35,11 @@ final class Featured( } private def getOne = oneId flatMap fetch filter valid orElse { - feature ~ { o ⇒ - val newOneId = o map (_.id) - newOneId foreach { newId ⇒ - lobbyRef ! ChangeFeatured(oneId, newId) + feature ~ { newOne ⇒ + oneId = newOne map (_.id) + newOne foreach { game ⇒ + lobbyRef ! ChangeFeatured(views.html.game.featuredJsNoCtx(game).toString) } - oneId = newOneId } } diff --git a/app/game/GameHelper.scala b/app/game/GameHelper.scala index 42f3ec771e..ce58d6781c 100644 --- a/app/game/GameHelper.scala +++ b/app/game/GameHelper.scala @@ -101,4 +101,15 @@ trait GameHelper { self: I18nHelper with UserHelper with StringHelper with AiHel Forsyth exportBoard game.toChess.board, game.lastMove | "") } + + def gameFenNoCtx(game: DbGame, color: Color) = Html { + var live = game.isBeingPlayed + """""".format( + routes.Round.watcher(game.id, color.name), + live.fold("live live_" + game.id, ""), + live.fold(game.id, ""), + color.name, + Forsyth exportBoard game.toChess.board, + game.lastMove | "") + } } diff --git a/app/lobby/Hub.scala b/app/lobby/Hub.scala index bbc68cf07a..7ec99b88a8 100644 --- a/app/lobby/Hub.scala +++ b/app/lobby/Hub.scala @@ -61,13 +61,11 @@ final class Hub( members.values filter (_ ownsHook hook) foreach fn } - case ChangeFeatured(oldId, newId) ⇒ notifyFeatured(oldId, newId) + case ChangeFeatured(html) ⇒ notifyFeatured(html) } - def notifyFeatured(oldId: Option[String], newId: String) { - val msg = makeMessage("featured", JsObject(Seq( - "oldId" -> oldId.fold(JsString(_), JsNull), - "newId" -> JsString(newId)))) + def notifyFeatured(html: String) { + val msg = makeMessage("featured", JsString(html)) members.values foreach (_.channel push msg) } diff --git a/app/socket/actorApi.scala b/app/socket/actorApi.scala index 122c36018a..476c5be2a3 100644 --- a/app/socket/actorApi.scala +++ b/app/socket/actorApi.scala @@ -28,5 +28,5 @@ case class Quit(uid: String) case class SendTo(userId: String, message: JsObject) case class Fen(gameId: String, fen: String, lastMove: Option[String]) case class LiveGames(uid: String, gameIds: List[String]) -case class ChangeFeatured(oldId: Option[String], newId: String) +case class ChangeFeatured(html: String) case class Resync(uid: String) diff --git a/app/views/game/featuredJs.scala.html b/app/views/game/featuredJs.scala.html index 0ff4c013bf..9997ae6b7b 100644 --- a/app/views/game/featuredJs.scala.html +++ b/app/views/game/featuredJs.scala.html @@ -1,10 +1,3 @@ -@(gameOption: Option[DbGame])(implicit ctx: Context) - - +@(g: DbGame)(implicit ctx: Context) +@gameFen(g, g.creator.color) +
@usernameWithElo(g.creator) - @usernameWithElo(g.invited)
diff --git a/app/views/game/featuredJsNoCtx.scala.html b/app/views/game/featuredJsNoCtx.scala.html new file mode 100644 index 0000000000..177ef5da8e --- /dev/null +++ b/app/views/game/featuredJsNoCtx.scala.html @@ -0,0 +1,3 @@ +@(g: DbGame) +@gameFenNoCtx(g, g.creator.color) +
@usernameWithElo(g.creator) - @usernameWithElo(g.invited)
diff --git a/app/views/lobby/home.scala.html b/app/views/lobby/home.scala.html index 484e65a421..b978402f5a 100644 --- a/app/views/lobby/home.scala.html +++ b/app/views/lobby/home.scala.html @@ -26,7 +26,11 @@ } @underchat = { -@game.featuredJs(featured) + } @baseline = { diff --git a/conf/routes b/conf/routes index a292e1afc4..e65212c5ba 100644 --- a/conf/routes +++ b/conf/routes @@ -8,7 +8,6 @@ GET /games/checkmate controllers.Game.checkmate(page: Int ?= 1) GET /games/bookmark controllers.Game.bookmark(page: Int ?= 1) GET /games/popular controllers.Game.popular(page: Int ?= 1) GET /games/analysed controllers.Game.analysed(page: Int ?= 1) -GET /games/featured/js controllers.Game.featuredJs(id: String ?= "") # Search GET /games/search controllers.Search.form(page: Int ?= 1) diff --git a/public/javascripts/opening.js b/public/javascripts/opening.js index 55dffa3195..608c4b42cb 100644 --- a/public/javascripts/opening.js +++ b/public/javascripts/opening.js @@ -233,18 +233,9 @@ $(function() { $("table.tournaments").html(data); } - function changeFeatured(data) { - $('div.featured_game').each(function() { - var $featured = $(this); - $.get( - $featured.data("href"), - { id: data.newId }, - function(html) { - $featured.replaceWith(html); - $('body').trigger('lichess.content_loaded'); - } - ); - }); + function changeFeatured(html) { + $('#featured_game').html(html); + $('body').trigger('lichess.content_loaded'); } function renderTimeline(data) { diff --git a/public/stylesheets/opening.css b/public/stylesheets/opening.css index 05a26acb49..e374093bea 100644 --- a/public/stylesheets/opening.css +++ b/public/stylesheets/opening.css @@ -184,7 +184,7 @@ div.hooks a.user_link { padding-left: 0; } -div.featured_game div.vstext { +#featured_game div.vstext { margin-top: 0.7em; text-align: center; } diff --git a/todo b/todo index 81ab32246d..df2a38435a 100644 --- a/todo +++ b/todo @@ -47,4 +47,3 @@ save game prefs in cookie redis pubsub / rabbitmq for ai IP smart tools for admins lsof show the open websockets on prod. The recurrent bug could come from the open descriptors limit (ulimit -n) -send featured fen through websocket instead of multiple ajax call