public static List <PlayerVariable> QuickSortList(this List <PlayerVariable> list) { System.Random r = new System.Random(); List <PlayerVariable> less = new List <PlayerVariable> (); List <PlayerVariable> greater = new List <PlayerVariable> (); if (list.Count <= 1) { return(list); } int pos = r.Next(list.Count); PlayerVariable pivot = list[pos]; list.RemoveAt(pos); foreach (PlayerVariable player in list) { if (player.CurrentScore <= pivot.CurrentScore) { less.Add(player); } else { greater.Add(player); } } return(concat(QuickSortList(less), pivot, QuickSortList(greater))); }
public static List <PlayerVariable> concat(List <PlayerVariable> less, PlayerVariable pivot, List <PlayerVariable> greater) { List <PlayerVariable> sorted = new List <PlayerVariable> (less); sorted.Add(pivot); foreach (PlayerVariable i in greater) { sorted.Add(i); } return(sorted); }
public async Task CommandExecutedAsync(Optional <CommandInfo> command, ICommandContext context, IResult result) { // if a command isn't found, log that info to console and exit this method if (!command.IsSpecified) { Console.WriteLine($"Command failed to execute for [" + context.Message + "] <-> [" + context.User + "]!"); return; } // log success to the console and exit this method if (result.IsSuccess) { Console.WriteLine($"Command [" + context.Message + "] executed for -> [" + context.User + "]"); return; } var user = context.User; ulong serverId = context.Guild.Id; Dictionary <ulong, PlayerVariable> playVarDict = DeserializePlayerVariableDictionary(serverId); PlayerVariable pv = new PlayerVariable(); if (playVarDict.ContainsKey(user.Id)) { pv = playVarDict[user.Id]; } DateTimeOffset lastForage = pv.GetLastForage(); DateTimeOffset now = DateTimeOffset.Now; TimeSpan diff = now - lastForage; double minBetween = diff.TotalMinutes; if (command.Value.Name == "forage" && minBetween < 3 && !result.IsSuccess) { StringBuilder sb = new StringBuilder(); sb.AppendLine(result.ErrorReason); if (minBetween < 3) { TimeSpan cd = new TimeSpan(0, 3, 0); TimeSpan timeLeft = cd - diff; int min = timeLeft.Minutes; int sec = timeLeft.Seconds; if (min == 1) { sb.AppendLine($"Time to the next day: " + min + " minute, " + sec + " seconds."); } else { sb.AppendLine($"Time to the next day: " + min + " minutes, " + sec + " seconds."); } } EmbedBuilder embed = new EmbedBuilder(); embed.Title = "Foraging..."; embed.Description = sb.ToString(); embed.WithColor(new Color(0, 255, 0)); await context.Channel.SendMessageAsync(null, false, embed.Build()); return; } if (command.Value.Name == "forage" && !pv.isForaging && !result.IsSuccess) { StringBuilder sb = new StringBuilder(); sb.AppendLine(result.ErrorReason); EmbedBuilder embed = new EmbedBuilder(); embed.Title = "Foraging..."; embed.Description = sb.ToString(); embed.WithColor(new Color(0, 255, 0)); await context.Channel.SendMessageAsync(null, false, embed.Build()); return; } if (command.Value.Name == "forage" && pv.isForaging && !result.IsSuccess) { StringBuilder sb = new StringBuilder(); sb.AppendLine(result.ErrorReason); EmbedBuilder embed = new EmbedBuilder(); embed.Title = "Foraging..."; embed.Description = sb.ToString(); embed.WithColor(new Color(0, 255, 0)); await context.Channel.SendMessageAsync(null, false, embed.Build()); return; } // failure scenario, let's let the user know StringBuilder stringB = new StringBuilder(); stringB.AppendLine(result.ErrorReason); EmbedBuilder eb = new EmbedBuilder(); eb.Title = "Something went wrong!"; eb.Description = stringB.ToString(); eb.WithColor(new Color(0, 255, 0)); await context.Channel.SendMessageAsync(null, false, eb.Build()); StringBuilder errorSB = new StringBuilder(); errorSB.AppendLine(result.ErrorReason); errorSB.AppendLine(result.Error.ToString()); if (!Directory.Exists("D:\\Zombot Files\\Servers\\" + serverId + "\\Error Logs")) { Directory.CreateDirectory("D:\\Zombot Files\\Servers\\" + serverId + "\\Error Logs"); } using (StreamWriter file = new StreamWriter("D:\\Zombot Files\\Servers\\" + serverId + "\\Error Logs\\" + DateTimeOffset.Now.Date.Day + "-" + DateTimeOffset.Now.Date.Month + "-" + DateTimeOffset.Now.Date.Year + ".txt")) { file.Write(errorSB.ToString()); } }