Module:Item Data

p={}; --

function p.table(frame) --prepare to generate table frame=frame:getParent; args=frame.args; coin=frame:expandTemplate({title="infoicon",args={"coin"}}); --gentrate table: retVal="{|border=\"1\"\n";--table start line --get image image=args.image; if image==nil then image=args.name..".png"; end retVal=retVal.."|colspan=\"2\" align=\"center\"|\n"; retVal=retVal.."|-\n"; --name of product name=args.name; retVal=retVal.."|Name||"..name.."\n"; plural=pluralForm(args); if not (plural=="yes" or plural=="no") then retVal = retVal.."|-\n"; retVal = retVal .. "|Plural||"..plural.."\n"; end retVal=retVal.."|-\n"; --source of product local src=args.source; retVal=retVal.."|Source||"..src.."\n"; retVal=retVal.."|-\n"; --type of product: Crop, Product, Animal good local type; type=itemType(src); retVal=retVal.."|Type||"..onlyFirstCap(type).."\n"; retVal=retVal.."|-\n"; --Minimum level for owning product if (args.rlevel~=nil) then local level=args.level; if (level==nil) then level="34"; end retVal=retVal.."|Level||"..level.." (Rep. "..args.rlevel..")\n"; else retVal=retVal.."|Level||"..args.level.."\n"; end retVal=retVal.."|-\n"; --Price of product if args.price=="?" then retVal=retVal.."|Price||?\n|-\n"; elseif (src~="Lure Workbench" and src~="Net Maker") then--the products of these buildings can't be sold retVal=retVal.."|Default Price||"..args.price..coin.."\n"; retVal=retVal.."|-\n"; retVal=retVal.."|Maximum Price||"..math.floor(args.price*3.6)..coin.."\n"; retVal=retVal.."|-\n"; end --Time for manufacture local timeStr=args.time; local time=extractTime(timeStr); retVal=retVal.."|Manufacture Time||"..displayTime(time).."\n"; --For products, we need the time for stared machines. Note that the star character may cause the cursor to be in the wrong place in your editor for the rest of the line. retVal=retVal.."|-\n"; if type=="Product" and src~="Mine" then --The feed mill and the fishing area machines use the first 2 stars for time discounts, since their products don't get ordered by trucks. if (src=="Lure Workbench" or src=="Net Maker" or src=="Feed Mill") then retVal=retVal.."|★||"..displayTime(math.floor(time*19/20)).."\n"; retVal=retVal.."|-\n"; retVal=retVal.."|★★||"..displayTime(math.floor(time*9/10)).."\n"; retVal=retVal.."|-\n"; end retVal=retVal.."|★★★||"..displayTime(math.floor(time*17/20)).."\n"; retVal=retVal.."|-\n"; --handle the ingredients. These are done using "ingredient" and "amount" parameters. retVal=retVal.."|Ingredients ||"; local count=1; while (args["ingredient"..count]~=nil) do           retVal = retVal..args["amount"..count].."X ";            ingredientName=args["ingredient"..count];            if mw.title.new("Template:Item Data/"..ingredientName).exists then                retVal=retVal..frame:expandTemplate{title="Template:Item Data/"..ingredientName,args={detail="Name"}}.." (data)"; else retVal=retVal.. ingredientName.."]]"; if ingredientName~="Diamond" and not endsWith(ingredientName,"Voucher") then retVal=retVal.."(create data)"; end end retVal=retVal.." "; count=count+1; end if count>1 then -- that is, we have at least one ingredient. Remove the newline code from the end of the string. retVal=string.sub(retVal,1,-6).."\n"; else retVal=retVal.."None\n"; end retVal=retVal.."|-\n"; end if src~="Mine" then --the amount of diamonds to create item instinaniously local diamonds=args.diamonds; if diamonds==nil then diamonds = 1; while (diamonds<#diamondCost and diamondCost[diamonds+1][1]<=time) do               diamonds = diamonds+1; end elseif diamonds=="?" then -- The string is broken up to prevent this line from applying to the module itself. retVal=retVal..""; end retVal=retVal.."|Diamonds||"..tostring(diamonds).."\n"; retVal=retVal.."|-\n"; end --XP for making product retVal=retVal.."|XP||"..args.xp.."\n"; --close table retVal=retVal.."|}"; --add category. The string is broken up to prevent this line from applying to the module itself. if mw.title.getCurrentTitle.subpageText == args.name then retVal=retVal..""; end return retVal; end

function pluralForm(args) plural=args.plural; if plural==nil then name=args.name; if (string.sub(name,-1,-1)=="y") then plural=string.sub(name,1,-2).."ies"; elseif (string.sub(name,-1,-1) == "f") then plural=string.sub(name,1,-2).."ves"; else plural=name.."s"; end elseif string.sub(plural,1,1)=="+" then plural=args.name..string.sub(plural,2,-1); end return plural; end

function p.infobox(frame) --prepare to generate infobox params frame=frame:getParent; args=frame.args; infoboxParams={}; src=args.source; --parameter title: type of product type=itemType(src); infoboxParams.title=""..onlyFirstCap(type)..""; --parameter level: The lowest level you may have this product. infoboxParams.level=args.level; infoboxParams.rlevel=args.rlevel; if (src~="Lure Workbench" and src~="Net Maker") then--the products of these buildings can't be sold --parameter price: the price for selling the product in the Roadside Shop: price=args.price; maxPrice=math.floor(price*3.6); infoboxParams.price=price.." to "..maxPrice; --paramter base: the default prioce in the Roadside Shop infoboxParams.base=price; end if (src=="Smelter") then infoboxParams.upgrades=frame:expandTemplate(           {                title="infoicon",                args={"ptrain","big",link="Personal Train"}            }); end --time to manufacture local timeStr=args.time; local time=extractTime(timeStr); infoboxParams.time=displayTime(time); if type=="Product" then infoboxParams.time3=displayTime(math.floor(time*0.85)); end --diamonds for instantanious production: local diamonds=args.diamonds; if diamonds==nil then diamonds = 1; while (diamonds<#diamondCost and diamondCost[diamonds+1][1]<=time) do           diamonds = diamonds+1; end end infoboxParams.dia=diamonds; --XP for production infoboxParams.xp=args.xp; local srcIconName=sourceIcons[src]; if srcIconName==nill then srcIconName=defaultSrcName(src); end infoboxParams.source=frame:expandTemplate(       {            title="Infoicon",            args={srcIconName,"bigger",link=args.source}        }); infoboxParams.auto="y"; return frame:expandTemplate({title="Infobox",args=infoboxParams}); end

function defaultSrcName(src) parts=mw.text.split(src," "); retVal=parts[1]; for i=2,#parts do       retVal=retVal..string.sub(parts[i],1,1); end return retVal; end

function p.getType(frame) return itemType(frame:getParent.args.source); end

function p.getIngredients(frame) frame=frame:getParent; args=frame.args; local retVal=""

local count=1; while (args["ingredient"..count]~=nil) do       retVal = retVal..args["amount"..count].." "..args["ingredient"..count]..",";

count=count+1; end if (count==1) then return ""; else return retVal; end end

function p.getUsage(frame) frame=frame:getParent; args=frame.args; local name=mw.text.trim(args[1]); listParams={name}; local count=2; while (args[count]~=nill) do       local itemData="Item Data/"..mw.text.trim(args[count]); table.insert(listParams,frame:expandTemplate{title=itemData,args={detail="Name"}}); ingredients=(frame:expandTemplate{title=itemData,args={detail="Ingredients"}}); index=string.find(ingredients,name); local numLen; if (index==3 or string.sub(ingredients,index-3,index-3)==",") then numLen=1; else numLen=2; end table.insert(listParams,string.sub(ingredients,index-1-numLen,index-1-1)); table.insert(listParams,frame:expandTemplate{title=itemData,args={detail="Level"}}); count=count+1; end return frame:expandTemplate({title="Item Usage",args=listParams}); end

function extractTime(timeStr) if (timeStr=="Instant") then return timeStr; end local time; if (string.sub(timeStr,-1,-1)=="h") then time = tonumber(string.sub(timeStr,1,-2))*60; elseif (string.sub(timeStr,-1,-1)=="m") then time = tonumber(string.sub(timeStr,1,-2)); else timeSeparator = string.find(timeStr,":"); time=tonumber(string.sub(timeStr,1,timeSeparator-1))*60+ tonumber(string.sub(timeStr,timeSeparator+1,-1)); end return time; end

function displayTime(time) if (time=="Instant") then return time; end hours = math.floor(time/60); minutes = time%60; retVal = ""; if (hours>0) then retVal = retVal .. tostring(hours).." h "; end if (minutes>0) then retVal = retVal .. tostring(minutes).." min"; end return mw.text.trim(retVal); end

animalSources= {   "Chicken", "Cow", "Pig", "Sheep", "Fish", "Goat", "Honey Bee", "Lobster", "Duck" }

sourceIcons= {   ["Blackberry Bush"]="blackb", ["Raspberry Bush"]="raspb" };

function itemType(src) if src=="Field" or endsWith(src,"Tree") or endsWith(src,"Bush") then return "Crop"; elseif contains(animalSources,src) then return "Animal Good"; else return "Product"; end end

function contains(sequence,item) for i=1,#sequence do       if sequence[i]==item then return true; end end return false; end

-- costs for making items instantaneously based on full time diamondCost= {   {5,5},--1    {10,20},--2    {25,50},--3    {60,80},--4    {90,135},--5    {150,240},--6    {300,360},--7    {390,450},--8    {480,510},--9    {600,600},--10 (none actually observed, need a placeholder) {720,720},--11   {960,960},--12    {1200,1200},--13    {1440,1440}--14 };

function p.diamondTable(frame) local retVal="{| Border=\"1\"\n"; retVal=retVal.."|Diamonds||Minimum time||Maximum time\n" diamonds = 1; while (diamonds<=#diamondCost) do       retVal=retVal.."|-\n"; retVal=retVal.."|"..diamonds.."||" ..displayTime(diamondCost[diamonds][1]).."||" ..displayTime(diamondCost[diamonds][2]).."\n"; diamonds = diamonds+1; end retVal=retVal.."|}\n"; return retVal; end

function endsWith(fullStr,tailStr) return string.len(fullStr)>=string.len(tailStr) and string.sub(fullStr,-string.len(tailStr))==tailStr; end

function onlyFirstCap(name) return string.upper(string.sub(name,1,1)) .. string.lower(string.sub(name,2)); end

function p.TemplateList(frame) frame=frame:getParent; args=frame.args; retVal=""; i=1; while(args[i]~=nil) do       retVal=retVal.."#Template:Item Data/"..mw.text.trim(args[i]).."\n"; i=i+1; end return retVal; end

return p;