פתיחת התפריט הראשי

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:Chess games/תיעוד

local pgn = require('module:pgn')

local fileMap = {
	a = 'א',
	b = 'ב',
	c = 'ג',
	d = 'ד',
	e = 'ה',
	f = 'ו',
	g = 'ז',
	h = 'ח',
}

local pieceMap = {
	R = 'צ',
	B = 'ר',
	N = 'פ',
	Q = 'מה',
	K = 'מ',
	x = ':'
}

local iconMap = {
	R = '♖',
	B = '♗',
	N = '♘',
	Q = '♕',
	K = '♔',
}

local function moveToIndex(move)
	if not move:match('^%d+[dl]$') then return -1 end -- not a move
	local num, color = move:match('(%d+)([dl])')
	return num * 2 + (color == 'd' and 1 or 0)
end

local function mergeArgs(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do args[k] = v end
	for k, v in pairs(frame.args) do args[k] = v end
	return args
end

local function _moveNotationToHeb(move) 
	move = move:gsub('l', ' של הלבן')
	move = move:gsub('d', ' של השחור')
	return move
end

local function moveNotationToHeb(frame)
	return _moveNotationToHeb(mergeArgs(frame)[1])
end

local function _chessNotationToHeb(notation, icon)
	local replacement = function(c)
		return fileMap[c] or
			icon and iconMap[c] or
			pieceMap[c]
	end
	local toheb = mw.ustring.gsub(notation or '', '[a-hRNBQKx]', replacement)
	toheb = mw.ustring.gsub(toheb, '([1-8א-ח])([א-ח])', '%1-%2') -- note that due to rtl/ltr issues, this looks like shit. to edit, turn off "code", and use bidiOver display.
	return toheb
end

local function chessNotationToHeb(frame)
	local args = mergeArgs(frame)
	return _chessNotationToHeb(args[1], args['סמליל'])
end

local function indexToMove(index)
	return string.format('%d%s', math.floor( index / 2), index % 2 == 0 and 'l' or 'd')
end
	
local function display_game(frame)
	local args = mergeArgs(frame) 
	local game = args['pgn']
	if not game then error('must have "pgn" parameter') end
	local moves, metadata, notations = pgn.pgn2fen(game)
	local template = args['template']
	if not template then error('must have "template" parameter') end
	local tmTable = {}
	
	local hydrate = function(index, comment, arg) 
		assert(index <= #moves, 'pgn does not contain move ' .. arg)
		local temp = template:gsub('%$fen%$', moves[index])
		temp = temp:gsub('%$comment%$', comment)
		temp = temp:gsub('%$move%$', indexToMove(index))
		temp = temp:gsub('%$notation%$', notations[index - 1])
		table.insert(tmTable,  { index, temp } )
	end
	template = mw.text.unstripNoWiki( template )
	for arg, val in pairs(args) do -- collect boards of the form "12l = comment"
		local index = moveToIndex(arg)
		if index >= 0 then hydrate(index, val, arg) end
	end

	for arg, val in pairs(args) do -- collect boards of the form "| from1 = 7d | to1 = 8l | comments1 = { "comment", "comment" }
		if arg:match('^from%d+$') then
			local hunk = arg:match(('^from(%d+)$'))
			toMove = args['to' .. hunk]
			if not toMove then error (string.format('parameter %s exists, but no parameter to%s', arg, hunk)) end
			local fromIndex = moveToIndex(val)
			if fromIndex < 0 then error(string.format('malformed value for parameter %s', arg)) end
			local toIndex = moveToIndex(toMove)
			if toIndex < 0 then error(string.format('malformed value for parameter to%s', hunk)) end
			local comments = {}
			local commentsVal = args['comments' .. hunk] or ''
			for comment in commentsVal:gmatch('{([^}]*)}') do table.insert(comments, comment) end
			for index = fromIndex, toIndex do hydrate(index, table.remove(comments, 1) or '', toMove) end
		end
	end
	table.sort(tmTable, function(a, b) return a[1] < b[1] end)
	local res = ''
	for _, item in ipairs(tmTable) do res = res ..  item[2] end
	return frame:preprocess(res)
end

local function display_game_2(frame)
	local args = mergeArgs(frame) 
	local game = args['pgn']
	if not game then error('must have "pgn" parameter') end
	local moves, metadata, notations = pgn.pgn2fen(game)
	local squaresize = args.size or 26
	local tmTable = {}
	local cff = require('Module:Chess from fen')
	local fenToBoard = require('Module:Chess from fen').f_newstyle
--	mw.log(template)
--	mw.log(frame:preprocess(template))
--	template = frame:preprocess(template)
	local template = args.template or [[
<div style="width:DIVWIDTHpx" class="chess-game-boards">
{| class="chess-template" style="border:1px #b0b0b0 solid;font-size:85%;text-align:center;vertical-align:middle;padding:0 5px;margin:auto;background-color:white;font-family:monospace;" cellpadding="0" cellspacing="0"
|- 
| || style="width: BOARDWIDTHpx;line-height:90%;" | 
{|  style = "table-layout: fixed; width: 100%;display:table;" 
|-
| ח || ז || ו || ה || ד || ג || ב || א
|}
|- 
| style="height: BOARDWIDTHpx;line-height:90%;" | 
{| style="display:table;vertical-align:middle;height:100%;width:100%;text-aligh:left"
|-
| 8
|-
| 7
|-
| 6
|-
| 5
|-
| 4
|-
| 3
|-
| 2
|-
| 1
|}
| $dummy$
| style="height: 160px;line-height:90%;" | 
{| style="display:table;vertical-align:middle;height:100%;width:100%;text-aligh:right"
|-
| 8
|-
| 7
|-
| 6
|-
| 5
|-
| 4
|-
| 3
|-
| 2
|-
| 1
|}
|-
| || style="line-height:90%;" | 
{|  style = "table-layout: fixed; width: 100%;display:table;" 
|-
| ח || ז || ו || ה || ד || ג || ב || א
|}
|}<div class="thumbcaption" style="margin-left:4px;margin-right:4px;">$footer$</div>
</div>
</div>]]
    template = template:gsub('BOARDWIDTH', squaresize*8):gsub('DIVWIDTH', squaresize*8+40)

	local hydrate = function(index, comment, moveNotation) 
		assert(index <= #moves, 'pgn does not contain move ' .. moveNotation)
		local notation = notations[index - 1]
		local move = indexToMove(index)
		local footer = string.format('אחרי מסע %s, %s. %s', _moveNotationToHeb(move), _chessNotationToHeb(notations[index - 1]), comment)
		local board = fenToBoard( { ['args'] = { ['fen'] = moves[index], ['size'] = squaresize } } )
		local res = template:gsub('%$footer%$', footer):gsub('%$dummy%$', board)
		table.insert(tmTable,  { index,  res } )
	end
	
	for arg, val in pairs(args) do -- collect boards of the form "12l = comment"
		if arg == 'template' or arg == 'pgn' or arg == 'size' then -- continue end
		else 
			local index = moveToIndex(arg)
			if index >= 0 then hydrate(index, val, arg) end
		end
	end

	for arg, val in pairs(args) do -- collect boards of the form "| from1 = 7d | to1 = 8l | comments1 = { "comment", "comment" }
		if arg:match('^from%d+$') then
			local hunk = arg:match(('^from(%d+)$'))
			toMove = args['to' .. hunk]
			if not toMove then error (string.format('parameter %s exists, but no parameter to%s', arg, hunk)) end
			local fromIndex = moveToIndex(val)
			if fromIndex < 0 then error(string.format('malformed value for parameter %s', arg)) end
			local toIndex = moveToIndex(toMove)
			if toIndex < 0 then error(string.format('malformed value for parameter to%s', hunk)) end
			local comments = {}
			local commentsVal = args['comments' .. hunk] or ''
			for comment in commentsVal:gmatch('{([^}]*)}') do table.insert(comments, comment) end
			for index = fromIndex, toIndex do hydrate(index, table.remove(comments, 1) or '', toMove) end
		end
	end
	table.sort(tmTable, function(a, b) return a[1] < b[1] end)
	local res = ''
	for _, item in ipairs(tmTable) do res = res ..  item[2] end
	return res
end


return {
	['display game 2'] = display_game_2,
	['display game'] = display_game,
	['עברות מסע'] =  moveNotationToHeb,
	['עברות רישום'] = chessNotationToHeb,
}