Ejemplo n.º 1
0
        public static async void ChangeTeam(Player player, Team team, bool captain = false, IRole role = null)
        {
            var guild = _client.Guilds.ToList().Where(x => x.Name.Contains(_serverName)).First();
            var user  = guild.Users.Where(x => x.Mention == player.DiscordMention).First();

#if QUALIFIER
            if (player.Team != "-1")
            {
                var oldTeam = new Team(player.Team);
                await user.RemoveRoleAsync(guild.Roles.FirstOrDefault(x => Regex.Replace(x.Name.ToLower(), "[^a-z0-9 ]", "") == oldTeam.TeamName.ToLower()));
            }
#endif

#if !BTH
            //Add the role of the team we're being switched to
            //Note that this WILL NOT remove the role of the team the player is currently on, if there is one.
            if (role != null)
            {
                await user.AddRoleAsync(role);
            }
            else
            {
                await user.AddRoleAsync(guild.Roles.FirstOrDefault(x => Regex.Replace(x.Name.ToLower(), "[^a-z0-9 ]", "") == team.TeamName.ToLower()));
            }
#endif
            player.Team = team.TeamId;

            //Sort out existing scores
            string userId = player.UserId;
            IDictionary <SongConstruct, ScoreConstruct> playerScores = GetScoresForPlayer(userId);

            if (playerScores.Count >= 0)
            {
                playerScores.ToList().ForEach(x =>
                {
                    BeatSaver.Song songData = new BeatSaver.Song(x.Key.SongHash);
                    ExecuteCommand($"UPDATE scoreTable SET team = \'{team.TeamId}\' WHERE songHash=\'{x.Key.SongHash}\' AND userId=\'{userId}\'");
                });
            }

            if (captain)
            {
                team.Captain = player.UserId;
            }

            string teamName = team.TeamName;
            await SendToInfoChannel($"{player.DiscordMention} has been assigned {(captain ? "as the captain of" : "to")} `{((teamName == string.Empty || teamName == null )? team.TeamId: teamName)}`!");
        }
Ejemplo n.º 2
0
        public async Task LeaderboardsAsync()
        {
            if (!IsAdmin())
            {
                return;
            }

            string finalMessage = "Leaderboard:\n\n";

            if (Config.ServerFlags.HasFlag(ServerFlags.Teams))
            {
                foreach (var team in GetAllTeams())
                {
                    finalMessage += $"({team.TeamName})\n\n";

                    foreach (var song in GetAllScores(teamId: team.TeamId))
                    {
                        if (song.Scores.Count > 0) //Don't print if no one submitted scores
                        {
                            finalMessage += $"{song.Name} ({song.Scores.First().Difficulty}):\n";

                            int place = 1;
                            foreach (var score in song.Scores)
                            {
                                //Incredibly inefficient to open a song info file every time, but only the score structure is guaranteed to hold the real difficutly,
                                //seeing as auto difficulty is what would be represented in the songconstruct
                                string percentage = "???%";
                                if (!OstHelper.IsOst(song.SongHash))
                                {
                                    var maxScore = new BeatSaver.Song(song.SongHash).GetMaxScore(score.Characteristic, score.Difficulty);
                                    percentage = ((double)score.Score / maxScore).ToString("P", CultureInfo.InvariantCulture);
                                }

                                finalMessage += place + ": " + new Player(score.UserId).DiscordName + " - " + score.Score + $" ({percentage})" + (score.FullCombo ? " (Full Combo)" : "");
                                if (Config.ServerFlags.HasFlag(ServerFlags.Tokens))
                                {
                                    if (place == 1)
                                    {
                                        finalMessage += " (+3 Tokens)";
                                    }
                                    else if (place == 2)
                                    {
                                        finalMessage += " (+2 Tokens)";
                                    }
                                    else if (place == 3)
                                    {
                                        finalMessage += " (+1 Token)";
                                    }
                                }
                                finalMessage += "\n";
                                place++;
                            }
                            finalMessage += "\n";
                        }
                    }
                    finalMessage += "\n";
                }
            }
            else
            {
                List <SongConstruct> songs = GetActiveSongs(true);

                songs.ForEach(x =>
                {
                    string hash = x.SongHash;

                    if (x.Scores.Count > 0) //Don't print if no one submitted scores
                    {
                        var song      = new Song(hash, x.Difficulty, x.Characteristic);
                        finalMessage += song.SongName + ":\n";

                        int place = 1;
                        foreach (ScoreConstruct item in x.Scores)
                        {
                            //Incredibly inefficient to open a song info file every time, but only the score structure is guaranteed to hold the real difficutly,
                            //seeing as auto difficulty is what would be represented in the songconstruct
                            string percentage = "???%";
                            if (!OstHelper.IsOst(hash))
                            {
                                var maxScore = new BeatSaver.Song(hash).GetMaxScore(item.Characteristic, item.Difficulty);
                                percentage   = ((double)item.Score / maxScore).ToString("P", CultureInfo.InvariantCulture);
                            }

                            finalMessage += place + ": " + new Player(item.UserId).DiscordName + " - " + item.Score + $" ({percentage})" + (item.FullCombo ? " (Full Combo)" : "");
                            finalMessage += "\n";
                            place++;
                        }
                        finalMessage += "\n";
                    }
                });
            }

            //Deal with long messages
            if (finalMessage.Length > 2000)
            {
                for (int i = 0; finalMessage.Length > 2000; i++)
                {
                    await ReplyAsync(finalMessage.Substring(0, finalMessage.Length > 2000 ? 2000 : finalMessage.Length));

                    finalMessage = finalMessage.Substring(2000);
                }
            }
            await ReplyAsync(finalMessage);
        }
