Skip to content

Commit

Permalink
totaly rework the app logic by removing the callback principe
Browse files Browse the repository at this point in the history
implement entityinfo
  • Loading branch information
HandyS11 committed May 29, 2024
1 parent 40720a1 commit 5cad425
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 36 deletions.
31 changes: 21 additions & 10 deletions RustPlusApi/Examples/GetEntityChanges/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,34 @@
using static __Constants.ExamplesConst;

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

rustPlus.Connected += async (_, _) =>
{
await rustPlus.GetEntityInfoAsync(entityId, message =>
{
Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
return true;
});
// The message would be SmartSwitchInfo, AlarmInfo, or StorageMonitorInfo
// If you want to do your own parsing you can set the useRawObject parameter to true
var message = await rustPlus.GetEntityInfoAsync(entityId);

Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
};

rustPlus.MessageReceived += (_, message) =>
rustPlus.OnSmartSwitchTriggered += (_, message) =>
{
if (message.Broadcast is not { EntityChanged: not null }) return;
Console.WriteLine($"SmartSwitch:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
};

var entityChanged = message.Broadcast.EntityChanged;
Console.WriteLine($"Message:\n{JsonConvert.SerializeObject(entityChanged, JsonSettings)}");
rustPlus.OnStorageMonitorTriggered += (_, message) =>
{
Console.WriteLine($"StorageMonitor:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
};

// If you want to get the raw message for the notification you can use the MessageReceived event
//rustPlus.MessageReceived += (_, message) =>
//{
// if (message.Broadcast is not { EntityChanged: not null }) return;

// var entityChanged = message.Broadcast.EntityChanged;
// Console.WriteLine($"Message:\n{JsonConvert.SerializeObject(entityChanged, JsonSettings)}");
//};

await rustPlus.ConnectAsync();
15 changes: 8 additions & 7 deletions RustPlusApi/Examples/GetEntityInfo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
using static __Constants.ExamplesConst;

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

rustPlus.Connected += async (_, _) =>
{
await rustPlus.GetEntityInfoAsync(entityId, message =>
{
Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");
rustPlus.Dispose();
return true;
});
// The message would be SmartSwitchInfo, AlarmInfo, or StorageMonitorInfo
// If you want to do your own parsing you can set the useRawObject parameter to true
var message = await rustPlus.GetEntityInfoAsync(entityId);

Console.WriteLine($"Infos:\n{JsonConvert.SerializeObject(message, JsonSettings)}");

rustPlus.Dispose();
};

await rustPlus.ConnectAsync();
7 changes: 7 additions & 0 deletions RustPlusApi/RustPlusApi/Data/AlarmInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RustPlusApi.Data
{
public class AlarmInfo
{
public bool Value { get; set; }
}
}
7 changes: 7 additions & 0 deletions RustPlusApi/RustPlusApi/Data/Events/SmartSwitchEventArg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RustPlusApi.Data.Events
{
public class SmartSwitchEventArg : SmartSwitchInfo
{
public uint Id { get; set; }
}
}
7 changes: 7 additions & 0 deletions RustPlusApi/RustPlusApi/Data/Events/StorageMonitorEventArg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RustPlusApi.Data.Events
{
public class StorageMonitorEventArg : StorageMonitorInfo
{
public uint Id { get; set; }
}
}
7 changes: 7 additions & 0 deletions RustPlusApi/RustPlusApi/Data/SmartSwitchInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RustPlusApi.Data
{
public class SmartSwitchInfo
{
public bool Value { get; set; }
}
}
10 changes: 10 additions & 0 deletions RustPlusApi/RustPlusApi/Data/StorageMonitorInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace RustPlusApi.Data
{
public class StorageMonitorInfo
{
public int? Capacity { get; set; }
public bool? HasProtection { get; set; }
public uint? ProtectionExpiry { get; set; }
public List<StorageMonitorItemInfo>? Items { get; set; }
}
}
9 changes: 9 additions & 0 deletions RustPlusApi/RustPlusApi/Data/StorageMonitorItemInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace RustPlusApi.Data
{
public class StorageMonitorItemInfo
{
public int Id { get; set; }
public int? Quantity { get; set; }
public bool? IsItemBlueprint { get; set; }
}
}
62 changes: 62 additions & 0 deletions RustPlusApi/RustPlusApi/Extensions/AppEntityInfoToModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using RustPlusApi.Data;

using RustPlusContracts;

namespace RustPlusApi.Extensions
{
public static class AppEntityInfoToModel
{
public static object ToEntityInfo(this AppEntityInfo entity)
{
return entity.Type switch
{
AppEntityType.Switch => entity.ToSmartSwitchInfo(),
AppEntityType.Alarm => entity.ToAlarmInfo(),
AppEntityType.StorageMonitor => entity.ToStorageMonitorInfo(),
_ => throw new ArgumentException($"The given type is not possible: {entity.Type}")
};
}

public static SmartSwitchInfo ToSmartSwitchInfo(this AppEntityInfo entity)
{
return new SmartSwitchInfo
{
Value = entity.Payload.Value
};
}

public static AlarmInfo ToAlarmInfo(this AppEntityInfo entity)
{
return new AlarmInfo
{
Value = entity.Payload.Value
};
}

public static StorageMonitorInfo ToStorageMonitorInfo(this AppEntityInfo entity)
{
return new StorageMonitorInfo
{
Capacity = entity.Payload.Capacity,
HasProtection = entity.Payload.HasProtection,
ProtectionExpiry = entity.Payload.ProtectionExpiry,
Items = entity.Payload.Items.ToStorageMonitorItemsInfo().ToList()
};
}

public static StorageMonitorItemInfo ToStorageMonitorItemInfo(this AppEntityPayload.Types.Item item)
{
return new StorageMonitorItemInfo
{
Id = item.ItemId,
Quantity = item.Quantity,
IsItemBlueprint = item.ItemIsBlueprint,
};
}

public static IEnumerable<StorageMonitorItemInfo> ToStorageMonitorItemsInfo(this IEnumerable<AppEntityPayload.Types.Item> items)
{
return items.Select(ToStorageMonitorItemInfo);
}
}
}
30 changes: 30 additions & 0 deletions RustPlusApi/RustPlusApi/Extensions/EntityChangedToModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using RustPlusApi.Data.Events;

using RustPlusContracts;

namespace RustPlusApi.Extensions
{
public static class EntityChangedToModel
{
public static SmartSwitchEventArg ToSmartSwitchEvent(this AppEntityChanged entityChanged)
{
return new SmartSwitchEventArg
{
Id = entityChanged.EntityId,
Value = entityChanged.Payload.Value
};
}

public static StorageMonitorEventArg ToStorageMonitorEvent(this AppEntityChanged entityChanged)
{
return new StorageMonitorEventArg
{
Id = entityChanged.EntityId,
Capacity = entityChanged.Payload.Capacity,
HasProtection = entityChanged.Payload.HasProtection,
ProtectionExpiry = entityChanged.Payload.ProtectionExpiry,
Items = entityChanged.Payload.Items.ToStorageMonitorItemsInfo().ToList()
};
}
}
}
Loading

0 comments on commit 5cad425

Please sign in to comment.