コード例 #1
0
        public static string GetAoeString(string field, int id)
        {
            AoeSettings aoeSettings = AoeSettings.Instance;

            if (aoeSettings.AoeStrings == null)
            {
                aoeSettings.AoeStrings = new Dictionary <string, Dictionary <int, string> >();

                string  url      = $"https://aoe2.net/api/strings?game=aoe2de&language={aoeSettings.AoeLanguage}";
                JObject response = JObject.Parse(GetWebsiteAsString(url));
                foreach (string name in response.Properties().Select(p => p.Name).ToList())
                {
                    if (name != "language")
                    {
                        JArray fieldObj = (JArray)response.GetValue(name);
                        aoeSettings.AoeStrings.Add(name, new Dictionary <int, string>());
                        foreach (JToken entryToken in fieldObj.Children())
                        {
                            JObject entryObj   = (JObject)entryToken;
                            string  entryId    = entryObj.GetValue("id").ToObject <string>();
                            string  entryValue = entryObj.GetValue("string").ToObject <string>();
                            aoeSettings.AoeStrings[name].Add(int.Parse(entryId), entryValue);
                        }
                    }
                }

                aoeSettings.DelayedSave();
            }

            return(aoeSettings.AoeStrings[field][id]);
        }
コード例 #2
0
        public override void HandleCommand(NotifyTextMessageEvent evt, string command, List <string> parameters, Action <string> messageCallback)
        {
            lock (LockObject) {
                if (IsBusy)
                {
                    messageCallback.Invoke(ColorCoder.ErrorBright("The fetcher is busy, try again when the current request has completed..."));
                    return;
                }
                IsBusy = true;
            }

            Elos = Parent.EloSettings;
            if (Elos.LastUpdated + Elos.UpdateFrequency < DateTime.Now)
            {
                messageCallback.Invoke($"Leaderboards were outdated (older than 1 month), started refreshing cache. This could take a few minutes...");

                Parent.LogMessage("[*] Starting fetch AoE2DE leaderboards...");
                foreach (string mode in LeaderboardIds.Keys)
                {
                    FetchClientList(mode, messageCallback);
                    Parent.LogMessage($"[*] {mode} done");
                }
                Parent.LogMessage("[*] AoE2DE leaderboards fetch done");

                //Implicitly saves the elos to the hard drive
                Elos.LastUpdated = DateTime.Now;
            }


            //HttpClient client = new HttpClient();
            SelectedLeaderboard = SelectedLeaderboard ?? ModeTgName;
            int fromElo, toElo;

            if (!string.IsNullOrEmpty(SelectedLevel))
            {
                (fromElo, toElo) = EloRanges[SelectedLeaderboard][SelectedLevel];
                if (fromElo == -1)
                {
                    fromElo = Elos.AllPlayersElos[SelectedLeaderboard].Last().Item1;
                }
                if (toElo == -1)
                {
                    toElo = Elos.AllPlayersElos[SelectedLeaderboard].First().Item1;
                }
            }
            else if (InputFromElo != uint.MaxValue)
            {
                if (InputFromElo > InputToElo)
                {
                    uint tempElo = InputFromElo;
                    InputFromElo = InputToElo;
                    InputToElo   = tempElo;
                }

                fromElo = Math.Max((int)InputFromElo, Elos.AllPlayersElos[SelectedLeaderboard].Last().Item1);
                toElo   = Math.Min((int)InputToElo, Elos.AllPlayersElos[SelectedLeaderboard].First().Item1);
            }
            else
            {
                fromElo = EloRanges[SelectedLeaderboard]["lel"].Item1;
                toElo   = EloRanges[SelectedLeaderboard]["good"].Item2;
            }

            Random r      = new Random();
            int    offset = r.Next(1, 10);

            int lowerRankIndex = int.MaxValue, upperRankIndex = int.MinValue; //indices inclusive bounds
            List <Tuple <int, string> > leaderboard = Elos.AllPlayersElos[SelectedLeaderboard];

            for (int i = 0; i < leaderboard.Count; i++)
            {
                Tuple <int, string> position = leaderboard[i];
                if (upperRankIndex == int.MinValue && position.Item1 <= toElo)
                {
                    upperRankIndex = i;
                }
                if (lowerRankIndex == int.MaxValue && position.Item1 < fromElo)
                {
                    lowerRankIndex = i - 1;
                }
            }


            bool   hasFoundMatch = false;
            string downloadLink  = "";

            while (!hasFoundMatch)
            {
                int    selectedIndex = r.Next(upperRankIndex, lowerRankIndex + 1);
                string steamId       = Elos.AllPlayersElos[SelectedLeaderboard][selectedIndex].Item2;

                string fetchUrl = $"https://aoe2.net/api/player/matches?game=aoe2de&steam_id={steamId}&count=10&start=1";

                HttpClient          client   = new HttpClient();
                HttpResponseMessage response = client.GetAsync(fetchUrl).Result;
                string content = response.Content.ReadAsStringAsync().Result;

                JArray  matches    = JArray.Parse(content);
                JObject foundMatch = null;

                foreach (JToken matchToken in matches.Children())
                {
                    JObject match       = (JObject)matchToken;
                    bool    isRanked    = match.GetValue("ranked").ToObject <bool>();
                    int     playerCount = match.GetValue("num_players").ToObject <int>();

                    if (!isRanked)
                    {
                        Parent.LogMessage("Game was not ranked, skipping...");
                        continue;
                    }

                    if ((SelectedLeaderboard == Mode1v1Name && playerCount == 2) || (SelectedLeaderboard == ModeTgName && playerCount > 2))
                    {
                        foundMatch = match;
                    }
                    else
                    {
                        Parent.LogMessage($"Game was not of leaderboard '{SelectedLeaderboard}', skipping...");
                    }
                }

                if (foundMatch == null)
                {
                    Parent.LogMessage($"Randomly selected player did not play a ranked '{SelectedLeaderboard}' game in the last 10 games");
                    continue;
                }

                string  matchId     = foundMatch.GetValue("match_id").ToObject <string>();
                JArray  players     = foundMatch.GetValue("players").ToObject <JArray>();
                JObject firstPlayer = (JObject)players[0];

                long profileId = firstPlayer.GetValue("profile_id").ToObject <long>();
                downloadLink  = $"https://aoe.ms/replay/?gameId={matchId}&profileId={profileId}";
                hasFoundMatch = true;
            }

            messageCallback.Invoke($"Here is your {ColorCoder.Bold(SelectedLeaderboard.ToUpper())} replay of elo range ({fromElo}-{toElo}): [url={downloadLink}]Download[/url]");

            lock (LockObject) {
                IsBusy = false;
            }
        }