private async Task <Func <ulong, string> > PlayInternal(string[] args, ITextChannel chan, ulong playerId) { if (args.Length == 0) { return(Sentences.InvalidGameName); } string gameName = args[0].ToLower(); Difficulty difficulty = Difficulty.Normal; bool isFull = false; bool sendImage = false; bool isCropped = false; APreload.Shadow isShaded = APreload.Shadow.None; APreload.Multiplayer isMultiplayer = APreload.Multiplayer.SoloOnly; if (args.Length > 1) { foreach (string s in args.Skip(1).Select(x => x.ToLower())) { switch (s) { case "full": isFull = true; break; case "multi": case "multiplayer": isMultiplayer = APreload.Multiplayer.MultiOnly; break; case "easy": difficulty = Difficulty.Easy; break; case "normal": case "solo": break; // These case exist so the user can precise them, but they do nothing case "crop": case "cropped": isCropped = true; break; case "shade": case "shadow": case "shaded": isShaded = APreload.Shadow.Transparency; break; case "image": sendImage = true; break; default: return(Sentences.InvalidGameArgument); } } } foreach (var game in Constants.allGames) { APreload preload = (APreload)Activator.CreateInstance(game.Item1); if (preload.ContainsName(gameName)) { if (!chan.IsNsfw && preload.IsNsfw()) { return(Modules.Base.Sentences.ChanIsNotNsfw); } if (isMultiplayer == APreload.Multiplayer.MultiOnly && preload.DoesAllowMultiplayer() == APreload.Multiplayer.SoloOnly) { return(Sentences.MultiNotAvailable); } if (isMultiplayer == APreload.Multiplayer.SoloOnly && preload.DoesAllowMultiplayer() == APreload.Multiplayer.MultiOnly) { return(Sentences.SoloNotAvailable); } if (isFull && !preload.DoesAllowFull()) { return(Sentences.FullNotAvailable); } if (sendImage && !preload.DoesAllowSendImage()) { return(Sentences.SendImageNotAvailable); } if (isCropped && !preload.DoesAllowCropped()) { return(Sentences.CropNotAvailable); } if (isCropped && preload.DoesAllowShadow() == APreload.Shadow.None) { return(Sentences.ShadowNotAvailable); } if (isShaded != APreload.Shadow.None) { isShaded = preload.DoesAllowShadow(); } try { string introMsg = ""; if (isMultiplayer == APreload.Multiplayer.MultiOnly) { introMsg += Sentences.LobbyCreation(chan.GuildId, MultiplayerLobby.lobbyTime.ToString()) + Environment.NewLine + Environment.NewLine; } introMsg += "**" + Sentences.Rules(chan.GuildId) + ":**" + Environment.NewLine + preload.GetRules(chan.GuildId, isMultiplayer == APreload.Multiplayer.MultiOnly) + Environment.NewLine; if (isMultiplayer == APreload.Multiplayer.MultiOnly) { if (preload.GetMultiplayerType() == APreload.MultiplayerType.Elimination) { introMsg += Sentences.RulesMultiElimination(chan.GuildId) + Environment.NewLine; } else { introMsg += Sentences.RulesMultiBestOf(chan.GuildId, AGame.nbMaxTry, AGame.nbQuestions) + Environment.NewLine; } } introMsg += Sentences.RulesTimer(chan.GuildId, preload.GetTimer() * (int)difficulty) + Environment.NewLine + Environment.NewLine + Sentences.RulesReset(chan.GuildId); await chan.SendMessageAsync(introMsg); AGame newGame = (AGame)Activator.CreateInstance(game.Item2, chan, new Config(preload.GetTimer(), difficulty, preload.GetGameName(), isFull, sendImage, isCropped, isShaded, isMultiplayer, preload.GetMultiplayerType()), playerId); _games.Add(newGame); if (Program.p.sendStats) { await Program.p.UpdateElement(new Tuple <string, string>[] { new Tuple <string, string>("games", preload.GetGameName()) }); } return(null); } catch (NoDictionnaryException) { return(Sentences.NoDictionnary); } } } return(Sentences.InvalidGameName); }
public async Task Score(params string[] _) { if (Context.Guild == null) { await ReplyAsync(Modules.Base.Sentences.CommandDontPm(Context.Guild)); return; } Utilities.CheckAvailability(Context.Guild, Program.Module.Game); await Program.p.DoAction(Context.User, Program.Module.Game); var scores = await Program.p.db.GetAllScores(); if (!scores.Any(x => x.Key == Context.Guild.Id.ToString())) { await ReplyAsync(Sentences.NoScore(Context.Guild)); return; } var me = scores[Context.Guild.Id.ToString()]; StringBuilder finalStr = new StringBuilder(); float finalScore = 0; bool ranked = false; int nbGuilds = scores.Count(x => x.Value.Count > 0); foreach (var game in Constants.allRankedGames) { APreload preload = (APreload)Activator.CreateInstance(game.Item1); string gameName = preload.GetGameName(); if (!me.ContainsKey(preload.GetGameName())) { finalStr.Append("**" + preload.GetGameSentence(Context.Guild) + "**:" + Environment.NewLine + Sentences.NotRanked(Context.Guild) + Environment.NewLine + Environment.NewLine); continue; } ranked = true; string[] myElems = me[gameName].Split('|'); var users = await Context.Guild.GetUsersAsync(); int myScore = int.Parse(myElems[0]); string[] contributors = myElems.Skip(1).Select(x => users.Where(y => y.Id.ToString() == x).FirstOrDefault()?.ToString() ?? "(Unknown)").ToArray(); int rankedNumber = scores.Where(x => Program.p.client.GetGuild(ulong.Parse(x.Key)) != null && x.Value.ContainsKey(gameName)).Count(); int myRanking = scores.Where(x => Program.p.client.GetGuild(ulong.Parse(x.Key)) != null && x.Value.ContainsKey(gameName) && int.Parse(x.Value[gameName].Split('|')[0]) > myScore).Count() + 1; int bestScore = scores.Where(x => x.Value.ContainsKey(gameName)).Max(x => int.Parse(x.Value[gameName].Split('|')[0])); finalStr.Append("**" + preload.GetGameSentence(Context.Guild) + "**:" + Environment.NewLine + Sentences.ScoreText(Context.Guild, myRanking, rankedNumber, myScore, bestScore) + Environment.NewLine + Sentences.ScoreContributors(Context.Guild) + " " + string.Join(", ", contributors) + Environment.NewLine + Environment.NewLine); finalScore += myScore * 100f / bestScore; } int myGlobalRanking = 1; if (ranked) { foreach (var s in scores) { int sScore = 0; foreach (var elem in s.Value) { int best = scores.Where(x => x.Value.ContainsKey(elem.Key)).Max(x => int.Parse(x.Value[elem.Key].Split('|')[0])); sScore += int.Parse(elem.Value.Split('|')[0]) * 100 / best; } if (sScore > finalScore) { myGlobalRanking++; } } } await ReplyAsync((ranked ? Sentences.GlobalRanking(Context.Guild, myGlobalRanking, nbGuilds, finalScore / Constants.allRankedGames.Length) : Sentences.NoGlobalRanking(Context.Guild)) + Environment.NewLine + Environment.NewLine + finalStr.ToString()); }
public static async Task <string> GetBestScores() { var scores = await Program.p.db.GetAllScores(); string globalRankingStr = ""; Dictionary <string, Tuple <int, bool> > globalRanking = new Dictionary <string, Tuple <int, bool> >(); foreach (var s in scores) { int sScore = 0; foreach (var elem in s.Value) { int best = scores.Where(x => x.Value.ContainsKey(elem.Key)).Max(x => int.Parse(x.Value[elem.Key].Split('|')[0])); sScore += int.Parse(elem.Value.Split('|')[0]) * 100 / best; } if (sScore > 0) { globalRanking.Add(s.Key, new Tuple <int, bool>(sScore, Program.p.db.IsAnonymized(ulong.Parse(s.Key)))); } } int i = 0; while (i < 5 && globalRanking.Count > 0) { var elem = globalRanking.First(x => x.Value.Equals(globalRanking.Values.Max())); IGuild guild = Program.p.client.GetGuild(ulong.Parse(elem.Key)); if (guild != null && guild.Name != null) { if (globalRankingStr != "") { globalRankingStr += "|"; } try { globalRankingStr += (elem.Value.Item2 ? "Anonymous" : Program.p.GetName(guild.Name)) + "|" + (elem.Value.Item1 / Constants.allRankedGames.Length); } catch (NullReferenceException) { } // No idea why this goes here i++; } globalRanking.Remove(elem.Key); } StringBuilder finalStr = new StringBuilder(); // Output format: Game1Place1Server | Game1Place1Score | Game1Place2Server..... Game1Place3Score $ Game2Place1Server | Game2Place1Score foreach (var game in Constants.allRankedGames) { APreload preload = (APreload)Activator.CreateInstance(game.Item1); string gameName = preload.GetGameName(); // Prepare array containing all games serverId/score List <Tuple <string, string> > allScores = new List <Tuple <string, string> >(); foreach (var elem in scores) { if (elem.Value.ContainsKey(gameName)) { allScores.Add(new Tuple <string, string>(elem.Key, elem.Value[gameName].Split('|')[0])); } } List <Tuple <string, int> > best = new List <Tuple <string, int> >(); string bestServer; int bestScore; List <string> alreadySaid = new List <string>(); while (best.Count < 5 && allScores.Count > alreadySaid.Count) { bestServer = null; bestScore = -1; foreach (var elem in allScores) { int score = int.Parse(elem.Item2); if (!alreadySaid.Contains(elem.Item1) && (bestServer == null || score > bestScore)) { bestServer = elem.Item1; bestScore = score; } } alreadySaid.Add(bestServer); IGuild guild = Program.p.client.GetGuild(ulong.Parse(bestServer)); if (guild != null) { var name = (Program.p.db.IsAnonymized(guild.Id) ? "Anonymous" : Program.p.client.GetGuild(ulong.Parse(bestServer)).Name); if (name != null) { best.Add(new Tuple <string, int>(Program.p.GetName(name), bestScore)); } } } finalStr.Append(string.Join("|", best.Select(x => x.Item1 + "|" + x.Item2)) + "$"); } return(globalRankingStr + "$" + finalStr); }