Skip to content

Commit

Permalink
using new API for upcoming matches, more detailed match- and playerstats
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPstr committed Feb 12, 2021
1 parent 3b529a3 commit 07734b6
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 140 deletions.
7 changes: 1 addition & 6 deletions Modules/CacheCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ public class CacheCleaner : ModuleBase<SocketCommandContext>
{
public void Cleaner(DiscordSocketClient client)
{
//matches
string matches = File.ReadAllText("./cache/matchIDs.txt");
if((matches.Split("\n").Length - 1) > 400)
{
File.WriteAllText("./cache/matchIDs.txt", matches.Substring(matches.Split("\n")[0].Length + 1));
}

//upcoming.json
string upcoming = File.ReadAllText("./cache/upcoming.json");
JArray jArr = JArray.Parse(upcoming);
Expand Down
130 changes: 107 additions & 23 deletions Modules/Upcoming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,55 @@
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace HLTVDiscordBridge.Modules
{
public class Upcoming : ModuleBase<SocketCommandContext>
{
/// <summary>
/// Gets upcoming HLTV matches and their star rating and saves them in ./cache/upcoming.json
/// </summary>
/// <returns>All upcoming matches</returns>
public async Task UpdateUpcomingMatches()
{
var URI = new Uri("https://hltv-api-steel.vercel.app/api/matches");
HttpClient http = new HttpClient();
http.BaseAddress = URI;
HttpResponseMessage httpResponse = await http.GetAsync(URI);
string httpResult = await httpResponse.Content.ReadAsStringAsync();
JArray jArr;
try { jArr = JArray.Parse(httpResult); }
catch (Newtonsoft.Json.JsonReaderException) { Console.WriteLine($"{DateTime.Now.ToString().Substring(11)}API\t API down"); return; }
JArray myJArr = new JArray();
Directory.CreateDirectory("./cache");
if (!File.Exists("./cache/upcoming.json"))
{
FileStream fs = File.Create("./cache/upcoming.json");
fs.Close();
File.WriteAllText("./cache/upcoming.json", jArr.ToString());
return;
}
else
{
myJArr = JArray.Parse(File.ReadAllText("./cache/upcoming.json"));
//Console.WriteLine(myJArr);
}

foreach (JToken jToken in jArr)
{
if (!myJArr.ToString().Contains(JObject.Parse(jToken.ToString()).GetValue("id").ToString()))
{
myJArr.Add(jToken);
/*if(myJArr.Contains(JObject.Parse(jToken.ToString()).GetValue("id").ToString()))
{
myJArr.IndexOf()
}*/
}
}
File.WriteAllText("./cache/upcoming.json", myJArr.ToString());
}
[Command("upcoming")]
public async Task GetUpcoming([Remainder] string arg = "")
{
Expand All @@ -34,44 +77,69 @@ private Embed BuildEmbed(string arg)
} else if(jArr.Count == 1)
{
JObject jObj = JObject.Parse(jArr[0].ToString());
string team1name = JObject.Parse(JArray.Parse(jObj.GetValue("teams").ToString())[0].ToString()).GetValue("name").ToString();
string team1name = JObject.Parse(JObject.Parse(jObj.GetValue("team1").ToString()).ToString()).GetValue("name").ToString();
if (team1name == "") { team1name = "TBD"; }
string team2name = JObject.Parse(JArray.Parse(jObj.GetValue("teams").ToString())[1].ToString()).GetValue("name").ToString();
string team2name = JObject.Parse(JObject.Parse(jObj.GetValue("team2").ToString()).ToString()).GetValue("name").ToString();
if (team2name == "") { team2name = "TBD"; }
string format = jObj.GetValue("map").ToString();
string format = jObj.GetValue("format").ToString();
string eventname = JObject.Parse(JObject.Parse(jObj.GetValue("event").ToString()).ToString()).GetValue("name").ToString();
if (eventname == "") { eventname = "n.A"; }
string link = jObj.GetValue("link").ToString();
DateTime time = DateTime.Parse(jObj.GetValue("time").ToString());
string link;
if (team1name != "" && team2name != "" && eventname != "")
{
link = $"https://www.hltv.org/matches/{jObj.GetValue("id")}/{team1name.Replace(' ', '-')}-vs-" +
$"{team2name.Replace(' ', '-')}-{eventname.Replace(' ', '-')}";
} else
{
link = "n.A";
}
double time = double.Parse(JObject.Parse(jObj.ToString()).GetValue("date").ToString());
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds(time);
builder.AddField("match:", $"{team1name} vs. {team2name}")
.AddField("event:", eventname)
.AddField("time:", time.ToString().Substring(0, 16), true)
.AddField("time:", dtDateTime.ToString().Substring(0, 16), true)
.AddField("format:", format, true)
.AddField("link:", $"https://hltv.org{link}");
.AddField("details:", $"[click here for more details]({link})");
} else if(jArr.Count > 1)
{
int i = 0;
foreach (JToken jTok in jArr)
{
if (i == 2) { break; }
JObject jObj = JObject.Parse(jTok.ToString());
string team1name = JObject.Parse(JArray.Parse(jObj.GetValue("teams").ToString())[0].ToString()).GetValue("name").ToString();
string team2name = JObject.Parse(JArray.Parse(jObj.GetValue("teams").ToString())[1].ToString()).GetValue("name").ToString();
string format = jObj.GetValue("map").ToString();
string team1name = JObject.Parse(JObject.Parse(jObj.GetValue("team1").ToString()).ToString()).GetValue("name").ToString();
if (team1name == "") { team1name = "TBD"; }
string team2name = JObject.Parse(JObject.Parse(jObj.GetValue("team2").ToString()).ToString()).GetValue("name").ToString();
if (team2name == "") { team2name = "TBD"; }
string format = jObj.GetValue("format").ToString();
string eventname = JObject.Parse(JObject.Parse(jObj.GetValue("event").ToString()).ToString()).GetValue("name").ToString();
string link = jObj.GetValue("link").ToString();
DateTime time = DateTime.Parse(jObj.GetValue("time").ToString());
if (eventname == "") { eventname = "n.A"; }
string link;
if (team1name != "" && team2name != "" && eventname != "")
{
link = $"https://www.hltv.org/matches/{jObj.GetValue("id")}/{team1name.Replace(' ', '-')}-vs-" +
$"{team2name.Replace(' ', '-')}-{eventname.Replace(' ', '-')}";
}
else
{
link = "n.A";
}

double time = double.Parse(JObject.Parse(jObj.ToString()).GetValue("date").ToString());
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds(time);

builder.AddField("match:", $"{team1name} vs. {team2name}", true)
.AddField("time:", time.ToString().Substring(0, 16), true)
.AddField("time:", dtDateTime.ToString().Substring(0, 16), true)
.AddField("\u200b", "\u200b", true)
.AddField("event:", eventname, true)
.AddField("format:", format, true)
.AddField("\u200b", "\u200b", true)
.AddField("details:", $"[click here for more details](https://hltv.org{link})");
.AddField("details:", $"[click here for more details]({link})");
if (i == 0) { }
i++;
}


builder.WithFooter($"and {jArr.Count - 2} more");
}
Expand All @@ -86,7 +154,10 @@ private JArray SearchUpcoming()
JArray result = JArray.Parse("[]");
foreach(JToken jTok in jArr)
{
if (DateTime.Parse(JObject.Parse(jTok.ToString()).GetValue("time").ToString()).CompareTo(DateTime.Now) != -1)
double time = double.Parse(JObject.Parse(jTok.ToString()).GetValue("date").ToString());
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds(time);
if (dtDateTime.CompareTo(DateTime.Now) != -1)
{
result.Add(jTok);
}
Expand All @@ -99,12 +170,23 @@ private JArray SearchUpcoming(string arg)
JArray result = JArray.Parse("[]");
foreach (JToken jTok in jArr)
{
string eventname = JObject.Parse(JObject.Parse(jTok.ToString()).GetValue("event").ToString()).GetValue("name").ToString().ToLower();
string team1name = JObject.Parse(JArray.Parse(JObject.Parse(jTok.ToString()).GetValue("teams").ToString())[0].ToString()).GetValue("name").ToString().ToLower();
string team2name = JObject.Parse(JArray.Parse(JObject.Parse(jTok.ToString()).GetValue("teams").ToString())[1].ToString()).GetValue("name").ToString().ToLower();
string eventname;
string team1name;
string team2name;

if (JObject.Parse(jTok.ToString()).GetValue("event") == null) { eventname = "n.A"; }
else { eventname = JObject.Parse(JObject.Parse(jTok.ToString()).GetValue("event").ToString()).GetValue("name").ToString().ToLower(); }
if (JObject.Parse(jTok.ToString()).GetValue("team1") == null) { team1name = "n.A"; }
else { team1name = JObject.Parse(JObject.Parse(jTok.ToString()).GetValue("team1").ToString()).GetValue("name").ToString().ToLower(); }
if(JObject.Parse(jTok.ToString()).GetValue("team2") == null) { team2name = "n.A"; }
else { team2name = JObject.Parse(JObject.Parse(jTok.ToString()).GetValue("team2").ToString()).GetValue("name").ToString().ToLower(); }

double time = double.Parse(JObject.Parse(jTok.ToString()).GetValue("date").ToString());
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds(time);
if (arg.ToLower() == eventname || arg.ToLower() == team1name || arg.ToLower() == team2name)
{
if (DateTime.Parse(JObject.Parse(jTok.ToString()).GetValue("time").ToString()).CompareTo(DateTime.Now) != -1) { result.Add(jTok); }
if (dtDateTime.CompareTo(DateTime.Now) != -1) { result.Add(jTok); }
}
}
return result;
Expand All @@ -115,10 +197,12 @@ private JArray SearchUpcoming(DateTime date)
JArray result = JArray.Parse("[]");
foreach (JToken jTok in jArr)
{
string time = JObject.Parse(jTok.ToString()).GetValue("time").ToString().Substring(0,10);
if(time.Substring(0,10) == date.ToString().Substring(0, 10))
double time = double.Parse(JObject.Parse(jTok.ToString()).GetValue("date").ToString());
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds(time);
if (dtDateTime.ToString().Substring(0, 10) == date.ToString().Substring(0, 10))
{
if (DateTime.Parse(JObject.Parse(jTok.ToString()).GetValue("time").ToString()).CompareTo(DateTime.Now) != -1) { result.Add(jTok); }
if (dtDateTime.CompareTo(DateTime.Now) != -1) { result.Add(jTok); }
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion Modules/developer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task ServerCount()
foreach (SocketGuild guild in Context.Client.Guilds)
{
totalUser += guild.Users.Count;
servernames += $"{guild.Name}\n";
servernames += $"{guild.Name} | {guild.Users.Count}\n";

}
await ReplyAsync($"{Context.Client.Guilds.Count} server and {totalUser} user \n\n{servernames}");
Expand Down
Loading

0 comments on commit 07734b6

Please sign in to comment.