Bước tới nội dung

Mô đun:ListHayDay

Tủ sách mở Wikibooks

Có thể viết tài liệu về mô đun này tại Mô đun:ListHayDay/tài liệu.

p={};

--This is the main table buiilder for the ingredient lists
function p.usage(frame)
    frame = frame:getParent();
    pairs = pairs(frame.args);
    
    items = {};
    
    for i,j in pairs do
        items[i]=j;
    end
    
    --the first parameter is the product name, then each product takes 3 parameters
    paramCount= #items;
    itemCount = (paramCount-1)/3;
    
    local percent;
    
    columnCount = items["columns"];

    if columnCount==nil then
    
        if itemCount<=4 then
            columnCount= itemCount;
        else
            columnCount=3;
        end
    end
    
    rowCount = math.ceil(itemCount/columnCount);
    --we need to trim off the newline character
    ingredient= mw.text.trim(items[1]);
    
    
    --start building the table
    retVal = "{| border=\"0\" cellpadding=\"4\" cellspacing=\"2\" style=\"text-align: center; margin: .5em auto; border-collapse: collapse; border: 1px solid #ccc\"\n";
    
    if columnCount==2 then
        percent=50;
    elseif columnCount==4 then
        percent=25;
    else
        percent=33;
    end
    for row=1,rowCount do
        
        retVal = retVal .. "|- style=\"background: #ddd;\"\n";
        
        for col =1,columnCount,1 do
            retVal = retVal .. "|";
            --First row we need the width information
            if row==1 then
                retVal = retVal .. "style=\"width: "..percent.."%\".|\n";
            end
            index = (row-1)*columnCount+col;
            if index<=itemCount then
                name=items[3*index-1];
                imageName=overRideImageName[name];
                if imageName == nil then
                    imageName =name..".png";
                end
                retVal = retVal .. "[[File:"..imageName.."|75px|link="..name.."|"..toolTipName(name).."]]";
            end
            retVal = retVal .. "\n";
        end
    
        retVal = retVal .. "|- style=\"background: #ddd;\"\n";
    
        for col = 1,columnCount do
            index = (row-1)*columnCount+col;
            retVal = retVal.. "|";
            if index<=itemCount then
                retVal = retVal .. "'''[["..items[3*index-1].."|"..autowrap(items[3*index-1],20).."]]'''";
            end
            retVal = retVal .. "\n";
        end
        retVal = retVal .. "|- style=\"border: 1px solid #ccc;\"\n";
    
        for col=1,columnCount do
            index = (row-1)*columnCount+col;
            retVal = retVal .. "|";
            if index<=itemCount then
                retVal = retVal .. "[[File:".. ingredient..".png|50px]]'''x"..items[index*3].."'''"
            end
            retVal = retVal .. "\n";
        end
    
        retVal = retVal .. "|- style=\"border: 1px solid #ccc;\"\n";

        for col=1,columnCount do
            index = (row-1)*columnCount+col;
            retVal = retVal .. "|";
            if index<=itemCount then
                retVal = retVal .. "[[Experience|Level]] "..items[3*index+1];
            end
            retVal = retVal .. "\n";
        end
    end
    
    retVal=retVal .. "|}";
    
    return retVal;
end

