Merge pull request #3053 from niklasf/round2-more-ts

more ts for round2
This commit is contained in:
Thibault Duplessis 2017-05-19 14:37:59 +02:00 committed by GitHub
commit f32119c20c
6 changed files with 88 additions and 91 deletions

View file

@ -1,7 +1,8 @@
var util = require('chessground/util');
import * as util from 'chessground/util';
import * as cg from 'chessground/types';
function capture(ctrl, key) {
var exploding = [];
export function capture(ctrl, key: cg.Key) {
var exploding: cg.Key[] = [];
var diff = {};
var orig = util.key2pos(key);
var minX = Math.max(1, orig[0] - 1),
@ -24,13 +25,8 @@ function capture(ctrl, key) {
}
// needs to explicitly destroy the capturing pawn
function enpassant(ctrl, key, color) {
var pos = util.key2pos(key);
var pawnPos = [pos[0], pos[1] + (color === 'white' ? -1 : 1)];
export function enpassant(ctrl, key: cg.Key, color: cg.Color) {
const pos = util.key2pos(key);
const pawnPos: cg.Pos = [pos[0], pos[1] + (color === 'white' ? -1 : 1)];
capture(ctrl, util.pos2key(pawnPos));
}
module.exports = {
capture: capture,
enpassant: enpassant
};

View file

@ -1,28 +0,0 @@
var plyStep = require('./round').plyStep;
var playable = require('game').game.playable;
var found = false;
function truncateFen(fen) {
return fen.split(' ')[0];
}
module.exports = {
subscribe: function(ctrl) {
// allow everyone to cheat against the AI
if (ctrl.data.opponent.ai) return;
// allow registered players to use assistance in casual games
if (!ctrl.data.game.rated && ctrl.userId) return;
lichess.storage.make('ceval.fen').listen(function(ev) {
var d = ctrl.data;
if (!found && ev.newValue && ctrl.vm.ply > 14 && playable(d) &&
truncateFen(plyStep(d, ctrl.vm.ply).fen) === truncateFen(ev.newValue)) {
$.post('/jslog/' + d.game.id + d.player.id + '?n=ceval');
found = true;
}
});
},
publish: function(ctrl, move) {
if (ctrl.data.opponent.ai) lichess.storage.set('ceval.fen', move.fen);
}
};

27
ui/round2/src/cevalSub.ts Normal file
View file

@ -0,0 +1,27 @@
import { plyStep } from './round';
import { game } from 'game';
var found = false;
function truncateFen(fen) {
return fen.split(' ')[0];
}
export function subscribe(ctrl) {
// allow everyone to cheat against the AI
if (ctrl.data.opponent.ai) return;
// allow registered players to use assistance in casual games
if (!ctrl.data.game.rated && ctrl.userId) return;
window.lichess.storage.make('ceval.fen').listen(function(ev) {
var d = ctrl.data;
if (!found && ev.newValue && ctrl.vm.ply > 14 && game.playable(d) &&
truncateFen(plyStep(d, ctrl.vm.ply).fen) === truncateFen(ev.newValue)) {
$.post('/jslog/' + d.game.id + d.player.id + '?n=ceval');
found = true;
}
});
}
export function publish(ctrl, move) {
if (ctrl.data.opponent.ai) window.lichess.storage.set('ceval.fen', move.fen);
}

View file

@ -1,3 +1,4 @@
/// <reference types="types/lichess" />
/// <reference types="types/lichess-jquery" />
import makeCtrl = require('./ctrl');

View file

@ -1,52 +0,0 @@
var game = require('game').game;
var status = require('game').status;
var initialTitle = document.title;
var curFaviconIdx = 0;
var F = [
'/assets/images/favicon-32-white.png',
'/assets/images/favicon-32-black.png'
].map(function(path, i) {
return function() {
if (curFaviconIdx !== i) {
document.getElementById('favicon').href = path;
curFaviconIdx = i;
}
};
});
var tickerTimer = undefined;
function resetTicker() {
tickerTimer = clearTimeout(tickerTimer);
F[0]();
}
function startTicker() {
function tick() {
if (!document.hasFocus()) {
F[1 - curFaviconIdx]();
tickerTimer = setTimeout(tick, 1000);
}
}
if (!tickerTimer) tickerTimer = setTimeout(tick, 200);
};
module.exports = {
init: function(ctrl) { window.addEventListener('focus', resetTicker); },
set: function(ctrl, text) {
if (ctrl.data.player.spectator) return;
if (!text) {
if (status.finished(ctrl.data)) {
text = ctrl.trans('gameOver');
} else if (game.isPlayerTurn(ctrl.data)) {
text = ctrl.trans('yourTurn');
if (!document.hasFocus()) startTicker();
} else {
text = ctrl.trans('waitingForOpponent');
resetTicker();
}
}
document.title = text + " - " + initialTitle;
}
};

53
ui/round2/src/title.ts Normal file
View file

@ -0,0 +1,53 @@
import { game, status } from 'game';
const initialTitle = document.title;
var curFaviconIdx = 0;
const F = [
'/assets/images/favicon-32-white.png',
'/assets/images/favicon-32-black.png'
].map(function(path, i) {
return function() {
if (curFaviconIdx !== i) {
const favicon = document.getElementById('favicon') as HTMLAnchorElement;
favicon.href = path;
curFaviconIdx = i;
}
};
});
var tickerTimer;
function resetTicker() {
tickerTimer = clearTimeout(tickerTimer);
F[0]();
}
function startTicker() {
function tick() {
if (!document.hasFocus()) {
F[1 - curFaviconIdx]();
tickerTimer = setTimeout(tick, 1000);
}
}
if (!tickerTimer) tickerTimer = setTimeout(tick, 200);
}
export function init() {
window.addEventListener('focus', resetTicker);
}
export function set(ctrl, text) {
if (ctrl.data.player.spectator) return;
if (!text) {
if (status.finished(ctrl.data)) {
text = ctrl.trans('gameOver');
} else if (game.isPlayerTurn(ctrl.data)) {
text = ctrl.trans('yourTurn');
if (!document.hasFocus()) startTicker();
} else {
text = ctrl.trans('waitingForOpponent');
resetTicker();
}
}
document.title = text + " - " + initialTitle;
}