Merge pull request #679 from Nitix/mp-fix

Fix ;mp command
This commit is contained in:
Master Kwoth 2016-10-07 19:07:16 +02:00 committed by GitHub
commit 4d1bc01718
2 changed files with 70 additions and 15 deletions

View File

@ -153,6 +153,7 @@ namespace NadekoBot.Modules.Permissions
toAdd.Next = perm; 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) public static void Insert(this Permission perm, int index, Permission toAdd)
{ {
if (index < 0) if (index < 0)
@ -183,7 +184,7 @@ namespace NadekoBot.Modules.Permissions
toAdd.Previous = previous; toAdd.Previous = previous;
previous.Next = toAdd; previous.Next = toAdd;
} }
*/
public static Permission RemoveAt(this Permission perm, int index) 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 if (index <= 0) //can't really remove at 0, that means deleting the element right now. Just use perm.Next if its 0

View File

@ -134,32 +134,86 @@ namespace NadekoBot.Modules.Permissions
{ {
try try
{ {
Permission toInsert; Permission fromPerm = null;
Permission toPerm = null;
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var config = uow.GuildConfigs.PermissionsFor(channel.Guild.Id); var config = uow.GuildConfigs.PermissionsFor(channel.Guild.Id);
var perms = config.RootPermission; 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) if (from == 0)
{ {
toInsert = perms; root = next;
perms = perms.Next; }
toInsert.Previous = null; await uow.CompleteAsync().ConfigureAwait(false);
toInsert.Next = null; //Inserting
perms.Previous = null; pre = toPerm.Previous;
if (isLast)
{
toPerm.Next = fromPerm;
fromPerm.Previous = toPerm;
fromPerm.Next = null;
} }
else else
{ {
toInsert = perms.RemoveAt(from); fromPerm.Next = toPerm;
toInsert.Previous = null; fromPerm.Previous = pre;
if (pre != null)
{
pre.Next = fromPerm;
}
else
{
root = fromPerm;
}
toPerm.Previous = fromPerm;
} }
var size = perms.Count(); config.RootPermission = root;
if (from == size || to == size)
throw new IndexOutOfRangeException();
perms.Insert(to, toInsert);
config.RootPermission = perms.GetRoot();
await uow.CompleteAsync().ConfigureAwait(false); 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; return;
} }
catch (Exception e) when (e is ArgumentOutOfRangeException || e is IndexOutOfRangeException) catch (Exception e) when (e is ArgumentOutOfRangeException || e is IndexOutOfRangeException)