我正在创建一个跟踪数据库,收集信息以报告每小时、每天、每周、每月、每年、每个级别、每个班级和每个层级。因此,我有一个名为的表tracker
,它由表的子集组成,反映了不同的报告(即table["ThisHour"]
,等)。我想知道是否有一种方法可以整合所有这些实际上只是添加相同信息的行。这是我正在使用的函数,以及两个子表(而不是我上面列出的 8 个):
function exp_update(xp, gold, str, con, dex, wis, int, luck, bpracs, btrains, pracs, trains, qp)
local xtable = tracker[playername]["Exp"]
addto(xtable["ThisHour"],"Exp",xp)
addto(xtable["ThisHour"],"Gold",gold)
addto(xtable["ThisHour"],"BonusStr",str)
addto(xtable["ThisHour"],"BonusCon",con)
addto(xtable["ThisHour"],"BonusDex",dex)
addto(xtable["ThisHour"],"BonusWis",wis)
addto(xtable["ThisHour"],"BonusInt",int)
addto(xtable["ThisHour"],"BonusLuck",luck)
addto(xtable["ThisHour"],"BonusPracs",bpracs)
addto(xtable["ThisHour"],"BonusTrains",btrains)
addto(xtable["ThisHour"],"Pracs",pracs)
addto(xtable["ThisHour"],"Trains",trains)
addto(xtable["ThisHour"],"Qp",qp)
addto(xtable["ThisHour"],"Quests")
addto(xtable["ThisLevel"],"Exp",xp)
addto(xtable["ThisLevel"],"Gold",gold)
addto(xtable["ThisLevel"],"BonusStr",str)
addto(xtable["ThisLevel"],"BonusCon",con)
addto(xtable["ThisLevel"],"BonusDex",dex)
addto(xtable["ThisLevel"],"BonusWis",wis)
addto(xtable["ThisLevel"],"BonusInt",int)
addto(xtable["ThisLevel"],"BonusLuck",luck)
addto(xtable["ThisLevel"],"BonusPracs",bpracs)
addto(xtable["ThisLevel"],"BonusTrains",btrains)
addto(xtable["ThisLevel"],"Pracs",pracs)
addto(xtable["ThisLevel"],"Trains",trains)
addto(xtable["ThisLevel"],"Qp",qp)
addto(xtable["ThisLevel"],"Quests")
end
如您所见,对于两个子表,这已经是相当多的行了。每个子表有 14 行代码,或者,如果我将它们全部放入,我希望将它们合并到尽可能少的 112 行代码。
addto 函数定义如下,以防您需要它:
function addto(t,k,v)
t[k] = t[k] + (v or 1)
end