Jump to content

Daily Quest


Go to solution Solved by farmmedal,

Recommended Posts

Dear Community!

I would like to ask for your help.

I'm trying to figure this out for a while, I would like to make a simple quest, thats repeatable: but not every 24hours from completing one. (Like every Daily quest uploaded everywhere)

I would like my quest to become repeatable every midnight (server time).

I tried it with

pc.setqf("daily_cd", 24 - get_global_time());

But did not work, messed up the whole quest.

What can you advice, how could this problem be solved?

 

Link to comment
https://metin2.dev/topic/33289-daily-quest/
Share on other sites

Thanks for the tip!

I'm working on it, yet I have to admit, I haven't doing this for a long time now, so I may not understand something simple.

Quest for testing:

quest timertest begin
	state start begin
		when login begin
			set_state(timertest)
		end
	end
	state timertest begin
		when letter begin
			send_letter("Timerboi")
		end
		when button or info begin
			local midnight_time = get_global_time() + 86400 - (get_global_time() / 86400)  --it did not work with %
			notice(""..midnight_time..")
		end
	end
end

Result in notice:

1733752324.1889

1733752325.1889

etc.

 

If I understand correctly: the local variable called "midnight_time" is made of global time +86400 seconds, minus global time / 86400 seconds.

The problem is, I don't even understand why.

 

Please, be so kind and help me with this matter. Thanks!

Link to comment
https://metin2.dev/topic/33289-daily-quest/#findComment-168555
Share on other sites

  • Active Member

It returns the Unix timestamp of the next midnight, which represents:

00:00:00 (start of the next day) in server time.

For example:

If the current time is 20:00:00 (8:00 PM), this will return the timestamp corresponding to 00:00:00 of the next day.

Link to comment
https://metin2.dev/topic/33289-daily-quest/#findComment-168580
Share on other sites

  • Honorable Member
--[[
    Description:
        Returns a string representing the total time in either the "Days, Hours, Minutes and Seconds" or "d:h:m:s" format.

    Arguments:
        sec (number): The total amount of seconds.
        format_type (string): Optional parameter, either "verbose" or "clock".
        simplify (boolean): Optional parameter, valid only if format_type is "clock". If true, the labels ("d", "h", "m", "s") will be omitted.
    
    Example:
        get_time_format(52165786, "verbose") -- Returns "603 days, 18 hours, 29 minutes and 46 seconds"
        get_time_format(52165786, "clock") -- Returns "603d:18h:29m:46s"
        get_time_format(52165786, "clock", true) -- Returns "603:18:29:46"

    Returns:
        string: A string representing the total time.

    Time complexity: O(1).
    Space complexity: O(1).
]]
local lang_time_words = {
    ["day"] = {"day", "days"},
    ["hour"] = {"hour", "hours"},
    ["minute"] = {"minute", "minutes"},
    ["second"] = {"second", "seconds"}
};

get_time_format = function(sec, format_type, simplify)
    format_type = format_type or "verbose";
    simplify = simplify or false;

    local days = math.floor(sec / time_day_to_sec(1));
    local hours = math.floor(math.mod(sec, time_day_to_sec(1)) / time_hour_to_sec(1));
    local minutes = math.floor(math.mod(sec, time_hour_to_sec(1)) / time_min_to_sec(1));
    local seconds = math.mod(sec, time_min_to_sec(1));

    if (format_type == "verbose") then
        local parts = {};
        local function add_part(quantity, words)
            if (quantity > 0) then
                table.insert(parts, string.format("%d %s", quantity, words[quantity > 1 and 2 or 1]));
            end -- if
        end -- function

        add_part(days, lang_time_words["day"]);
        add_part(hours, lang_time_words["hour"]);
        add_part(minutes, lang_time_words["minute"]);
        add_part(seconds, lang_time_words["second"]);

        local parts_num = table_get_count(parts);
        if (parts_num > 1) then
            parts[parts_num - 1] = string.format("%s and %s", parts[parts_num - 1], parts[parts_num]);
            table.remove(parts);
        end -- if

        return table.concat(parts, ", ");

    elseif (format_type == "clock") then
        if (simplify) then
            return string.format("%02d:%02d:%02d:%02d", days, hours, minutes, seconds);
        end -- if

        return string.format("%02dd:%02dh:%02dm:%02ds", days, hours, minutes, seconds);
    end -- if/elseif
end -- function

--[[
    Description:
        Returns the number of seconds remaining until a specified time of day.

    Arguments:
        time_ex (string): The target time of day in "HH:MM" 24-hour format.

    Example:
        time_remaining_until("23:00") -- Returns the number of seconds remaining until 11 PM.

    Returns:
        number: The number of seconds remaining.

    Time complexity: O(1).
    Space complexity: O(1).
]]
time_remaining_until = function(time_ex)
    local current_time = os.date("*t");
    local current_time_secs = current_time.hour * time_hour_to_sec(1) + current_time.min * time_min_to_sec(1);

    local dot_pos = string.find(time_ex, ":", 1, true);
    local hours_ex = tonumber(string.sub(time_ex, 1, dot_pos - 1));
    local minutes_ex = tonumber(string.sub(time_ex, dot_pos + 1));

    local time_ex_secs = hours_ex * time_hour_to_sec(1) + minutes_ex * time_min_to_sec(1);
    local seconds_remaining = time_ex_secs - current_time_secs;
    if (seconds_remaining < 0) then
        seconds_remaining = seconds_remaining + time_day_to_sec(1);
    end -- if

    return seconds_remaining;
end -- function

--[[
    Description:
        Calculates the future time based on the current system time and an additional number of seconds. 
        The function retrieves the current hour and minute from the system, adds the specified seconds, 
        and returns the resulting time in a 24-hour format.

    Arguments:
        sec (number): The number of seconds to add to the current system time.

    Example:
        get_time_after_seconds(600) -- If the current time is 18:00:00, this returns "18:10:00"

    Returns:
        string: A string representing the future time in "HH:MM:SS" format, based on the current system time 
        and the added seconds.

    Notes:
        The function handles the day transition, so if the time addition crosses midnight, the hour is adjusted accordingly.
        The function assumes that the system time is accurate and correctly set.

    Time complexity: O(1).
    Space complexity: O(1).
]]
get_time_after_seconds = function(sec)
    local current_time = os.date("*t");
    local total_seconds = current_time.sec + sec;
    local total_minutes = current_time.min + math.floor(total_seconds / time_min_to_sec(1));
    local new_seconds = math.mod(total_seconds, time_min_to_sec(1));

    local new_hour = current_time.hour + math.floor(total_minutes / time_min_to_sec(1));
    local new_minute = math.mod(total_minutes, time_min_to_sec(1))

    if (new_hour >= 24) then
        new_hour = new_hour - 24;
    end -- if

    return string.format("%02d:%02d:%02d", new_hour, new_minute, new_seconds);
end -- function

 

Edited by Syreldar
  • Metin2 Dev 2

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Link to comment
https://metin2.dev/topic/33289-daily-quest/#findComment-168584
Share on other sites

  • Solution

Thank you both for your work!

Here is my solution:

I added @ Syreldar's functions, used them with a simple timer.

quest timetester begin
	state start begin
		when login begin
			set_state(timetester)
		end
	end
	state timetester begin
		when letter begin
			send_letter("Timetester")
		end
		when button or info begin
			local t = time_remaining_until("23:00")	--simplifying it for myself
			say_title("Timetester")
			say(""..t.."") --showing if it tells the right time
			wait()
			timer("dailytimer", t)
		end
		when "dailytimer".timer begin
			notice("timer reset!")
		end
	end
end

Works like a charm, using this function makes this really easy.

Thank you for all your efforts!

Link to comment
https://metin2.dev/topic/33289-daily-quest/#findComment-168607
Share on other sites

Don't use any images from : imgur, turkmmop, freakgamers, inforge, hizliresim... Or your content will be deleted without notice...
Use : https://metin2.download/media/add/

Please use https://metin2.download/ when uploading files smaller than 100MB, otherwise the approval will take longer due to manual upload.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.