RuneScape Wiki
Advertisement

Documentation for this module may be created at Module:Sandbox/Mol/doc

local p = {}

local scrabble_vals = {
	A = 1, B = 3, C = 3, D = 2, E = 1, F = 4, G = 2,
	H = 4, I = 1, J = 8, K = 5, L = 1, M = 3, N = 1,
	O = 1, P = 3, Q = 10, R = 1, S = 1, T = 1, U = 1,
	V = 4, W = 4, X = 8, Y = 4, Z = 10
}
function p.scrabble(frame)
	local word = frame.args[1]
	local form = string.lower(frame.args.format or 'all')
	word = string.upper(word)
	local words = mw.text.split(word,'%A')
	local values = {}
	local total = 0
	for _, v in ipairs (words) do
		if v:find('%a') then
			local _v = mw.text.split(v,'')
			local sum = 0
			for _, x in ipairs(_v) do
				sum = sum + scrabble_vals[x] or 0
			end
			total = total + sum
			table.insert(values,{ word = v, score = sum})
		end
	end

	if form == 'all' then
		local ret = mw.html.create('table')
					:addClass('wikitable'):addClass('sortable'):addClass('align-right-2')
					:tag('tr')
						:tag('th'):wikitext('Word'):done()
						:tag('th'):wikitext('Score'):done()
					:done()
		for _, v in ipairs(values) do
			ret	:tag('tr')
					:tag('td'):wikitext(v.word):done()
					:tag('td'):wikitext(v.score):done()
				:done()
		end

		ret	:tag('tr'):css('text-align','right')
				:tag('th'):css('text-align','right'):wikitext('Total'):done()
				:tag('td'):wikitext(total):done()
			:done()
		return ret
	elseif form == 'total' then
		return total
	end
end

local nums = '0123456789ABCDEF'
function dec_to_hex(dec)
	local d1 = math.floor(dec/16) + 1
	local d2 = math.fmod(dec,16) + 1
	return string.sub(nums,d1,d1)..string.sub(nums,d2,d2)
end

function p.mojibake(frame)
	local txt = frame.args[1]
	local _txt = mw.text.split(txt,'')
	txt = {}

	for i, v in ipairs(_txt) do
		if v:match('[!"#$%%&\'%(%)%*%+,%-./%d%a:;<=>?@%[%]\\^_`%{%}|~ ]') then
			local _v = string.byte(v)
			_v = _v - 32
			table.insert(txt,mw.text.decode(string.format('&#xFF%s;',dec_to_hex(_v))))
		else
			table.insert(txt,v)
		end
	end 

	_txt = mw.text.split(table.concat(txt),' ')
	txt = {}
	for _, v in ipairs(_txt) do
		table.insert(txt,'{{{{{|safesubst:}}}urlencode:'..v..'}}')
	end

	txt = table.concat(txt,' ')
	txt = frame:preprocess(txt)
	txt = string.gsub(txt,'%%(..)','&#x%1;')

	return txt
end

return p
Advertisement