diff --git a/PayCheck3ServerApp/Program.cs b/PayCheck3ServerApp/Program.cs index fc0c985..1b26608 100644 --- a/PayCheck3ServerApp/Program.cs +++ b/PayCheck3ServerApp/Program.cs @@ -1,4 +1,5 @@ -using PayCheckServerLib; +using Newtonsoft.Json; +using PayCheckServerLib; namespace PayCheck3ServerApp { @@ -7,9 +8,20 @@ internal class Program static void Main(string[] args) { Console.WriteLine("Starting Server(s)!"); + ServerManager.UpdateFinished += ServerManager_UpdateFinished; + ServerManager.Pre(); + } + + private static void ServerManager_UpdateFinished(object? sender, bool e) + { ServerManager.Start(); - Console.ReadLine(); + Console.WriteLine("Enter q to quit."); + string stop = ""; + while (stop != "q") + { + stop = Console.ReadLine(); + }; ServerManager.Stop(); - } + } } } \ No newline at end of file diff --git a/PayCheckServerLib/Jsons/AttribError.cs b/PayCheckServerLib/Jsons/AttribError.cs index 0f07b2f..a6e20d7 100644 --- a/PayCheckServerLib/Jsons/AttribError.cs +++ b/PayCheckServerLib/Jsons/AttribError.cs @@ -22,6 +22,12 @@ public class AttribError public partial class AttribSuccess { + [JsonProperty("createdAt")] + public string createdAt { get; set; } = "0001-01-01T00:00:00Z"; + + [JsonProperty("deletedAt")] + public string deletedAt { get; set; } = "0001-01-01T00:00:00Z"; + [JsonProperty("crossplayEnabled")] public bool CrossplayEnabled { get; set; } diff --git a/PayCheckServerLib/Jsons/Chats.cs b/PayCheckServerLib/Jsons/Chats.cs index ebca62a..9bd2a3d 100644 --- a/PayCheckServerLib/Jsons/Chats.cs +++ b/PayCheckServerLib/Jsons/Chats.cs @@ -24,7 +24,7 @@ public class actionQueryTopic : ChatBase public class actionQueryTopicRSP : ChatBase { - [JsonProperty("params")] + [JsonProperty("result")] public actionQueryTopicRSPResult Params { get; set; } } diff --git a/PayCheckServerLib/Jsons/DataPaging.cs b/PayCheckServerLib/Jsons/DataPaging.cs index 3e4fff2..45a7c87 100644 --- a/PayCheckServerLib/Jsons/DataPaging.cs +++ b/PayCheckServerLib/Jsons/DataPaging.cs @@ -8,9 +8,10 @@ public class DataPaging where T : class public List Data { get; set; } [JsonProperty("paging")] - public Paging Paging { get; set; } + public PagingClass Paging { get; set; } } - public partial class Paging + + public class PagingClass { [JsonProperty("first")] public string First { get; set; } diff --git a/PayCheckServerLib/Jsons/EntitlementsJson.cs b/PayCheckServerLib/Jsons/EntitlementsJson.cs index 3ee8f5a..16c9bc2 100644 --- a/PayCheckServerLib/Jsons/EntitlementsJson.cs +++ b/PayCheckServerLib/Jsons/EntitlementsJson.cs @@ -47,12 +47,12 @@ public class EntitlementsData public string UpdatedAt { get; set; } [JsonProperty("features", NullValueHandling = NullValueHandling.Ignore)] - public object[] Features { get; set; } + public List Features { get; set; } [JsonProperty("useCount", NullValueHandling = NullValueHandling.Ignore)] - public long? UseCount { get; set; } + public long UseCount { get; set; } [JsonProperty("stackable", NullValueHandling = NullValueHandling.Ignore)] - public bool? Stackable { get; set; } + public bool Stackable { get; set; } } } diff --git a/PayCheckServerLib/Responses/Challenge.cs b/PayCheckServerLib/Responses/Challenge.cs index ac6adf1..bfada87 100644 --- a/PayCheckServerLib/Responses/Challenge.cs +++ b/PayCheckServerLib/Responses/Challenge.cs @@ -1,6 +1,7 @@ using NetCoreServer; using Newtonsoft.Json; using PayCheckServerLib.Jsons; +using System.Linq; namespace PayCheckServerLib.Responses { @@ -16,7 +17,7 @@ public static bool ChallengeEligibility(HttpRequest _, PC3Server.PC3Session sess } [HTTP("GET", "/challenge/v1/public/namespaces/pd3/users/me/records?limit=2147483647&offset=0")] - public static bool ChallengeRecords(HttpRequest _, PC3Server.PC3Session session) + public static bool ChallengeRecordsAll(HttpRequest _, PC3Server.PC3Session session) { var auth = session.Headers["authorization"].Replace("Bearer ", ""); var token = TokenHelper.ReadToken(auth); @@ -31,5 +32,23 @@ public static bool ChallengeRecords(HttpRequest _, PC3Server.PC3Session session) session.SendResponse(creator.GetResponse()); return true; } + + [HTTP("GET", "/challenge/v1/public/namespaces/pd3/users/me/records?limit={limit}&offset={offset}")] + public static bool ChallengeRecordsSplit(HttpRequest _, PC3Server.PC3Session session) + { + var auth = session.Headers["authorization"].Replace("Bearer ", ""); + var token = TokenHelper.ReadToken(auth); + ResponseCreator creator = new(); + var challenges = JsonConvert.DeserializeObject>(File.ReadAllText("Files/ChallengeRecords.json")) ?? throw new Exception("ChallengeRecords is null!"); + challenges.Data.Skip(int.Parse(session.HttpParam["offset"])).Take(int.Parse(session.HttpParam["limit"])); + foreach (var item in challenges.Data) + { + item.UserId = token.UserId; + } + + creator.SetBody(JsonConvert.SerializeObject(challenges)); + session.SendResponse(creator.GetResponse()); + return true; + } } } diff --git a/PayCheckServerLib/Responses/Entitlements.cs b/PayCheckServerLib/Responses/Entitlements.cs index f486b0f..f117b02 100644 --- a/PayCheckServerLib/Responses/Entitlements.cs +++ b/PayCheckServerLib/Responses/Entitlements.cs @@ -9,22 +9,36 @@ public class Entitlements [HTTP("GET", "/platform/public/namespaces/pd3/users/{userId}/entitlements?limit={limit}")] public static bool GetUserEntitlements(HttpRequest _, PC3Server.PC3Session session) { - var responsecreator = new ResponseCreator(); - var entitlements = JsonConvert.DeserializeObject>(File.ReadAllText("./Files/Entitlements.json")) ?? throw new Exception("Entitlements is null!"); - var newentitlements = new List(); - foreach (var entitlement in entitlements.Data) + try { - entitlement.UserId = session.HttpParam["userId"]; - newentitlements.Add(entitlement); + var responsecreator = new ResponseCreator(); + var entitlements = JsonConvert.DeserializeObject>(File.ReadAllText("./Files/Entitlements.json")) ?? throw new Exception("Entitlements is null!"); + var newentitlements = new List(); + foreach (var entitlement in entitlements.Data) + { + entitlement.UserId = session.HttpParam["userId"]; + newentitlements.Add(entitlement); + } + DataPaging payload = new() + { + Data = newentitlements, + Paging = new() + { + First = "", + Last = "", + Next = "", + Previous = "", + } + }; + responsecreator.SetBody(JsonConvert.SerializeObject(payload)); + session.SendResponse(responsecreator.GetResponse()); + return true; } - DataPaging payload = new() + catch (Exception ex) { - Data = newentitlements, - Paging = { } - }; - responsecreator.SetBody(JsonConvert.SerializeObject(payload)); - session.SendResponse(responsecreator.GetResponse()); - return true; + Debugger.PrintError(ex.ToString()); + } + return false; } } } diff --git a/PayCheckServerLib/Responses/Social.cs b/PayCheckServerLib/Responses/Social.cs index bceb212..7b5a3e3 100644 --- a/PayCheckServerLib/Responses/Social.cs +++ b/PayCheckServerLib/Responses/Social.cs @@ -25,7 +25,7 @@ public static bool PutStatItemsBulk(HttpRequest request, PC3Server.PC3Session se return true; } - [HTTP("GET", "/social/v1/public/namespaces/pd3/users/{userId}/statitems?limit=1000&offset=0")] + [HTTP("GET", "/social/v1/public/namespaces/pd3/users/{userId}/statitems?limit={limit}&offset=0")] public static bool GetUserStatItems(HttpRequest request, PC3Server.PC3Session session) { ResponseCreator response = new ResponseCreator(); diff --git a/PayCheckServerLib/ServerManager.cs b/PayCheckServerLib/ServerManager.cs index 388db86..75ab196 100644 --- a/PayCheckServerLib/ServerManager.cs +++ b/PayCheckServerLib/ServerManager.cs @@ -6,11 +6,20 @@ public class ServerManager { static GSTATICServer.GSServer STATICServer; static PD3UDPServer UDPServer; - public static void Start() + /// + /// Use this to check if update finished (Either Cancelled or Success) + /// + public static event EventHandler UpdateFinished; + public static void Pre() { Debugger.logger.Info("Lib Info: " + BranchHelper.GetBranch() + " - " + BranchHelper.GetBuildDate() + " " + BranchHelper.GetCommitId()); if (ConfigHelper.ServerConfig.EnableAutoUpdate) Updater.CheckForJsonUpdates(); + UpdateFinished?.Invoke(null, true); + } + + public static void Start() + { if (ConfigHelper.ServerConfig.Hosting.WSS) PC3Server.Start("127.0.0.1", 443); if (ConfigHelper.ServerConfig.Hosting.Gstatic) diff --git a/PayCheckServerLib/Servers/PC3Server.cs b/PayCheckServerLib/Servers/PC3Server.cs index 6b19252..20b3735 100644 --- a/PayCheckServerLib/Servers/PC3Server.cs +++ b/PayCheckServerLib/Servers/PC3Server.cs @@ -123,7 +123,7 @@ public override void OnWsConnected(HttpRequest request) Debugger.PrintWarn("The fuck? This User now wants to to join to WS again! " + WS_ID); } WSS_Stuff.Add(token.UserId + "_" + WS_ID.ToString().ToLower(), this); - var x = "y{\"jsonrpc\":\"2.0\",\"method\":\"eventConnected\",\"params\":{\"sessionId\":\"9f51a15b940b4c538cc48281950de549\"}}CaEd"; + var x = "CaSr{\"jsonrpc\":\"2.0\",\"method\":\"eventConnected\",\"params\":{\"sessionId\":\"9f51a15b940b4c538cc48281950de549\"}}CaEd"; SendBinaryAsync(Encoding.UTF8.GetBytes(x)); } Debugger.PrintInfo(WSUserId + " joined to " + WS_ID); diff --git a/PayCheckServerLib/Updater.cs b/PayCheckServerLib/Updater.cs index 890fecc..a894bc1 100644 --- a/PayCheckServerLib/Updater.cs +++ b/PayCheckServerLib/Updater.cs @@ -8,7 +8,7 @@ public class Updater // that or https://raw.githubusercontent.com/MoolahModding/PayCheck3Files/main static readonly string FilesUrl = @"https://github.com/MoolahModding/PayCheck3Files/raw/main/"; - public static async void CheckForJsonUpdates(bool UIHandleUpdate = false) + public static void CheckForJsonUpdates(bool UIHandleUpdate = false) { Dictionary LocalFiles = new(); foreach (var file in Directory.GetFiles("./Files")) @@ -22,8 +22,9 @@ public static async void CheckForJsonUpdates(bool UIHandleUpdate = false) { try { + HttpClient client = new(); - var FilesData = await client.GetStringAsync(FilesUrl + "Hashes.json"); + var FilesData = client.GetStringAsync(FilesUrl + "Hashes.json").Result; Files = JsonConvert.DeserializeObject>(FilesData)!; } catch @@ -36,7 +37,9 @@ public static async void CheckForJsonUpdates(bool UIHandleUpdate = false) { Files = JsonConvert.DeserializeObject>(File.ReadAllText(Path.Join(FilesUrl, "Hashes.json")))!; } - + //Remove 2 Jsons to not cause error! + Files.Remove("UpdatedAtTimes.json"); + Files.Remove("WeaponTables.json"); foreach (var KeyPair in Files) { try @@ -55,23 +58,23 @@ public static async void CheckForJsonUpdates(bool UIHandleUpdate = false) var inp = Console.ReadLine(); - if (inp == null || string.IsNullOrEmpty(inp)) + if (string.IsNullOrEmpty(inp)) { - Console.WriteLine("Your input was wrong, skipping"); + //Console.WriteLine("Your input was wrong, skipping"); continue; } inp = inp.ToLower(); if (inp == "y") { - Console.WriteLine("Updating started!"); + Debugger.PrintInfo("Updating started!"); HttpClient client = new(); - var FilesData = await client.GetStringAsync(FilesUrl + KeyPair.Key); + var FilesData = client.GetStringAsync(FilesUrl + KeyPair.Key).Result; File.WriteAllText(KeyPair.Key, FilesData); } else { - Console.WriteLine("Not want to update, Skipping"); + //Debugger.PrintInfo("Not want to update, Skipping"); continue; } @@ -79,11 +82,13 @@ public static async void CheckForJsonUpdates(bool UIHandleUpdate = false) } } } - catch + catch (Exception ex) { + Debugger.PrintWarn(ex.ToString()); Debugger.PrintWarn("Unable to fetch get file to update", "Updater"); } } + Debugger.PrintInfo("Update Finished!"); } public static event EventHandler UpdateWithUI; diff --git a/PayCheckServerLib/WSController/ChatControl.cs b/PayCheckServerLib/WSController/ChatControl.cs index 01af15d..6346a84 100644 --- a/PayCheckServerLib/WSController/ChatControl.cs +++ b/PayCheckServerLib/WSController/ChatControl.cs @@ -27,7 +27,7 @@ public static void Control(byte[] buffer, long offset, long size, PC3Session ses Method = chatbase.Method, Params = new() { - Processed = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() + Processed = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString() } }; var resp = "CaSr" + JsonConvert.SerializeObject(rsp) + "CaEd"; diff --git a/PayCheckServerLib/WSController/LobbyControl.cs b/PayCheckServerLib/WSController/LobbyControl.cs index 49d7cc7..541658b 100644 --- a/PayCheckServerLib/WSController/LobbyControl.cs +++ b/PayCheckServerLib/WSController/LobbyControl.cs @@ -35,101 +35,115 @@ public static void Control(byte[] buffer, long offset, long size, PC3Session ses static void SwitchingType(Dictionary kv, PC3Session session) { - User? user; - Dictionary rsp = new(); - switch (kv["type"]) + try { - case "setUserStatusRequest": - rsp.Add("type", "setUserStatusResponse"); - rsp.Add("id", kv["id"]); - rsp.Add("code", "0"); - user = UserController.GetUser(session.WSUserId); - if (user == null) - { - Debugger.PrintWarn($"User not found! ({session.WSUserId}) WSS Cannot continue"); - new Exception("UserId is null"); + User? user; + Dictionary rsp = new(); + Debugger.PrintDebug("[" + kv["type"] + "]"); + switch (kv["type"]) + { + case "setUserStatusRequest": + rsp.Add("type", "setUserStatusResponse"); + rsp.Add("id", kv["id"]); + rsp.Add("code", "0"); + user = UserController.GetUser(session.WSUserId); + if (user == null) + { + Debugger.PrintWarn($"User not found! ({session.WSUserId}) WSS Cannot continue"); + new Exception("UserId is null"); + break; + } + user.Status.activity = kv["activity"]; + user.Status.availability = kv["availability"]; + user.Status.platform = kv["platform"]; + user.Status.lastSeenAt = DateTime.UtcNow.ToString("O"); + UserController.SaveUser(user); + SendToLobby(rsp, session); + rsp.Remove("type"); + rsp.Remove("code"); + rsp.Add("type", "userStatusNotif"); + rsp.Add("userID", session.WSUserId); + rsp.Add("activity", kv["activity"]); + rsp.Add("availability", kv["availability"]); + rsp.Add("platform", kv["platform"]); + rsp.Add("lastSeenAt", DateTime.UtcNow.ToString("O")); + foreach (var id in session.WSSServer().WSUserIds) + { + if (id == session.WSUserId) + continue; + SendToLobby(rsp, session.GetWSLobby(id)); + } break; - } - user.Status.activity = rsp["activity"]; - user.Status.availability = rsp["availability"]; - user.Status.platform = rsp["platform"]; - user.Status.lastSeenAt = DateTime.UtcNow.ToString("O"); - UserController.SaveUser(user); - SendToLobby(rsp, session); - rsp.Remove("type"); - rsp.Remove("code"); - rsp.Add("type", "userStatusNotif"); - rsp.Add("userID", session.WSUserId); - rsp.Add("activity", rsp["activity"]); - rsp.Add("availability", rsp["availability"]); - rsp.Add("platform", rsp["platform"]); - rsp.Add("lastSeenAt", DateTime.UtcNow.ToString("O")); - session.WSSServer().WSUserIds.ForEach(x => SendToLobby(rsp, session.GetWSLobby(x))); - break; - case "joinDefaultChannelRequest": - rsp.Add("type", "joinDefaultChannelResponse"); - rsp.Add("id", kv["id"]); - rsp.Add("channelSlug", "default-channel"); - rsp.Add("code", "0"); - SendToLobby(rsp, session); - break; - case "listIncomingFriendsRequest": - rsp.Add("type", "listIncomingFriendsResponse"); - rsp.Add("id", kv["id"]); - rsp.Add("code", "0"); - rsp.Add("friendsId", "[]"); - SendToLobby(rsp, session); - break; - case "listOutgoingFriendsRequest": - rsp.Add("type", "listOutgoingFriendsResponse"); - rsp.Add("id", kv["id"]); - rsp.Add("code", "0"); - rsp.Add("friendsId", "[]"); - SendToLobby(rsp, session); - break; - case "friendsStatusRequest": - rsp.Add("type", "friendsStatusResponse"); - rsp.Add("id", kv["id"]); - rsp.Add("code", "0"); - user = UserController.GetUser(session.WSUserId); - if (user == null) - { - Debugger.PrintWarn($"User not found! ({session.WSUserId}) WSS Cannot continue"); - throw new Exception("UserId is null"); - } - List friendsId = new(); - List availability = new(); - List activity = new(); - List platform = new(); - List lastSeenAt = new(); - foreach (var fr in user.Friends) - { - var fud = fr.UserId; - friendsId.Add(fud); - - var fuser = UserController.GetUser(fud); - if (fuser == null) + case "joinDefaultChannelRequest": + rsp.Add("type", "joinDefaultChannelResponse"); + rsp.Add("id", kv["id"]); + rsp.Add("channelSlug", "default-channel"); + rsp.Add("code", "0"); + SendToLobby(rsp, session); + break; + case "listIncomingFriendsRequest": + rsp.Add("type", "listIncomingFriendsResponse"); + rsp.Add("id", kv["id"]); + rsp.Add("code", "0"); + rsp.Add("friendsId", "[]"); + SendToLobby(rsp, session); + break; + case "listOutgoingFriendsRequest": + rsp.Add("type", "listOutgoingFriendsResponse"); + rsp.Add("id", kv["id"]); + rsp.Add("code", "0"); + rsp.Add("friendsId", "[]"); + SendToLobby(rsp, session); + break; + case "friendsStatusRequest": + rsp.Add("type", "friendsStatusResponse"); + rsp.Add("id", kv["id"]); + rsp.Add("code", "0"); + user = UserController.GetUser(session.WSUserId); + if (user == null) { Debugger.PrintWarn($"User not found! ({session.WSUserId}) WSS Cannot continue"); throw new Exception("UserId is null"); } - availability.Add(fuser.Status.availability); - activity.Add(fuser.Status.activity); - platform.Add(fuser.Status.platform); - lastSeenAt.Add(fuser.Status.lastSeenAt); - } + List friendsId = new(); + List availability = new(); + List activity = new(); + List platform = new(); + List lastSeenAt = new(); + foreach (var fr in user.Friends) + { + var fud = fr.UserId; + friendsId.Add(fud); + + var fuser = UserController.GetUser(fud); + if (fuser == null) + { + Debugger.PrintWarn($"User not found! ({session.WSUserId}) WSS Cannot continue"); + throw new Exception("UserId is null"); + } + availability.Add(fuser.Status.availability); + activity.Add(fuser.Status.activity); + platform.Add(fuser.Status.platform); + lastSeenAt.Add(fuser.Status.lastSeenAt); + } - rsp.Add("friendsId", ListToStr(friendsId)); - rsp.Add("availability", ListToStr(availability)); - rsp.Add("activity", ListToStr(activity)); - rsp.Add("platform", ListToStr(platform)); - rsp.Add("lastSeenAt", ListToStr(lastSeenAt)); - SendToLobby(rsp, session); - break; - default: - Debugger.PrintWebsocket("Not sending back anything: " + kv["type"]); - break; + rsp.Add("friendsId", ListToStr(friendsId)); + rsp.Add("availability", ListToStr(availability)); + rsp.Add("activity", ListToStr(activity)); + rsp.Add("platform", ListToStr(platform)); + rsp.Add("lastSeenAt", ListToStr(lastSeenAt)); + SendToLobby(rsp, session); + break; + default: + Debugger.PrintWebsocket("Not sending back anything: " + kv["type"]); + break; + } + } + catch (Exception ex) + { + Debugger.PrintError(ex.ToString()); } + } @@ -147,9 +161,9 @@ public static void SendToLobby(Dictionary kv, PC3Session session var str = ""; foreach (var item in kv) { - str = item.Key + ": " + item.Value + "\n"; + str += item.Key + ": " + item.Value + "\n"; } - str = str.Remove(-2); + str = str.Remove(str.Length -2); Debugger.PrintWebsocket(str); session.SendBinaryAsync(str); } diff --git a/PayCheckServerLib/config.json b/PayCheckServerLib/config.json index 025bf3d..7ac3a70 100644 --- a/PayCheckServerLib/config.json +++ b/PayCheckServerLib/config.json @@ -13,5 +13,5 @@ "Enable_PartySession": false, "UsePWInsteadSteamToken": false }, - "EnableAutoUpdate": true + "EnableAutoUpdate": false } \ No newline at end of file