예제 #1
0
        /// <summary>
        /// Parses the match JSON string from the API
        /// </summary>
        /// <param name="JSONstring">The match JSON string to parse</param>
        /// <returns></returns>
        public APIMatch MatchPhraser(string JSONstring)
        {
            var      jsonmatch = Phraser.FromJson(JSONstring);
            APIMatch Match     = new APIMatch
            {
                BaseJSON = JSONstring
            };

            foreach (var item in jsonmatch.Included)
            {
                if (item.Type == DatumType.Participant)
                {
                    if (item.Attributes.Stats != null)
                    {
                        APIPlayer player = new APIPlayer();
                        player = item.Attributes.Stats;
                        Match.PlayerList.Add(player);
                    }
                }
                if (item.Type == DatumType.Roster)
                {
                    if (item.Relationships.Participants.Data != null)
                    {
                        APITeam team = new APITeam();
                        foreach (var teamamte in item.Relationships.Participants.Data)
                        {
                            team.TeammateIDList.Add(teamamte.Id);
                        }
                        team.TeamID = (int)item.Attributes.Stats.TeamId;
                        team.rank   = (int)item.Attributes.Stats.Rank;
                        team.Won    = false;
                        if (item.Attributes.Won == Won.True)
                        {
                            team.Won = true;
                        }
                        Match.TeamList.Add(team);
                    }
                }
                if (item.Type == DatumType.Asset)
                {
                    if (item.Attributes.Url != null)
                    {
                        if (item.Attributes.Name == "telemetry")
                        {
                            Match.TelemetryURL = item.Attributes.Url;
                        }
                    }
                }
            }
            return(Match);
        }
예제 #2
0
        /// <summary>
        /// Requests a single match from the PUBG Developer API
        /// </summary>
        /// <param name="APIKey">The API Key to use during the request</param>
        /// <param name="PlatformRegion">The platform-region of the request (xbox-na, pc-oc, etc)</param>
        /// <param name="MatchID">The Match ID of the map</param>
        /// <returns>If null, the request failed</returns>
        public APIRequest RequestSingleMatch(string APIKey, string PlatformRegion, string MatchID)
        {
            APIStatus  status     = new APIStatus();
            APIRequest APIRequest = new APIRequest();

            if (status.bIsOnline)
            {
                try
                {
                    string APIURL         = "https://api.playbattlegrounds.com/shards/" + PlatformRegion + "/matches/" + MatchID;
                    var    webRequest     = WebRequest.Create(APIURL);
                    var    HTTPAPIRequest = (HttpWebRequest)webRequest;
                    HTTPAPIRequest.PreAuthenticate = true;
                    HTTPAPIRequest.Headers.Add("Authorization", "Bearer " + APIKey);
                    HTTPAPIRequest.Headers.Add("Access-Control-Allow-Origins", "*");
                    HTTPAPIRequest.Headers.Add("Access-Control-Expose-Headers", "Content-Length");
                    HTTPAPIRequest.Accept = "application/json";
                    using (var APIResponse = HTTPAPIRequest.GetResponse())
                    {
                        using (var responseStream = APIResponse.GetResponseStream())
                        {
                            var myStreamReader = new StreamReader(responseStream, Encoding.Default);

                            APIRequest.JSONString = myStreamReader.ReadToEnd();
                            APIRequest.Match      = MatchPhraser(APIRequest.JSONString);
                            using (WebClient client = new WebClient())
                            {
                                APIRequest.Telemetry = TelemetryPhraser(client.DownloadString(APIRequest.Match.TelemetryURL));
                            }
                            return(APIRequest);
                        }
                    }
                }
                catch (WebException e)
                {
                    APIRequest = new APIRequest
                    {
                        exception = e
                    };
                    return(APIRequest);
                }
            }
            return(APIRequest);
        }