Implement thread-safe ChatBot API (#1510, #1579)

+ Rework task scheduling in chatbots
+ Switch back terrain processing to tasks
This commit is contained in:
ORelio 2021-05-15 17:36:16 +02:00
parent c1cfaf520d
commit 95d6318350
7 changed files with 517 additions and 461 deletions

View file

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient
{
/// <summary>
/// Holds a task with delay
/// </summary>
class TaskWithDelay
{
private Action _task;
private int tickCounter;
private DateTime dateToLaunch;
public Action Task { get { return _task; } }
public TaskWithDelay(Action task, int delayTicks)
{
_task = task;
tickCounter = delayTicks;
dateToLaunch = DateTime.MaxValue;
}
public TaskWithDelay(Action task, TimeSpan delay)
{
_task = task;
tickCounter = int.MaxValue;
dateToLaunch = DateTime.Now + delay;
}
/// <summary>
/// Tick the counter
/// </summary>
/// <returns>Return true if the task should run now</returns>
public bool Tick()
{
tickCounter--;
if (tickCounter <= 0 || dateToLaunch < DateTime.Now)
return true;
return false;
}
}
}