Packages can be loaded/unloaded. IUnloadableService interface added whose method Unload, if service implements it, will be called when the module is unloaded.

This commit is contained in:
Master Kwoth
2017-10-05 00:51:12 +02:00
parent 599245b1ca
commit 33ac43e1b5
74 changed files with 866 additions and 520 deletions

View File

@ -4,27 +4,24 @@ using Discord.WebSocket;
using NadekoBot.Extensions;
using NadekoBot.Services;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Gambling.Common.AnimalRacing.Exceptions;
using NadekoBot.Modules.Gambling.Common.AnimalRacing;
using NadekoBot.Modules.Gambling.Services;
namespace NadekoBot.Modules.Gambling
{
public partial class Gambling
{
[Group]
public class AnimalRacingCommands : NadekoSubmodule
public class AnimalRacingCommands : NadekoSubmodule<AnimalRaceService>
{
private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs;
private readonly DiscordSocketClient _client;
public static ConcurrentDictionary<ulong, AnimalRace> AnimalRaces { get; } = new ConcurrentDictionary<ulong, AnimalRace>();
public AnimalRacingCommands(IBotConfigProvider bc, CurrencyService cs, DiscordSocketClient client)
{
_bc = bc;
@ -39,7 +36,7 @@ namespace NadekoBot.Modules.Gambling
public Task Race()
{
var ar = new AnimalRace(_cs, _bc.BotConfig.RaceAnimals.Shuffle().ToArray());
if (!AnimalRaces.TryAdd(Context.Guild.Id, ar))
if (!_service.AnimalRaces.TryAdd(Context.Guild.Id, ar))
return Context.Channel.SendErrorAsync(GetText("animal_race"), GetText("animal_race_already_started"));
ar.Initialize();
@ -66,7 +63,7 @@ namespace NadekoBot.Modules.Gambling
Task Ar_OnEnded(AnimalRace race)
{
_client.MessageReceived -= _client_MessageReceived;
AnimalRaces.TryRemove(Context.Guild.Id, out _);
_service.AnimalRaces.TryRemove(Context.Guild.Id, out _);
var winner = race.FinishedUsers[0];
if (race.FinishedUsers[0].Bet > 0)
{
@ -126,7 +123,7 @@ namespace NadekoBot.Modules.Gambling
private Task Ar_OnStartingFailed(AnimalRace race)
{
AnimalRaces.TryRemove(Context.Guild.Id, out _);
_service.AnimalRaces.TryRemove(Context.Guild.Id, out _);
return ReplyErrorLocalized("animal_race_failed");
}
@ -134,7 +131,7 @@ namespace NadekoBot.Modules.Gambling
[RequireContext(ContextType.Guild)]
public async Task JoinRace(int amount = 0)
{
if (!AnimalRaces.TryGetValue(Context.Guild.Id, out var ar))
if (!_service.AnimalRaces.TryGetValue(Context.Guild.Id, out var ar))
{
await ReplyErrorLocalized("race_not_exist").ConfigureAwait(false);
return;

View File

@ -17,6 +17,7 @@ using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Gambling
{
//todo mess, needs unload thing too - refactor
public partial class Gambling
{
[Group]
@ -274,7 +275,6 @@ namespace NadekoBot.Modules.Gambling
_log.Warn("Stopping flower reaction event because it expired.");
await End();
}
}
}

View File

@ -2,6 +2,13 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<OutputPath>..\src\NadekoBot\bin\$(Configuration)\netcoreapp2.0\modules\$(AssemblyName)\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
<ItemGroup>

View File

@ -0,0 +1,21 @@
using System.Threading.Tasks;
using NadekoBot.Services;
using System.Collections.Concurrent;
using NadekoBot.Modules.Gambling.Common.AnimalRacing;
namespace NadekoBot.Modules.Gambling.Services
{
public class AnimalRaceService : INService, IUnloadableService
{
public ConcurrentDictionary<ulong, AnimalRace> AnimalRaces { get; } = new ConcurrentDictionary<ulong, AnimalRace>();
public Task Unload()
{
foreach (var kvp in AnimalRaces)
{
try { kvp.Value.Dispose(); } catch { }
}
return Task.CompletedTask;
}
}
}

View File

@ -176,7 +176,7 @@ namespace NadekoBot.Modules.Gambling
using (var file = _images.SlotEmojis[numbers[i]].ToStream())
using (var randomImage = ImageSharp.Image.Load(file))
{
bgImage.DrawImage(randomImage, 100, default(Size), new Point(95 + 142 * i, 330));
bgImage.DrawImage(randomImage, 100, default, new Point(95 + 142 * i, 330));
}
}
@ -189,7 +189,7 @@ namespace NadekoBot.Modules.Gambling
using (var fs = _images.SlotNumbers[digit].ToStream())
using (var img = ImageSharp.Image.Load(fs))
{
bgImage.DrawImage(img, 100, default(Size), new Point(230 - n * 16, 462));
bgImage.DrawImage(img, 100, default, new Point(230 - n * 16, 462));
}
n++;
} while ((printWon /= 10) != 0);
@ -202,7 +202,7 @@ namespace NadekoBot.Modules.Gambling
using (var fs = _images.SlotNumbers[digit].ToStream())
using (var img = ImageSharp.Image.Load(fs))
{
bgImage.DrawImage(img, 100, default(Size), new Point(395 - n * 16, 462));
bgImage.DrawImage(img, 100, default, new Point(395 - n * 16, 462));
}
n++;
} while ((printAmount /= 10) != 0);

View File

@ -44,7 +44,7 @@ namespace NadekoBot.Modules.Gambling
Depraved,
Harlot
}
//todo unclaimed waifus should lose 5% of their value a day
[Group]
public class WaifuClaimCommands : NadekoSubmodule
{