예제 #1
0
        private static string Sc2RaceToString(Sc2Race race)
        {
            switch (race)
            {
            case Sc2Race.Terran:
                return("T");

            case Sc2Race.Zerg:
                return("Z");

            case Sc2Race.Protoss:
                return("P");

            case Sc2Race.Random:
                return("R");
            }
            return(string.Empty);
        }
        public async Task UpdateCurrentGameAsync()
        {
            var newGameData = await Program.Sc2ClientHelper.FetchCurrentGameAsync();

            bool isInProgressChanged = CurrentGame != null &&
                                       CurrentGame.isInProgress != newGameData?.isInProgress;

            CurrentGame = newGameData;

            if (isInProgressChanged && CurrentGame != null && !CurrentGame.isInProgress)
            {
                OnGameFinished(CurrentGame);
            }

            // This MMR value is used in case multiple players have the same
            // display name. Then we choose MMR of the player who has MMR
            // the closest to this value
            // Usually we set this to the local player's MMR value
            long expectedMmr = 4500;

            for (int playerIndex = 0; playerIndex < 4; ++playerIndex)
            {
                var playerInfo = GetPlayerInfo(playerIndex);
                if (playerInfo == null || playerInfo.IsComputer)
                {
                    continue;
                }

                Sc2Race playerRace = GetPlayerRace(playerIndex);

                // Do not perform lookup too often as it is slow operation
                var key = MakeMmrDictionaryKey(playerInfo.Name, playerRace);
                if (Program.FindProfile(playerInfo.Name) == null &&
                    playerMmrs_.TryGetValue(key, out PlayerMmr cachedMmr) &&
                    cachedMmr.timestamp.AddMinutes(5.0) < DateTime.Now)
                {
                    continue;
                }

                LadderManager.PlayerStats playerStats =
                    await LadderManager.FetchPlayerStatsAsync(playerInfo.Name, playerRace, expectedMmr);

                if (playerStats != null)
                {
                    if (playerIndex == 0)
                    {
                        expectedMmr = playerStats.Rating;
                    }

                    if (playerMmrs_.TryGetValue(key, out PlayerMmr playerMmr))
                    {
                        playerMmr.currentMmr = playerStats.Rating;
                        playerMmr.timestamp  = DateTime.Now;
                    }
                    else
                    {
                        playerMmrs_.Add(key, new PlayerMmr()
                        {
                            currentMmr = playerStats.Rating,
                            initialMmr = playerStats.Rating
                        });
                    }
                }
            }
        }
        public static async Task <PlayerStats> FetchPlayerStatsAsync(string displayName, Sc2Race race,
                                                                     long expectedMmr)
        {
            // Check if we know this player and have her full profile path
            var profile = Program.FindProfile(displayName);

            if (profile != null)
            {
                Program.RecentSc2Region = profile.RegionCode;
                return(await NetworkHelper.FetchAsync <PlayerStats>(
                           Program.ServerUri + $"/data/{profile.RegionCode}/{profile.Id}/{profile.Realm}/"
                           + $"{profile.DisplayName}/mmr/{race}"));
            }
            // Try to find player using only her display name
            // Look up in the most recent used sc2 region
            var playersList = await NetworkHelper.FetchAsync <PlayerStatsList>(
                Program.ServerUri + $"/data/{Program.RecentSc2Region}/lookup/{displayName}/{race}");

            return(playersList?.Results?.OrderBy(x => Math.Abs(x.Rating - expectedMmr)).FirstOrDefault());
        }
 private static string MakeMmrDictionaryKey(string playerName, Sc2Race playerRace)
 {
     return(playerName + "@" + playerRace);
 }