Skip to content

Commit

Permalink
not all variants are supported in blind mode
Browse files Browse the repository at this point in the history
Because we need to validate moves on client side,
and only standard chess is implemented there.
Feel free to improve:
https://github.com/ornicar/lila/blob/master/ui/round/src/plugins/sanWriter.js

Some variants have same move generation,
and have therefore been included:
chess960, kingOfTheHill, threeCheck.

Horde could be included if moving first rank pawn
by two squares was implemented.
  • Loading branch information
ornicar committed Jan 30, 2019
1 parent 650a75d commit a05181c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
4 changes: 3 additions & 1 deletion app/views/setup/bits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ private object bits {
def renderVariant(form: Form[_], variants: List[SelectChoice])(implicit ctx: Context) =
div(cls := "variant label_select")(
renderLabel(form("variant"), trans.variant.frag()),
renderSelect(form("variant"), variants)
renderSelect(form("variant"), variants.filter {
case (id, _, _) => ctx.noBlind || lila.game.Game.blindModeVariants.exists(_.id.toString == id)
})
)

def renderSelect(
Expand Down
8 changes: 8 additions & 0 deletions modules/game/src/main/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,14 @@ object Game {
chess.variant.Antichess
)

val blindModeVariants: Set[Variant] = Set(
chess.variant.Standard,
chess.variant.Chess960,
chess.variant.KingOfTheHill,
chess.variant.ThreeCheck,
chess.variant.FromPosition
)

val hordeWhitePawnsSince = new DateTime(2015, 4, 11, 10, 0)

def isOldHorde(game: Game) =
Expand Down
6 changes: 3 additions & 3 deletions modules/setup/src/main/Config.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lila.setup

import chess.{ Game => ChessGame, Situation, Clock, Speed }
import chess.variant.FromPosition
import chess.variant.{ Variant, FromPosition }
import chess.format.FEN

import lila.game.Game
Expand All @@ -22,7 +22,7 @@ private[setup] trait Config {
val days: Int

// Game variant code
val variant: chess.variant.Variant
val variant: Variant

// Creator player color
val color: Color
Expand All @@ -31,7 +31,7 @@ private[setup] trait Config {

lazy val creatorColor = color.resolve

def makeGame(v: chess.variant.Variant): ChessGame =
def makeGame(v: Variant): ChessGame =
ChessGame(situation = Situation(v), clock = makeClock.map(_.toClock))

def makeGame: ChessGame = makeGame(variant)
Expand Down
7 changes: 6 additions & 1 deletion ui/nvui/src/chess.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { h } from 'snabbdom'
import { VNode } from 'snabbdom/vnode'
// import { GameData } from 'game';
import { Piece, Pieces } from 'chessground/types';
import { invRanks, allKeys } from 'chessground/util';
import { Setting, makeSetting } from './setting';
Expand All @@ -13,6 +12,12 @@ const anna: { [letter: string]: string } = { a: 'anna', b: 'bella', c: 'cesar',
const roles: { [letter: string]: string } = { P: 'pawn', R: 'rook', N: 'knight', B: 'bishop', Q: 'queen', K: 'king' };
const letters = { pawn: 'p', rook: 'r', knight: 'n', bishop: 'b', queen: 'q', king: 'k' };

export function supportedVariant(key: string) {
return [
'standard', 'chess960', 'kingOfTheHill', 'threeCheck', 'fromPosition'
].indexOf(key) > -1;
}

export function styleSetting(): Setting<Style> {
return makeSetting<Style>({
choices: [
Expand Down
24 changes: 15 additions & 9 deletions ui/round/src/plugins/nvui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Player } from 'game';
import { renderSan, renderPieces, renderBoard, styleSetting } from 'nvui/chess';
import { renderSetting } from 'nvui/setting';
import { Notify } from 'nvui/notify';
import { castlingFlavours, Style } from 'nvui/chess';
import { castlingFlavours, supportedVariant, Style } from 'nvui/chess';
import { commands } from 'nvui/command';

type Sans = {
Expand All @@ -33,13 +33,17 @@ window.lichess.RoundNVUI = function(redraw: Redraw) {

return {
render(ctrl: RoundController): VNode {
const d = ctrl.data, step = plyStep(d, ctrl.ply), style = moveStyle.get();
if (!ctrl.chessground) ctrl.setChessground(Chessground(document.createElement("div"), {
...makeCgConfig(ctrl),
animation: { enabled: false },
drawable: { enabled: false },
coordinates: false
}));
const d = ctrl.data, step = plyStep(d, ctrl.ply), style = moveStyle.get(),
variantNope = !supportedVariant(d.game.variant.key) && 'Sorry, this variant is not supported in blind mode.';
if (!ctrl.chessground) {
ctrl.setChessground(Chessground(document.createElement("div"), {
...makeCgConfig(ctrl),
animation: { enabled: false },
drawable: { enabled: false },
coordinates: false
}));
if (variantNope) setTimeout(() => notify.set(variantNope), 3000);
}
return h('div.nvui', [
h('h1', 'Textual representation'),
h('h2', 'Game info'),
Expand Down Expand Up @@ -91,7 +95,9 @@ window.lichess.RoundNVUI = function(redraw: Redraw) {
name: 'move',
'type': 'text',
autocomplete: 'off',
autofocus: true
autofocus: true,
disabled: !!variantNope,
title: variantNope
}
})
])
Expand Down

0 comments on commit a05181c

Please sign in to comment.