Skip to content

Commit

Permalink
Merge hotfix/fixReturnTypeOnSetMethods into develop
Browse files Browse the repository at this point in the history
fix return types
  • Loading branch information
HandyS11 authored Jun 2, 2024
1 parent 64cbedd commit 90083bd
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 43 deletions.
8 changes: 5 additions & 3 deletions RustPlusApi/Examples/PromoteToLeader/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using RustPlusApi;
using Newtonsoft.Json;

using RustPlusApi;

using static __Constants.ExamplesConst;

Expand All @@ -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();
8 changes: 5 additions & 3 deletions RustPlusApi/Examples/SendTeamMessage/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using RustPlusApi;
using Newtonsoft.Json;

using RustPlusApi;

using static __Constants.ExamplesConst;

var rustPlus = new RustPlus(Ip, Port, PlayerId, PlayerToken);

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();
11 changes: 8 additions & 3 deletions RustPlusApi/Examples/SetSmartSwitchValue/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using RustPlusApi;
using Newtonsoft.Json;

using RustPlusApi;

using static __Constants.ExamplesConst;

Expand All @@ -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();
11 changes: 8 additions & 3 deletions RustPlusApi/Examples/SetSubscription/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using RustPlusApi;
using Newtonsoft.Json;

using RustPlusApi;

using static __Constants.ExamplesConst;

Expand All @@ -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();
11 changes: 8 additions & 3 deletions RustPlusApi/Examples/StrobeSmartSwitch/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using RustPlusApi;
using Newtonsoft.Json;

using RustPlusApi;

using static __Constants.ExamplesConst;

Expand All @@ -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();
11 changes: 8 additions & 3 deletions RustPlusApi/Examples/ToggleSmartSwitch/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using RustPlusApi;
using Newtonsoft.Json;

using RustPlusApi;

using static __Constants.ExamplesConst;

Expand All @@ -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();
51 changes: 27 additions & 24 deletions RustPlusApi/RustPlusApi/RustPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ protected override void ParseNotification(AppBroadcast? broadcast)
/// Promotes a player to leader asynchronously.
/// </summary>
/// <param name="steamId">The Steam ID of the player to promote.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the asynchronous operation. The task result contains a <see cref="Response{T}"/> with the promotion result.</returns>
public async Task PromoteToLeaderAsync(ulong steamId)
/// <returns>A <see cref="Task{TResult}"/> representing the asynchronous operation. The task result contains a <see cref="Response{T}"/> indicating the success of the operation.</returns>
public async Task<Response<bool?>> PromoteToLeaderAsync(ulong steamId)
{
var request = new AppRequest
{
Expand All @@ -222,15 +222,15 @@ public async Task PromoteToLeaderAsync(ulong steamId)
SteamId = steamId
}
};
await SendRequestAsync(request);
return await ProcessRequestAsync<bool?>(request, r => r.Response.Success is not null);
}

/// <summary>
/// Sends a team message asynchronously.
/// </summary>
/// <param name="message">The message to send.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SendTeamMessageAsync(string message)
/// <returns>A <see cref="Task{TResult}"/> representing the asynchronous operation. The task result contains a <see cref="Response{T}"/> with the sent team message.</returns>
public async Task<Response<TeamMessage?>> SendTeamMessageAsync(string message)
{
var request = new AppRequest
{
Expand All @@ -239,16 +239,16 @@ public async Task SendTeamMessageAsync(string message)
Message = message
}
};
await SendRequestAsync(request);
return await ProcessRequestAsync<TeamMessage?>(request, r => r.Broadcast.TeamMessage.Message.ToTeamMessage());
}

/// <summary>
/// Sets the value of a smart switch asynchronously.
/// </summary>
/// <param name="smartSwitchId">The ID of the smart switch entity.</param>
/// <param name="smartSwitchValue">The value to set for the smart switch.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SetSmartSwitchValue(uint smartSwitchId, bool smartSwitchValue)
/// <returns>A <see cref="Task{TResult}"/> representing the asynchronous operation. The task result contains a <see cref="Response{T}"/> with the updated smart switch information.</returns>
public async Task<Response<SmartSwitchInfo?>> SetSmartSwitchValue(uint smartSwitchId, bool smartSwitchValue)
{
var request = new AppRequest
{
Expand All @@ -258,16 +258,16 @@ public async Task SetSmartSwitchValue(uint smartSwitchId, bool smartSwitchValue)
Value = smartSwitchValue
},
};
await SendRequestAsync(request);
return await ProcessRequestAsync<SmartSwitchInfo?>(request, r => r.Broadcast.EntityChanged.ToSmartSwitchEvent());
}

/// <summary>
/// Sets the subscription status of an entity asynchronously.
/// </summary>
/// <param name="entityId">The ID of the entity.</param>
/// <param name="doSubscribe">Specifies whether to subscribe or unsubscribe.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SetSubscriptionAsync(uint entityId, bool doSubscribe = true)
/// <returns>A <see cref="Task{TResult}"/> representing the asynchronous operation. The task result contains a <see cref="Response{T}"/> indicating the success of the operation.</returns>
public async Task<Response<bool?>> SetSubscriptionAsync(uint entityId, bool doSubscribe = true)
{
var request = new AppRequest
{
Expand All @@ -277,36 +277,39 @@ public async Task SetSubscriptionAsync(uint entityId, bool doSubscribe = true)
Value = doSubscribe
}
};
await SendRequestAsync(request);
return await ProcessRequestAsync<bool?>(request, r => r.Response.Success is not null);
}

/// <summary>
/// Strobes a smart switch asynchronously by toggling its value on and off with a specified timeout.
/// Strobes a smart switch asynchronously.
/// </summary>
/// <param name="entityId">The ID of the smart switch entity.</param>
/// <param name="timeoutMilliseconds">The timeout in milliseconds between toggling the smart switch value.</param>
/// <param name="value">The initial value to set for the smart switch.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task StrobeSmartSwitchAsync(uint entityId, int timeoutMilliseconds = 1000, bool value = true)
/// <param name="timeoutMilliseconds">The duration of each state in milliseconds.</param>
/// <param name="value">The initial value of the smart switch.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the asynchronous operation. The task result contains a <see cref="Response{T}"/> with the updated smart switch information.</returns>
public async Task<Response<SmartSwitchInfo?>> 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);
}

/// <summary>
/// Toggles the value of a smart switch asynchronously.
/// Toggles a smart switch asynchronously.
/// </summary>
/// <param name="entityId">The ID of the smart switch entity.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task ToggleSmartSwitchAsync(uint entityId)
/// <returns>A <see cref="Task{TResult}"/> representing the asynchronous operation. The task result contains a <see cref="Response{T}"/> with the updated smart switch information.</returns>
public async Task<Response<SmartSwitchInfo?>> 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);
}
}
}
7 changes: 6 additions & 1 deletion RustPlusApi/RustPlusApi/RustPlusSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ protected virtual void ParseNotification(AppBroadcast? broadcast) { }
/// </summary>
/// <param name="response">The AppMessage response to check.</param>
/// <returns>True if the response is an error; otherwise, false.</returns>
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;
}
}
}

0 comments on commit 90083bd

Please sign in to comment.