Skip to content

Commit

Permalink
[MTGOSDK/API] Add ServerStatus class
Browse files Browse the repository at this point in the history
Create a helper class for querying MTGO server status.
  • Loading branch information
Qonfused committed Dec 27, 2023
1 parent 1f9a365 commit 809f2c8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MTGOSDK/MTGOSDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<ItemGroup>
<PackageReference Include="ImpromptuInterface"
Version="8.0.4" />
<PackageReference Include="Newtonsoft.Json"
Version="13.0.3" />
<ProjectReference Include="..\MTGOSDK.Win32\MTGOSDK.Win32.csproj" />
<ProjectReference Include="..\third_party\RemoteNET\src\RemoteNET\RemoteNET.csproj" />
<ProjectReference Include="..\third_party\RemoteNET\src\ScubaDiver.API\ScubaDiver.API.csproj" />
Expand Down
47 changes: 47 additions & 0 deletions MTGOSDK/src/API/ServerStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @file
Copyright (c) 2023, Cory Bennett. All rights reserved.
SPDX-License-Identifier: Apache-2.0
**/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

using Newtonsoft.Json.Linq;


namespace MTGOSDK.API;

/// <summary>
/// Check the status of the MTGO servers.
/// </summary>
public static class ServerStatus
{
/// <summary>
/// Check if the MTGO servers are online.
/// </summary>
public static async Task<bool> IsOnline()
{
using (HttpClient client = new HttpClient())
{
string url = "https://census.daybreakgames.com/s:dgc/get/global/game_server_status?game_code=mtgo&c:limit=1000";
using var response = await client.GetAsync(url);

if (!response.IsSuccessStatusCode)
throw new Exception("Failed to fetch server status");

using var content = response.Content;
var json = JObject.Parse(await content.ReadAsStringAsync());

if (json["returned"].ToObject<int>() == 0)
throw new Exception("No servers found");

// Check if any servers are online.
IList<string> statuses = [ "high", "medium", "low" ];
return json["game_server_status_list"].Any(s =>
statuses.Contains(s["last_reported_state"].ToObject<string>()));
}
}
}

0 comments on commit 809f2c8

Please sign in to comment.