PRoConEvents.MULTIbalancer.SendBattlelogRequestBF4 C# (CSharp) Method

SendBattlelogRequestBF4() private method

private SendBattlelogRequestBF4 ( String name, String requestType, PlayerModel player ) : void
name String
requestType String
player PlayerModel
return void
        private void SendBattlelogRequestBF4(String name, String requestType, PlayerModel player)
        {
            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)");

            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/bf4/user/" + name);
            if (!fIsEnabled) return;

            if (!ok) return;

            // Extract the personaId
            MatchCollection pid = Regex.Matches(result, @"bf4/soldier/" + name + @"/stats/(\d+)(['""]|/\s*['""]|/[^/'""]+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            foreach (Match match in pid) {
                if (match.Success && !Regex.Match(match.Groups[2].Value.Trim(), @"(ps3|xbox)", RegexOptions.IgnoreCase).Success) {
                    player.PersonaId = match.Groups[1].Value.Trim();
                    break;
                }
            }

            if (String.IsNullOrEmpty(player.PersonaId)) {
                DebugFetch("Request for ^b" + name +"^n failed, could not find persona-id!");
                status.State = FetchState.Failed;
                return;
            }
            }

            if (requestType == "clanTag") {
            // Get the stats page
            bool ok = false;
            status.State = FetchState.Failed;
            if (!fIsEnabled) return;
            String bf4furl = "http://battlelog.battlefield.com/bf4/warsawoverviewpopulate/" + player.PersonaId + "/1/";
            ok = FetchWebPage(ref result, bf4furl);
            if (!fIsEnabled) return;
            if (!ok) return;

            // Get tag from json
            Hashtable jsonBF4 = (Hashtable)JSON.JsonDecode(result);

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

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

            // verify there is viewedPersonaInfo structure, okay if null!
            Hashtable info = null;
            if (!data.ContainsKey("viewedPersonaInfo") || (info = (Hashtable)data["viewedPersonaInfo"]) == null) {
                if (DebugLevel >= 7) DebugFetch("Request BF4" + status.RequestType + "(^b" + name + "^n): JSON response data does not contain viewedPersonaInfo");
                // No tag
                player.Tag = String.Empty;
                player.TagVerified = true;
                status.State = FetchState.Succeeded;
                DebugFetch("^4Battlelog says ^b" + player.Name + "^n has no BF4 tag (no viewedPersonaInfo)");
                return;
            }

            // Extract the player tag
            String bf4Tag = String.Empty;
            if (!info.ContainsKey("tag") || String.IsNullOrEmpty(bf4Tag = (String)info["tag"])) {
                // No tag
                player.Tag = String.Empty;
                player.TagVerified = true;
                status.State = FetchState.Succeeded;
                DebugFetch("^4Battlelog says ^b" + player.Name + "^n has no BF4 tag");
            } else {
                Hashtable tmp = new Hashtable();
                tmp["clanTag"] = bf4Tag;
                SetTag(player, tmp); // sets status.State
                DebugFetch("^4Battlelog BF4 tag updated: ^b^1" + player.FullName);
            }
            } else if (requestType == "overview") {
            //DebugFetch("Stats fetch not supported for BF4 yet: " + player.Name);
            status.State = FetchState.Failed;
            if (!fIsEnabled || WhichBattlelogStats == BattlelogStats.ClanTagOnly) return;
            String furl = "http://battlelog.battlefield.com/bf4/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("overviewStats") || (stats = (Hashtable)data["overviewStats"]) == 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