Module:Random world names

local replacementArgs = require('Module:GameText').replacementArgs local gameText = require('Module:GameText').getText

-- credit: http://richard.warburton.it -- this version is with trim. local function explode(div,str) if (div=='') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function return string.find(str,div,pos,true) end do		table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr, string.sub(str,pos)) -- Attach chars right of last divider return arr end

local db = {}

local function getList(key, lang) local lang = lang or 'en' if not db[lang] then if lang == 'de' or lang == 'es' or lang == 'fr' or lang == 'it' or lang == 'pl' or lang == 'pt' or lang == 'ru' or lang =='zh' then db[lang] = mw.loadData('Module:GameText/db-' .. lang) else db[lang] = mw.loadData('Module:GameText/db-en') end end

if key then local valuelist = {} for _, v in pairs(db[lang][key]) do			table.insert(valuelist, v)		end return valuelist end end

return {

go = function(frame)

local lang = frame.args['lang']

-- load lists of all available strings local adjectivelist = getList('RandomWorldName_Adjective', lang) local locationlist = getList('RandomWorldName_Location', lang) local nounlist = getList('RandomWorldName_Noun', lang)

-- default strings for the generic composition displayed in the left cell local arg_adj = frame.args['adjective'] local arg_loc = frame.args['location'] local arg_nou = frame.args['noun']

-- number of random world names to generate for each composition, default to 3. local worldcount = frame.args['count'] or 3

-- init the RNG: seed is based on current date and time and changes every second local seed = os.time; math.randomseed(seed)

-- invisible info string local output = ' Random world names seed: ' .. seed .. ' (' .. os.date .. ' UTC) '

for _, composition in pairs(explode(',', frame.args['compositions'])) do		-- print a table row for each composition output = output .. ' '

-- left cell: generic composition

-- use the default strings for adjective, location, and noun as defined above, -- but overwrite where there is a definition for this specific composition -- (e.g. "|3:adjective=...") for the adjective of composition 3 local x_Adj = frame.args[composition .. ':adjective'] or arg_adj local x_Loc = frame.args[composition .. ':location'] or arg_loc local x_Nou = frame.args[composition .. ':noun'] or arg_nou local replaceArgs = replacementArgs({			x_Adjective = &lt;' .. x_Adj .. '&gt;,			x_Location = &lt;' .. x_Loc .. '&gt;,			x_Noun = &lt;' .. x_Nou .. '&gt;		})

output = output .. gameText('RandomWorldName_Composition.' .. composition, lang, replaceArgs) output = output .. ' '

-- right cell: list of some random examples for the composition

for _ = 1, worldcount do -- generate a world name the defined number of times local worldname = ''

-- keep generating random world names until one is found that is at max 27 chars in length -- (this is the same behavior as in-game) repeat -- select a random adjective, location, and noun local arg_adj = adjectivelist[math.random(1, #adjectivelist)] local arg_loc = locationlist[math.random(1, #locationlist)] local arg_nou = nounlist[math.random(1, #nounlist)]

if arg_adj and arg_loc and arg_nou then -- make world name local replaceArgs = replacementArgs({x_Adjective = arg_adj, x_Location = arg_loc, x_Noun = arg_nou}) worldname = gameText('RandomWorldName_Composition.' .. composition, lang, replaceArgs) end until worldname ~= '' and #worldname <= 27

output = output .. '' .. worldname .. '' end

output = output .. ' ' end

return output

end

}