Performance improvements. Timely command almost done

This commit is contained in:
Master Kwoth 2017-10-26 11:31:44 +02:00
parent ffcaa594c9
commit 8220487672
18 changed files with 2084 additions and 23 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,37 @@
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
namespace NadekoBot.Migrations
{
public partial class timely : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "TimelyCurrency",
table: "BotConfig",
type: "INTEGER",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "TimelyCurrencyPeriod",
table: "BotConfig",
type: "INTEGER",
nullable: false,
defaultValue: 0);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "TimelyCurrency",
table: "BotConfig");
migrationBuilder.DropColumn(
name: "TimelyCurrencyPeriod",
table: "BotConfig");
}
}
}

View File

@ -185,6 +185,10 @@ namespace NadekoBot.Migrations
b.Property<bool>("RotatingStatuses"); b.Property<bool>("RotatingStatuses");
b.Property<int>("TimelyCurrency");
b.Property<int>("TimelyCurrencyPeriod");
b.Property<int>("TriviaCurrencyReward"); b.Property<int>("TriviaCurrencyReward");
b.Property<int>("XpMinutesTimeout") b.Property<int>("XpMinutesTimeout")

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using NadekoBot.Common.Attributes; using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Administration.Services; using NadekoBot.Modules.Administration.Services;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Modules.Administration namespace NadekoBot.Modules.Administration
{ {
@ -28,7 +29,7 @@ namespace NadekoBot.Modules.Administration
bool enabled; bool enabled;
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate(set => set);
enabled = config.RotatingStatuses = !config.RotatingStatuses; enabled = config.RotatingStatuses = !config.RotatingStatuses;
uow.Complete(); uow.Complete();
@ -45,7 +46,7 @@ namespace NadekoBot.Modules.Administration
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate(set => set.Include(x => x.RotatingStatusMessages));
var toAdd = new PlayingStatus { Status = status }; var toAdd = new PlayingStatus { Status = status };
config.RotatingStatusMessages.Add(toAdd); config.RotatingStatusMessages.Add(toAdd);
await uow.CompleteAsync(); await uow.CompleteAsync();
@ -79,7 +80,7 @@ namespace NadekoBot.Modules.Administration
string msg; string msg;
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate(set => set.Include(x => x.RotatingStatusMessages));
if (index >= config.RotatingStatusMessages.Count) if (index >= config.RotatingStatusMessages.Count)
return; return;

View File

@ -190,7 +190,7 @@ namespace NadekoBot.Modules.Administration
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate(set => set);
config.ForwardMessages = !config.ForwardMessages; config.ForwardMessages = !config.ForwardMessages;
uow.Complete(); uow.Complete();
} }
@ -208,7 +208,7 @@ namespace NadekoBot.Modules.Administration
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var config = uow.BotConfig.GetOrCreate(); var config = uow.BotConfig.GetOrCreate(set => set);
lock (_locker) lock (_locker)
config.ForwardToAllOwners = !config.ForwardToAllOwners; config.ForwardToAllOwners = !config.ForwardToAllOwners;
uow.Complete(); uow.Complete();

View File

