From 6060d0ddbf308cb2962e4a6ce70037477f112d0b Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Fri, 7 Mar 2025 17:47:17 -0800 Subject: [PATCH] implement buddy precense reporting --- src/Core/ApiWebService.cs | 22 ++++++++++++++++++++++ src/Core/Client.cs | 9 +++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Core/ApiWebService.cs b/src/Core/ApiWebService.cs index d0d0844..0b21553 100644 --- a/src/Core/ApiWebService.cs +++ b/src/Core/ApiWebService.cs @@ -74,6 +74,28 @@ public class ApiWebService } catch (Exception e) { LogError(e.Message); return false; } } + public bool SetOnline(Client client, bool online) + { + HttpClient httpClient = new(); + var content = new FormUrlEncodedContent + ( + new Dictionary + { + { "token", client.PlayerData.UNToken }, + { "online", online.ToString() } + } + ); + + try + { + var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Precense/SetVikingOnline", content).Result; + Log("Precense/SetVikingOnline"); + + if (response.StatusCode == System.Net.HttpStatusCode.OK && response.Content != null) return response.Content.ReadFromJsonAsync().Result; + else return false; + } catch (Exception e) { LogError(e.Message); return false; } + } + private void Log(string endpoint) => Console.WriteLine($"Sent API Request To {Configuration.ServerConfiguration.ApiUrl}/{endpoint}"); private void LogError(string message) => Console.WriteLine($"An Error Has Occured When Sending An API Request - {message}"); diff --git a/src/Core/Client.cs b/src/Core/Client.cs index 0ff7659..ddb9617 100644 --- a/src/Core/Client.cs +++ b/src/Core/Client.cs @@ -47,6 +47,9 @@ public class Client { } public void SetRoom(Room? room) { + // api web service for setting precense + ApiWebService apiWebService = new(); + lock(clientLock) { // set variable player data as not valid, but do not reset all player data PlayerData.IsValid = false; @@ -59,6 +62,8 @@ public class Client { data.Add("r", Room.Id); data.Add("u", ClientID); Room.Send(NetworkObject.WrapObject(0, 1004, data).Serialize()); + + apiWebService.SetOnline(this, false); } // set new room (null when SetRoom is used as LeaveRoom) @@ -70,6 +75,8 @@ public class Client { Send(Room.SubscribeRoom()); if (Room.Name != "LIMBO") UpdatePlayerUserVariables(); // do not update user vars if room is limbo + + apiWebService.SetOnline(this, true); } } } @@ -98,11 +105,13 @@ public class Client { } public void ScheduleDisconnect() { + ApiWebService apiWebService = new(); if (Room != null) { // quiet remove from room (to avoid issues in Room.Send) // - do not change Room value here // - full remove will be will take place Server.HandleClient (before real disconnected) Room.RemoveClient(this); + apiWebService.SetOnline(this, false); } scheduledDisconnect = true; }