本文整理汇总了TypeScript中common.storedProp函数的典型用法代码示例。如果您正苦于以下问题:TypeScript storedProp函数的具体用法?TypeScript storedProp怎么用?TypeScript storedProp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了storedProp函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: ctrl
export function ctrl(send: SocketSend, chapters: Prop<StudyChapterMeta[]>, setTab: () => void, root: AnalyseCtrl) {
const multiPgnMax = 20;
const vm = {
variants: [],
open: false,
initial: prop(false),
tab: storedProp('study.form.tab', 'init'),
editor: null,
editorFen: prop(null)
};
function loadVariants() {
if (!vm.variants.length) xhrVariants().then(function(vs) {
vm.variants = vs;
root.redraw();
});
};
function open() {
vm.open = true;
loadVariants();
vm.initial(false);
}
function close() {
vm.open = false;
}
return {
vm,
open,
root,
openInitial() {
open();
vm.initial(true);
},
close,
toggle() {
if (vm.open) close();
else open();
},
submit(d) {
d.initial = vm.initial();
d.sticky = root.study!.vm.mode.sticky;
if (!d.pgn) send("addChapter", d);
else importPgn(root.study!.data.id, d);
close();
setTab();
},
chapters,
startTour: () => chapterTour(tab => {
vm.tab(tab);
root.redraw();
}),
multiPgnMax,
redraw: root.redraw
}
}
示例2: controller
export function controller(game: Game, onClose: () => void, trans: Trans, redraw: () => void): ExplorerConfigCtrl {
const variant = (game.variant.key === 'fromPosition') ? 'standard' : game.variant.key;
const available: ExplorerDb[] = ['lichess'];
if (variant === 'standard') available.unshift('masters');
const data: ExplorerConfigData = {
open: prop(false),
db: {
available,
selected: available.length > 1 ? storedProp('explorer.db.' + variant, available[0]) : function() {
return available[0];
}
},
rating: {
available: [1600, 1800, 2000, 2200, 2500],
selected: storedJsonProp('explorer.rating', [1600, 1800, 2000, 2200, 2500])
},
speed: {
available: allSpeeds,
selected: storedJsonProp<ExplorerSpeed[]>('explorer.speed', allSpeeds)
}
};
const toggleMany = function(c, value) {
if (c().indexOf(value) === -1) c(c().concat([value]));
else if (c().length > 1) c(c().filter(v => v !== value));
};
return {
trans,
redraw,
data,
toggleOpen() {
data.open(!data.open());
if (!data.open()) onClose();
},
toggleDb(db) {
data.db.selected(db);
},
toggleRating(v) { toggleMany(data.rating.selected, v) },
toggleSpeed(v) { toggleMany(data.speed.selected, v) },
fullHouse() {
return data.db.selected() === 'masters' || (
data.rating.selected().length === data.rating.available.length &&
data.speed.selected().length === data.speed.available.length
);
}
};
}
示例3: ctrl
export function ctrl(initialValue: TreeViewKey = 'column'): TreeView {
const value = storedProp<TreeViewKey>('treeView', initialValue);
function inline() {
return value() === 'inline';
}
function set(i: boolean) {
value(i ? 'inline' : 'column');
}
return {
get: value,
set,
toggle() {
set(!inline());
},
inline
};
}
示例4: function
export default function(root: AnalyseCtrl, studyData: StudyData, data: StudyPracticeData): StudyPracticeCtrl {
const goal = prop<Goal>(root.data.practiceGoal!),
nbMoves = prop(0),
// null = ongoing, true = win, false = fail
success = prop<boolean | null>(null),
sound = makeSound(),
analysisUrl = prop(''),
autoNext = storedProp('practice-auto-next', true);
function onLoad() {
root.showAutoShapes = readOnlyProp(true);
root.showGauge = readOnlyProp(true);
root.showComputer = readOnlyProp(true);
goal(root.data.practiceGoal!);
nbMoves(0);
success(null);
const chapter = studyData.chapter;
history.replaceState(null, chapter.name, data.url + '/' + chapter.id);
analysisUrl('/analysis/standard/' + root.node.fen.replace(/ /g, '_') + '?color=' + root.bottomColor());
}
onLoad();
function computeNbMoves(): number {
let plies = root.node.ply - root.tree.root.ply;
if (root.bottomColor() !== root.data.player.color) plies--;
return Math.ceil(plies / 2);
}
function getStudy(): StudyCtrl {
return root.study!;
}
function checkSuccess(): void {
const gamebook = getStudy().gamebookPlay();
if (gamebook) {
if (gamebook.state.feedback === 'end') onVictory();
return;
}
if (!getStudy().data.chapter.practice) {
return saveNbMoves();
}
if (success() !== null) return;
nbMoves(computeNbMoves());
const res = success(makeSuccess(root, goal(), nbMoves()));
if (res) onVictory();
else if (res === false) onFailure();
}
function onVictory(): void {
saveNbMoves();
sound.success();
if (autoNext()) setTimeout(goToNext, 1000);
}
function saveNbMoves(): void {
const chapterId = getStudy().currentChapter().id,
former = data.completion[chapterId];
if (typeof former === 'undefined' || nbMoves() < former) {
data.completion[chapterId] = nbMoves();
xhr.practiceComplete(chapterId, nbMoves());
}
}
function goToNext() {
const next = getStudy().nextChapter();
if (next) getStudy().setChapter(next.id);
}
function onFailure(): void {
root.node.fail = true;
sound.failure();
}
return {
onLoad,
onJump() {
// reset failure state if no failed move found in mainline history
if (success() === false && !root.nodeList.find(n => !!n.fail)) success(null);
checkSuccess();
},
onCeval: checkSuccess,
data,
goal,
success,
nbMoves,
reset() {
root.tree.root.children = [];
root.userJump('');
root.practice!.reset();
onLoad();
root.practice!.resume();
},
isWhite: root.bottomIsWhite,
analysisUrl,
autoNext,
goToNext
};
}
示例5: function
export default function(root: AnalyseCtrl, opts, allow: boolean): ExplorerCtrl {
const allowed = prop(allow),
enabled = root.embed ? prop(false) : storedProp('explorer.enabled', false),
loading = prop(true),
failing = prop(false),
hovering = prop<Hovering | null>(null),
movesAway = prop(0),
gameMenu = prop<string | null>(null);
if ((location.hash === '#explorer' || location.hash === '#opening') && !root.embed) enabled(true);
let cache = {};
function onConfigClose() {
root.redraw();
cache = {};
setNode();
}
const withGames = synthetic(root.data) || gameUtil.replayable(root.data) || !!root.data.opponent.ai;
const effectiveVariant = root.data.game.variant.key === 'fromPosition' ? 'standard' : root.data.game.variant.key;
const config = configCtrl(root.data.game, onConfigClose, root.trans, root.redraw);
const fetch = window.lichess.fp.debounce(function() {
const fen = root.node.fen;
const request: JQueryPromise<ExplorerData> = (withGames && tablebaseRelevant(effectiveVariant, fen)) ?
xhr.tablebase(opts.tablebaseEndpoint, effectiveVariant, fen) :
xhr.opening(opts.endpoint, effectiveVariant, fen, config.data, withGames);
request.then((res: ExplorerData) => {
cache[fen] = res;
movesAway(res.moves.length ? 0 : movesAway() + 1);
loading(false);
failing(false);
root.redraw();
}, () => {
loading(false);
failing(true);
root.redraw();
});
}, 250, true);
const empty = {
opening: true,
moves: {}
};
function setNode() {
if (!enabled()) return;
gameMenu(null);
const node = root.node;
if (node.ply > 50 && !tablebaseRelevant(effectiveVariant, node.fen)) {
cache[node.fen] = empty;
}
const cached = cache[root.node.fen];
if (cached) {
movesAway(cached.moves.length ? 0 : movesAway() + 1);
loading(false);
failing(false);
} else {
loading(true);
fetch();
}
};
return {
allowed,
enabled,
setNode,
loading,
failing,
hovering,
movesAway,
config,
withGames,
gameMenu,
current: () => cache[root.node.fen],
toggle() {
movesAway(0);
enabled(!enabled());
setNode();
root.autoScroll();
},
disable() {
if (enabled()) {
enabled(false);
gameMenu(null);
root.autoScroll();
}
},
setHovering(fen, uci) {
hovering(uci ? {
fen,
uci,
} : null);
root.setAutoShapes();
},
fetchMasterOpening: (function() {
const masterCache = {};
return (fen: Fen): JQueryPromise<OpeningData> => {
if (masterCache[fen]) return $.Deferred().resolve(masterCache[fen]).promise() as JQueryPromise<OpeningData>;
//.........这里部分代码省略.........
示例6: ctrl
export function ctrl(send: SocketSend, chapters: Prop<StudyChapterMeta[]>, setTab: () => void, root: AnalyseCtrl) {
const multiPgnMax = 20;
const vm = {
variants: [],
open: false,
initial: prop(false),
tab: storedProp('study.form.tab', 'init'),
editor: null,
editorFen: prop(null)
};
function loadVariants() {
if (!vm.variants.length) xhrVariants().then(function(vs) {
vm.variants = vs;
root.redraw();
});
};
function open() {
vm.open = true;
loadVariants();
vm.initial(false);
};
function close() {
vm.open = false;
};
function identity<A>(x: A): A {
return x;
}
function submitMultiPgn(d) {
if (d.pgn) {
const lines = d.pgn.split('\n');
const parts = lines.map(function(l, i) {
// ensure 2 spaces after each game
if (!l.trim() && i && lines[i - 1][0] !== '[') return '\n';
return l;
}).join('\n').split('\n\n\n').map(function(part) {
// remove empty lines in each game
return part.split('\n').filter(identity).join('\n');
}).filter(identity); // remove empty games
if (parts.length > 1) {
if (parts.length > multiPgnMax && !confirm('Import the first ' + multiPgnMax + ' of the ' + parts.length + ' games?')) return;
const step = function(ds) {
if (ds.length) {
send('addChapter', ds[0]);
setTimeout(function() {
step(ds.slice(1));
}, 600);
} else {}
};
const firstIt = vm.initial() ? 1 : (chapters().length + 1);
step(parts.slice(0, multiPgnMax).map(function(pgn, i) {
return {
initial: !i && vm.initial(),
mode: d.mode,
name: 'Chapter ' + (firstIt + i),
orientation: d.orientation,
pgn,
variant: d.variant,
sticky: root.study!.vm.mode.sticky
};
}));
return true;
}
}
};
function submitSingle(d) {
d.initial = vm.initial();
d.sticky = root.study!.vm.mode.sticky;
send("addChapter", d)
};
return {
vm,
open,
root,
openInitial() {
open();
vm.initial(true);
},
close,
toggle() {
if (vm.open) close();
else open();
},
submit(d) {
if (!submitMultiPgn(d)) submitSingle(d);
close();
setTab();
},
chapters,
startTour: () => chapterTour(tab => {
vm.tab(tab);
root.redraw();
}),
//.........这里部分代码省略.........
示例7: function
//.........这里部分代码省略.........
reorderChildren(vm.initialPath, true);
// try and play the solution next move
var next = vm.node.children[0];
if (next && next.puzzle === 'good') userJump(vm.path + next.id);
else {
var firstGoodPath = treeOps.takePathWhile(vm.mainline, function(node) {
return node.puzzle !== 'good';
});
if (firstGoodPath) userJump(firstGoodPath + tree.nodeAtPath(firstGoodPath).children[0].id);
}
vm.autoScrollRequested = true;
redraw();
startCeval();
};
const socket = socketBuild({
send: opts.socketSend,
addNode: addNode,
addDests: addDests,
reset: function() {
withGround(showGround);
redraw();
}
});
function recentHash(): string {
return data.puzzle.id + (data.user ? data.user.recent.reduce(function(h, r) {
return h + r[0];
}, '') : '');
}
const nbToVoteCall = storedProp('puzzle.vote-call', 3);
let thanksUntil: number | undefined;
const callToVote = () => parseInt(nbToVoteCall()) < 1;
const vote = throttle(1000, false, function(v) {
if (callToVote()) thanksUntil = Date.now() + 2000;
nbToVoteCall(5);
vm.voted = v;
xhr.vote(data.puzzle.id, v).then(function(res) {
data.puzzle.vote = res[1];
redraw();
});
});
initiate(opts.data);
const promotion = makePromotion(vm, ground, redraw);
keyboard({
vm,
userJump,
getCeval,
toggleCeval,
toggleThreatMode,
redraw,
playBestMove() {
var uci = nextNodeBest() || (vm.node.ceval && vm.node.ceval.pvs[0].moves[0]);
if (uci) playUci(uci);
}
});
// If the page loads while being hidden (like when changing settings),
示例8: function
export default function(opts: CevalOpts): CevalCtrl {
const storageKey = function(k: string): string {
return opts.storageKeyPrefix ? opts.storageKeyPrefix + '.' + k : k;
};
const pnaclSupported: boolean = !opts.failsafe && 'application/x-pnacl' in navigator.mimeTypes;
const wasmSupported = !opts.failsafe && typeof WebAssembly === 'object' && WebAssembly.validate(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
const minDepth = 6;
const maxDepth = storedProp<number>(storageKey('ceval.max-depth'), 18);
const multiPv = storedProp(storageKey('ceval.multipv'), opts.multiPvDefault || 1);
const threads = storedProp(storageKey('ceval.threads'), Math.ceil((navigator.hardwareConcurrency || 1) / 2));
const hashSize = storedProp(storageKey('ceval.hash-size'), 128);
const infinite = storedProp('ceval.infinite', false);
let curEval: Tree.ClientEval | null = null;
const enableStorage = li.storage.make(storageKey('client-eval-enabled'));
const allowed = prop(true);
const enabled = prop(opts.possible && allowed() && enableStorage.get() == '1' && !document.hidden);
let started: Started | false = false;
let lastStarted: Started | false = false; // last started object (for going deeper even if stopped)
const hovering = prop<Hovering | null>(null);
const isDeeper = prop(false);
const sfPath = '/assets/vendor/stockfish/stockfish';
const pool = new Pool({
asmjs: li.assetUrl(sfPath + '.js', {sameDomain: true}),
pnacl: pnaclSupported && li.assetUrl(sfPath + '.nmf'),
wasm: wasmSupported && li.assetUrl(sfPath + '.wasm.js', {sameDomain: true}),
onCrash: opts.onCrash
}, {
minDepth,
variant: opts.variant.key,
threads: pnaclSupported && threads,
hashSize: pnaclSupported && hashSize
});
// adjusts maxDepth based on nodes per second
const npsRecorder = (function() {
const values: number[] = [];
const applies = function(ev: Tree.ClientEval) {
return ev.knps && ev.depth >= 16 &&
typeof ev.cp !== 'undefined' && Math.abs(ev.cp) < 500 &&
(ev.fen.split(/\s/)[0].split(/[nbrqkp]/i).length - 1) >= 10;
}
return function(ev: Tree.ClientEval) {
if (!applies(ev)) return;
values.push(ev.knps);
if (values.length >= 5) {
var depth = 18,
knps = median(values) || 0;
if (knps > 100) depth = 19;
if (knps > 150) depth = 20;
if (knps > 250) depth = 21;
if (knps > 500) depth = 22;
if (knps > 1000) depth = 23;
if (knps > 2000) depth = 24;
if (knps > 3500) depth = 25;
if (knps > 5000) depth = 26;
if (knps > 7000) depth = 27;
maxDepth(depth);
if (values.length > 20) values.shift();
}
};
})();
let lastEmitFen: string | null = null;
const onEmit = throttle(500, (ev: Tree.ClientEval, work: Work) => {
sortPvsInPlace(ev.pvs, (work.ply % 2 === (work.threatMode ? 1 : 0)) ? 'white' : 'black');
npsRecorder(ev);
curEval = ev;
opts.emit(ev, work);
if (ev.fen !== lastEmitFen) {
lastEmitFen = ev.fen;
li.storage.set('ceval.fen', ev.fen);
}
});
const effectiveMaxDepth = function(): number {
return (isDeeper() || infinite()) ? 99 : parseInt(maxDepth());
};
const sortPvsInPlace = function(pvs: Tree.PvData[], color: Color) {
pvs.sort(function(a, b) {
return povChances(color, b) - povChances(color, a);
});
};
const start = function(path: Tree.Path, steps: Step[], threatMode: boolean, deeper: boolean) {
if (!enabled() || !opts.possible) return;
isDeeper(deeper);
const maxD = effectiveMaxDepth();
const step = steps[steps.length - 1];
const existing = threatMode ? step.threat : step.ceval;
if (existing && existing.depth >= maxD) return;
//.........这里部分代码省略.........