מדיה ויקי:Gadget-pgnviewer.js: הבדלים בין גרסאות בדף
default start for first position when there's fen |
support chess960 |
||
| שורה 33: | שורה 33: | ||
// some global, utility functions. | // some global, utility functions. | ||
function bindex(file, row) { return 8 * file + row; } | |||
function file(ind) { return Math.floor(ind / 8);} | function file(ind) { return Math.floor(ind / 8);} | ||
function row(ind) { return ind % 8; } | function row(ind) { return ind % 8; } | ||
| שורה 177: | שורה 177: | ||
clearTimeout( this.timer ); | clearTimeout( this.timer ); | ||
if ( this.autoPlayButton.state ) { | if ( this.autoPlayButton.state ) { | ||
if ( this.currentGame.done() ) | |||
this.toggleAutoPlay(); | |||
else { | |||
var gs = this; | |||
this.timer = setTimeout( function() { gs.autoPlayCallback(); }, this.autoPlayRate ); | |||
} | |||
} | } | ||
}, | }, | ||
| שורה 236: | שורה 240: | ||
ChessPiece.prototype.move = function(file, row) { | ChessPiece.prototype.move = function(file, row) { | ||
if ( this.game.pieceAt( this.file, this.row ) == this ) // with chess960 castling, we sometimes have to test. | |||
this.game.clearSquare(this.file, this.row); | |||
this.game.pieceAt(file, row, this); // place it on the board) | this.game.pieceAt(file, row, this); // place it on the board) | ||
this.game.registerMove({what:'m', piece: this, file: file, row: row}); | this.game.registerMove({what:'m', piece: this, file: file, row: row}); | ||
| שורה 358: | שורה 363: | ||
} | } | ||
Game.prototype.done = function() { return this.moves.length - 1 <= this.index; } | |||
Game.prototype.pieceAt = function(file, row, piece) { | Game.prototype.pieceAt = function(file, row, piece) { | ||
| שורה 432: | שורה 437: | ||
this.advance(delta); | this.advance(delta); | ||
} | } | ||
} | } | ||
| שורה 474: | שורה 477: | ||
this.gotoBoard(0); | this.gotoBoard(0); | ||
} | } | ||
Game.prototype.castle = function( color, side, kingTargetFile, rookTargetFile ) { | |||
var king = this.piecesByTypeCol['k'][color][0] | var king = this.piecesByTypeCol['k'][color][0], | ||
rook = this.piecesByTypeCol['r'][color][side]; | |||
if (!rook || rook.type != 'r') | if (!rook || rook.type != 'r') | ||
throw 'attempt to castle without rook on appropriate square'; | throw 'attempt to castle without rook on appropriate square'; | ||
king.move(fileOfStr( | king.move(fileOfStr(kingTargetFile), king.row); | ||
rook.move(fileOfStr( | rook.move(fileOfStr(rookTargetFile), rook.row); | ||
} | |||
Game.prototype.kingSideCastle = function(color) { | |||
this.castle( color, 1, 'g', 'f' ); | |||
} | } | ||
Game.prototype.queenSideCastle = function(color) { | Game.prototype.queenSideCastle = function(color) { | ||
this.castle( color, 0, 'c', 'd' ); | |||
} | } | ||
| שורה 594: | שורה 597: | ||
Game.prototype.preAnalyzePgn = function(pgn) { | Game.prototype.preAnalyzePgn = function(pgn) { | ||
function tryMatch(regex) { | function tryMatch(regex) { | ||
var | var match = pgn.match(regex); | ||
if (match) | |||
pgn = pgn.replace( match, '' ); | |||
return match && match[0]; | |||
return | |||
} | } | ||
var match; | |||
while (match = tryMatch(/^\s*\[[^\]]*\]/)) | |||
this.addDescription(match); | this.addDescription(match); | ||
this.pgn = pgn; | this.pgn = pgn; | ||
} | } | ||
| שורה 645: | שורה 634: | ||
return rmatch && rmatch[0]; | return rmatch && rmatch[0]; | ||
} | } | ||
pgn = pgn.replace(/;(.*)\n/g, ' {$1} ').replace(/\s+/g, ' '); // replace to-end-of-line comments with block comments, remove newlines and noramlize spaces to 1 | pgn = pgn.replace(/;(.*)\n/g, ' {$1} ').replace(/\s+/g, ' '); // replace to-end-of-line comments with block comments, remove newlines and noramlize spaces to 1 | ||
this.populateBoard(this.descriptions.FEN || 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR'); | this.populateBoard( this.descriptions.FEN || 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR' ); | ||
var prevLen = -1; | var prevLen = -1; | ||
this.addMoveLink(); | this.addMoveLink(); | ||
| שורה 668: | שורה 657: | ||
} | } | ||
} | } | ||
this.index = indexOfMove[this.descriptions.FirstMove || 'nofirstnmove'] || | |||
( this.descriptions.FEN ? 0 : this.moves.length - 1 ); | |||
(this.descriptions.FEN | |||
} | } | ||
| שורה 680: | שורה 664: | ||
var fenar = fen.split(/[\/\s]/); | var fenar = fen.split(/[\/\s]/); | ||
if (fenar.length < 8) | if (fenar.length < 8) | ||
throw 'illegal fen: "' + fen + '"'; | |||
for (var row = 0; row < 8; row++) { | for (var row = 0; row < 8; row++) { | ||
var file = 0; | var file = 0; | ||
| שורה 864: | שורה 848: | ||
function populateImages() { | function populateImages() { | ||
var | var | ||
flist = []; | flist = []; | ||
| שורה 880: | שורה 862: | ||
}; | }; | ||
$( [WHITE, BLACK] ).each( function( _, c ) { | |||
$( ['p', 'r', 'n', 'b', 'q', 'k'] ).each( function( _, t ) { | |||
images[ | var tc = t + c; | ||
images[tc] = { fn: 'Chess ' + tc + 't45.svg' }; | |||
} ); | |||
} ); | |||
for ( var i in images ) flist.push( 'File:' + images[i].fn ); | for ( var i in images ) flist.push( 'File:' + images[i].fn ); | ||
var params = { titles: flist.join( '|' ), prop: 'imageinfo', iiprop: 'url' }; | var params = { titles: flist.join( '|' ), prop: 'imageinfo', iiprop: 'url' }; | ||
if ( brainDamage ) | if ( brainDamage ) | ||
params.iiurlwidth = 48; | params.iiurlwidth = 48; | ||
new mw.Api().get(params) | new mw.Api().get(params) | ||
.done( function( data ) { | .done( function( data ) { | ||
if (data && data.query) | if (data && data.query) | ||
$.each(data.query.pages, function( | $.each(data.query.pages, function(_, page) { | ||
var | var | ||
ia = page.imageinfo[0], | ia = page.imageinfo[0], | ||
title = page.title, | title = page.title, | ||
url = ( brainDamage && /\.svg$/.test( title ) ) ? ia.thumburl : ia.url; | url = ( brainDamage && /\.svg$/.test( title ) ) ? ia.thumburl : ia.url; | ||
for ( key in images ) | |||
if ( ~ title.indexOf( images[key].fn ) ) | |||
images[key].url = url; | |||
} ); | } ); | ||
doIt(); | doIt(); | ||