announce clocks from keyboard input

This commit is contained in:
Thibault Duplessis 2019-05-01 15:14:36 +07:00
parent b4f35a84e4
commit eedfbbe140
2 changed files with 26 additions and 2 deletions

View file

@ -3,6 +3,7 @@ import { sanToRole } from 'chess'
import * as cg from 'chessground/types';
import { Step, Redraw } from './interfaces';
import RoundController from './ctrl';
import { ClockController } from './clock/clockCtrl';
import { valid as crazyValid } from './crazy/crazyCtrl';
import { sendPromotion } from './promotion'
import { onInsert } from './util'
@ -26,6 +27,7 @@ export interface KeyboardMove {
confirmMove(): void;
usedSan: boolean;
jump(delta: number): void;
clock(): ClockController | undefined;
}
export function ctrl(root: RoundController, step: Step, redraw: Redraw): KeyboardMove {
@ -87,7 +89,8 @@ export function ctrl(root: RoundController, step: Step, redraw: Redraw): Keyboar
jump(delta: number) {
root.userJump(root.ply + delta);
redraw();
}
},
clock: () => root.clock
};
}
@ -109,7 +112,8 @@ export function render(ctrl: KeyboardMove) {
san: ctrl.san,
drop: ctrl.drop,
promote: ctrl.promote,
jump: ctrl.jump
jump: ctrl.jump,
clock: ctrl.clock
}));
});
})

View file

@ -38,6 +38,9 @@ window.lichess.keyboardMove = function(opts: any) {
if (v.length === 3) v = 'P' + v;
opts.drop(v.slice(2), v[0].toUpperCase());
clear();
} else if (v.startsWith('clock')) {
readClocks(opts.clock());
clear();
} else
opts.input.classList.toggle('wrong', v.length && sans && !sanCandidates(v, sans).length);
};
@ -115,3 +118,20 @@ function focusChat() {
const chatInput = document.querySelector('.mchat .mchat__say') as HTMLInputElement;
if (chatInput) chatInput.focus();
}
function readClocks(clockCtrl: any | undefined) {
if (!clockCtrl) return;
const msgs = ['white', 'black'].map(color => {
const time = clockCtrl.millisOf(color);
const date = new Date(time);
const msg = (time >= 3600000 ? simplePlural(Math.floor(time / 3600000), 'hour') : '') + ' ' +
simplePlural(date.getUTCMinutes(), 'minute') + ' ' +
simplePlural(date.getUTCSeconds(), 'seconds');
return `${color}: ${msg}`;
});
window.lichess.sound.say(msgs.join('. '));
}
function simplePlural(nb: number, word: string) {
return `${nb} ${word}${nb != 1 ? 's' : ''}`;
}