PRoConEvents.MULTIbalancer.SendBattlelogRequestBFH C# (CSharp) Method

SendBattlelogRequestBFH() private method

private SendBattlelogRequestBFH ( String name, String requestType, PlayerModel player ) : void
name String
requestType String
player PlayerModel
return void
        private void SendBattlelogRequestBFH(String name, String requestType, PlayerModel player)
        {
            // TBD
            try {
            String result = String.Empty;
            String err = String.Empty;

            if (player == null) player = GetPlayer(name);
            if (player == null) return;

            FetchInfo status = (requestType == "clanTag") ? player.TagFetchStatus : player.StatsFetchStatus;
            status.State = FetchState.Requesting;
            status.Since = DateTime.Now;
            status.RequestType = requestType;
            DebugFetch("Fetching from Battlelog BF4 " + requestType + "(^b" + name + "^n)");

            /*
            using (var client = new WebClient())
            {
            try
            {
            //Get persona
            DoBattlelogWait();
            String userResponse = client.DownloadString("http://battlelog.battlefield.com/bfh/user/" + aPlayer.player_name + "?nocacherandom=" + Environment.TickCount);
            Match pid = Regex.Match(userResponse, @"agent\/" + aPlayer.player_name + @"\/stats\/(\d+)");
            if (!pid.Success)
            {
            Log.Warn("Could not find BFHL persona ID for " + aPlayer.player_name);
            return;
            }
            aPlayer.player_personaID = pid.Groups[1].Value.Trim();
            Log.Debug("Persona ID fetched for " + aPlayer.player_name + ":" + aPlayer.player_personaID, 4);
            //Get tag
            DoBattlelogWait();
            String soldierResponse = client.DownloadString("http://battlelog.battlefield.com/bfh/agent/" + aPlayer.player_name + "/stats/" + aPlayer.player_personaID + "/pc/" + "?nocacherandom=" + Environment.TickCount);
            Match tag = Regex.Match(soldierResponse, @"\[\s*([a-zA-Z0-9]+)\s*\]\s*</span>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (!tag.Success || String.IsNullOrEmpty(tag.Groups[1].Value.Trim()))
            {
            Log.Debug("Could not find BFHL clan tag for " + aPlayer.player_name, 4);
            }
            else
            {
            aPlayer.player_clanTag = tag.Groups[1].Value.Trim();
            Log.Debug("Clan tag [" + aPlayer.player_clanTag + "] found for " + aPlayer.player_name, 4);
            }
            }
            catch (Exception e)
            {
            Log.Exception("Error fetching BFHL player info", e);
            }
            }
            */

            if (String.IsNullOrEmpty(player.PersonaId)) {
            // Get the main page
            bool ok = false;
            status.State = FetchState.Failed;
            if (!fIsEnabled) return;
            ok = FetchWebPage(ref result, "http://battlelog.battlefield.com/bfh/user/" + name + "?nocacherandom=" + Environment.TickCount);
            if (!fIsEnabled) return;
            if (!ok) return;

            // Extract the personaId
            Match pid = Regex.Match(result, @"agent\/" + name + @"\/stats\/(\d+)");
            if (!pid.Success)
            {
                DebugFetch("Request for ^b" + name +"^n failed, could not find persona-id!");
                status.State = FetchState.Failed;
                return;
            }
            player.PersonaId = pid.Groups[1].Value.Trim();
            DebugFetch("Persona ID fetched for " + name + ":" + player.PersonaId);
            }

            if (requestType == "clanTag") {
            // Get the stats page
            bool ok = false;
            status.State = FetchState.Failed;
            if (!fIsEnabled) return;
            String bfhfurl = "http://battlelog.battlefield.com/bfh/agent/" + name + "/stats/" + player.PersonaId + "/pc/" + "?nocacherandom=" + Environment.TickCount;
            ok = FetchWebPage(ref result, bfhfurl);
            if (!fIsEnabled) return;
            if (!ok) return;

            // Extract the player tag
            String bfhTag = String.Empty;
            Match tag = Regex.Match(result, @"\[\s*([a-zA-Z0-9]+)\s*\]\s*</span>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            if (tag.Success) {
                bfhTag = tag.Groups[1].Value.Trim();
            }
            if (String.IsNullOrEmpty(bfhTag)) {
                // No tag
                player.Tag = String.Empty;
                player.TagVerified = true;
                status.State = FetchState.Succeeded;
                DebugFetch("^4Battlelog says ^b" + player.Name + "^n has no BFH tag");
            } else {
                Hashtable tmp = new Hashtable();
                tmp["clanTag"] = bfhTag;
                SetTag(player, tmp); // sets status.State
                DebugFetch("^4Battlelog BFH tag updated: ^b^1" + player.FullName);
            }
            } else if (requestType == "overview") {
            status.State = FetchState.Failed;
            if (!fIsEnabled || WhichBattlelogStats == BattlelogStats.ClanTagOnly) return;
            String furl = "http://battlelog.battlefield.com/bfh/warsawoverviewpopulate/" + player.PersonaId + "/1/";
            if (FetchWebPage(ref result, furl)) {
                if (!fIsEnabled) return;

                Hashtable json = (Hashtable)JSON.JsonDecode(result);

                // verify we got a success message
                if (!CheckSuccess(json, out err)) {
                    DebugFetch("Request " + status.RequestType + "(^b" + name + "^n): " + err);
                    return;
                }

                // verify there is data structure
                Hashtable data = null;
                if (!json.ContainsKey("data") || (data = (Hashtable)json["data"]) == null) {
                    DebugFetch("Request " + status.RequestType + "(^b" + name + "^n): JSON response does not contain a data field (^4" + furl + "^0)");
                    return;
                }

                // verify there is stats structure
                Hashtable stats = null;
                if (!data.ContainsKey("generalStats") || (stats = (Hashtable)data["generalStats"]) == null) {
                    DebugFetch("Request " + status.RequestType + "(^b" + name + "^n): JSON response data does not contain overviewStats (^4" + furl + "^0)");
                    return;
                }

                // extract the fields from the stats
                SetStats(player, stats); // sets status.State
            }
            }
            } catch (Exception e) {
            ConsoleException(e);
            }
        }
MULTIbalancer