Skip to content

Commit

Permalink
feat: Use Theater method to fetch current server for BFBC2 players
Browse files Browse the repository at this point in the history
  • Loading branch information
cetteup committed Mar 25, 2023
1 parent a1118fd commit 45d5f59
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
16 changes: 16 additions & 0 deletions Battlefield rich presence/Api.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using BattlefieldRichPresence.Structs;
using System.Text;
using Microsoft.AspNetCore.WebUtilities;
using Newtonsoft.Json;

namespace BattlefieldRichPresence
Expand Down Expand Up @@ -36,6 +38,20 @@ public static ServerInfo OldTitleServerInfo(string unescapedPlayerName, string g
return JsonConvert.DeserializeObject<ServerInfo> (responseContent);
}

public static ServerInfo GetBfbc2ServerInfo(string unescapedPlayerName)
{
var query = new Dictionary<string, string>()
{
["name"] = Uri.EscapeDataString(unescapedPlayerName),
["platform"] = "pc"
};
var uri = QueryHelpers.AddQueryString("https://api.gametools.network/bfbc2/currentserver/", query);
HttpResponseMessage httpResponse = new HttpClient().GetAsync(uri).Result;
httpResponse.EnsureSuccessStatusCode();
string responseContent = httpResponse.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<ServerInfo> (responseContent);
}

public static ServerInfo GetCurrentServer(string playerName, Resources.Statics.Game game_name)
{
var payload = new
Expand Down
16 changes: 1 addition & 15 deletions Battlefield rich presence/ChangePrensence/OlderTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,10 @@ internal class OlderTitles
{
public static void Update(DiscordRpcClient client, DateTime startTime, GameInfo gameInfo, ServerInfo serverInfo)
{
string mapName;
switch (gameInfo.Game)
{
case Statics.Game.Bf1942:
case Statics.Game.Bfvietnam:
case Statics.Game.Bf2:
case Statics.Game.Bf2142:
mapName = serverInfo.MapName;
break;
default:
mapName = serverInfo.MapLabel;
break;
}

RichPresence presence = new RichPresence
{
Details = $"{serverInfo.Name}",
State = $"{serverInfo.GetPlayerCountString()} - {mapName}",
State = $"{serverInfo.GetPlayerCountString()} - {serverInfo.MapName}",
Timestamps = new Timestamps
{
Start = startTime
Expand Down
13 changes: 11 additions & 2 deletions Battlefield rich presence/DiscordPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,17 @@ public void Update()
default:
throw new NotImplementedException();
}

ServerInfo serverInfo = Api.OldTitleServerInfo(playerName, gameInfo.ShortName.ToLower());

ServerInfo serverInfo;
switch (gameInfo.Game)
{
case Statics.Game.Bfbc2:
serverInfo = Api.GetBfbc2ServerInfo(playerName);
break;
default:
serverInfo = Api.OldTitleServerInfo(playerName, gameInfo.ShortName.ToLower());
break;
}
UpdatePresence(gameInfo, serverInfo);
}
catch (Exception)
Expand Down

0 comments on commit 45d5f59

Please sign in to comment.