Skip to content

Commit

Permalink
Adjust hourly task
Browse files Browse the repository at this point in the history
- Move current hourly task implementation to NextTimeAmountTask
- Implemented a new hourly task which occurs wgen transitioning from x:59
  to y:00.
  • Loading branch information
pacampbell committed Dec 14, 2024
1 parent b7f6059 commit 22b5634
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Arrowgene.Ddon.GameServer/Tasks/HourlyTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ namespace Arrowgene.Ddon.GameServer.Tasks
{
public abstract class HourlyTask : SchedulerTask
{
/// <summary>
/// Task which is always scheduled to run on the hour in the timezone of the server currently running.
/// </summary>
/// <param name="type">The task type associated with this task</param>
public HourlyTask(TaskType type) : base(ScheduleInterval.Hourly, type)
{
}

public override long NextTimestamp()
{
return new DateTimeOffset(DateTime.Now.AddHours(1)).ToUnixTimeSeconds();
var now = DateTime.Now;
var next = now.AddHours(1);
var nextTime = new DateTime(next.Year, next.Month, next.Day, next.Hour, 0, 0);
return new DateTimeOffset(now.Add(nextTime - now)).ToUnixTimeSeconds();
}
}
}
22 changes: 22 additions & 0 deletions Arrowgene.Ddon.GameServer/Tasks/NextTimeAmountTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Arrowgene.Ddon.Shared.Model.Scheduler;
using System;

namespace Arrowgene.Ddon.GameServer.Tasks
{
public abstract class NextTimeAmountTask : SchedulerTask
{
public uint Hours { get; }
public uint Minutes { get; }

public NextTimeAmountTask(TaskType type, uint hours, uint minutes = 0) : base(ScheduleInterval.Hourly, type)
{
Hours = hours;
Minutes = minutes;
}

public override long NextTimestamp()
{
return new DateTimeOffset(DateTime.Now.AddHours(Hours).AddMinutes(Minutes)).ToUnixTimeSeconds();
}
}
}
21 changes: 20 additions & 1 deletion Arrowgene.Ddon.GameServer/Tasks/SchedulerTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ public abstract class SchedulerTask
public TaskType Type { get; }
public ScheduleInterval Interval { get; }

/// <summary>
/// Constructor for SchedulerTask.
/// </summary>
/// <param name="interval">Hint for the type of interval this task is expected to occur at.</param>
/// <param name="type">
/// The task type which is stored in the DB and used to resume the scheduler
/// timer when the head server starts.
/// </param>
public SchedulerTask(ScheduleInterval interval, TaskType type)
{
Type = type;
Expand All @@ -18,10 +26,21 @@ public SchedulerTask(ScheduleInterval interval, TaskType type)
/// Should use the RPC manage if it is required to update clients on different channels
/// or send annoucements to players.
/// </summary>
/// <param name="server"></param>
/// <param name="server">The head server object</param>
public abstract void RunTask(DdonGameServer server);

/// <summary>
/// Generates the next unix timestamp to store in the database for the task.
/// </summary>
/// <returns>Returns the unix timestamp which represents the next time this task should activate.</returns>
public abstract long NextTimestamp();

/// <summary>
/// By default, all tasks will return that they are enabled. A child class can override
/// this function to provide custom checks for enablement.
/// </summary>
/// <param name="server">The head server object</param>
/// <returns>Returns true if this task is enabled, otherwise false.</returns>
public virtual bool IsEnabled(DdonGameServer server)
{
return true;
Expand Down

0 comments on commit 22b5634

Please sign in to comment.