bug fixes and remove hasPlayed
parent
5dd6446ded
commit
1d02dd61c1
|
@ -30,9 +30,7 @@ object Puzzle extends LilaController {
|
|||
_.map(_.id) ?? env.api.puzzle.find
|
||||
}) { puzzle =>
|
||||
negotiate(
|
||||
html = (ctx.me ?? { env.api.round.hasPlayed(_, puzzle) map (!_) }) flatMap { asPlay =>
|
||||
renderShow(puzzle, asPlay.fold("play", "try")) map { Ok(_) }
|
||||
},
|
||||
html = fuccess(Ok("play")),
|
||||
api = _ => puzzleJson(puzzle) map { Ok(_) }
|
||||
) map { NoCache(_) }
|
||||
}
|
||||
|
@ -47,9 +45,7 @@ object Puzzle extends LilaController {
|
|||
|
||||
def show(id: PuzzleId) = Open { implicit ctx =>
|
||||
OptionFuOk(env.api.puzzle find id) { puzzle =>
|
||||
(ctx.me ?? { env.api.round.hasPlayed(_, puzzle) map (!_) }) flatMap { asPlay =>
|
||||
renderShow(puzzle, asPlay.fold("play", "try"))
|
||||
}
|
||||
renderShow(puzzle, "play")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,10 +56,9 @@ object Puzzle extends LilaController {
|
|||
}
|
||||
|
||||
private def puzzleJson(puzzle: PuzzleModel)(implicit ctx: Context) =
|
||||
(env userInfos ctx.me) zip
|
||||
(ctx.me ?? { env.api.round.hasPlayed(_, puzzle) map (!_) }) map {
|
||||
case (infos, asPlay) => JsData(puzzle, infos, asPlay.fold("play", "try"), animationDuration = env.AnimationDuration)
|
||||
}
|
||||
(env userInfos ctx.me) map {
|
||||
infos => JsData(puzzle, infos, "play", animationDuration = env.AnimationDuration)
|
||||
}
|
||||
|
||||
def history = Auth { implicit ctx =>
|
||||
me =>
|
||||
|
|
|
@ -16,6 +16,7 @@ private[puzzle] final class Finisher(
|
|||
def apply(puzzle: Puzzle, user: User, data: DataForm.RoundData): Fu[(Round, Option[Boolean])] =
|
||||
api.head.find(user) flatMap {
|
||||
case Some(PuzzleHead(_, Some(c), _)) if c == puzzle.id =>
|
||||
api.head.solved(user, puzzle.id)
|
||||
val userRating = user.perfs.puzzle.toRating
|
||||
val puzzleRating = puzzle.perf.toRating
|
||||
updateRatings(userRating, puzzleRating, data.isWin.fold(Glicko.Result.Win, Glicko.Result.Loss))
|
||||
|
|
|
@ -80,11 +80,6 @@ private[puzzle] final class PuzzleApi(
|
|||
Round.BSONFields.userRatingDiff -> a.userRatingDiff
|
||||
)))
|
||||
} void
|
||||
|
||||
def hasPlayed(user: User, puzzle: Puzzle): Fu[Boolean] =
|
||||
roundColl.exists($doc(
|
||||
Round.BSONFields.id -> Round.makeId(puzzle.id, user.id)
|
||||
))
|
||||
}
|
||||
|
||||
object learning {
|
||||
|
@ -151,7 +146,10 @@ private[puzzle] final class PuzzleApi(
|
|||
|
||||
def find(user: User): Fu[Option[PuzzleHead]] = headColl.byId[PuzzleHead](user.id)
|
||||
|
||||
def add(h: PuzzleHead) = headColl insert h void
|
||||
def add(h: PuzzleHead) = headColl update(
|
||||
$id(h.id),
|
||||
h,
|
||||
upsert = true) void
|
||||
|
||||
def solved(user: User, id: PuzzleId) = head find user flatMap {
|
||||
case Some(PuzzleHead(_, Some(c), _)) if c == id => headColl.update(
|
||||
|
|
|
@ -33,7 +33,7 @@ module.exports = function(cfg, router, i18n) {
|
|||
setTimeout(function() {
|
||||
if (this.data.mode == 'play') {
|
||||
this.chessground.stop();
|
||||
xhr.attempt(this, false);
|
||||
xhr.round(this, false);
|
||||
} else this.revert(this.data.puzzle.id);
|
||||
}.bind(this), 500);
|
||||
this.data.comment = 'fail';
|
||||
|
@ -42,7 +42,7 @@ module.exports = function(cfg, router, i18n) {
|
|||
this.userFinalizeMove([orig, dest, promotion], newProgress);
|
||||
if (newLines == 'win' || (Object.keys(newLines).length === 1 && newLines[Object.keys(newLines)[0]] == 'win')) {
|
||||
this.chessground.stop();
|
||||
xhr.attempt(this, true);
|
||||
xhr.round(this, true);
|
||||
} else setTimeout(partial(this.playOpponentNextMove, this.data.puzzle.id), 1000);
|
||||
break;
|
||||
}
|
||||
|
@ -159,7 +159,7 @@ module.exports = function(cfg, router, i18n) {
|
|||
this.data.progress.push(move);
|
||||
if (puzzle.getCurrentLines(this.data) == 'win') {
|
||||
this.chessground.stop();
|
||||
xhr.attempt(this, true);
|
||||
xhr.round(this, true);
|
||||
}
|
||||
}.bind(this);
|
||||
|
||||
|
|
|
@ -83,21 +83,21 @@ function renderRatingDiff(diff) {
|
|||
return m('strong.rating', diff > 0 ? '+' + diff : diff);
|
||||
}
|
||||
|
||||
function renderWin(ctrl, attempt) {
|
||||
function renderWin(ctrl, round) {
|
||||
return m('div.comment.win', [
|
||||
m('h3.text[data-icon=E]', [
|
||||
m('strong', ctrl.trans('victory')),
|
||||
attempt ? renderRatingDiff(attempt.userRatingDiff) : null
|
||||
round ? renderRatingDiff(round.userRatingDiff) : null
|
||||
]),
|
||||
attempt ? m('span', ctrl.trans('puzzleSolvedInXSeconds', attempt.seconds)) : null
|
||||
round ? m('span', ctrl.trans('puzzleSolvedInXSeconds', round.seconds)) : null
|
||||
]);
|
||||
}
|
||||
|
||||
function renderLoss(ctrl, attempt) {
|
||||
function renderLoss(ctrl, round) {
|
||||
return m('div.comment.loss',
|
||||
m('h3.text[data-icon=k]', [
|
||||
m('strong', ctrl.trans('puzzleFailed')),
|
||||
attempt ? renderRatingDiff(attempt.userRatingDiff) : null
|
||||
round ? renderRatingDiff(round.userRatingDiff) : null
|
||||
])
|
||||
);
|
||||
}
|
||||
|
@ -109,11 +109,11 @@ function renderResult(ctrl) {
|
|||
case false:
|
||||
return renderLoss(ctrl, null);
|
||||
default:
|
||||
switch (ctrl.data.attempt && ctrl.data.attempt.win) {
|
||||
switch (ctrl.data.round && ctrl.data.round.win) {
|
||||
case true:
|
||||
return renderWin(ctrl, ctrl.data.attempt);
|
||||
return renderWin(ctrl, ctrl.data.round);
|
||||
case false:
|
||||
return renderLoss(ctrl, ctrl.data.attempt);
|
||||
return renderLoss(ctrl, ctrl.data.round);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ function renderPlayTable(ctrl) {
|
|||
el.classList.add('revealed');
|
||||
}, 1000);
|
||||
},
|
||||
onclick: partial(xhr.attempt, ctrl, 0)
|
||||
onclick: partial(xhr.round, ctrl, 0)
|
||||
}, ctrl.trans('giveUp'))
|
||||
)
|
||||
])
|
||||
|
@ -154,16 +154,16 @@ function renderPlayTable(ctrl) {
|
|||
}
|
||||
|
||||
function renderVote(ctrl) {
|
||||
return m('div.upvote' + (ctrl.data.attempt ? '.enabled' : ''), [
|
||||
return m('div.upvote' + (ctrl.data.round ? '.enabled' : ''), [
|
||||
m('a[data-icon=S]', {
|
||||
title: ctrl.trans('thisPuzzleIsCorrect'),
|
||||
class: ctrl.data.attempt.vote ? ' active' : '',
|
||||
class: ctrl.data.round.vote ? ' active' : '',
|
||||
onclick: partial(xhr.vote, ctrl, 1)
|
||||
}),
|
||||
m('span.count.hint--bottom[data-hint=Popularity]', ctrl.data.puzzle.vote),
|
||||
m('a[data-icon=R]', {
|
||||
title: ctrl.trans('thisPuzzleIsWrong'),
|
||||
class: ctrl.data.attempt.vote === false ? ' active' : '',
|
||||
class: ctrl.data.round.vote === false ? ' active' : '',
|
||||
onclick: partial(xhr.vote, ctrl, 0)
|
||||
})
|
||||
]);
|
||||
|
@ -201,7 +201,7 @@ function renderViewTable(ctrl) {
|
|||
onclick: partial(xhr.newPuzzle, ctrl)
|
||||
}, ctrl.trans('continueTraining')) : m('a.continue.button.text[data-icon=G]', {
|
||||
onclick: partial(xhr.newPuzzle, ctrl)
|
||||
}, ctrl.trans('continueTraining')), !(ctrl.data.win === null ? ctrl.data.attempt.win : ctrl.data.win) ? m('a.retry.text[data-icon=P]', {
|
||||
}, ctrl.trans('continueTraining')), !(ctrl.data.win === null ? ctrl.data.round.win : ctrl.data.win) ? m('a.retry.text[data-icon=P]', {
|
||||
onclick: partial(xhr.retry, ctrl)
|
||||
}, ctrl.trans('retryThisPuzzle')) : null
|
||||
])
|
||||
|
|
|
@ -14,11 +14,11 @@ function uncache(url) {
|
|||
return url + '?_=' + new Date().getTime();
|
||||
}
|
||||
|
||||
function attempt(ctrl, win) {
|
||||
function round(ctrl, win) {
|
||||
showLoading(ctrl);
|
||||
m.request({
|
||||
method: 'POST',
|
||||
url: ctrl.router.Puzzle.attempt(ctrl.data.puzzle.id).url,
|
||||
url: ctrl.router.Puzzle.round(ctrl.data.puzzle.id).url,
|
||||
data: {
|
||||
win: win ? 1 : 0,
|
||||
time: new Date().getTime() - (ctrl.data.startedAt || new Date()).getTime()
|
||||
|
@ -39,7 +39,7 @@ function vote(ctrl, v) {
|
|||
},
|
||||
config: xhrConfig
|
||||
}).then(function(res) {
|
||||
ctrl.data.attempt.vote = res[0];
|
||||
ctrl.data.round.vote = res[0];
|
||||
ctrl.data.puzzle.vote = res[1];
|
||||
});
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ function newPuzzle(ctrl) {
|
|||
}
|
||||
|
||||
module.exports = {
|
||||
attempt: attempt,
|
||||
round: round,
|
||||
vote: vote,
|
||||
retry: retry,
|
||||
setDifficulty: setDifficulty,
|
||||
|
|
Loading…
Reference in New Issue