Rename js -> ts

pull/9560/head
Albert Ford 2021-07-31 12:09:02 -07:00
parent f16dcb48cc
commit 1fbb60e8e7
No known key found for this signature in database
GPG Key ID: 9C200032321A04A3
46 changed files with 272 additions and 270 deletions

View File

@ -3,8 +3,7 @@ import rollupProject from '@build/rollupProject';
export default rollupProject({
main: {
name: 'LichessLearn',
input: 'src/main.js',
input: 'src/main.ts',
output: 'learn',
js: true,
},
});

View File

@ -1,14 +1,14 @@
var readKeys = require('./util').readKeys;
const readKeys = require('./util').readKeys;
function pieceMatch(piece, matcher) {
if (!piece) return false;
for (var k in matcher) if (piece[k] !== matcher[k]) return false;
for (const k in matcher) if (piece[k] !== matcher[k]) return false;
return true;
}
function pieceOnAnyOf(matcher, keys) {
return function (level) {
for (var i in keys) if (pieceMatch(level.chess.get(keys[i]), matcher)) return true;
for (const i in keys) if (pieceMatch(level.chess.get(keys[i]), matcher)) return true;
return false;
};
}
@ -34,7 +34,7 @@ module.exports = {
noPieceOn: function (keys) {
keys = readKeys(keys);
return function (level) {
for (var key in level.chess.occupation()) if (!keys.includes(key)) return true;
for (const key in level.chess.occupation()) if (!keys.includes(key)) return true;
return false;
};
},
@ -43,7 +43,7 @@ module.exports = {
},
extinct: function (color) {
return function (level) {
var fen = level.chess.fen().split(' ')[0].replace(/\//g, '');
const fen = level.chess.fen().split(' ')[0].replace(/\//g, '');
return fen === (color === 'white' ? fen.toLowerCase() : fen.toUpperCase());
};
},
@ -55,7 +55,7 @@ module.exports = {
},
lastMoveSan: function (san) {
return function (level) {
var moves = level.chess.instance.history();
const moves = level.chess.instance.history();
return moves[moves.length - 1] === san;
};
},
@ -75,7 +75,7 @@ module.exports = {
};
},
and: function () {
var asserts = [].slice.call(arguments);
const asserts = [].slice.call(arguments);
return function (level) {
return asserts.every(function (a) {
return a(level);
@ -83,7 +83,7 @@ module.exports = {
};
},
or: function () {
var asserts = [].slice.call(arguments);
const asserts = [].slice.call(arguments);
return function (level) {
return asserts.some(function (a) {
return a(level);

View File

@ -1,12 +1,12 @@
var Chess = require('chess.js').Chess;
var util = require('./util');
const Chess = require('chess.js').Chess;
const util = require('./util');
module.exports = function (fen, appleKeys) {
var chess = new Chess(fen);
const chess = new Chess(fen);
// adds enemy pawns on apples, for collisions
if (appleKeys) {
var color = chess.turn() === 'w' ? 'b' : 'w';
const color = chess.turn() === 'w' ? 'b' : 'w';
appleKeys.forEach(function (key) {
chess.put(
{
@ -23,8 +23,8 @@ module.exports = function (fen, appleKeys) {
}
function setColor(c) {
var turn = c === 'white' ? 'w' : 'b';
var newFen = util.setFenTurn(chess.fen(), turn);
const turn = c === 'white' ? 'w' : 'b';
let newFen = util.setFenTurn(chess.fen(), turn);
chess.load(newFen);
if (getColor() !== c) {
// the en passant square prevents setting color
@ -33,7 +33,7 @@ module.exports = function (fen, appleKeys) {
}
}
var findCaptures = function () {
const findCaptures = function () {
return chess
.moves({
verbose: true,
@ -52,9 +52,9 @@ module.exports = function (fen, appleKeys) {
return {
dests: function (opts) {
opts = opts || {};
var dests = {};
const dests = {};
chess.SQUARES.forEach(function (s) {
var ms = chess.moves({
const ms = chess.moves({
square: s,
verbose: true,
legal: !opts.illegal,
@ -79,16 +79,16 @@ module.exports = function (fen, appleKeys) {
});
},
occupation: function () {
var map = {};
const map = {};
chess.SQUARES.forEach(function (s) {
var p = chess.get(s);
const p = chess.get(s);
if (p) map[s] = p;
});
return map;
},
kingKey: function (color) {
for (var i in chess.SQUARES) {
var p = chess.get(chess.SQUARES[i]);
for (const i in chess.SQUARES) {
const p = chess.get(chess.SQUARES[i]);
if (p && p.type === 'k' && p.color === (color === 'white' ? 'w' : 'b')) return chess.SQUARES[i];
}
},
@ -97,7 +97,7 @@ module.exports = function (fen, appleKeys) {
},
findUnprotectedCapture: function () {
return findCaptures().find(function (capture) {
var clone = new Chess(chess.fen());
const clone = new Chess(chess.fen());
clone.move({ from: capture.orig, to: capture.dest });
return !clone
.moves({
@ -110,9 +110,9 @@ module.exports = function (fen, appleKeys) {
},
checks: function () {
if (!chess.in_check()) return null;
var color = getColor();
const color = getColor();
setColor(color === 'white' ? 'black' : 'white');
var checks = chess
const checks = chess
.moves({
verbose: true,
})
@ -129,11 +129,11 @@ module.exports = function (fen, appleKeys) {
return checks;
},
playRandomMove: function () {
var moves = chess.moves({
const moves = chess.moves({
verbose: true,
});
if (moves.length) {
var move = moves[Math.floor(Math.random() * moves.length)];
const move = moves[Math.floor(Math.random() * moves.length)];
chess.move(move);
return {
orig: move.from,

View File

@ -1,5 +1,5 @@
function shuffle(a) {
var j, x, i;
let j, x, i;
for (i = a.length; i; i -= 1) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
@ -8,7 +8,7 @@ function shuffle(a) {
}
}
var list = [
const list = [
'awesome',
'excellent',
'greatJob',
@ -22,7 +22,7 @@ var list = [
];
shuffle(list);
var it = 0;
let it = 0;
module.exports = function () {
return list[it++ % list.length];

View File

@ -1,14 +1,14 @@
var chessground = require('chessground');
var raf = chessground.util.requestAnimationFrame;
var util = require('./util');
const chessground = require('chessground');
const raf = chessground.util.requestAnimationFrame;
const util = require('./util');
const timeouts = require('./timeouts');
var cg = new chessground.controller();
const cg = new chessground.controller();
module.exports = {
instance: cg,
set: function (opts) {
var check = opts.chess.instance.in_check();
const check = opts.chess.instance.in_check();
cg.set({
fen: opts.chess.fen(),
lastMove: null,
@ -68,7 +68,7 @@ module.exports = {
});
},
fen: function (fen, color, dests, lastMove) {
var config = {
const config = {
turnColor: color,
fen: fen,
movable: {
@ -80,7 +80,7 @@ module.exports = {
cg.set(config);
},
check: function (chess) {
var checks = chess.checks();
const checks = chess.checks();
cg.set({
check: checks ? checks[0].dest : null,
});
@ -92,8 +92,8 @@ module.exports = {
);
},
promote: function (key, role) {
var pieces = {};
var piece = cg.data.pieces[key];
const pieces = {};
const piece = cg.data.pieces[key];
if (piece && piece.role === 'pawn') {
pieces[key] = {
color: piece.color,
@ -114,7 +114,7 @@ module.exports = {
},
showCapture: function (move) {
raf(function () {
var $square = $('#learn-app piece[data-key=' + move.orig + ']');
const $square = $('#learn-app piece[data-key=' + move.orig + ']');
$square.addClass('wriggle');
timeouts.setTimeout(function () {
$square.removeClass('wriggle');
@ -124,11 +124,11 @@ module.exports = {
});
},
showCheckmate: function (chess) {
var turn = chess.instance.turn() === 'w' ? 'b' : 'w';
var fen = [cg.getFen(), turn, '- - 0 1'].join(' ');
const turn = chess.instance.turn() === 'w' ? 'b' : 'w';
const fen = [cg.getFen(), turn, '- - 0 1'].join(' ');
chess.instance.load(fen);
var kingKey = chess.kingKey(turn === 'w' ? 'black' : 'white');
var shapes = chess.instance
const kingKey = chess.kingKey(turn === 'w' ? 'black' : 'white');
const shapes = chess.instance
.moves({
verbose: true,
})

View File

@ -1,18 +1,18 @@
var m = require('mithril');
var util = require('./util');
const m = require('mithril');
const util = require('./util');
module.exports = {
ctrl: function (blueprint) {
var items = {};
const items = {};
util.readKeys(blueprint.apples).forEach(function (key) {
items[key] = 'apple';
});
var get = function (key) {
const get = function (key) {
return items[key];
};
var list = function () {
const list = function () {
return Object.keys(items).map(get);
};
@ -28,8 +28,8 @@ module.exports = {
return list().includes(item);
},
appleKeys: function () {
var keys = [];
for (var k in items) if (items[k] === 'apple') keys.push(k);
const keys = [];
for (const k in items) if (items[k] === 'apple') keys.push(k);
return keys;
},
};

View File

@ -1,20 +1,20 @@
var m = require('mithril');
var makeItems = require('./item').ctrl;
var itemView = require('./item').view;
var makeScenario = require('./scenario');
var makeChess = require('./chess');
var ground = require('./ground');
var scoring = require('./score');
var sound = require('./sound');
var promotion = require('./promotion');
const m = require('mithril');
const makeItems = require('./item').ctrl;
const itemView = require('./item').view;
const makeScenario = require('./scenario');
const makeChess = require('./chess');
const ground = require('./ground');
const scoring = require('./score');
const sound = require('./sound');
const promotion = require('./promotion');
const timeouts = require('./timeouts');
module.exports = function (blueprint, opts) {
var items = makeItems({
const items = makeItems({
apples: blueprint.apples,
});
var vm = {
const vm = {
lastStep: false,
completed: false,
willComplete: false,
@ -23,7 +23,7 @@ module.exports = function (blueprint, opts) {
nbMoves: 0,
};
var complete = function () {
const complete = function () {
vm.willComplete = true;
vm.score += scoring.getLevelBonus(blueprint, vm.nbMoves);
opts.onCompleteImmediate();
@ -43,7 +43,7 @@ module.exports = function (blueprint, opts) {
// cheat
// Mousetrap.bind(['shift+enter'], complete);
var assertData = function () {
const assertData = function () {
return {
scenario: scenario,
chess: chess,
@ -51,21 +51,21 @@ module.exports = function (blueprint, opts) {
};
};
var detectFailure = function () {
var failed = blueprint.failure && blueprint.failure(assertData());
const detectFailure = function () {
const failed = blueprint.failure && blueprint.failure(assertData());
if (failed) sound.failure();
return failed;
};
var detectSuccess = function () {
const detectSuccess = function () {
if (blueprint.success) return blueprint.success(assertData());
else return !items.hasItem('apple');
};
var detectCapture = function () {
const detectCapture = function () {
if (!blueprint.detectCapture) return false;
var fun = blueprint.detectCapture === 'unprotected' ? 'findUnprotectedCapture' : 'findCapture';
var move = chess[fun]();
const fun = blueprint.detectCapture === 'unprotected' ? 'findUnprotectedCapture' : 'findCapture';
const move = chess[fun]();
if (!move) return;
vm.failed = true;
ground.stop();
@ -74,9 +74,9 @@ module.exports = function (blueprint, opts) {
return true;
};
var sendMove = function (orig, dest, prom) {
const sendMove = function (orig, dest, prom) {
vm.nbMoves++;
var move = chess.move(orig, dest, prom);
const move = chess.move(orig, dest, prom);
if (move) ground.fen(chess.fen(), blueprint.color, {});
else {
// moving into check
@ -85,7 +85,7 @@ module.exports = function (blueprint, opts) {
sound.failure();
return m.redraw();
}
var took = false,
let took = false,
inScenario,
captured = false;
items.withItem(move.to, function (item) {
@ -116,7 +116,7 @@ module.exports = function (blueprint, opts) {
if (vm.failed) {
if (blueprint.showFailureFollowUp && !captured)
timeouts.setTimeout(function () {
var rm = chess.playRandomMove();
const rm = chess.playRandomMove();
ground.fen(chess.fen(), blueprint.color, {}, [rm.orig, rm.dest]);
}, 600);
} else {
@ -135,8 +135,8 @@ module.exports = function (blueprint, opts) {
});
};
var onMove = function (orig, dest) {
var piece = ground.get(dest);
const onMove = function (orig, dest) {
const piece = ground.get(dest);
if (!piece || piece.color !== blueprint.color) return;
if (!promotion.start(orig, dest, sendMove)) sendMove(orig, dest);
};

View File

@ -1,6 +1,6 @@
var mapView = require('./mapView');
var stages = require('../stage/list');
var scoring = require('../score');
const mapView = require('./mapView');
const stages = require('../stage/list');
const scoring = require('../score');
const timeouts = require('../timeouts');
module.exports = function (opts, trans) {
@ -15,15 +15,15 @@ module.exports = function (opts, trans) {
data: opts.storage.data,
trans: trans,
isStageIdComplete: function (stageId) {
var stage = stages.byId[stageId];
const stage = stages.byId[stageId];
if (!stage) return true;
var result = opts.storage.data.stages[stage.key];
const result = opts.storage.data.stages[stage.key];
if (!result) return false;
return result.scores.filter(scoring.gtz).length >= stage.levels.length;
},
stageProgress: function (stage) {
var result = opts.storage.data.stages[stage.key];
var complete = result ? result.scores.filter(scoring.gtz).length : 0;
const result = opts.storage.data.stages[stage.key];
const complete = result ? result.scores.filter(scoring.gtz).length : 0;
return [complete, stage.levels.length];
},
};

View File

@ -1,7 +1,7 @@
var m = require('mithril');
var util = require('../util');
var stages = require('../stage/list');
var scoring = require('../score');
import m = require('mithril');
let util = require('../util');
let stages = require('../stage/list');
let scoring = require('../score');
function renderInStage(ctrl) {
return m('div.learn__side-map', [
@ -38,8 +38,8 @@ function renderInStage(ctrl) {
m(
'div.categ_stages',
categ.stages.map(function (s) {
var result = ctrl.data.stages[s.key];
var status = s.id === ctrl.active() ? 'active' : result ? 'done' : 'future';
let result = ctrl.data.stages[s.key];
let status = s.id === ctrl.active() ? 'active' : result ? 'done' : 'future';
return m(
'a',
{
@ -64,7 +64,7 @@ function renderInStage(ctrl) {
}
function renderHome(ctrl) {
var progress = ctrl.progress();
let progress = ctrl.progress();
return m('div.learn__side-home', [
m('i.fat'),
m('h1', ctrl.trans.noarg('learnChess')),
@ -96,7 +96,7 @@ function renderHome(ctrl) {
module.exports = function (opts, trans) {
return {
controller: function () {
var categId = m.prop(0);
let categId = m.prop(0);
m.redraw.strategy('diff');
return {
data: opts.storage.data,
@ -111,10 +111,10 @@ module.exports = function (opts, trans) {
categId(stages.stageIdToCategId(stage.id));
},
progress: function () {
var max = stages.list.length * 10;
var data = opts.storage.data.stages;
var total = Object.keys(data).reduce(function (t, key) {
var rank = scoring.getStageRank(stages.byKey[key], data[key].scores);
let max = stages.list.length * 10;
let data = opts.storage.data.stages;
let total = Object.keys(data).reduce(function (t, key) {
let rank = scoring.getStageRank(stages.byKey[key], data[key].scores);
if (rank === 1) return t + 10;
if (rank === 2) return t + 8;
return t + 5;

View File

@ -1,11 +1,11 @@
var m = require('mithril');
var util = require('../util');
var scoring = require('../score');
var stages = require('../stage/list');
const m = require('mithril');
const util = require('../util');
const scoring = require('../score');
const stages = require('../stage/list');
function makeStars(nb) {
var stars = [];
for (var i = 0; i < 4 - nb; i++)
const stars = [];
for (let i = 0; i < 4 - nb; i++)
stars.push(
m('i', {
'data-icon': '',
@ -16,9 +16,9 @@ function makeStars(nb) {
function ribbon(ctrl, s, status, res) {
if (status === 'future') return;
var content;
let content;
if (status === 'ongoing') {
var p = ctrl.stageProgress(s);
const p = ctrl.stageProgress(s);
content = p[0] ? p.join(' / ') : ctrl.trans.noarg('play');
} else content = makeStars(scoring.getStageRank(s, res.scores));
if (status !== 'future')
@ -35,8 +35,8 @@ function ribbon(ctrl, s, status, res) {
}
function whatNext(ctrl) {
var makeStage = function (href, img, title, subtitle, done) {
var transTitle = ctrl.trans.noarg(title);
const makeStage = function (href, img, title, subtitle, done) {
const transTitle = ctrl.trans.noarg(title);
return m(
'a',
{
@ -52,7 +52,7 @@ function whatNext(ctrl) {
]
);
};
var userId = ctrl.data._id;
const userId = ctrl.data._id;
return m('div.categ.what_next', [
m('h2', ctrl.trans.noarg('whatNext')),
m('p', ctrl.trans.noarg('youKnowHowToPlayChess')),
@ -83,13 +83,13 @@ module.exports = function (ctrl) {
m(
'div.categ_stages',
categ.stages.map(function (s) {
var res = ctrl.data.stages[s.key];
var complete = ctrl.isStageIdComplete(s.id);
var prevComplete = ctrl.isStageIdComplete(s.id - 1);
var status = 'future';
const res = ctrl.data.stages[s.key];
const complete = ctrl.isStageIdComplete(s.id);
const prevComplete = ctrl.isStageIdComplete(s.id - 1);
let status = 'future';
if (complete) status = 'done';
else if (prevComplete || res) status = 'ongoing';
var title = ctrl.trans.noarg(s.title);
const title = ctrl.trans.noarg(s.title);
return m(
'a',
{

View File

@ -1,12 +1,12 @@
var m = require('mithril');
var scoring = require('./score');
const m = require('mithril');
const scoring = require('./score');
var star = m('i[data-icon=]');
const star = m('i[data-icon=]');
function makeStars(level, score) {
var rank = scoring.getLevelRank(level, score);
var stars = [];
for (var i = 3; i >= rank; i--) stars.push(star);
const rank = scoring.getLevelRank(level, score);
const stars = [];
for (let i = 3; i >= rank; i--) stars.push(star);
return m('span.stars.st' + stars.length, stars);
}
@ -24,9 +24,9 @@ module.exports = {
return m(
'div.progress',
ctrl.stage.levels.map(function (level) {
var score = ctrl.score(level);
var status = level.id === ctrl.level.blueprint.id ? 'active' : score ? 'done' : 'future';
var label = score ? makeStars(level, score) : m('span.id', level.id);
const score = ctrl.score(level);
const status = level.id === ctrl.level.blueprint.id ? 'active' : score ? 'done' : 'future';
const label = score ? makeStars(level, score) : m('span.id', level.id);
return m(
'a',
{

View File

@ -1,13 +1,13 @@
var m = require('mithril');
var chessground = require('chessground');
var ground = require('./ground');
var opposite = chessground.util.opposite;
var key2pos = chessground.util.key2pos;
const m = require('mithril');
const chessground = require('chessground');
const ground = require('./ground');
const opposite = chessground.util.opposite;
const key2pos = chessground.util.key2pos;
var promoting = false;
let promoting = false;
function start(orig, dest, callback) {
var piece = ground.pieces()[dest];
const piece = ground.pieces()[dest];
if (
piece &&
piece.role == 'pawn' &&
@ -33,10 +33,10 @@ function finish(role) {
function renderPromotion(ctrl, dest, pieces, color, orientation, explain) {
if (!promoting) return;
var left = (8 - key2pos(dest)[0]) * 12.5;
let left = (8 - key2pos(dest)[0]) * 12.5;
if (orientation === 'white') left = 87.5 - left;
var vertical = color === orientation ? 'top' : 'bottom';
const vertical = color === orientation ? 'top' : 'bottom';
return m('div#promotion-choice.' + vertical, [
pieces.map(function (serverRole, i) {
@ -70,7 +70,7 @@ module.exports = {
view: function (ctrl, stage) {
if (!promoting) return;
var pieces = ['queen', 'knight', 'rook', 'bishop'];
const pieces = ['queen', 'knight', 'rook', 'bishop'];
return renderPromotion(
ctrl,

View File

@ -1,22 +1,22 @@
var m = require('mithril');
var stages = require('../stage/list');
var makeLevel = require('../level');
var makeProgress = require('../progress').ctrl;
var sound = require('../sound');
const m = require('mithril');
const stages = require('../stage/list');
const makeLevel = require('../level');
const makeProgress = require('../progress').ctrl;
const sound = require('../sound');
const timeouts = require('../timeouts');
module.exports = function (opts, trans) {
timeouts.clearTimeouts();
var stage = stages.byId[m.route.param('stage')];
const stage = stages.byId[m.route.param('stage')];
if (!stage) m.route('/');
opts.side.ctrl.setStage(stage);
var levelId =
const levelId =
m.route.param('level') ||
(function () {
var result = opts.storage.data.stages[stage.key];
var it = 0;
const result = opts.storage.data.stages[stage.key];
let it = 0;
if (result) while (result.scores[it]) it++;
if (it >= stage.levels.length) it = 0;
return it + 1;
@ -38,8 +38,8 @@ module.exports = function (opts, trans) {
},
});
var stageScore = function () {
var res = opts.storage.data.stages[stage.key];
const stageScore = function () {
const res = opts.storage.data.stages[stage.key];
return res
? res.scores.reduce(function (a, b) {
return a + b;
@ -59,7 +59,7 @@ module.exports = function (opts, trans) {
isRestarting.set(false);
var getNext = function () {
const getNext = function () {
return stages.byId[stage.id + 1];
};
if (vm.stageStarting()) sound.stageStart();

View File

@ -1,5 +1,5 @@
var runCtrl = require('./runCtrl');
var runView = require('./runView');
const runCtrl = require('./runCtrl');
const runView = require('./runView');
module.exports = function (opts, trans) {
return {

View File

@ -1,13 +1,13 @@
var m = require('mithril');
var chessground = require('chessground');
var util = require('../util');
var ground = require('../ground');
var congrats = require('../congrats');
var stageStarting = require('./stageStarting');
var stageComplete = require('./stageComplete');
var renderPromotion = require('../promotion').view;
var renderProgress = require('../progress').view;
var makeStars = require('../progress').makeStars;
const m = require('mithril');
const chessground = require('chessground');
const util = require('../util');
const ground = require('../ground');
const congrats = require('../congrats');
const stageStarting = require('./stageStarting');
const stageComplete = require('./stageComplete');
const renderPromotion = require('../promotion').view;
const renderProgress = require('../progress').view;
const makeStars = require('../progress').makeStars;
function renderFailed(ctrl) {
return m(
@ -34,8 +34,8 @@ function renderCompleted(ctrl, level) {
}
module.exports = function (ctrl) {
var stage = ctrl.stage;
var level = ctrl.level;
const stage = ctrl.stage;
const level = ctrl.level;
return m(
'div',

View File

@ -1,18 +1,18 @@
var m = require('mithril');
var util = require('../util');
var scoring = require('../score');
var numberSpread = require('common/number').numberSpread;
const m = require('mithril');
const util = require('../util');
const scoring = require('../score');
const numberSpread = require('common/number').numberSpread;
function makeStars(rank) {
var stars = [];
for (var i = 3; i > 0; i--) stars.push(m('div.star-wrap', rank <= i ? m('i.star') : null));
const stars = [];
for (let i = 3; i > 0; i--) stars.push(m('div.star-wrap', rank <= i ? m('i.star') : null));
return stars;
}
module.exports = function (ctrl) {
var stage = ctrl.stage;
var next = ctrl.getNext();
var score = ctrl.stageScore();
const stage = ctrl.stage;
const next = ctrl.getNext();
const score = ctrl.stageScore();
return m(
'div.learn__screen-overlay',
{

View File

@ -1,5 +1,5 @@
var m = require('mithril');
var util = require('../util');
const m = require('mithril');
const util = require('../util');
module.exports = function (ctrl) {
return m(

View File

@ -1,9 +1,9 @@
var util = require('./util');
var ground = require('./ground');
const util = require('./util');
const ground = require('./ground');
const timeouts = require('./timeouts');
module.exports = function (blueprint, opts) {
var steps = (blueprint || []).map(function (step) {
const steps = (blueprint || []).map(function (step) {
if (step.move) return step;
return {
move: step,
@ -11,19 +11,19 @@ module.exports = function (blueprint, opts) {
};
});
var it = 0;
var isFailed = false;
let it = 0;
let isFailed = false;
var fail = function () {
const fail = function () {
isFailed = true;
return false;
};
var opponent = function () {
var step = steps[it];
const opponent = function () {
const step = steps[it];
if (!step) return;
var move = util.decomposeUci(step.move);
var res = opts.chess.move(move[0], move[1], move[2]);
const move = util.decomposeUci(step.move);
const res = opts.chess.move(move[0], move[1], move[2]);
if (!res) return fail();
it++;
ground.fen(opts.chess.fen(), opts.chess.color(), opts.makeChessDests(), move);
@ -42,7 +42,7 @@ module.exports = function (blueprint, opts) {
},
opponent: opponent,
player: function (move) {
var step = steps[it];
const step = steps[it];
if (!step) return;
if (step.move !== move) return fail();
it++;

View File

@ -1,30 +1,30 @@
var util = require('./util');
const util = require('./util');
var apple = 50;
var capture = 50;
var scenario = 50;
const apple = 50;
const capture = 50;
const scenario = 50;
var levelBonus = {
const levelBonus = {
1: 500,
2: 300,
3: 100,
};
function getLevelBonus(l, nbMoves) {
var late = nbMoves - l.nbMoves;
const late = nbMoves - l.nbMoves;
if (late <= 0) return levelBonus[1];
if (late <= Math.max(1, l.nbMoves / 8)) return levelBonus[2];
return levelBonus[3];
}
function getLevelMaxScore(l) {
var score = util.readKeys(l.apples).length * apple;
let score = util.readKeys(l.apples).length * apple;
if (l.pointsForCapture) score += (l.captures || 0) * capture;
return score + levelBonus[1];
}
function getLevelRank(l, score) {
var max = getLevelMaxScore(l);
const max = getLevelMaxScore(l);
if (score >= max) return 1;
if (score >= max - 200) return 2;
return 3;
@ -37,14 +37,14 @@ function getStageMaxScore(s) {
}
function getStageRank(s, score) {
var max = getStageMaxScore(s);
const max = getStageMaxScore(s);
if (typeof score !== 'number') score = score.reduce((a, b) => a + b, 0);
if (score >= max) return 1;
if (score >= max - Math.max(200, s.levels.length * 150)) return 2;
return 3;
}
var pieceValues = {
const pieceValues = {
q: 90,
r: 50,
b: 30,

View File

@ -1,4 +1,4 @@
var util = require('./util');
const util = require('./util');
const make = (file, volume) => {
lichess.sound.loadOggOrMp3(file, `${lichess.sound.baseUrl}/${file}`);

View File

@ -1,5 +1,5 @@
var util = require('../util');
var arrow = util.arrow;
const util = require('../util');
const arrow = util.arrow;
module.exports = {
key: 'bishop',

View File

@ -1,8 +1,8 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/bowman.svg';
const imgUrl = util.assetUrl + 'images/learn/bowman.svg';
module.exports = {
key: 'capture',

View File

@ -1,17 +1,17 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow,
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow,
circle = util.circle;
var imgUrl = util.assetUrl + 'images/learn/castle.svg';
const imgUrl = util.assetUrl + 'images/learn/castle.svg';
var castledKingSide = assert.lastMoveSan('O-O');
var castledQueenSide = assert.lastMoveSan('O-O-O');
var cantCastleKingSide = assert.and(
const castledKingSide = assert.lastMoveSan('O-O');
const castledQueenSide = assert.lastMoveSan('O-O-O');
const cantCastleKingSide = assert.and(
assert.not(castledKingSide),
assert.or(assert.pieceNotOn('K', 'e1'), assert.pieceNotOn('R', 'h1'))
);
var cantCastleQueenSide = assert.and(
const cantCastleQueenSide = assert.and(
assert.not(castledQueenSide),
assert.or(assert.pieceNotOn('K', 'e1'), assert.pieceNotOn('R', 'a1'))
);

View File

@ -1,8 +1,8 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/winged-sword.svg';
const imgUrl = util.assetUrl + 'images/learn/winged-sword.svg';
module.exports = {
key: 'check1',

View File

@ -1,8 +1,8 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/crossed-swords.svg';
const imgUrl = util.assetUrl + 'images/learn/crossed-swords.svg';
module.exports = {
key: 'check2',

View File

@ -1,8 +1,8 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/guillotine.svg';
const imgUrl = util.assetUrl + 'images/learn/guillotine.svg';
module.exports = {
key: 'checkmate1',

View File

@ -1,8 +1,8 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/battle-gear.svg';
const imgUrl = util.assetUrl + 'images/learn/battle-gear.svg';
module.exports = {
key: 'combat',

View File

@ -1,8 +1,8 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/spinning-blades.svg';
const imgUrl = util.assetUrl + 'images/learn/spinning-blades.svg';
module.exports = {
key: 'enpassant',

View File

@ -1,10 +1,10 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/crossed-swords.svg';
const imgUrl = util.assetUrl + 'images/learn/crossed-swords.svg';
var twoMoves = 'Threaten the opponent king<br>in two moves!';
const twoMoves = 'Threaten the opponent king<br>in two moves!';
module.exports = {
key: 'check2',

View File

@ -1,5 +1,5 @@
var util = require('../util');
var arrow = util.arrow;
const util = require('../util');
const arrow = util.arrow;
module.exports = {
key: 'king',

View File

@ -1,5 +1,5 @@
var util = require('../util');
var arrow = util.arrow;
const util = require('../util');
const arrow = util.arrow;
module.exports = {
key: 'knight',

View File

@ -1,4 +1,4 @@
var categs = [
let categs = [
{
key: 'chess-pieces',
name: 'chessPieces',
@ -40,8 +40,8 @@ var categs = [
},
];
var stageId = 1;
var stages = [];
let stageId = 1;
const stages = [];
categs = categs.map(function (c) {
c.stages = c.stages.map(function (stage) {
@ -52,12 +52,12 @@ categs = categs.map(function (c) {
return c;
});
var stagesByKey = {};
const stagesByKey = {};
stages.forEach(function (s) {
stagesByKey[s.key] = s;
});
var stagesById = {};
const stagesById = {};
stages.forEach(function (s) {
stagesById[s.id] = s;
});
@ -68,8 +68,8 @@ module.exports = {
byKey: stagesByKey,
categs: categs,
stageIdToCategId: function (stageId) {
var stage = stagesById[stageId];
for (var id in categs)
const stage = stagesById[stageId];
for (const id in categs)
if (
categs[id].stages.some(function (s) {
return s.key === stage.key;

View File

@ -1,7 +1,7 @@
var util = require('../util');
var arrow = util.arrow;
const util = require('../util');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/guards.svg';
const imgUrl = util.assetUrl + 'images/learn/guards.svg';
module.exports = {
key: 'outOfCheck',

View File

@ -1,6 +1,6 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow;
module.exports = {
key: 'pawn',

View File

@ -1,7 +1,7 @@
var util = require('../util');
var arrow = util.arrow;
const util = require('../util');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/bolt-shield.svg';
const imgUrl = util.assetUrl + 'images/learn/bolt-shield.svg';
module.exports = {
key: 'protection',

View File

@ -1,5 +1,5 @@
var util = require('../util');
var arrow = util.arrow;
const util = require('../util');
const arrow = util.arrow;
module.exports = {
key: 'queen',

View File

@ -1,5 +1,5 @@
var util = require('../util');
var arrow = util.arrow;
const util = require('../util');
const arrow = util.arrow;
module.exports = {
key: 'rook',

View File

@ -1,9 +1,9 @@
var util = require('../util');
var assert = require('../assert');
var and = assert.and;
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const and = assert.and;
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/rally-the-troops.svg';
const imgUrl = util.assetUrl + 'images/learn/rally-the-troops.svg';
module.exports = {
key: 'setup',

View File

@ -1,9 +1,9 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow,
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow,
circle = util.circle;
var imgUrl = util.assetUrl + 'images/learn/scales.svg';
const imgUrl = util.assetUrl + 'images/learn/scales.svg';
module.exports = {
key: 'stalemate',

View File

@ -1,8 +1,8 @@
var util = require('../util');
var assert = require('../assert');
var arrow = util.arrow;
const util = require('../util');
const assert = require('../assert');
const arrow = util.arrow;
var imgUrl = util.assetUrl + 'images/learn/sprint.svg';
const imgUrl = util.assetUrl + 'images/learn/sprint.svg';
module.exports = {
key: 'value',

View File

@ -1,8 +1,8 @@
var m = require('mithril');
const m = require('mithril');
var key = 'learn.progress';
const key = 'learn.progress';
var defaultValue = {
const defaultValue = {
stages: {},
};

View File

@ -1,4 +1,4 @@
var m = require('mithril');
const m = require('mithril');
module.exports = {
toLevel: function (l, it) {

View File

@ -0,0 +1,3 @@
{
"extends": "../tsconfig.base.json"
}