An ugly way to do "JP Midnight"
Posted: Thu Feb 28, 2013 5:13 am
What most scripts do at the moment is store os.date("%j"), which is basically the day of the year. This poses two problems:
1. If you do a quest on December 27th, and the quest looks for os.date("%j") > variable, then if you don't complete the quest before New Years, you're waiting a year for another 4 day window.
2. If you don't play for a long period, you're increasingly likely to run into the above.
I'm working on something now and I really didn't want to make those compromises, mostly because of the stakes involved. Here's what I did, so an LUA guru can come along and say "you're doing it wrong," and produce better code that I can steal tomorrow.What this does is convert "today's date" at midnight into the epoch value similar to what you get from os.date(t).
From there, add 86400 (24*60*60) to get tomorrow's date, either before you store the variable of afterward. Compare to os.time(t) and you'll always have "after midnight" on the server.
You could probably dump os.date("*t") into an array and pull the values out for os.time() as well. This might be worth putting in a function in common.lua if there's not an easier way to do it.
Example usage:
player:setVar("VARIABLE_NAME",os.time{year=os.date("%Y"), month=os.date("%m"), day=os.date("%d"), hour=0} + 86400);
if os.time(t) > player:getVar("VARIABLE_NAME") then do stuff; end;
1. If you do a quest on December 27th, and the quest looks for os.date("%j") > variable, then if you don't complete the quest before New Years, you're waiting a year for another 4 day window.
2. If you don't play for a long period, you're increasingly likely to run into the above.
I'm working on something now and I really didn't want to make those compromises, mostly because of the stakes involved. Here's what I did, so an LUA guru can come along and say "you're doing it wrong," and produce better code that I can steal tomorrow.
Code: Select all
os.time{year=os.date("%Y"), month=os.date("%m"), day=os.date("%d"), hour=0}
From there, add 86400 (24*60*60) to get tomorrow's date, either before you store the variable of afterward. Compare to os.time(t) and you'll always have "after midnight" on the server.
You could probably dump os.date("*t") into an array and pull the values out for os.time() as well. This might be worth putting in a function in common.lua if there's not an easier way to do it.
Example usage:
player:setVar("VARIABLE_NAME",os.time{year=os.date("%Y"), month=os.date("%m"), day=os.date("%d"), hour=0} + 86400);
if os.time(t) > player:getVar("VARIABLE_NAME") then do stuff; end;