@ -8,6 +8,7 @@ using NadekoBot.Core.Services.Database.Models;
using System.Collections.Generic; using System.Collections.Generic;
using NadekoBot.Common; using NadekoBot.Common;
using NadekoBot.Common.Attributes; using NadekoBot.Common.Attributes;
using System;
namespace NadekoBot.Modules.Gambling namespace NadekoBot.Modules.Gambling
{ {
@ -16,16 +17,19 @@ namespace NadekoBot.Modules.Gambling
private readonly IBotConfigProvider _bc; private readonly IBotConfigProvider _bc;
private readonly DbService _db; private readonly DbService _db;
private readonly CurrencyService _currency; private readonly CurrencyService _currency;
private readonly IDataCache _cache;
private string CurrencyName => _bc.BotConfig.CurrencyName; private string CurrencyName => _bc.BotConfig.CurrencyName;
private string CurrencyPluralName => _bc.BotConfig.CurrencyPluralName; private string CurrencyPluralName => _bc.BotConfig.CurrencyPluralName;
private string CurrencySign => _bc.BotConfig.CurrencySign; private string CurrencySign => _bc.BotConfig.CurrencySign;
public Gambling(IBotConfigProvider bc, DbService db, CurrencyService currency) public Gambling(IBotConfigProvider bc, DbService db, CurrencyService currency,
IDataCache cache)
{ {
_bc = bc; _bc = bc;
_db = db; _db = db;
_currency = currency; _currency = currency;
_cache = cache;
} }
public long GetCurrency(ulong id) public long GetCurrency(ulong id)
@ -36,6 +40,45 @@ namespace NadekoBot.Modules.Gambling
} }
} }
[NadekoCommand, Usage, Description, Aliases]
public async Task Timely()
{
var val = _bc.BotConfig.TimelyCurrency;
var period = _bc.BotConfig.TimelyCurrencyPeriod;
if (val <= 0)
{
await ReplyErrorLocalized("timely_none").ConfigureAwait(false);
return;
}
TimeSpan? rem;
if ((rem = _cache.AddTimelyClaim(Context.User.Id, period)) != null)
{
await ReplyErrorLocalized("timely_already_claimed", rem?.ToString(@"HH\:mm\:ss")).ConfigureAwait(false);
return;
}
await ReplyConfirmLocalized("timely", val + _bc.BotConfig.CurrencySign, period).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task TimelySet(int num, int period = 24)
{
if (num < 0 || period < 1)
return;
using (var uow = _db.UnitOfWork)
{
uow.BotConfig.GetOrCreate(set => set)
.TimelyCurrency = num;
uow.Complete();
}
if(num == 0)
await ReplyConfirmLocalized("timely_set_none").ConfigureAwait(false);
else
await ReplyConfirmLocalized("timely_set", num, period).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task Raffle([Remainder] IRole role = null) public async Task Raffle([Remainder] IRole role = null)
@ -46,7 +89,7 @@ namespace NadekoBot.Modules.Gambling
var membersArray = members as IUser[] ?? members.ToArray(); var membersArray = members as IUser[] ?? members.ToArray();
if (membersArray.Length == 0) if (membersArray.Length == 0)
{ {
return;
} }
var usr = membersArray[new NadekoRandom().Next(0, membersArray.Length)]; var usr = membersArray[new NadekoRandom().Next(0, membersArray.Length)];
await Context.Channel.SendConfirmAsync("🎟 "+ GetText("raffled_user"), $"**{usr.Username}#{usr.Discriminator}**", footer: $"ID: {usr.Id}").ConfigureAwait(false); await Context.Channel.SendConfirmAsync("🎟 "+ GetText("raffled_user"), $"**{usr.Username}#{usr.Discriminator}**", footer: $"ID: {usr.Id}").ConfigureAwait(false);

View File

@ -8,6 +8,7 @@ using NadekoBot.Common.Attributes;
using NadekoBot.Common.TypeReaders; using NadekoBot.Common.TypeReaders;
using NadekoBot.Modules.Permissions.Services; using NadekoBot.Modules.Permissions.Services;
using NadekoBot.Core.Services.Database.Models; using NadekoBot.Core.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Modules.Permissions namespace NadekoBot.Modules.Permissions
{ {
@ -55,7 +56,7 @@ namespace NadekoBot.Modules.Permissions
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var bc = uow.BotConfig.GetOrCreate(); var bc = uow.BotConfig.GetOrCreate(set => set.Include(x => x.BlockedModules));
bc.BlockedModules.Add(new BlockedCmdOrMdl bc.BlockedModules.Add(new BlockedCmdOrMdl
{ {
Name = moduleName, Name = moduleName,
@ -69,7 +70,7 @@ namespace NadekoBot.Modules.Permissions
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var bc = uow.BotConfig.GetOrCreate(); var bc = uow.BotConfig.GetOrCreate(set => set.Include(x => x.BlockedModules));
bc.BlockedModules.RemoveWhere(x => x.Name == moduleName); bc.BlockedModules.RemoveWhere(x => x.Name == moduleName);
uow.Complete(); uow.Complete();
} }
@ -87,7 +88,7 @@ namespace NadekoBot.Modules.Permissions
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var bc = uow.BotConfig.GetOrCreate(); var bc = uow.BotConfig.GetOrCreate(set => set.Include(x => x.BlockedCommands));
bc.BlockedCommands.Add(new BlockedCmdOrMdl bc.BlockedCommands.Add(new BlockedCmdOrMdl
{ {
Name = commandName, Name = commandName,
@ -101,7 +102,7 @@ namespace NadekoBot.Modules.Permissions
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var bc = uow.BotConfig.GetOrCreate(); var bc = uow.BotConfig.GetOrCreate(set => set.Include(x => x.BlockedCommands));
bc.BlockedCommands.RemoveWhere(x => x.Name == commandName); bc.BlockedCommands.RemoveWhere(x => x.Name == commandName);
uow.Complete(); uow.Complete();
} }

View File

@ -71,7 +71,7 @@ namespace NadekoBot.Modules.Permissions.Services
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var bc = uow.BotConfig.GetOrCreate(); var bc = uow.BotConfig.GetOrCreate(set => set);
var log = LogManager.GetCurrentClassLogger(); var log = LogManager.GetCurrentClassLogger();
if (bc.PermissionVersion <= 1) if (bc.PermissionVersion <= 1)
{ {

View File

@ -145,7 +145,7 @@ namespace NadekoBot.Modules.Utility
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
uow.BotConfig.GetOrCreate().RemindMessageFormat = arg.Trim(); uow.BotConfig.GetOrCreate(set => set).RemindMessageFormat = arg.Trim();
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
} }

View File

@ -30,6 +30,8 @@ namespace NadekoBot.Core.Services.Database.Models
public float Betroll67Multiplier { get; set; } = 2; public float Betroll67Multiplier { get; set; } = 2;
public float Betroll91Multiplier { get; set; } = 4; public float Betroll91Multiplier { get; set; } = 4;
public float Betroll100Multiplier { get; set; } = 10; public float Betroll100Multiplier { get; set; } = 10;
public int TimelyCurrency { get; set; } = 0;
public int TimelyCurrencyPeriod { get; set; } = 0;
//public HashSet<CommandCost> CommandCosts { get; set; } = new HashSet<CommandCost>(); //public HashSet<CommandCost> CommandCosts { get; set; } = new HashSet<CommandCost>();
/// <summary> /// <summary>

View File

@ -0,0 +1,19 @@
using NadekoBot.Common.Collections;
using System.Collections.Generic;
namespace NadekoBot.Core.Services.Database.Models
{
public class Poll
{
public ulong GuildId { get; set; }
public string Question { get; set; }
public IndexedCollection<PollAnswer> Answers { get; set; }
public HashSet<PollVote> Votes { get; set; }
}
public class PollAnswer : IIndexed
{
public int Index { get; set; }
public string Text { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Core.Services.Database.Models
{
public class PollVote
{
}
}

View File

@ -1,4 +1,5 @@
using StackExchange.Redis; using StackExchange.Redis;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot.Core.Services namespace NadekoBot.Core.Services
@ -10,5 +11,6 @@ namespace NadekoBot.Core.Services
Task<(bool Success, string Data)> TryGetAnimeDataAsync(string key); Task<(bool Success, string Data)> TryGetAnimeDataAsync(string key);
Task SetImageDataAsync(string key, byte[] data); Task SetImageDataAsync(string key, byte[] data);
Task SetAnimeDataAsync(string link, string data); Task SetAnimeDataAsync(string link, string data);
TimeSpan? AddTimelyClaim(ulong id, int period);
} }
} }

View File

@ -1,7 +1,5 @@
using System; using NadekoBot.Common;
using NadekoBot.Common;
using NadekoBot.Core.Services.Database.Models; using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Core.Services;
namespace NadekoBot.Core.Services.Impl namespace NadekoBot.Core.Services.Impl
{ {
@ -28,7 +26,7 @@ namespace NadekoBot.Core.Services.Impl
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var bc = uow.BotConfig.GetOrCreate(); var bc = uow.BotConfig.GetOrCreate(set => set);
switch (type) switch (type)
{ {
case BotConfigEditType.CurrencyGenerationChance: case BotConfigEditType.CurrencyGenerationChance:

View File

@ -106,7 +106,7 @@ namespace NadekoBot.Core.Services.Impl
{ {
using (var uow = _db.UnitOfWork) using (var uow = _db.UnitOfWork)
{ {
var bc = uow.BotConfig.GetOrCreate(); var bc = uow.BotConfig.GetOrCreate(set => set);
bc.Locale = ci.Name; bc.Locale = ci.Name;
uow.Complete(); uow.Complete();
} }

View File

@ -1,4 +1,6 @@
using StackExchange.Redis; using NadekoBot.Extensions;
using StackExchange.Redis;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot.Core.Services.Impl namespace NadekoBot.Core.Services.Impl
@ -7,12 +9,14 @@ namespace NadekoBot.Core.Services.Impl
{ {
public ConnectionMultiplexer Redis { get; } public ConnectionMultiplexer Redis { get; }
private readonly IDatabase _db; private readonly IDatabase _db;
private readonly string _redisKey;
public RedisCache() public RedisCache(IBotCredentials creds)
{ {
Redis = ConnectionMultiplexer.Connect("127.0.0.1"); Redis = ConnectionMultiplexer.Connect("127.0.0.1");
Redis.PreserveAsyncOrder = false; Redis.PreserveAsyncOrder = false;
_db = Redis.GetDatabase(); _db = Redis.GetDatabase();
_redisKey = creds.RedisKey();
} }
// things here so far don't need the bot id // things here so far don't need the bot id
@ -40,5 +44,21 @@ namespace NadekoBot.Core.Services.Impl
{ {
return _db.StringSetAsync("anime_" + key, data); return _db.StringSetAsync("anime_" + key, data);
} }
private readonly object timelyLock = new object();
public TimeSpan? AddTimelyClaim(ulong id, int period)
{
lock (timelyLock)
{
var time = TimeSpan.FromHours(period);
if ((bool?)_db.StringGet($"{_redisKey}_timelyclaim_{id}") == null)
{
_db.StringSet($"{_redisKey}_timelyclaim_{id}", true);
_db.KeyExpire($"{_redisKey}_timelyclaim_{id}", time);
return time;
}
return _db.KeyTimeToLive($"{_redisKey}_timelyclaim_{id}");
}
}
} }
} }

View File

@ -65,8 +65,8 @@ namespace NadekoBot
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
TerribleElevatedPermissionCheck(); TerribleElevatedPermissionCheck();
Cache = new RedisCache();
Credentials = new BotCredentials(); Credentials = new BotCredentials();
Cache = new RedisCache(Credentials);
_db = new DbService(Credentials); _db = new DbService(Credentials);
Client = new DiscordSocketClient(new DiscordSocketConfig Client = new DiscordSocketClient(new DiscordSocketConfig
{ {
@ -185,7 +185,6 @@ namespace NadekoBot
throw; throw;
} }
toReturn.Add(x); toReturn.Add(x);
//_log.Info("Loaded {0} typereader.", x.GetType().Name);
} }
return toReturn; return toReturn;

View File

@ -893,5 +893,10 @@
"gambling_rafflecur_already_joined": "You have already joined this raffle or the value you used is not valid.", "gambling_rafflecur_already_joined": "You have already joined this raffle or the value you used is not valid.",
"gambling_rafflecur_ended": "{0} raffle ended. {1} won {2}!", "gambling_rafflecur_ended": "{0} raffle ended. {1} won {2}!",
"music_autodc_enable": "I will disconnect from the voice channel when there are no more songs to play.", "music_autodc_enable": "I will disconnect from the voice channel when there are no more songs to play.",
"music_autodc_disable": "I will no longer disconnect from the voice channel when there are no more songs to play." "music_autodc_disable": "I will no longer disconnect from the voice channel when there are no more songs to play.",
"gambling_timely_none": "Bot owner didn't specify a timely reward.",
"gambling_timely_already_claimed": "You've already claimed your timely reward. You can get it again in {0}.",
"gambling_timely": "You've claimed your {0}. You can claim again in {1}h",
"gambling_timely_set": "Users will be able to claim {0} every {1}h",
"gambling_timely_set_none": "Users will not be able to claim any timely currency."
} }