function p.building(frame)
    frame = frame:getParent();
    pairs = pairs(frame.args);
    
    items = {};
    
    for i,j in pairs do
        items[i]=j;
    end
    
    --Each product takes 4 parameters
    paramCount= #items;
    itemCount = paramCount/4;
    
    local percent;
    
    columnCount = items["columns"];
    
    if columnCount==nil then
    
        if itemCount<=4 then
            columnCount= itemCount;
        else
            columnCount=3;
        end
    end
    
    rowCount = math.ceil(itemCount/columnCount);
    
    retVal = "{| border=\"0\" cellpadding=\"5\" cellspacing=\"1\" style=\"text-align: center; margin: 1em auto; border-collapse: collapse; border: 1px solid #bbb;\"\n";

    if columnCount==2 then
        percent=50;
    elseif columnCount==4 then
        percent=25;
    else
        percent=33;
    end
    for row=1,rowCount do
        
        retVal = retVal .. "|- style=\"background: #ddd;\"\n";
        
        for col =1,columnCount,1 do
            retVal = retVal .. "|";
            --First row we need the width information
            if row==1 then
                retVal = retVal .. "style=\"width: "..percent.."%\".|\n";
            end
            index = (row-1)*columnCount+col;
            if index<=itemCount then
                name=items[4*index-3];
                imageName=overRideImageName[name];
                if imageName == nil then
                    imageName =name..".png";
                end
                retVal = retVal .. "[[File:"..imageName.."|75px|link="..name.."|"..toolTipName(name).."]]";
            end
            retVal = retVal .. "\n";
        end
    
        retVal = retVal .. "|- style=\"background: #ddd;\"\n";
    
        for col = 1,columnCount do
            index = (row-1)*columnCount+col;
            retVal = retVal.. "|";
            if index<=itemCount then
                retVal = retVal .. "'''[["..items[4*index-3].."]]'''";
            end
            retVal = retVal .. "\n";
        end

        retVal = retVal .. "|- style=\"border: 1px solid #ccc;\"\n";
    
        for col=1,columnCount do
            index = (row-1)*columnCount+col;
            retVal = retVal .. "|";
            if index<=itemCount then
                retVal = retVal .. "[[Experience|Level]] "..items[4*index-2];
            end
            retVal = retVal .. "\n";
        end
        
        retVal = retVal .. "|- style=\"border: 1px solid #ccc;\"\n";
    
        for col=1,columnCount do
            index = (row-1)*columnCount+col;
            retVal = retVal .. "|";
            if index<=itemCount then
                timeStr = items[4*index-1];
                local diamonds;
                slash = string.find(timeStr,"/");
                if (slash ~= nil) then
                    diamonds = tonumber(string.sub(timeStr,slash+1,-1));
                    timeStr = string.sub(timeStr,1,slash-1);
                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
                retVal = retVal .. displayTime(time);
                retVal = retVal .. "<br />★★★ "
                    ..displayTime(math.floor(time*17/20));
                if diamonds==nil then
                    diamonds = 1;
                    while (diamonds<#diamondCost and diamondCost[diamonds+1][1]<=time) do
                        diamonds = diamonds+1;
                    end
                end
                retVal = retVal .. "<br />"..tostring(diamonds)..
                    frame:expandTemplate({title="Infoicon",args={"dia"}});
            end
            retVal = retVal .. "\n";
        end

        
    end

    
    retVal = retVal .. "|}";
    return retVal;
end

--Special cases for image file name
overRideImageName=
{
    ["Caffè Latte"]="Caffe Latte.png",
    ["Caffè Mocha"]="Caffe Mocha.png",
    ["Shepherd's Pie"]="Shepherds Pie.png"
};

-- 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
};


-- Special cases for tooltip name
overRideToolTipName=
{
    ["BLT Salad"] = "BLT salad"
};

function toolTipName(name)
    if overRideToolTipName[name] then
        return overRideToolTipName[name];
    else
        return string.upper(string.sub(name,1,1)) .. 
            string.lower(string.sub(name,2));
    end
end

function autowrap(str,maxLen)
    if (#str<=maxLen) then
        return str;
    end
    
    wrapIndex = string.find(str," ",#str/2);
    
    if (wrapIndex~=nil) then
        str = string.sub(str,1,wrapIndex-1).."<br/>"..string.sub(str,wrapIndex+1);
    end
    
    return str;
end
    

function displayTime(time)
    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

function getDetail(frame,product,detail)
    return frame:expandTemplate{title="Item Data/"..mw.text.trim(product),args={detail=detail}};
end

function p.allProducts(frame)
    args=require("Module:Full Item List");
    data=require("Module:Item Data");
    
    products={};
    
    for i,j in pairs(args) do
        if(data.itemType(j.src)=="Product") then
            table.insert(products,i);
        end
    end
    return #products;
end

return p;