even more tournament fixes and improvements

pull/595/head
Thibault Duplessis 2015-06-14 13:23:40 +02:00
parent f07f5994ff
commit f48836c032
17 changed files with 69 additions and 58 deletions

View File

@ -45,18 +45,18 @@ object Tournament extends LilaController {
env allCreatedSorted true zip repo.publicStarted zip repo.finished(10)
def show(id: String) = Open { implicit ctx =>
val page = getInt("page") | 1
val page = getInt("page")
negotiate(
html = repo byId id flatMap {
_.fold(tournamentNotFound.fuccess) { tour =>
env.version(tour.id) zip env.jsonView(tour, page.some, ctx.userId) zip chatOf(tour) map {
env.version(tour.id) zip env.jsonView(tour, page, ctx.userId) zip chatOf(tour) map {
case ((version, data), chat) => html.tournament.show(tour, version, data, chat)
}
}
},
api = _ => repo byId id flatMap {
case None => NotFound(Json.obj("error" -> "No such tournament")).fuccess
case Some(tour) => env.jsonView(tour, page.some, ctx.userId) map { Ok(_) }
case Some(tour) => env.jsonView(tour, page, ctx.userId) map { Ok(_) }
} map (_ as JSON)
)
}

View File

@ -10,12 +10,19 @@ import lila.user.User
final class JsonView(
getLightUser: String => Option[LightUser]) {
def apply(tour: Tournament, page: Option[Int], me: Option[String]): Fu[JsObject] = for {
def apply(
tour: Tournament,
page: Option[Int],
me: Option[String]): Fu[JsObject] = for {
pairings <- PairingRepo.recentByTour(tour.id, 40)
games <- GameRepo games pairings.map(_.gameId)
games <- GameRepo games pairings.take(4).map(_.gameId)
podiumJson <- tour.isFinished ?? podiumJson(tour).map(_.some)
stand <- page ?? { standing(tour, _) }
myInfo <- me ?? { PlayerRepo.playerInfo(tour.id, _) }
stand <- (myInfo, page) match {
case (_, Some(p)) => standing(tour, p)
case (Some(i), _) => standing(tour, i.page)
case _ => standing(tour, 1)
}
} yield Json.obj(
"id" -> tour.id,
"createdBy" -> tour.createdBy,
@ -77,14 +84,14 @@ final class JsonView(
"speed" -> s.speed.name)
private def sheetJson(sheet: ScoreSheet) = sheet match {
case s: arena.ScoringSystem.Sheet => Json.obj(
"scores" -> s.scores.reverse.map { score =>
if (score.flag == arena.ScoringSystem.Normal) JsNumber(score.value)
else Json.arr(score.value, score.flag.id)
},
"total" -> s.total,
"fire" -> s.onFire.option(true)
).noNull
case s: arena.ScoringSystem.Sheet =>
val o = Json.obj(
"scores" -> s.scores.reverse.map { score =>
if (score.flag == arena.ScoringSystem.Normal) JsNumber(score.value)
else Json.arr(score.value, score.flag.id)
},
"total" -> s.total)
s.onFire.fold(o + ("fire" -> JsBoolean(true)), o)
}
private def playerJson(sheets: Map[String, ScoreSheet], tour: Tournament)(rankedPlayer: RankedPlayer) = {
@ -126,11 +133,11 @@ final class JsonView(
private def pairingJson(p: Pairing) = Json.obj(
"id" -> p.gameId,
"st" -> p.status.id,
"u1" -> pairingUserJson(p.user1),
"u2" -> pairingUserJson(p.user2),
"w" -> p.winner.map {
case w if w == p.user1 => 1
case _ => 0
})
"u" -> Json.arr(pairingUserJson(p.user1), pairingUserJson(p.user2)),
"s" -> (if (p.finished) p.winner match {
case Some(w) if w == p.user1 => 2
case Some(w) => 3
case _ => 1
}
else 0))
}

View File

@ -30,8 +30,8 @@ object PlayerRepo {
coll.find(selectTour(tourId)).sort(bestSort).skip(skip).cursor[Player].collect[List](nb)
def bestByTourWithRank(tourId: String, nb: Int, skip: Int = 0): Fu[RankedPlayers] =
bestByTour(tourId, nb, skip).map {
_.foldRight(List.empty[RankedPlayer] -> (nb + skip)) {
bestByTour(tourId, nb, skip).map { res =>
res.foldRight(List.empty[RankedPlayer] -> (res.size + skip)) {
case (p, (res, rank)) => (RankedPlayer(rank, p) :: res, rank - 1)
}._1
}

View File

@ -5,5 +5,9 @@ case class MiniStanding(
standing: Option[RankedPlayers])
case class PlayerInfo(
rank: Int,
withdraw: Boolean)
rank: Int,
withdraw: Boolean) {
def page = {
math.floor((rank - 1) / 10) + 1
}.toInt
}

View File

@ -215,6 +215,9 @@ ol.scheduled_tournaments a:hover {
#tournament table.slist .small {
font-size: 0.8em;
}
#tournament div.standing_wrap {
overflow: hidden;
}
#tournament div.standing_wrap:hover {
overflow-y: auto;
}

View File

@ -2,7 +2,6 @@ var m = require('mithril');
var socket = require('./socket');
var xhr = require('./xhr');
var pagination = require('./pagination');
var tournament = require('./tournament');
var util = require('chessground').util;
module.exports = function(env) {

View File

@ -1,4 +1,3 @@
var tournament = require('./tournament');
var m = require('mithril');
var maxPerPage = 10;
@ -28,24 +27,22 @@ function scrollToMeButton(ctrl, pag) {
function paginate(ctrl, page) {
var nbResults = ctrl.data.nbPlayers;
var max = nbResults > 15 ? maxPerPage : 15; // don't paginate 15 or less elements
var from = (page - 1) * max;
var to = Math.min(nbResults, page * max);
var from = (page - 1) * maxPerPage;
var to = Math.min(nbResults, page * maxPerPage);
return {
currentPage: page,
maxPerPage: max,
maxPerPage: maxPerPage,
from: from,
to: to,
currentPageResults: ctrl.vm.pages[page],
nbResults: nbResults,
nbPages: Math.ceil(nbResults / max)
nbPages: Math.ceil(nbResults / maxPerPage)
};
}
module.exports = {
render: function(ctrl, pag, table) {
return [
// loader,
pag.currentPageResults ? table() : m('div.loader'),
pag.nbPages > 1 ? m('div.pager', [
button('First', 'W', function() {

View File

@ -1,14 +1,11 @@
var status = require('game').status;
module.exports = {
createdByMe: function(ctrl) {
return ctrl.userId && ctrl.userId === ctrl.data.createdBy;
},
myCurrentPairing: function(ctrl) {
if (!ctrl.userId) return null;
return ctrl.data.pairings.filter(function(p) {
return p.st < status.ids.mate && (
p.u1.toLowerCase() === ctrl.userId || p.u2.toLowerCase() === ctrl.userId
return p.s === 0 && (
p.u[0].toLowerCase() === ctrl.userId || p.u[1].toLowerCase() === ctrl.userId
);
})[0];
}

View File

@ -1,6 +1,5 @@
var m = require('mithril');
var partial = require('chessground').util.partial;
var tournament = require('../tournament');
var util = require('./util');
var button = require('./button');

View File

@ -1,6 +1,5 @@
var m = require('mithril');
var partial = require('chessground').util.partial;
var tournament = require('../tournament');
var xhr = require('../xhr');
function withdraw(ctrl) {

View File

@ -1,6 +1,5 @@
var m = require('mithril');
var partial = require('chessground').util.partial;
var tournament = require('../tournament');
var util = require('./util');
var button = require('./button');
var xhr = require('../xhr');

View File

@ -1,6 +1,5 @@
var m = require('mithril');
var partial = require('chessground').util.partial;
var tournament = require('../tournament');
var util = require('./util');
var arena = require('./arena');
var swiss = require('./swiss');

View File

@ -3,16 +3,16 @@ var partial = require('chessground').util.partial;
var util = require('./util');
var status = require('game').status;
function user(pairing, username) {
var id = username.toLowerCase();
var statusClasses = ['playing', 'draw', 'win', 'loss'];
function user(p, it) {
return {
tag: pairing.st >= status.ids.mate ? (
pairing.w === null ? 'draw' : (
(!!pairing.w === (pairing.u1.toLowerCase() === id)) ? 'win' : 'loss'
tag: p.s === 0 ? 'playing' : (
p.s === 1 ? 'draw' : (
(p.s === 2) === (it === 0) ? 'win' : 'loss'
)
) : 'playing',
attrs: {},
children: [username]
),
children: [p.u[it]]
};
}
@ -25,9 +25,9 @@ module.exports = function(ctrl) {
href: '/' + p.id
},
children: [
user(p, p.u1),
user(p, 0),
'vs',
user(p, p.u2)
user(p, 1)
]
};
};

View File

@ -1,15 +1,24 @@
var m = require('mithril');
var partial = require('chessground').util.partial;
var tournament = require('../tournament');
var util = require('./util');
var arena = require('./arena');
var swiss = require('./swiss');
var pairings = require('./pairings');
var pagination = require('../pagination');
function myCurrentPairing(ctrl) {
if (!ctrl.userId) return null;
return ctrl.data.pairings.filter(function(p) {
return p.s === 0 && (
p.u[0].toLowerCase() === ctrl.userId || p.u[1].toLowerCase() === ctrl.userId
);
})[0];
}
module.exports = {
main: function(ctrl) {
var myPairing = tournament.myCurrentPairing(ctrl);
var myPairing = myCurrentPairing(ctrl);
var gameId = myPairing ? myPairing.id : null;
var pag = pagination.players(ctrl);
return [

View File

@ -1,6 +1,5 @@
var m = require('mithril');
var partial = require('chessground').util.partial;
var tournament = require('../tournament');
var util = require('./util');
var button = require('./button');

View File

@ -7,7 +7,7 @@ function miniGame(game) {
return m('div', [
m('a', {
key: game.id,
href: '/' + game.id,
href: '/' + game.id + (game.color === 'white' ? '' : '/black'),
class: 'mini_board live_' + game.id + ' parse_fen is2d',
'data-color': game.color,
'data-fen': game.fen,

View File

@ -34,7 +34,7 @@ function reloadTournament(ctrl) {
url: '/tournament/' + ctrl.data.id,
config: xhrConfig,
data: {
page: ctrl.vm.page
page: ctrl.vm.focusOnMe ? null : ctrl.vm.page
}
}).then(ctrl.reload, reloadPage);
}
@ -44,5 +44,5 @@ module.exports = {
join: partial(tourAction, 'join'),
withdraw: partial(tourAction, 'withdraw'),
loadPage: throttle(1000, false, loadPage),
reloadTournament: throttle(1000, false, reloadTournament)
reloadTournament: throttle(2000, false, reloadTournament)
};