Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement timer's (set interval & pause/resume) #1013

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SDK
3 changes: 3 additions & 0 deletions Server/Components/Pawn/Manager/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,9 @@ void PawnManager::openAMX(PawnScript& script, bool isEntryScript, bool restartin
script.Register("IsRepeatingTimer", &utils::pawn_IsRepeatingTimer);
script.Register("GetTimerRemaining", &utils::pawn_GetTimerRemaining);
script.Register("GetTimerInterval", &utils::pawn_GetTimerInterval);
script.Register("SetTimerInterval", &utils::pawn_SetTimerInterval);
script.Register("ToggleTimerPause", &utils::pawn_ToggleTimerPause);
script.Register("IsTimerPaused", &utils::pawn_IsTimerPaused);
script.Register("SetModeRestartTime", &utils::pawn_SetModeRestartTime);
script.Register("GetModeRestartTime", &utils::pawn_GetModeRestartTime);

Expand Down
50 changes: 50 additions & 0 deletions Server/Components/Pawn/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,36 @@ inline cell AMX_NATIVE_CALL pawn_settimerex(AMX* amx, cell const* params)
return PawnTimerImpl::Get()->setTimerEx(callback, Milliseconds(params[2]), params[3], fmt, amx, &params[5]);
}

inline cell AMX_NATIVE_CALL pawn_IsTimerPaused(AMX* amx, cell const* params)
{
AMX_MIN_PARAMETERS("IsTimerPaused", params, 1);
ITimer* timer = PawnTimerImpl::Get()->getTimer(params[1]);
if (timer == nullptr || !timer->running())
{
return false;
}

if (!timer->paused())
{
return false;
}

return true;
}

inline cell AMX_NATIVE_CALL pawn_ToggleTimerPause(AMX* amx, cell const* params)
{
AMX_MIN_PARAMETERS("ToggleTimerPause", params, 2);
ITimer* timer = PawnTimerImpl::Get()->getTimer(params[1]);
if (timer == nullptr || !timer->running())
{
return false;
}

timer->togglePause(params[2]);
return true;
}

#define GET_TIMER(timer, name, failRet) \
AMX_MIN_PARAMETERS(name, params, 1); \
ITimer* timer = PawnTimerImpl::Get()->getTimer(params[1]); \
Expand Down Expand Up @@ -235,6 +265,26 @@ inline cell AMX_NATIVE_CALL pawn_GetTimerInterval(AMX* amx, cell const* params)
return timer->interval().count();
}

inline cell AMX_NATIVE_CALL pawn_SetTimerInterval(AMX* amx, cell const* params)
{
AMX_MIN_PARAMETERS("SetTimerInterval", params, 2);
ITimer* timer = PawnTimerImpl::Get()->getTimer(params[1]);
if (timer == nullptr || !timer->running())
{
return false;
}

int interval = static_cast<int>(params[2]);

if(interval < 1)
{
return false;
}

timer->setInterval(static_cast<Milliseconds>(interval));
return true;
}

inline cell AMX_NATIVE_CALL pawn_IsRepeatingTimer(AMX* amx, cell const* params)
{
GET_TIMER(timer, "IsRepeatingTimer", false)
Expand Down
38 changes: 37 additions & 1 deletion Server/Components/Timers/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class Timer final : public ITimer
{
private:
bool running_;
bool paused_;
unsigned int count_;
const Milliseconds interval_;
Milliseconds interval_;
TimePoint timeout_;
TimePoint pausedTime_;
TimerTimeOutHandler* const handler_;

public:
Expand All @@ -28,8 +30,30 @@ class Timer final : public ITimer
timeout_ = timeout;
}

TimePoint getPausedTime() const
{
return pausedTime_;
}

void togglePause(bool paused) override
{
paused_ = paused;

if (paused)
{
pausedTime_ = Time::now();
}
else
{
TimePoint now = Time::now();
Milliseconds pauseDuration = duration_cast<Milliseconds>(now - pausedTime_);
timeout_ += pauseDuration;
}
}

Timer(TimerTimeOutHandler* handler, Milliseconds initial, Milliseconds interval, unsigned int count)
: running_(true)
, paused_(false)
, count_(count)
, interval_(interval)
, timeout_(Time::now() + initial)
Expand All @@ -42,6 +66,11 @@ class Timer final : public ITimer
return running_;
}

bool paused() const override
{
return paused_;
}

Milliseconds remaining() const override
{
return duration_cast<Milliseconds>(timeout_ - Time::now());
Expand All @@ -57,6 +86,12 @@ class Timer final : public ITimer
return interval_;
}

void setInterval(Milliseconds interval) override
{
interval_ = interval;
timeout_ = Time::now() + interval_;
}

TimerTimeOutHandler* handler() const override
{
return handler_;
Expand All @@ -65,6 +100,7 @@ class Timer final : public ITimer
void kill() override
{
running_ = false;
paused_ = false;
}

bool trigger() override
Expand Down
6 changes: 6 additions & 0 deletions Server/Components/Timers/timers_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class TimersComponent final : public ITimersComponent, public CoreEventHandler
}
else
{
if (timer->paused())
{
++it;
continue;
}

const TimePoint now = Time::now();
const Milliseconds diff = duration_cast<Milliseconds>(now - timer->getTimeout());
if (diff.count() >= 0)
Expand Down