Page 1 of 1

An ugly way to do "JP Midnight"

Posted: Thu Feb 28, 2013 5:13 am
by PrBlahBlahtson
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.

Code: Select all

os.time{year=os.date("%Y"), month=os.date("%m"), day=os.date("%d"), hour=0}
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;

Re: An ugly way to do "JP Midnight"

Posted: Sat Mar 02, 2013 12:58 pm
by Ezekyel
just use :

player:setVar("variable_date",os.date("%j"));

if(tonumber(os.date("%j")) ~= player:getVar("variable_date"); -- if server date(dayoftheyear) is different of variable_date

for jp midnight, just change your time on your server ^^

Re: An ugly way to do "JP Midnight"

Posted: Sat Mar 02, 2013 1:13 pm
by PrBlahBlahtson
Which would set the variable as ~360 for the end of December, and would cause problems, which is why I don't like it.
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.

Re: An ugly way to do "JP Midnight"

Posted: Sat Mar 02, 2013 2:25 pm
by Ezekyel
if you use os.date("%j") > variable, yes you have a big problem with the new year
but with os.date("%j") ~= variable, no problem

Re: An ugly way to do "JP Midnight"

Posted: Sat Mar 02, 2013 2:29 pm
by bluekirby0
Making sure it is not the same day of the year is a decent work-around, but it won't work right that day next year. Sure, it is a fringe case, but it is still a problem. Also, making people set their server time to JST is not a good solution.

Lua time functions have a ! prefix you can add to your timer format specifiers that tells it you want UTC as your base. From there you can just add an offset to get JST.

Re: An ugly way to do "JP Midnight"

Posted: Sat Mar 02, 2013 4:37 pm
by PrBlahBlahtson
Added getMidnight() and getConquestTally() to common.lua. Example usage:

Code: Select all

	elseif (csid == 17) then
		player:setVar("ChasingQuotas_Progress",1);
		player:setVar("ChasingQuotas_date", getMidnight());
...
		if (player:getVar("ChasingQuotas_date") > os.time()) then
			player:startEvent(3); -- Fluff cutscene because you haven't waited a day
		else
			player:startEvent(7); -- Boss got mugged
		end