PRoConEvents.MULTIbalancer.CacheResponse C# (CSharp) Method

CacheResponse() public method

public CacheResponse ( ) : void
return void
        public void CacheResponse(params String[] response)
        {
            try {
            /*
            Called from the Battlelog Cache plugin Response thread
            */
            String val = null;
            if (DebugLevel >= 8) {
            DebugFetch("CacheResponse called with " + response.Length + " parameters");
            for (int i = 0; i < response.Length; ++i) {
                DebugFetch("#" + i + ") Length: " + response[i].Length);
                val = response[i];
                if (val.Length > 100) val = val.Substring(0, 500) + " ... ";
                if (val.Contains("{")) val = val.Replace('{', '<').Replace('}', '>'); // ConsoleWrite doesn't like messages with "{" in it
                DebugFetch("#" + i + ") Value: " + val);
            }
            }

            String name = response[0]; // Player's name
            val = response[1]; // JSON string
            if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(val)) {
            DebugFetch("Invalid response from Battlelog Cache!");
            return;
            }

            Hashtable header = (Hashtable)JSON.JsonDecode(val);

            if (header == null) {
            DebugFetch("Request for ^b" + name +"^n failed!");
            return;
            }

            String result = (String)header["type"];
            double fetchTime = -1;
            Double.TryParse((String)header["fetchTime"], out fetchTime);
            double age = -1;
            Double.TryParse((String)header["age"], out age);

            PlayerModel player = GetPlayer(name);
            if (player == null) {
            DebugFetch("Unknown player ^b" + name);
            return;
            }
            String err = String.Empty;
            String requestType = String.Empty;
            DateTime since = DateTime.Now;
            FetchInfo status = null;

            if (CheckSuccess(header, out err)) {
            // verify there is data structure
            Hashtable d = null;
            if (!header.ContainsKey("data") || (d = (Hashtable)header["data"]) == null) {
                ConsoleDebug("CacheResponse header does not contain data field!");
                // FetchStatus left in Requesting state, since we can't decide which requestType this is
                return;
            }
            if (d.ContainsKey("clanTag")) {
                requestType = "clanTag";
            } else if (d.ContainsKey("overviewStats")) {
                requestType = "overview";
            }

            if (player.TagFetchStatus.RequestType == requestType) {
                status = player.TagFetchStatus;
            } else if (player.StatsFetchStatus.RequestType == requestType) {
                status = player.StatsFetchStatus;
            } else {
                ConsoleDebug("CacheResponse unknown requestType: " + requestType);
                return;
            }
            since = status.Since;

            if (fetchTime > 0) {
                DebugFetch("Request " + status.RequestType + "(^b" + name + "^n) succeeded, cache refreshed from Battlelog, took ^2" + fetchTime.ToString("F1") + " seconds");
            } else if (age > 0) {
                TimeSpan a = TimeSpan.FromSeconds(age);
                DebugFetch("Request " + status.RequestType + "(^b" + name + "^n) succeeded, cached stats used, age is " + a.ToString().Substring(0, 8));
            }

            // Apply the result to the player
            switch (requestType) {
                case "clanTag":
                    SetTag(player, d);
                    if (String.IsNullOrEmpty(player.Tag)) {
                        DebugFetch("^4Battlelog Cache says ^b" + player.Name + "^n has no tag");
                    } else {
                        DebugFetch("^4Battlelog Cache tag updated: ^b" + player.FullName);
                    }
                    break;
                case "overview": {
                    // verify there is stats structure
                    Hashtable stats = null;
                    if ((stats = (Hashtable)d["overviewStats"]) == null) {
                        status.State = FetchState.Failed;
                        DebugFetch("Request " + status.RequestType + "(^b" + name + "^n): Battlelog Cache response data does not contain overviewStats");
                        return;
                    }
                    SetStats(player, stats);
                    break;
                }
                default:
                    break;
            }
            } else {
            if (player.TagFetchStatus.State == FetchState.Requesting) {
                player.TagFetchStatus.State = FetchState.Failed;
                requestType = "clanTag";
            } else if (player.StatsFetchStatus.State == FetchState.Requesting) {
                player.StatsFetchStatus.State = FetchState.Failed;
                requestType = "overview";
            }
            DebugFetch("Request " + requestType + "(^b" + name + "^n): " + err);
            }
            DebugFetch("^2^bTIME^n took " + DateTime.Now.Subtract(since).TotalSeconds.ToString("F2") + " secs, cache lookup for ^b" + name);
            } catch (Exception e) {
            ConsoleException(e);
            }
        }
MULTIbalancer