Skip to content

Commit

Permalink
better guess if the clock is running
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Mar 13, 2024
1 parent 14b297c commit 127ba9c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions ui/analyse/src/study/multiBoard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as licon from 'common/licon';
import { clockIsRunning, formatMs } from 'common/clock';
import { otbClockIsRunning, formatMs } from 'common/clock';
import { fenColor } from 'common/miniBoard';
import { MaybeVNode, VNode, bind, onInsert, looseH as h } from 'common/snabbdom';
import { opposite as CgOpposite, uciToMove } from 'chessground/util';
Expand Down Expand Up @@ -210,7 +210,7 @@ const renderUser = (player: ChapterPreviewPlayer): VNode =>
export const renderClock = (chapter: ChapterPreview, color: Color) => {
const turnColor = fenColor(chapter.fen);
const timeleft = computeTimeLeft(chapter, color);
const ticking = turnColor == color && clockIsRunning(chapter.fen, color);
const ticking = turnColor == color && otbClockIsRunning(chapter.fen);
return defined(timeleft)
? h(
'span.mini-game__clock.mini-game__clock',
Expand Down
4 changes: 3 additions & 1 deletion ui/common/src/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export const formatMs = (msTime: number) => {
return hours > 0 ? hours + ':' + pad(minutes) + ':' + pad(seconds) : minutes + ':' + pad(seconds);
};

export const clockIsRunning = (fen: string, color: Color) =>
export const otbClockIsRunning = (fen: string) => !fen.includes('PPPPPPPP/RNBQKBNR');

export const lichessClockIsRunning = (fen: string, color: Color) =>
color == 'white' ? !fen.includes('PPPPPPPP/RNBQKBNR') : !fen.startsWith('rnbqkbnr/pppppppp');

const pad = (x: number) => (x < 10 ? '0' : '') + x;
6 changes: 3 additions & 3 deletions ui/site/src/miniGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type Chessground } from 'chessground';
import * as domData from 'common/data';
import clockWidget from './clockWidget';
import StrongSocket from './socket';
import { clockIsRunning } from 'common/clock';
import { lichessClockIsRunning } from 'common/clock';

export const init = (node: Element, withCg?: typeof Chessground) => {
const [fen, color, lm] = node.getAttribute('data-state')!.split(','),
Expand Down Expand Up @@ -33,7 +33,7 @@ export const init = (node: Element, withCg?: typeof Chessground) => {
$el.find('.mini-game__clock--' + color).each(function (this: HTMLElement) {
clockWidget(this, {
time: parseInt(this.getAttribute('data-time')!),
pause: color != turnColor || !clockIsRunning(fen, color),
pause: color != turnColor || !lichessClockIsRunning(fen, color),
});
}),
);
Expand All @@ -60,7 +60,7 @@ export const update = (node: HTMLElement, data: MiniGameUpdateData) => {
if (!isNaN(time!))
clockWidget($el[0]?.querySelector('.mini-game__clock--' + color) as HTMLElement, {
time: time!,
pause: color != turnColor || !clockIsRunning(data.fen, color),
pause: color != turnColor || !lichessClockIsRunning(data.fen, color),
});
};
updateClock(data.wc, 'white');
Expand Down

0 comments on commit 127ba9c

Please sign in to comment.