Untitled
unknown
plain_text
3 years ago
4.3 kB
7
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.IO;
public class OpenDotA : MonoBehaviour
{
[SerializeField] string API_KEY = "05c53cc6-ef57-4e8c-a0aa-09df6f46830d";
[SerializeField] long match_id;
[SerializeField] long team_id;
[System.Serializable]
public class ProMatch
{
public long match_id;
public int duration;
public int start_time;
public int radiant_team_id;
public string radiant_name;
public int dire_team_id;
public string dire_name;
public int leagueid;
public string league_name;
public int series_id;
public int series_type;
public int radiant_score;
public int dire_score;
public bool radiant_win;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
List<ProMatch> matches = GetMatchData();
Debug.Log(matches.Count + " total pro matches.");
FilterSpecificLeagueOnly(ref matches, 14268);
Debug.Log(matches.Count + " total games are from The International 2022.");
string team_name = FilterSpecificTeamOnly(ref matches, team_id);
Debug.Log(matches.Count + " total games are from Team " + team_name + ".");
}
}
public List<ProMatch> GetMatchData()
{
List<ProMatch> matchData;
// access
{
string requestid = "https://api.opendota.com/api/proMatches/" + "?api_key=" + API_KEY;
Debug.Log(requestid);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestid);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd();
matchData = FromJson<ProMatch>(json);
}
return matchData;
}
public void FilterSpecificLeagueOnly(ref List<ProMatch> matches, long league_id)
{
for (int i = 0; i < matches.Count; i++)
{
if(matches[i].leagueid != league_id)
{
matches.RemoveAt(i);
i--; //decrement
}
}
}
string FilterSpecificTeamOnly(ref List<ProMatch> matches, long team_id)
{
string teamName = "Unknown";
bool foundTeamName = false;
for (int i = 0; i < matches.Count; i++)
{
if (matches[i].radiant_team_id != team_id && matches[i].dire_team_id != team_id)
{
matches.RemoveAt(i);
i--; //decrement
}
else if (!foundTeamName)
{
if (matches[i].radiant_team_id == team_id)
{
teamName = matches[i].radiant_name;
}
else
{
teamName = matches[i].dire_name;
}
foundTeamName = true;
}
}
return teamName;
}
public ProMatch GetSpecificMatchID(List<ProMatch> matches, long matchid)
{
ProMatch data = new ProMatch();
// search for specific match id
for (int i = 0; i < matches.Count; i++)
{
if (matches[i].match_id == matchid)
{
data = matches[i];
Debug.Log("match id " + matchid + " found.");
break;
}
}
return data;
}
public static List<T> FromJson<T>(string json)
{
if (json.StartsWith("[", System.StringComparison.Ordinal))
{
json = "{\"Items\":" + json + "}";
}
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
List<T> rtn = new List<T>(wrapper.Items);
return rtn;
}
[System.Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}
Editor is loading...