new possibleMoves encoding - mostly unused because of app BC

pull/4786/head
Thibault Duplessis 2018-12-15 22:09:13 +08:00
parent 3708cca31e
commit 595b69d9f1
4 changed files with 35 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import chess.variant.Crazyhouse
import chess.{ Centis, PromotableRole, Pos, Color, Situation, Move => ChessMove, Drop => ChessDrop, Clock => ChessClock, Status }
import JsonView._
import lila.chat.{ UserLine, PlayerLine }
import lila.common.ApiVersion
sealed trait Event {
def typ: String
@ -43,7 +44,7 @@ object Event {
extra ++ Json.obj(
"fen" -> fen,
"ply" -> state.turns,
"dests" -> PossibleMoves.json(possibleMoves)
"dests" -> PossibleMoves.oldJson(possibleMoves)
).add("clock" -> clock.map(_.data))
.add("status" -> state.status)
.add("winner" -> state.winner)
@ -148,7 +149,27 @@ object Event {
}
object PossibleMoves {
def json(moves: Map[Pos, List[Pos]]) =
def json(moves: Map[Pos, List[Pos]], apiVersion: ApiVersion) =
if (apiVersion gte 4) newJson(moves)
else oldJson(moves)
def newJson(moves: Map[Pos, List[Pos]]) =
if (moves.isEmpty) JsNull
else {
val sb = new java.lang.StringBuilder(64)
var first = true
moves foreach {
case (orig, dests) =>
if (first) first = false
else sb append " "
sb append orig.key
dests foreach { sb append _.key }
}
JsString(sb.toString)
}
def oldJson(moves: Map[Pos, List[Pos]]) =
if (moves.isEmpty) JsNull
else moves.foldLeft(JsObject(Nil)) {
case (res, (o, d)) => res + (o.key, JsString(d map (_.key) mkString))

View File

@ -111,7 +111,7 @@ final class JsonView(
.add("correspondence" -> game.correspondenceClock)
.add("takebackable" -> takebackable)
.add("crazyhouse" -> pov.game.board.crazyData)
.add("possibleMoves" -> possibleMoves(pov))
.add("possibleMoves" -> possibleMoves(pov, apiVersion))
.add("possibleDrops" -> possibleDrops(pov))
.add("expiration" -> game.expirable.option {
Json.obj(
@ -254,12 +254,9 @@ final class JsonView(
private def clockJson(clock: Clock): JsObject =
clockWriter.writes(clock) + ("moretime" -> JsNumber(moretimeSeconds))
private def possibleMoves(pov: Pov): Option[Map[String, String]] =
(pov.game playableBy pov.player) option {
pov.game.situation.destinations map {
case (from, dests) => from.key -> dests.mkString
}
}
private def possibleMoves(pov: Pov, apiVersion: ApiVersion): Option[JsValue] =
(pov.game playableBy pov.player) option
lila.game.Event.PossibleMoves.json(pov.game.situation.destinations, apiVersion)
private def possibleDrops(pov: Pov): Option[JsValue] = (pov.game playableBy pov.player) ?? {
pov.game.situation.drops map { drops =>

View File

@ -31,9 +31,9 @@ export interface SocketDrop {
b?: 1;
}
export interface EncodedDests {
export type EncodedDests = string | {
[key: string]: string;
}
};
export interface DecodedDests {
[key: string]: cg.Key[];
}
@ -110,7 +110,7 @@ export interface Step {
}
export interface ApiMove extends Step {
dests: { [key: string]: string };
dests: EncodedDests;
clock?: {
white: Seconds;
black: Seconds;

View File

@ -41,7 +41,11 @@ export function bind(eventName: string, f: (e: Event) => void, redraw?: Redraw):
export function parsePossibleMoves(dests?: EncodedDests): DecodedDests {
if (!dests) return {};
const dec: DecodedDests = {};
for (let k in dests) dec[k] = dests[k].match(/.{2}/g) as cg.Key[];
if (typeof dests == 'string')
dests.split(' ').forEach(ds => {
dec[ds.slice(0,2)] = ds.slice(2).match(/.{2}/g) as cg.Key[];
});
else for (let k in dests) dec[k] = dests[k].match(/.{2}/g) as cg.Key[];
return dec;
}