Lua:着色"胜利"状态失败

时间:2014-10-25 16:46:21

标签: if-statement lua

我已经有近两年的这个功能了,我似乎无法弄清楚为什么它不适用于着色。这是整个功能,但你会看到下面没有工作的核心部分。

function showscoreboard()
    local function len(arg)
            return string.len(arg)
    end

    local function tbuff(arg)
            if len(arg) < 3 then
                    return arg.." "
            else
                    return arg
            end
    end

    local function sbuff(arg)
            if len(arg) < 2 then
                    return " "..arg
            else
                    return arg
            end
    end

    local function cteam(t,s)
                    local status = s or nil
                    local forecolor = ""

                    if status == "p" then
                            forecolor = "yellow"
                    elseif status == "w" then
                            forecolor = "cyan"
                    else
                            forecolor = "limegreen"
                    end

                    return "<color fore="..forecolor..">"..t.."</color>"
            end

    local function bcolor(i)
            local i = i or 0

            if i%2 == 1 then
                    return "maroon"
            else
                    return "navy"
            end
    end

    local scorestring = ""
    local allteams = {["ATL"]=0,["WAS"]=0,["MIA"]=0,["CLE"]=0,["OAK"]=0,["SD"]=0,["IND"]=0,["NYJ"]=0,["TEN"]=0,["SEA"]=0,["PHI"]=0,["DEN"]=0,["GB"]=0,["BUF"]=0,["TB"]=0,["PIT"]=0,["MIN"]=0,["HOU"]=0,["DET"]=0,["TB"]=0,["CAR"]=0,["CHI"]=0,["STL"]=0,["NYG"]=0,["ARI"]=0,["NO"]=0,["KC"]=0,["SF"]=0,["NE"]=0}
    local byeweek = ""

    for _,v in ipairs(nflscores.ss) do
            allteams[v[5]] = 1
            allteams[v[7]] = 1
    end

    for i,v in pairs(allteams) do
            if v == 0 then
                    byeweek = byeweek .. "<color white>".. i .."</color>\r"
            end
    end

    for i,v in ipairs(nflscores.ss) do
            local hteam = v[7]
            local ateam = v[5]
            local qgame = v[3]
            local hscre = v[8] or 0
            local ascre = v[6] or 0
            if v[4] then
                    qtime = "<color white>Time: "..v[4].."</color>"
            else
                    qtime = ""
            end
            local gposs = v[9] or ""
            if gposs ~= "" then
                    if gposs == hteam then
                            hteam = cteam(tbuff(hteam),"p")
                            ateam = cteam(tbuff(ateam))
                    else
                            ateam = cteam(tbuff(ateam),"p")
                            hteam = cteam(tbuff(hteam))
                    end
            else
                    hteam = cteam(tbuff(hteam))
                    ateam = cteam(tbuff(ateam))
            end
            if qgame == "Final" or qgame == "final overtime" then
                    if hscre > ascre then
                            hteam = cteam(tbuff(hteam),"w")
                            ateam = cteam(tbuff(ateam))
                    elseif hscre < ascre then
                            ateam = cteam(tbuff(ateam),"w")
                            hteam = cteam(tbuff(hteam))
                    else
                            ateam = cteam(tbuff(ateam))
                            hteam = cteam(tbuff(hteam))
                    end
                    if qgame == "Final" then
                            qgame = "<color cyan>F</color>"
                            elseif qgame == "final overtime" then
                                    qgame = "<color cyan>F/OT</color>"
                    end
            elseif qgame == "Pregame" then
                    qgame = "<color cyan>Pre</color>"
            elseif qgame == "Halftime" then
                    qgame = "<color white>"..qgame.."</color>"
            else
                    qgame = "<color white>Q"..qgame.."</color>"
            end
            scorestring = scorestring .. "<color back="..bcolor(i) .. ">".. v[1] .. ": " .. ateam .. "<color white>: " .. sbuff(ascre) .. "</color> <color black>@</color> " .. hteam .. "<color white>: ".. sbuff(hscre) .."</color></color> " .. qgame .. " " .. qtime .. "\r"
    end
    return scorestring .. "<color white>Bye week:</color>\r"..byeweek
end

无法正常工作的部分是:

 if hscre > ascre then
                            hteam = cteam(tbuff(hteam),"w")
                            ateam = cteam(tbuff(ateam))
                    elseif hscre < ascre then
                            ateam = cteam(tbuff(ateam),"w")
                            hteam = cteam(tbuff(hteam))
                    else
                            ateam = cteam(tbuff(ateam))
                            hteam = cteam(tbuff(hteam))
                    end

cteam的功能是:

local function cteam(t,s)
                    local status = s or nil
                    local forecolor = ""

                    if status == "p" then
                            forecolor = "yellow"
                    elseif status == "w" then
                            forecolor = "cyan"
                    else
                            forecolor = "limegreen"
                    end

                    return "<color fore="..forecolor..">"..t.."</color>"
            end

现在,它将“p”状态调好。但是当状态变为“w”时,它会失败,而对于我的生活,我无法弄清楚为什么。我错过了什么吗?这段代码可以更清洁吗?

编辑:我没有找到问题的问题,但显然“elseif status ==”w“语句被完全绕过。当游戏正在播放时,正确的团队在在比赛结束后,两支球队都是柠檬绿,好像没有得分高于另一支球队。

第二次修改:第一个回答中列出的错误已得到纠正。不过,它并没有解决问题。我还是很茫然。

1 个答案:

答案 0 :(得分:1)

没有什么可以跳出去,我无法在这里测试,但这里有一些事情需要检查:

你提到cteam在游戏中正常工作,并且只有在游戏结束后,cteam才会给出正确的最终结果。所以cteam的逻辑是正确的。问题必须出在调用cteam的代码中:cteam是否会被调用s等于&#34; w&#34;:如果hscre和ascre始终相同,则不会发生这种情况。此外,分支代码中存在一个错误,它调用了cteam:

if hscre > ascre then
    hteam = cteam(tbuff(hteam),"w")
    ateam = cteam(tbuff(ateam))
elseif hscre < ascre then
    ateam = cteam(tbuff(ateam,"w")) -- ERR
    hteam = cteam(tbuff(hteam))
else
    ateam = cteam(tbuff(ateam))
    hteam = cteam(tbuff(hteam))
end

标记为ERR的行应为:

ateam = cteam(tbuff(ateam),"w")

关于清理代码:在StackOverflow的代码审查论坛上发布您的问题。