48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace MRBot.PassiveTasks;
|
|
|
|
internal interface IPassiveTaskList : IEnumerable<IPassiveTask>
|
|
{
|
|
public void AddTask(IPassiveTask task);
|
|
public void RemoveTask(IPassiveTask task);
|
|
|
|
}
|
|
|
|
internal sealed class PassiveTaskList : IPassiveTaskList
|
|
{
|
|
private readonly ILogger<PassiveTaskList> _logger;
|
|
private readonly ConcurrentDictionary<TaskType, IPassiveTask> _tasks;
|
|
|
|
public PassiveTaskList(ILogger<PassiveTaskList> logger)
|
|
{
|
|
_logger = logger;
|
|
_tasks = new ConcurrentDictionary<TaskType, IPassiveTask>();
|
|
}
|
|
public void AddTask(IPassiveTask task)
|
|
{
|
|
_logger.LogInformation($"Adding task {Enum.GetName(typeof(TaskType), task.Type)}");
|
|
_tasks[task.Type] = task;
|
|
}
|
|
|
|
public void RemoveTask(IPassiveTask task)
|
|
{
|
|
_logger.LogInformation($"Removing task {Enum.GetName(typeof(TaskType), task.Type)}");
|
|
_tasks.TryRemove(task.Type, out _);
|
|
}
|
|
|
|
public IEnumerator<IPassiveTask> GetEnumerator()
|
|
{
|
|
return _tasks.Values.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
} |