patternMinor
Tracking various stats on character development
Viewed 0 times
trackingstatscharacterdevelopmentvarious
Problem
The purpose of this code is to track player development on a Multi-User Dungeon I play. It allows saving multiple characters to the same database. It allows for updates on level, hour, tier, morts, months, years, weeks, and days.
Are there any improvements that could be made? Assume that any "missing" functions are functions I have elsewhere that have no real impact; this is the bulk of the code. Ultimately, I want to condense the code from the current 224 lines, if possible.
```
function report_type(rtype)
local reports = {["hour"] = "ThisHour", ["lasthour"] = "LastHour", ["today"] = "Today", ["yesterday"] = "Yesterday", ["tier"] = "ThisTier", ["lasttier"] = "LastTier", ["month"] = "ThisMonth", ["lastmonth"] = "LastMonth", ["year"] = "ThisYear", ["lastyear"] = "LastYear", ["mort"] = "ThisMort", ["lastmort"] = "LastMort", ["week"] = "ThisWeek", ["lastweek"] = "LastWeek"}
if rtype == "lastlevel" then
if trackerdb[charname]["HasPupped"] == 0 then
return report_info("LastLevel")
else
return report_info("LastLevel","Pup")
end
elseif rtype == "level" then
if trackerdb[charname]["HasPupped"] == 0 then
return report_info("ThisLevel")
else
return report_info("ThisLevel","Pup")
end
else
return report_info(reports[rtype])
end -- if
end -- function report_type
function QuestStart(t, r, a, c)
trackerdb[charname]["Quests"]["Report"] = "@x184{@x160On Quest@x184} @wKill @x160" .. t .. " @wLocation: @x160" .. r .. "@w in @x160" .. a .. "@w. Time to complete: @x160" .. c .. "@w minutes."
trackerdb[charname]["Timers"]["ThisQuest"]["Start"]=socket.gettime()
savetable()
return trackerdb[charname]["ReportChan"] .. " " .. trackerdb[charname]["Quests"]["Report"]
end
function QuestFail()
trackerdb[charname]["Quests"]["Report"] = "@x184{@x160Quest failed@x184}@w"
addto(trackerdb[charname]["Quests"]["Total"],"Failed")
savetable()
return trackerdb[charname]["ReportChan"] .. " " .. trackerdb[charnam
Are there any improvements that could be made? Assume that any "missing" functions are functions I have elsewhere that have no real impact; this is the bulk of the code. Ultimately, I want to condense the code from the current 224 lines, if possible.
```
function report_type(rtype)
local reports = {["hour"] = "ThisHour", ["lasthour"] = "LastHour", ["today"] = "Today", ["yesterday"] = "Yesterday", ["tier"] = "ThisTier", ["lasttier"] = "LastTier", ["month"] = "ThisMonth", ["lastmonth"] = "LastMonth", ["year"] = "ThisYear", ["lastyear"] = "LastYear", ["mort"] = "ThisMort", ["lastmort"] = "LastMort", ["week"] = "ThisWeek", ["lastweek"] = "LastWeek"}
if rtype == "lastlevel" then
if trackerdb[charname]["HasPupped"] == 0 then
return report_info("LastLevel")
else
return report_info("LastLevel","Pup")
end
elseif rtype == "level" then
if trackerdb[charname]["HasPupped"] == 0 then
return report_info("ThisLevel")
else
return report_info("ThisLevel","Pup")
end
else
return report_info(reports[rtype])
end -- if
end -- function report_type
function QuestStart(t, r, a, c)
trackerdb[charname]["Quests"]["Report"] = "@x184{@x160On Quest@x184} @wKill @x160" .. t .. " @wLocation: @x160" .. r .. "@w in @x160" .. a .. "@w. Time to complete: @x160" .. c .. "@w minutes."
trackerdb[charname]["Timers"]["ThisQuest"]["Start"]=socket.gettime()
savetable()
return trackerdb[charname]["ReportChan"] .. " " .. trackerdb[charname]["Quests"]["Report"]
end
function QuestFail()
trackerdb[charname]["Quests"]["Report"] = "@x184{@x160Quest failed@x184}@w"
addto(trackerdb[charname]["Quests"]["Total"],"Failed")
savetable()
return trackerdb[charname]["ReportChan"] .. " " .. trackerdb[charnam
Solution
Starting with your first function:
would work just as well. Instead of checking
You can use
As for your
I'm still going through rest of the function definitions. I'll keep updating this post.
report_type. You don't need to put the table indices inside square brackets.local reports = { hour = "ThisHour", lasthour = "LastHour", ... }would work just as well. Instead of checking
HasPupped in each of the if blocks, check it just once.function report_type(rtype)
local reports = {
hour = "ThisHour",
lasthour = "LastHour",
today = "Today",
yesterday = "Yesterday",
tier = "ThisTier",
lasttier = "LastTier",
month = "ThisMonth",
lastmonth = "LastMonth",
year = "ThisYear",
lastyear = "LastYear",
mort = "ThisMort",
lastmort = "LastMort",
week = "ThisWeek",
lastweek = "LastWeek"
}
local bHasPup = ( trackerdb[charname]["HasPupped"] == 0 and "Pup" )
if rtype == "lastlevel" then
return report_info( "LastLevel", bHasPup )
elseif rtype == "level" then
return report_info( "ThisLevel", bHasPup )
else
return report_info(reports[rtype])
end -- if
end -- function report_typeYou can use
os.difftime instead of computing your own time difference.As for your
addZero function, I think you can simply use a string.format call:function addZero( n )
return ("%02d"):format( n )
endI'm still going through rest of the function definitions. I'll keep updating this post.
Code Snippets
local reports = { hour = "ThisHour", lasthour = "LastHour", ... }function report_type(rtype)
local reports = {
hour = "ThisHour",
lasthour = "LastHour",
today = "Today",
yesterday = "Yesterday",
tier = "ThisTier",
lasttier = "LastTier",
month = "ThisMonth",
lastmonth = "LastMonth",
year = "ThisYear",
lastyear = "LastYear",
mort = "ThisMort",
lastmort = "LastMort",
week = "ThisWeek",
lastweek = "LastWeek"
}
local bHasPup = ( trackerdb[charname]["HasPupped"] == 0 and "Pup" )
if rtype == "lastlevel" then
return report_info( "LastLevel", bHasPup )
elseif rtype == "level" then
return report_info( "ThisLevel", bHasPup )
else
return report_info(reports[rtype])
end -- if
end -- function report_typefunction addZero( n )
return ("%02d"):format( n )
endContext
StackExchange Code Review Q#80534, answer score: 2
Revisions (0)
No revisions yet.