implemented time restrictions on repeating actions

This commit is contained in:
Sokomine 2021-08-05 18:24:11 +02:00
parent aa38ea2dbb
commit 1b480edfb7

View File

@ -145,7 +145,61 @@ yl_speak_up.execute_next_action = function(player, a_id, result_of_a_id)
-- same order
sorted_key_list = yl_speak_up.sort_keys(actions)
local nr = 0
if(a_id) then
if(not(a_id)) then
-- check if the action(s) can be executed
local time_now = yl_speak_up.get_time_in_seconds()
-- is there a limiton how many failed attempts there can be per time?
local timer_name = "timer_on_failure_"..tostring(d_id).."_"..tostring(o_id)
local timer_data = yl_speak_up.get_variable_metadata( timer_name, "parameter", true)
if(timer_data
and timer_data["max_attempts"] and tonumber(timer_data["max_attempts"]) > 0
and timer_data["duration"] and tonumber(timer_data["duration"]) > 0) then
local new_times = ""
local times = yl_speak_up.get_quest_variable_value(pname, timer_name)
local parts = string.split(times or "", " ")
local count = 0
for i, p in ipairs(parts) do
if(p and tonumber(p)
and (tonumber(p) + tonumber(timer_data["duration"])>time_now)) then
new_times = new_times.." "..p
count = count + 1
end
end
-- all timers are expired
if(count == 0) then
yl_speak_up.set_quest_variable_value(pname, timer_name, nil)
-- some timers are expired
elseif(new_times ~= times) then
yl_speak_up.set_quest_variable_value(pname, timer_name, new_times)
end
if(count >= tonumber(timer_data["max_attempts"])) then
-- show the same dialog again, but with the failure message
yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = d_id,
alternate_text = timer_data[ "alternate_text" ]
or yl_speak_up.standard_text_if_action_failed_too_often})
return
end
end
-- is there a limiton how fast the action may be repeated again?
timer_name = "timer_on_success_"..tostring(d_id).."_"..tostring(o_id)
timer_data = yl_speak_up.get_variable_metadata(timer_name, "parameter", true)
if(timer_data
and timer_data["duration"] and tonumber(timer_data["duration"]) > 0) then
local last_time = yl_speak_up.get_quest_variable_value(pname, timer_name)
if(last_time and tonumber(last_time)
and tonumber(last_time) + tonumber(timer_data["duration"]) > time_now) then
-- show the same dialog again, but with the failure message
yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = d_id,
alternate_text = timer_data[ "alternate_text" ]
or yl_speak_up.standard_text_if_action_repeated_too_soon})
return
else
-- the timer has expired
yl_speak_up.set_quest_variable_value(pname, timer_name, nil)
end
end
else -- if(a_id) then
nr = table.indexof(sorted_key_list, a_id)
-- did the player go back?
if(nr > -1 and result_of_a_id == nil) then
@ -163,6 +217,23 @@ yl_speak_up.execute_next_action = function(player, a_id, result_of_a_id)
return
-- did the action fail?
elseif(nr > -1 and not(result_of_a_id)) then
-- is there a limiton how many failed attempts there can be per time?
local timer_name = "timer_on_failure_"..tostring(d_id).."_"..tostring(o_id)
local timer_data = yl_speak_up.get_variable_metadata(
timer_name, "parameter", true)
-- store that (another?) attempt to do the action failed
if(timer_data
and timer_data["max_attempts"] and tonumber(timer_data["max_attempts"])> 0
and timer_data["duration"] and tonumber(timer_data["duration"])> 0) then
local times = yl_speak_up.get_quest_variable_value(pname, timer_name)
if(not(times)) then
times = ""
end
-- variables are stored as strings, not as lists
yl_speak_up.set_quest_variable_value(pname, timer_name,
times.." "..yl_speak_up.get_time_in_seconds())
end
local this_action = actions[ sorted_key_list[ nr ]]
-- if there is an on_failure target dialog: go there
if(this_action.a_on_failure) then
@ -206,6 +277,14 @@ yl_speak_up.execute_next_action = function(player, a_id, result_of_a_id)
end
end
-- when all actions are executed:
-- is there a limiton how fast the action may be repeated again?
local timer_name = "timer_on_success_"..tostring(d_id).."_"..tostring(o_id)
local timer_data = yl_speak_up.get_variable_metadata(timer_name, "parameter", true)
-- store that the action was executed successfully
if(timer_data
and timer_data["duration"] and tonumber(timer_data["duration"]) > 0) then
yl_speak_up.set_quest_variable_value(pname, timer_name, yl_speak_up.get_time_in_seconds())
end
-- set the current action to nil
yl_speak_up.speak_to[pname].a_id = nil
yl_speak_up.debug_msg(player, n_id, o_id, "All actions have been executed successfully. "..