From 90083bd7efc8859484739c2cff256aeb4b917c5e Mon Sep 17 00:00:00 2001 From: Clergue Valentin Date: Sun, 2 Jun 2024 15:05:49 +0200 Subject: [PATCH] Merge hotfix/fixReturnTypeOnSetMethods into develop fix return types --- .../Examples/PromoteToLeader/Program.cs | 8 +-- .../Examples/SendTeamMessage/Program.cs | 8 +-- .../Examples/SetSmartSwitchValue/Program.cs | 11 ++-- .../Examples/SetSubscription/Program.cs | 11 ++-- .../Examples/StrobeSmartSwitch/Program.cs | 11 ++-- .../Examples/ToggleSmartSwitch/Program.cs | 11 ++-- RustPlusApi/RustPlusApi/RustPlus.cs | 51 ++++++++++--------- RustPlusApi/RustPlusApi/RustPlusSocket.cs | 7 ++- 8 files changed, 75 insertions(+), 43 deletions(-) diff --git a/RustPlusApi/Examples/PromoteToLeader/Program.cs b/RustPlusApi/Examples/PromoteToLeader/Program.cs index a5acc5f..c38f738 100644 --- a/RustPlusApi/Examples/PromoteToLeader/Program.cs +++ b/RustPlusApi/Examples/PromoteToLeader/Program.cs @@ -1,4 +1,6 @@ -using RustPlusApi; +using Newtonsoft.Json; + +using RustPlusApi; using static __Constants.ExamplesConst; @@ -7,7 +9,7 @@ await rustPlus.ConnectAsync(); -await rustPlus.PromoteToLeaderAsync(steamId); -Console.WriteLine($"Player: {steamId} is now the team leader!"); +var message = await rustPlus.PromoteToLeaderAsync(steamId); +Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}"); await rustPlus.DisconnectAsync(); \ No newline at end of file diff --git a/RustPlusApi/Examples/SendTeamMessage/Program.cs b/RustPlusApi/Examples/SendTeamMessage/Program.cs index 62ed720..9226002 100644 --- a/RustPlusApi/Examples/SendTeamMessage/Program.cs +++ b/RustPlusApi/Examples/SendTeamMessage/Program.cs @@ -1,4 +1,6 @@ -using RustPlusApi; +using Newtonsoft.Json; + +using RustPlusApi; using static __Constants.ExamplesConst; @@ -6,7 +8,7 @@ await rustPlus.ConnectAsync(); -await rustPlus.SendTeamMessageAsync("Hello from RustPlusApi!"); -Console.WriteLine("Message sent in-game!"); +var message = await rustPlus.SendTeamMessageAsync("Hello from RustPlusApi!"); +Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}"); await rustPlus.DisconnectAsync(); \ No newline at end of file diff --git a/RustPlusApi/Examples/SetSmartSwitchValue/Program.cs b/RustPlusApi/Examples/SetSmartSwitchValue/Program.cs index a2b451f..4d385c0 100644 --- a/RustPlusApi/Examples/SetSmartSwitchValue/Program.cs +++ b/RustPlusApi/Examples/SetSmartSwitchValue/Program.cs @@ -1,4 +1,6 @@ -using RustPlusApi; +using Newtonsoft.Json; + +using RustPlusApi; using static __Constants.ExamplesConst; @@ -8,7 +10,10 @@ await rustPlus.ConnectAsync(); -await rustPlus.SetSmartSwitchValue(smartSwitchId, smartSwitchValue); -Console.WriteLine($"Smart switch: {smartSwitchId} is now {(smartSwitchValue ? "enabled" : "disabled")}!"); +var message = await rustPlus.SetSmartSwitchValue(smartSwitchId, smartSwitchValue); +Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}"); + +if (message.IsSuccess) + Console.WriteLine($"Smart switch: {smartSwitchId} is now {(smartSwitchValue ? "enable" : "disable")}!"); await rustPlus.DisconnectAsync(); \ No newline at end of file diff --git a/RustPlusApi/Examples/SetSubscription/Program.cs b/RustPlusApi/Examples/SetSubscription/Program.cs index fca7a1f..2dbaedc 100644 --- a/RustPlusApi/Examples/SetSubscription/Program.cs +++ b/RustPlusApi/Examples/SetSubscription/Program.cs @@ -1,4 +1,6 @@ -using RustPlusApi; +using Newtonsoft.Json; + +using RustPlusApi; using static __Constants.ExamplesConst; @@ -8,7 +10,10 @@ await rustPlus.ConnectAsync(); -await rustPlus.SetSubscriptionAsync(smartSwitchId, smartSwitchValue); -Console.WriteLine($"Smart switch: {smartSwitchId} is now {(smartSwitchValue ? "enable" : "disable")}!"); +var message = await rustPlus.SetSubscriptionAsync(smartSwitchId, smartSwitchValue); +Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}"); + +if (message.IsSuccess) + Console.WriteLine($"Smart switch: {smartSwitchId} have its notifications {(smartSwitchValue ? "enable" : "disable")}!"); await rustPlus.DisconnectAsync(); \ No newline at end of file diff --git a/RustPlusApi/Examples/StrobeSmartSwitch/Program.cs b/RustPlusApi/Examples/StrobeSmartSwitch/Program.cs index 377f727..20bd01d 100644 --- a/RustPlusApi/Examples/StrobeSmartSwitch/Program.cs +++ b/RustPlusApi/Examples/StrobeSmartSwitch/Program.cs @@ -1,4 +1,6 @@ -using RustPlusApi; +using Newtonsoft.Json; + +using RustPlusApi; using static __Constants.ExamplesConst; @@ -7,7 +9,10 @@ await rustPlus.ConnectAsync(); -await rustPlus.StrobeSmartSwitchAsync(smartSwitchId); -Console.WriteLine($"Smart switch: {smartSwitchId} have been strobe!"); +var message = await rustPlus.StrobeSmartSwitchAsync(smartSwitchId); +Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}"); + +if (message.IsSuccess) + Console.WriteLine($"Smart switch: {smartSwitchId} is now {(message.Data!.IsActive ? "enable" : "disable")}!"); await rustPlus.DisconnectAsync(); \ No newline at end of file diff --git a/RustPlusApi/Examples/ToggleSmartSwitch/Program.cs b/RustPlusApi/Examples/ToggleSmartSwitch/Program.cs index fe6ce7e..9303ae4 100644 --- a/RustPlusApi/Examples/ToggleSmartSwitch/Program.cs +++ b/RustPlusApi/Examples/ToggleSmartSwitch/Program.cs @@ -1,4 +1,6 @@ -using RustPlusApi; +using Newtonsoft.Json; + +using RustPlusApi; using static __Constants.ExamplesConst; @@ -7,7 +9,10 @@ await rustPlus.ConnectAsync(); -await rustPlus.ToggleSmartSwitchAsync(smartSwitchId); -Console.WriteLine($"Smart switch: {smartSwitchId} have been toggle!"); +var message = await rustPlus.ToggleSmartSwitchAsync(smartSwitchId); +Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}"); + +if (message.IsSuccess) + Console.WriteLine($"Smart switch: {smartSwitchId} is now {(message.Data!.IsActive ? "enable" : "disable")}!"); await rustPlus.DisconnectAsync(); \ No newline at end of file diff --git a/RustPlusApi/RustPlusApi/RustPlus.cs b/RustPlusApi/RustPlusApi/RustPlus.cs index 6f23dfe..d7ff1df 100644 --- a/RustPlusApi/RustPlusApi/RustPlus.cs +++ b/RustPlusApi/RustPlusApi/RustPlus.cs @@ -212,8 +212,8 @@ protected override void ParseNotification(AppBroadcast? broadcast) /// Promotes a player to leader asynchronously. /// /// The Steam ID of the player to promote. - /// A representing the asynchronous operation. The task result contains a with the promotion result. - public async Task PromoteToLeaderAsync(ulong steamId) + /// A representing the asynchronous operation. The task result contains a indicating the success of the operation. + public async Task> PromoteToLeaderAsync(ulong steamId) { var request = new AppRequest { @@ -222,15 +222,15 @@ public async Task PromoteToLeaderAsync(ulong steamId) SteamId = steamId } }; - await SendRequestAsync(request); + return await ProcessRequestAsync(request, r => r.Response.Success is not null); } /// /// Sends a team message asynchronously. /// /// The message to send. - /// A representing the asynchronous operation. - public async Task SendTeamMessageAsync(string message) + /// A representing the asynchronous operation. The task result contains a with the sent team message. + public async Task> SendTeamMessageAsync(string message) { var request = new AppRequest { @@ -239,7 +239,7 @@ public async Task SendTeamMessageAsync(string message) Message = message } }; - await SendRequestAsync(request); + return await ProcessRequestAsync(request, r => r.Broadcast.TeamMessage.Message.ToTeamMessage()); } /// @@ -247,8 +247,8 @@ public async Task SendTeamMessageAsync(string message) /// /// The ID of the smart switch entity. /// The value to set for the smart switch. - /// A representing the asynchronous operation. - public async Task SetSmartSwitchValue(uint smartSwitchId, bool smartSwitchValue) + /// A representing the asynchronous operation. The task result contains a with the updated smart switch information. + public async Task> SetSmartSwitchValue(uint smartSwitchId, bool smartSwitchValue) { var request = new AppRequest { @@ -258,7 +258,7 @@ public async Task SetSmartSwitchValue(uint smartSwitchId, bool smartSwitchValue) Value = smartSwitchValue }, }; - await SendRequestAsync(request); + return await ProcessRequestAsync(request, r => r.Broadcast.EntityChanged.ToSmartSwitchEvent()); } /// @@ -266,8 +266,8 @@ public async Task SetSmartSwitchValue(uint smartSwitchId, bool smartSwitchValue) /// /// The ID of the entity. /// Specifies whether to subscribe or unsubscribe. - /// A representing the asynchronous operation. - public async Task SetSubscriptionAsync(uint entityId, bool doSubscribe = true) + /// A representing the asynchronous operation. The task result contains a indicating the success of the operation. + public async Task> SetSubscriptionAsync(uint entityId, bool doSubscribe = true) { var request = new AppRequest { @@ -277,36 +277,39 @@ public async Task SetSubscriptionAsync(uint entityId, bool doSubscribe = true) Value = doSubscribe } }; - await SendRequestAsync(request); + return await ProcessRequestAsync(request, r => r.Response.Success is not null); } /// - /// Strobes a smart switch asynchronously by toggling its value on and off with a specified timeout. + /// Strobes a smart switch asynchronously. /// /// The ID of the smart switch entity. - /// The timeout in milliseconds between toggling the smart switch value. - /// The initial value to set for the smart switch. - /// A representing the asynchronous operation. - public async Task StrobeSmartSwitchAsync(uint entityId, int timeoutMilliseconds = 1000, bool value = true) + /// The duration of each state in milliseconds. + /// The initial value of the smart switch. + /// A representing the asynchronous operation. The task result contains a with the updated smart switch information. + public async Task> StrobeSmartSwitchAsync(uint entityId, int timeoutMilliseconds = 1000, bool value = true) { - await SetSmartSwitchValue(entityId, value); + var response = await SetSmartSwitchValue(entityId, value); + + if (!response.IsSuccess) return response; + await Task.Delay(timeoutMilliseconds); - await SetSmartSwitchValue(entityId, !value); + return await SetSmartSwitchValue(entityId, !value); } /// - /// Toggles the value of a smart switch asynchronously. + /// Toggles a smart switch asynchronously. /// /// The ID of the smart switch entity. - /// A representing the asynchronous operation. - public async Task ToggleSmartSwitchAsync(uint entityId) + /// A representing the asynchronous operation. The task result contains a with the updated smart switch information. + public async Task> ToggleSmartSwitchAsync(uint entityId) { var entityInfo = await GetSmartSwitchInfoAsync(entityId); - if (!entityInfo.IsSuccess) return; + if (!entityInfo.IsSuccess) return entityInfo; var value = entityInfo!.Data!.IsActive; - await SetSmartSwitchValue(entityId, !value); + return await SetSmartSwitchValue(entityId, !value); } } } \ No newline at end of file diff --git a/RustPlusApi/RustPlusApi/RustPlusSocket.cs b/RustPlusApi/RustPlusApi/RustPlusSocket.cs index 3119277..10b734b 100644 --- a/RustPlusApi/RustPlusApi/RustPlusSocket.cs +++ b/RustPlusApi/RustPlusApi/RustPlusSocket.cs @@ -234,6 +234,11 @@ protected virtual void ParseNotification(AppBroadcast? broadcast) { } /// /// The AppMessage response to check. /// True if the response is an error; otherwise, false. - protected static bool IsError(AppMessage response) => response.Response.Error is not null; + protected static bool IsError(AppMessage response) + { + if (response.Response is null && response.Broadcast is not null) return false; + if (response.Response!.Error is not null) return true; + return false; + } } }