From 0eec287e7b2edd1338739d651f3f88e5220fda7d Mon Sep 17 00:00:00 2001 From: Kwoth Date: Fri, 7 Oct 2016 15:00:42 +0200 Subject: [PATCH] ;mp almost fixed. Might need a rework >.> --- ...er.cs => 20161007091612_first.Designer.cs} | 2 +- ...30440_first.cs => 20161007091612_first.cs} | 0 .../Permissions/PermissionExtensions.cs | 4 +-- .../Modules/Permissions/Permissions.cs | 29 ++++++++++++------- .../Services/Database/Models/Permission.cs | 11 +++++++ .../Services/Database/NadekoContext.cs | 4 +-- 6 files changed, 35 insertions(+), 15 deletions(-) rename src/NadekoBot/Migrations/{20161005030440_first.Designer.cs => 20161007091612_first.Designer.cs} (99%) rename src/NadekoBot/Migrations/{20161005030440_first.cs => 20161007091612_first.cs} (100%) diff --git a/src/NadekoBot/Migrations/20161005030440_first.Designer.cs b/src/NadekoBot/Migrations/20161007091612_first.Designer.cs similarity index 99% rename from src/NadekoBot/Migrations/20161005030440_first.Designer.cs rename to src/NadekoBot/Migrations/20161007091612_first.Designer.cs index 1f862568..8a0b6f16 100644 --- a/src/NadekoBot/Migrations/20161005030440_first.Designer.cs +++ b/src/NadekoBot/Migrations/20161007091612_first.Designer.cs @@ -8,7 +8,7 @@ using NadekoBot.Services.Database.Impl; namespace NadekoBot.Migrations { [DbContext(typeof(NadekoSqliteContext))] - [Migration("20161005030440_first")] + [Migration("20161007091612_first")] partial class first { protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/src/NadekoBot/Migrations/20161005030440_first.cs b/src/NadekoBot/Migrations/20161007091612_first.cs similarity index 100% rename from src/NadekoBot/Migrations/20161005030440_first.cs rename to src/NadekoBot/Migrations/20161007091612_first.cs diff --git a/src/NadekoBot/Modules/Permissions/PermissionExtensions.cs b/src/NadekoBot/Modules/Permissions/PermissionExtensions.cs index ad38511d..af8e53c1 100644 --- a/src/NadekoBot/Modules/Permissions/PermissionExtensions.cs +++ b/src/NadekoBot/Modules/Permissions/PermissionExtensions.cs @@ -6,6 +6,7 @@ using NadekoBot.Services.Database; using NadekoBot.Services.Database.Models; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -159,8 +160,7 @@ namespace NadekoBot.Modules.Permissions if (index == 0) { - perm.Previous = toAdd; - toAdd.Next = perm; + perm.Prepend(toAdd); return; } diff --git a/src/NadekoBot/Modules/Permissions/Permissions.cs b/src/NadekoBot/Modules/Permissions/Permissions.cs index b19d8e41..b3a52765 100644 --- a/src/NadekoBot/Modules/Permissions/Permissions.cs +++ b/src/NadekoBot/Modules/Permissions/Permissions.cs @@ -91,7 +91,8 @@ namespace NadekoBot.Modules.Permissions Permission p; using (var uow = DbHandler.UnitOfWork()) { - var perms = uow.GuildConfigs.PermissionsFor(channel.Guild.Id).RootPermission; + var config = uow.GuildConfigs.PermissionsFor(channel.Guild.Id); + var perms = config.RootPermission; if (index == perms.Count() - 1) { return; @@ -99,7 +100,7 @@ namespace NadekoBot.Modules.Permissions else if (index == 0) { p = perms; - uow.GuildConfigs.PermissionsFor(channel.Guild.Id).RootPermission = perms.Next; + config.RootPermission = perms.Next; } else { @@ -114,7 +115,7 @@ namespace NadekoBot.Modules.Permissions uow2._context.SaveChanges(); } - await channel.SendMessageAsync($"{imsg.Author.Mention} removed permission **{p.GetCommand()}** from position #{index + 1}.").ConfigureAwait(false); + await channel.SendMessageAsync($"{imsg.Author.Mention} removed permission **{p.GetCommand(channel.Guild)}** from position #{index + 1}.").ConfigureAwait(false); } catch (ArgumentOutOfRangeException) { @@ -136,21 +137,29 @@ namespace NadekoBot.Modules.Permissions Permission toInsert; using (var uow = DbHandler.UnitOfWork()) { - var perms = uow.GuildConfigs.PermissionsFor(channel.Guild.Id).RootPermission; + var config = uow.GuildConfigs.PermissionsFor(channel.Guild.Id); + var perms = config.RootPermission; if (from == 0) + { toInsert = perms; + perms = perms.Next; + toInsert.Previous = null; + toInsert.Next = null; + perms.Previous = null; + } else + { toInsert = perms.RemoveAt(from); - if (from < to) - to -= 1; - var last = perms.Count() - 1; - if (from == last || to == last) + toInsert.Previous = null; + } + var size = perms.Count(); + if (from == size || to == size) throw new IndexOutOfRangeException(); perms.Insert(to, toInsert); - uow.GuildConfigs.PermissionsFor(channel.Guild.Id).RootPermission = perms.GetRoot(); + config.RootPermission = perms.GetRoot(); await uow.CompleteAsync().ConfigureAwait(false); } - await channel.SendMessageAsync($"`Moved permission:` \"{toInsert.GetCommand()}\" `from #{from} to #{to}.`").ConfigureAwait(false); + await channel.SendMessageAsync($"`Moved permission:` \"{toInsert.GetCommand(channel.Guild)}\" `from #{from} to #{to}.`").ConfigureAwait(false); return; } catch (Exception e) when (e is ArgumentOutOfRangeException || e is IndexOutOfRangeException) diff --git a/src/NadekoBot/Services/Database/Models/Permission.cs b/src/NadekoBot/Services/Database/Models/Permission.cs index aed70fa5..b88de0d3 100644 --- a/src/NadekoBot/Services/Database/Models/Permission.cs +++ b/src/NadekoBot/Services/Database/Models/Permission.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NadekoBot.Services.Database.Models { + [DebuggerDisplay("{global::NadekoBot.Modules.Permissions.PermissionExtensions.GetCommand(this)}", Target = typeof(Permission))] public class Permission : DbEntity { public Permission Previous { get; set; } = null; @@ -49,6 +51,15 @@ namespace NadekoBot.Services.Database.Models return blockNsfw; } + + public Permission Clone() => new Permission() + { + PrimaryTarget = PrimaryTarget, + SecondaryTarget = SecondaryTarget, + PrimaryTargetId = PrimaryTargetId, + SecondaryTargetName = SecondaryTargetName, + State = State, + }; } public enum PrimaryPermissionType diff --git a/src/NadekoBot/Services/Database/NadekoContext.cs b/src/NadekoBot/Services/Database/NadekoContext.cs index 81940a91..2661bdf8 100644 --- a/src/NadekoBot/Services/Database/NadekoContext.cs +++ b/src/NadekoBot/Services/Database/NadekoContext.cs @@ -186,8 +186,8 @@ namespace NadekoBot.Services.Database var permissionEntity = modelBuilder.Entity(); permissionEntity .HasOne(p => p.Next) - .WithOne(p => p.Previous); - + .WithOne(p => p.Previous) + .IsRequired(false); #endregion #region LogSettings