Ark Server API (ASE) - Wiki
Loading...
Searching...
No Matches
Timer.cpp
Go to the documentation of this file.
1#include <Timer.h>
2
3#include "../IBaseApi.h"
4
5namespace API
6{
8 {
10 }
11
13 {
15 }
16
18 {
19 static Timer instance;
20 return instance;
21 }
22
23 void Timer::DelayExecuteInternal(const std::function<void()>& callback, int delay_seconds)
24 {
25 const auto now = std::chrono::system_clock::now();
26 const auto exec_time = now + std::chrono::seconds(delay_seconds);
27
28 timer_funcs_.emplace_back(std::make_unique<TimerFunc>(exec_time, callback, true, 1, 0));
29 }
30
31 void Timer::RecurringExecuteInternal(const std::function<void()>& callback, int execution_interval,
32 int execution_counter, bool async)
33 {
34 if (async)
35 {
36 std::thread([callback, execution_interval, execution_counter]()
37 {
38 if (execution_counter == -1)
39 {
40 for (;;)
41 {
42 callback();
43 std::this_thread::sleep_for(std::chrono::seconds(execution_interval));
44 }
45 }
46
47 for (int i = 0; i < execution_counter; ++i)
48 {
49 callback();
50 std::this_thread::sleep_for(std::chrono::seconds(execution_interval));
51 }
52 }).detach();
53 }
54 else
55 {
56 const auto now = std::chrono::system_clock::now();
57 timer_funcs_.emplace_back(
58 std::make_unique<TimerFunc>(now, callback, false, execution_counter, execution_interval));
59 }
60 }
61
62 void Timer::Update()
63 {
64 if (timer_funcs_.empty())
65 {
66 return;
67 }
68
69 const auto now = std::chrono::system_clock::now();
70
71 bool remove = false;
72
73 for (const auto& data : timer_funcs_)
74 {
75 if (data == nullptr) continue;
76
77 if (now >= data->next_time)
78 {
79 if (data->exec_once)
80 {
81 remove = true;
82 }
83 else
84 {
85 if (data->execution_counter > 0)
86 {
87 --data->execution_counter;
88 }
89 else if (data->execution_counter != -1)
90 {
91 remove = true;
92 continue;
93 }
94
95 data->next_time = now + std::chrono::seconds(data->execution_interval);
96 }
97
98 data->callback();
99 }
100 }
101
102 if (remove)
103 {
104 timer_funcs_.erase(std::remove_if(timer_funcs_.begin(), timer_funcs_.end(), [&now](const auto& data)
105 {
106 return (now >= data->next_time && data->exec_once) ||
107 (!data->exec_once && data->execution_counter == 0);
108 }), timer_funcs_.end());
109 }
110 }
111} // namespace API
virtual std::unique_ptr< ArkApi::ICommands > & GetCommands()=0
ARK_API void RecurringExecuteInternal(const std::function< void()> &callback, int execution_interval, int execution_counter, bool async)
Definition Timer.cpp:31
void Update()
Definition Timer.cpp:62
ARK_API void DelayExecuteInternal(const std::function< void()> &callback, int delay_seconds)
Definition Timer.cpp:23
static ARK_API Timer & Get()
Definition Timer.cpp:17
std::vector< std::unique_ptr< TimerFunc > > timer_funcs_
Definition Timer.h:82
virtual bool RemoveOnTimerCallback(const FString &id)=0
Removes an on-timer callback.
virtual void AddOnTimerCallback(const FString &id, const std::function< void()> &callback)=0
Added function will be called every second.
Definition IBaseApi.h:9
std::unique_ptr< IBaseApi > game_api
Definition IBaseApi.h:25
Definition json.hpp:4518
std::function< void()> callback
Definition Timer.h:67
std::chrono::time_point< std::chrono::system_clock > next_time
Definition Timer.h:66