Skip to content

Commit

Permalink
Completely get rid of fog FEN creation on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
gbtami committed Jan 4, 2025
1 parent d202797 commit e08c5fb
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 37 deletions.
8 changes: 2 additions & 6 deletions client/cgCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FairyStockfish, Board, Notation } from 'ffish-es6';
import { boardSettings, BoardController } from '@/boardSettings';
import { CGMove, uci2cg } from '@/chess';
import { BoardName, PyChessModel } from '@/types';
import { Variant, VARIANTS, moddedVariant } from '@/variants';
import { fogFen, Variant, VARIANTS, moddedVariant } from '@/variants';

export abstract class ChessgroundController implements BoardController {
boardName: BoardName;
Expand Down Expand Up @@ -44,7 +44,7 @@ export abstract class ChessgroundController implements BoardController {
this.fog = model.variant === 'fogofwar';

const parts = this.fullfen.split(" ");
const fen_placement: cg.FEN = (this.fog) ? this.fogFen(parts[0]) : parts[0];
const fen_placement: cg.FEN = (this.fog) ? fogFen(parts[0]) : parts[0];

this.chessground = Chessground(el, {
fen: fen_placement as cg.FEN,
Expand Down Expand Up @@ -76,10 +76,6 @@ export abstract class ChessgroundController implements BoardController {
window.addEventListener('beforeunload', () => this.ffishBoard.delete());
}

fogFen(currentFen: string): string {
return currentFen.replace(/\*/g, '*~');
}

toggleOrientation(): void {
this.chessground.toggleOrientation();
}
Expand Down
1 change: 0 additions & 1 deletion client/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Variant, variantGroups } from './variants';

export const WHITE = 0;
export const BLACK = 1;
export const DARK_FEN = "*~*~*~*~*~*~*~*~/*~*~*~*~*~*~*~*~/*~*~*~*~*~*~*~*~/*~*~*~*~*~*~*~*~/*~*~*~*~*~*~*~*~/*~*~*~*~*~*~*~*~/*~*~*~*~*~*~*~*~/*~*~*~*~*~*~*~*~ w KQkq - 0 1"

export const ranksUCI = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] as const;
export type UCIRank = typeof ranksUCI[number];
Expand Down
4 changes: 2 additions & 2 deletions client/gameCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { sound } from './sound';
import { chatMessage, ChatController } from './chat';
import { selectMove } from './movelist';
import { Api } from "chessgroundx/api";
import { Variant } from "@/variants";
import { fogFen, Variant } from "./variants";
import { CheckCounterSvg, Counter } from './glyphs';

export abstract class GameController extends ChessgroundController implements ChatController {
Expand Down Expand Up @@ -318,7 +318,7 @@ export abstract class GameController extends ChessgroundController implements Ch

const fen = (this.mirrorBoard) ? this.getAliceFen(step.fen) : step.fen;
this.chessground.set({
fen: (this.fog) ? this.fogFen(fen) : fen,
fen: (this.fog) ? fogFen(fen) : fen,
turnColor: step.turnColor,
movable: {
color: step.turnColor,
Expand Down
4 changes: 2 additions & 2 deletions client/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Games = Map<string, GameData>;
function gameView(games: Games, game: Game) {
const variant = VARIANTS[game.variant];
let lastMove, fen;
[lastMove, fen] = getLastMoveFen(variant.name, game.lastMove, game.fen, '*')
[lastMove, fen] = getLastMoveFen(variant.name, game.lastMove, game.fen)
return h(`minigame#${game.gameId}.${variant.boardFamily}.${variant.pieceFamily}.${variant.ui.boardMark}`, {
class: {
"with-pockets": !!variant.pocket,
Expand Down Expand Up @@ -97,7 +97,7 @@ export function renderGames(model: PyChessModel): VNode[] {
let cg, variantName;
[cg, variantName] = gameData;
let lastMove, fen;
[lastMove, fen] = getLastMoveFen(variantName, message.lastMove, message.fen, '*')
[lastMove, fen] = getLastMoveFen(variantName, message.lastMove, message.fen)
cg.set({
fen: fen,
lastMove: lastMove,
Expand Down
4 changes: 2 additions & 2 deletions client/nowPlaying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function handleOngoingGameEvents(username: string, cgMap: {[gameId: strin
[cg, variantName] = cgMap[message.gameId];

let lastMove, fen;
[lastMove, fen] = getLastMoveFen(variantName, message.lastMove, message.fen, '*')
[lastMove, fen] = getLastMoveFen(variantName, message.lastMove, message.fen)

cg.set({
fen: fen,
Expand Down Expand Up @@ -95,7 +95,7 @@ export function gameViewPlaying(cgMap: {[gameId: string]: [Api, string]}, game:
const mycolor = (username === game.w) ? 'white' : 'black';

let lastMove, fen;
[lastMove, fen] = getLastMoveFen(variant.name, game.lastMove, game.fen, '*')
[lastMove, fen] = getLastMoveFen(variant.name, game.lastMove, game.fen)

return h(`a.${variant.boardFamily}.${variant.pieceFamily}.${variant.ui.boardMark}`, { attrs: { href: game.gameId } }, [
h(`div.cg-wrap.${variant.board.cg}`, {
Expand Down
2 changes: 1 addition & 1 deletion client/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function renderGames(model: PyChessModel, games: Game[]) {
teamSecond = game["us"][2] + "+" + game["us"][1];
}
let lastMove, fen;
[lastMove, fen] = getLastMoveFen(variant.name, game.lm, game.f, game.r)
[lastMove, fen] = getLastMoveFen(variant.name, game.lm, game.f)
return h('tr', [h('a', { attrs: { href : '/' + game["_id"] } }, [
h('td.board', { class: { "with-pockets": !!variant.pocket, "bug": isBug} },
isBug? renderGameBoardsBug(game, model["profileid"]): [
Expand Down
9 changes: 5 additions & 4 deletions client/roundCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { patch } from './document';
import { boardSettings } from './boardSettings';
import { Clock } from './clock';
import { sound } from './sound';
import { DARK_FEN, WHITE, BLACK, uci2LastMove, getCounting, isHandicap } from './chess';
import { fogFen } from "./variants";
import { WHITE, BLACK, uci2LastMove, getCounting, isHandicap } from './chess';
import { crosstableView } from './crosstable';
import { chatMessage, chatView } from './chat';
import { createMovelistButtons, updateMovelist, updateResult, selectMove } from './movelist';
Expand Down Expand Up @@ -884,7 +885,7 @@ export class RoundController extends GameController {
if (this.spectator) {
if (latestPly) {
this.chessground.set({
fen: (this.fog) ? DARK_FEN : this.fullfen,
fen: (this.fog) ? fogFen(this.fullfen) : this.fullfen,
turnColor: this.turnColor,
check: msg.check,
lastMove: (this.fog) ? undefined : lastMove,
Expand All @@ -906,7 +907,7 @@ export class RoundController extends GameController {
if (this.turnColor === this.mycolor) {
if (latestPly) {
this.chessground.set({
fen: (this.fog) ? this.fogFen(this.fullfen) : this.fullfen,
fen: (this.fog) ? fogFen(this.fullfen) : this.fullfen,
turnColor: this.turnColor,
movable: {
free: false,
Expand Down Expand Up @@ -941,7 +942,7 @@ export class RoundController extends GameController {
} else {
this.chessground.set({
// giving fen here will place castling rooks to their destination in chess960 variants
fen: (this.fog) ? this.fogFen(this.fullfen) : parts[0],
fen: (this.fog) ? fogFen(this.fullfen) : parts[0],
turnColor: this.turnColor,
check: msg.check,
lastMove: lastMove,
Expand Down
4 changes: 2 additions & 2 deletions client/tournament.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export class TournamentController implements ChatController {
hook: {
insert: vnode => {
let lastMove, fen;
[lastMove, fen] = getLastMoveFen(this.variant.name, game.lastMove, game.fen, '*')
[lastMove, fen] = getLastMoveFen(this.variant.name, game.lastMove, game.fen)
const cg = Chessground(vnode.elm as HTMLElement, {
fen: fen,
lastMove: lastMove,
Expand Down Expand Up @@ -620,7 +620,7 @@ export class TournamentController implements ChatController {
return;
};
let lastMove, fen;
[lastMove, fen] = getLastMoveFen(this.variant.name, msg.lastMove, msg.fen, msg.result)
[lastMove, fen] = getLastMoveFen(this.variant.name, msg.lastMove, msg.fen)

this.topGameChessground.set({
fen: fen,
Expand Down
17 changes: 8 additions & 9 deletions client/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { h, InsertHook, VNode } from 'snabbdom';
import * as cg from 'chessgroundx/types';
import * as util from 'chessgroundx/util';

import { DARK_FEN, BoardMarkType, ColorName, CountingType, MaterialPointType, PieceSoundType, PromotionSuffix, PromotionType, TimeControlType, uci2LastMove } from './chess';
import { BoardMarkType, ColorName, CountingType, MaterialPointType, PieceSoundType, PromotionSuffix, PromotionType, TimeControlType, uci2LastMove } from './chess';
import { _ } from './i18n';
import { calculateDiff, Equivalence, MaterialDiff } from './material';

Expand Down Expand Up @@ -1165,12 +1165,11 @@ export function moddedVariant(variantName: string, chess960: boolean, pieces: cg
return variantName;
}

export function getLastMoveFen(variantName: string, lastMove: string, fen: string, result: string): [cg.Orig[] | undefined, string] {
switch (variantName) {
case 'fogofwar':
// Prevent leaking ongoing game info
return [undefined, (result === "*") ? DARK_FEN : fen];
default:
return [uci2LastMove(lastMove), fen];
}
export function getLastMoveFen(variantName: string, lastMove: string, fen: string): [cg.Orig[] | undefined, string] {
return [uci2LastMove(lastMove), variantName === 'fogofwar' ? fogFen(fen) : fen];
}

// Replace all brick ("*") pieces to be promoted ("*~") to let them CSS style as fog instead of duck
export function fogFen(currentFen: string): string {
return currentFen.replace(/\*/g, '*~');
}
12 changes: 6 additions & 6 deletions server/game_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import pymongo

from compress import get_decode_method, C2V, V2C, C2R, decode_move_standard
from const import GRANDS, STARTED, MATE, VARIANTS, INVALIDMOVE, VARIANTEND, CLAIM
from const import DARK_FEN, GRANDS, STARTED, MATE, VARIANTS, INVALIDMOVE, VARIANTEND, CLAIM
from convert import zero2grand
from settings import ADMINS
from tournaments import get_tournament_name
Expand Down Expand Up @@ -344,9 +344,9 @@ async def get_user_games(request):
)
else:
if doc["s"] <= STARTED and variant == "fogofwar":
del doc["f"]
del doc["lm"]
del doc["m"]
doc["f"] = DARK_FEN
doc["lm"] = ""
doc["m"] = ""

game_doc_list.append(doc)

Expand Down Expand Up @@ -421,8 +421,8 @@ async def get_games(request):
{
"gameId": game.id,
"variant": game.variant,
"fen": game.board.fen,
"lastMove": game.lastmove,
"fen": DARK_FEN if game.variant == "fogofwar" else game.board.fen,
"lastMove": "" if game.variant == "fogofwar" else game.lastmove,
"tp": game.turn_player,
"w": game.wplayer.username,
"wTitle": game.wplayer.title,
Expand Down
5 changes: 3 additions & 2 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from broadcast import round_broadcast
from const import (
DARK_FEN,
NOTIFY_PAGE_SIZE,
STARTED,
VARIANT_960_TO_PGN,
Expand Down Expand Up @@ -980,8 +981,8 @@ def corr_games(games):
{
"gameId": game.id,
"variant": game.variant,
"fen": game.board.fen,
"lastMove": game.lastmove,
"fen": DARK_FEN if game.variant == "fogofwar" else game.board.fen,
"lastMove": "" if game.variant == "fogofwar" else game.lastmove,
"tp": game.turn_player,
"w": game.wplayer.username,
"wTitle": game.wplayer.title,
Expand Down

0 comments on commit e08c5fb

Please sign in to comment.