Ejemplo n.º 3
0
        public async Task AddSongAsync(string songId, [Remainder] string paramString = null)
        {
            if (IsAdmin())
            {
                //Parse the difficulty input, either as an int or a string
                LevelDifficulty parsedDifficulty = LevelDifficulty.ExpertPlus;

                string difficultyArg = ParseArgs(paramString, "difficulty");
                if (difficultyArg != null)
                {
                    //If the enum conversion doesn't succeed, try it as an int
                    if (!Enum.TryParse(difficultyArg, true, out parsedDifficulty))
                    {
                        await ReplyAsync("Could not parse difficulty parameter.\n" +
                                         "Usage: addSong [songId] [difficulty]");

                        return;
                    }
                }

                string characteristicArg = ParseArgs(paramString, "characteristic");
                characteristicArg = characteristicArg ?? "Standard";

                GameOptions   gameOptions   = GameOptions.None;
                PlayerOptions playerOptions = PlayerOptions.None;

                //Load up the GameOptions and PlayerOptions
                foreach (GameOptions o in Enum.GetValues(typeof(GameOptions)))
                {
                    if (ParseArgs(paramString, o.ToString()) == "true")
                    {
                        gameOptions = (gameOptions | o);
                    }
                }

                foreach (PlayerOptions o in Enum.GetValues(typeof(PlayerOptions)))
                {
                    if (ParseArgs(paramString, o.ToString()) == "true")
                    {
                        playerOptions = (playerOptions | o);
                    }
                }

                //Sanitize input
                if (songId.StartsWith("https://beatsaver.com/") || songId.StartsWith("https://bsaber.com/"))
                {
                    //Strip off the trailing slash if there is one
                    if (songId.EndsWith("/"))
                    {
                        songId = songId.Substring(0, songId.Length - 1);
                    }

                    //Strip off the beginning of the url to leave the id
                    songId = songId.Substring(songId.LastIndexOf("/") + 1);
                }

                if (songId.Contains("&"))
                {
                    songId = songId.Substring(0, songId.IndexOf("&"));
                }

                //Get the hash for the song
                var hash = BeatSaver.BeatSaverDownloader.GetHashFromID(songId);

                if (OstHelper.IsOst(hash))
                {
                    if (!Song.Exists(hash, parsedDifficulty, characteristicArg, true))
                    {
                        Song song = new Song(hash, parsedDifficulty, characteristicArg);
                        song.GameOptions   = (int)gameOptions;
                        song.PlayerOptions = (int)playerOptions;
                        await ReplyAsync($"Added: {OstHelper.GetOstSongNameFromLevelId(hash)} ({parsedDifficulty}) ({characteristicArg})" +
                                         $"{(gameOptions != GameOptions.None ? $" with game options: ({gameOptions.ToString()})" : "")}" +
                                         $"{(playerOptions != PlayerOptions.None ? $" with player options: ({playerOptions.ToString()})" : "!")}");
                    }
                    else
                    {
                        await ReplyAsync("Song is already active in the database");
                    }
                }
                else
                {
                    string songPath = BeatSaver.BeatSaverDownloader.DownloadSong(hash);
                    if (songPath != null)
                    {
                        BeatSaver.Song song     = new BeatSaver.Song(hash);
                        string         songName = song.SongName;

                        if (parsedDifficulty != LevelDifficulty.Auto && !song.GetLevelDifficulties(characteristicArg).Contains(parsedDifficulty))
                        {
                            LevelDifficulty nextBestDifficulty = song.GetClosestDifficultyPreferLower(parsedDifficulty);

                            if (Song.Exists(hash, nextBestDifficulty, characteristicArg))
                            {
                                await ReplyAsync($"{songName} doesn't have {parsedDifficulty}, and {nextBestDifficulty} is already in the database.\n" +
                                                 $"Song not added.");
                            }

                            else
                            {
                                var databaseSong = new Song(hash, nextBestDifficulty, characteristicArg);
                                databaseSong.GameOptions   = (int)gameOptions;
                                databaseSong.PlayerOptions = (int)playerOptions;
                                await ReplyAsync($"{songName} doesn't have {parsedDifficulty}, using {nextBestDifficulty} instead.\n" +
                                                 $"Added to the song list" +
                                                 $"{(gameOptions != GameOptions.None ? $" with game options: ({gameOptions.ToString()})" : "")}" +
                                                 $"{(playerOptions != PlayerOptions.None ? $" with player options: ({playerOptions.ToString()})" : "!")}");
                            }
                        }
                        else
                        {
                            var databaseSong = new Song(hash, parsedDifficulty, characteristicArg);
                            databaseSong.GameOptions   = (int)gameOptions;
                            databaseSong.PlayerOptions = (int)playerOptions;
                            await ReplyAsync($"{songName} ({parsedDifficulty}) ({characteristicArg}) downloaded and added to song list" +
                                             $"{(gameOptions != GameOptions.None ? $" with game options: ({gameOptions.ToString()})" : "")}" +
                                             $"{(playerOptions != PlayerOptions.None ? $" with player options: ({playerOptions.ToString()})" : "!")}");
                        }
                    }
                    else
                    {
                        await ReplyAsync("Could not download song.");
                    }
                }
            }
        }