PRoConEvents.MULTIbalancer.SendBattlelogRequest C# (CSharp) Method

SendBattlelogRequest() private method

private SendBattlelogRequest ( String name, String requestType, PlayerModel player ) : void
name String
requestType String
player PlayerModel
return void
        private void SendBattlelogRequest(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 " + 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/bf3/user/" + name);
            if (!fIsEnabled) return;

            if (!ok) return;

            // Extract the personaId
            MatchCollection pid = Regex.Matches(result, @"bf3/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") {
            // Extract the player tag
            Match tag = Regex.Match(result, player.PersonaId + @"/pc/[/'"">\s]+\[\s*([a-zA-Z0-9]+)\s*\]\s*" + name, RegexOptions.IgnoreCase | RegexOptions.Singleline); // Fixed #9
            //Match tag = Regex.Match(result, @"\[\s*([a-zA-Z0-9]+)\s*\]\s*" + name, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (tag.Success) {
                Hashtable data = new Hashtable();
                data["clanTag"] = tag.Groups[1].Value;
                SetTag(player, data); // sets status.State
                DebugFetch("^4Battlelog tag updated: ^b" + player.FullName);
            } else {
                // No tag
                player.TagVerified = true;
                status.State = FetchState.Succeeded;
                DebugFetch("^4Battlelog says ^b" + player.Name + "^n has no tag");
            }
            } else if (requestType == "overview") {
            status.State = FetchState.Failed;
            if (!fIsEnabled || WhichBattlelogStats == BattlelogStats.ClanTagOnly) return;
            String furl = "http://battlelog.battlefield.com/bf3/overviewPopulateStats/" + player.PersonaId + "/bf3-us-assault/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