From f89937fd0a417a73b4e313cf12c67f7bbda2e028 Mon Sep 17 00:00:00 2001 From: Nitix Date: Fri, 7 Oct 2016 18:53:04 +0200 Subject: [PATCH] Fix ;mp command --- .../Permissions/PermissionExtensions.cs | 3 +- .../Modules/Permissions/Permissions.cs | 82 +++++++++++++++---- 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/src/NadekoBot/Modules/Permissions/PermissionExtensions.cs b/src/NadekoBot/Modules/Permissions/PermissionExtensions.cs index af8e53c1..783e2b90 100644 --- a/src/NadekoBot/Modules/Permissions/PermissionExtensions.cs +++ b/src/NadekoBot/Modules/Permissions/PermissionExtensions.cs @@ -153,6 +153,7 @@ namespace NadekoBot.Modules.Permissions toAdd.Next = perm; } + /* /this can't work if index < 0 and perm isn't roo public static void Insert(this Permission perm, int index, Permission toAdd) { if (index < 0) @@ -183,7 +184,7 @@ namespace NadekoBot.Modules.Permissions toAdd.Previous = previous; previous.Next = toAdd; } - + */ public static Permission RemoveAt(this Permission perm, int index) { if (index <= 0) //can't really remove at 0, that means deleting the element right now. Just use perm.Next if its 0 diff --git a/src/NadekoBot/Modules/Permissions/Permissions.cs b/src/NadekoBot/Modules/Permissions/Permissions.cs index b3a52765..ee4d84ea 100644 --- a/src/NadekoBot/Modules/Permissions/Permissions.cs +++ b/src/NadekoBot/Modules/Permissions/Permissions.cs @@ -134,32 +134,86 @@ namespace NadekoBot.Modules.Permissions { try { - Permission toInsert; + Permission fromPerm = null; + Permission toPerm = null; using (var uow = DbHandler.UnitOfWork()) { var config = uow.GuildConfigs.PermissionsFor(channel.Guild.Id); var perms = config.RootPermission; + var root = perms; + var index = 0; + var fromFound = false; + var toFound = false; + var isLast = true; + while ((!toFound || !fromFound) && perms != null) + { + if (index == from) + { + fromPerm = perms; + fromFound = true; + } + if (index == to) + { + toPerm = perms; + toFound = true; + isLast = false; + } + if (!toFound) + { + toPerm = perms; //In case of to > size + } + perms = perms.Next; + index++; + } + if (perms == null) + { + if (!fromFound) + { + await channel.SendMessageAsync($"`Can't find permission at index `#{++from}`").ConfigureAwait(false); + return; + } + } + + //Change chain for from indx + var next = fromPerm.Next; + var pre = fromPerm.Previous; + if (pre != null) + pre.Next = next; + if (next != null) + { + next.Previous = pre; + } if (from == 0) { - toInsert = perms; - perms = perms.Next; - toInsert.Previous = null; - toInsert.Next = null; - perms.Previous = null; + root = next; + } + await uow.CompleteAsync().ConfigureAwait(false); + //Inserting + pre = toPerm.Previous; + if (isLast) + { + toPerm.Next = fromPerm; + fromPerm.Previous = toPerm; + fromPerm.Next = null; } else { - toInsert = perms.RemoveAt(from); - toInsert.Previous = null; + fromPerm.Next = toPerm; + fromPerm.Previous = pre; + if (pre != null) + { + pre.Next = fromPerm; + } + else + { + root = fromPerm; + } + toPerm.Previous = fromPerm; } - var size = perms.Count(); - if (from == size || to == size) - throw new IndexOutOfRangeException(); - perms.Insert(to, toInsert); - config.RootPermission = perms.GetRoot(); + config.RootPermission = root; await uow.CompleteAsync().ConfigureAwait(false); } - await channel.SendMessageAsync($"`Moved permission:` \"{toInsert.GetCommand(channel.Guild)}\" `from #{from} to #{to}.`").ConfigureAwait(false); + await channel.SendMessageAsync($"`Moved permission:` \"{fromPerm.GetCommand(channel.Guild)}\" `from #{++from} to #{++to}.`").ConfigureAwait(false); return; } catch (Exception e) when (e is ArgumentOutOfRangeException || e is IndexOutOfRangeException)