Skip to content

Commit

Permalink
More config
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Dec 16, 2023
1 parent 2d4b97b commit 4d8fbe8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Integrations.LethalCompany/Integrations.LethalCompany.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<TargetFramework>net472</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<AssemblyVersion>0.0.2</AssemblyVersion>
<FileVersion>0.0.2</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BepInEx.Analyzers" Version="1.*" PrivateAssets="all" />
Expand Down
101 changes: 79 additions & 22 deletions Integrations.LethalCompany/LethalCompanyOpenShock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
Expand All @@ -13,33 +16,81 @@ public class LethalCompanyOpenShock : BaseUnityPlugin
{
public static LethalCompanyOpenShock Instance { get; private set; } = null!;
public static ManualLogSource ModLogger { get; private set; } = null!;

private OpenShockApi.OpenShockApi _openShockApi;
private readonly Harmony harmony = new Harmony(PluginInfo.PLUGIN_GUID);

private OpenShockApi.OpenShockApi _openShockApi = null!;
private readonly Harmony _harmony = new(PluginInfo.PLUGIN_GUID);

// Config

// OpenShock Server
private ConfigEntry<string> _openShockServer = null!;
private ConfigEntry<string> _openShockApiToken = null!;
private ConfigEntry<string> _shockers = null!;

// Settings

// OnDeath
private ConfigEntry<byte> _settingOnDeathIntensity = null!;
private ConfigEntry<ushort> _settingOnDeathDuration = null!;

// OnDamage
private ConfigEntry<ushort> _settingOnDamageDuration = null!;

private IList<Guid> _shockersList = null!;

private void Awake()
{
ModLogger = Logger;
Instance = this;
Logger.LogInfo("Starting OpenShock.Integrations.LethalCompany");

// Config

_openShockServer = Config.Bind<string>("OpenShock", "Server", "https://api.shocklink.net", "The URL of the OpenShock backend");
_openShockApiToken = Config.Bind<string>("OpenShock", "ApiToken", "", "API token for authentication, can be found under API Tokens on the OpenShock dashboard (https://shocklink.net/#/dashboard/tokens)");
// OpenShock Server
_openShockServer = Config.Bind<string>("OpenShock", "Server", "https://api.shocklink.net",
"The URL of the OpenShock backend");
_openShockApiToken = Config.Bind<string>("OpenShock", "ApiToken", "",
"API token for authentication, can be found under API Tokens on the OpenShock dashboard (https://shocklink.net/#/dashboard/tokens)");
_shockers = Config.Bind<string>("Shockers", "Shockers", "comma,seperated,list,of,shocker,ids",
"A list of shocker IDs to use within the mod. Comma seperated.");


// Settings

// OnDeath
_settingOnDeathIntensity = Config.Bind<byte>("OnDeath", "Intensity", 100,
"The intensity of the shocker when the player dies");
_settingOnDeathDuration = Config.Bind<ushort>("OnDeath", "Duration", 5000,
"The duration of the shocker when the player dies");

// OnDamage
_settingOnDamageDuration = Config.Bind<ushort>("OnDamage", "Duration", 5000,
"The duration of the shocker when the player takes damage");

Logger.LogDebug("Patching PlayerController");
harmony.PatchAll(typeof(PlayerControllerPatches));
_harmony.PatchAll(typeof(PlayerControllerPatches));

_openShockServer.SettingChanged += (_, _) => SetupApi();
_openShockApiToken.SettingChanged += (_, _) => SetupApi();

_shockers.SettingChanged += (_, _) => ShockersSetup();

SetupApi();

Logger.LogInfo("Started OpenShock.Integrations.LethalCompany");
}

private void ShockersSetup()
{
var newList = new List<Guid>();
foreach (var shocker in _shockers.Value.Split(','))
{
if (Guid.TryParse(shocker, out var shockerGuid)) newList.Add(shockerGuid);
else Logger.LogError($"Failed to parse shocker ID {shocker}");
}

_shockersList = newList;
}

private void SetupApi()
{
_openShockApi = new OpenShockApi.OpenShockApi(new Uri(_openShockServer.Value), _openShockApiToken.Value);
Expand All @@ -48,23 +99,29 @@ private void SetupApi()
public void OnDamage(int health, int damageNumber)
{
Logger.LogInfo($"Received damage, health is {health}, damage is {damageNumber}");
LucTask.Run(() => _openShockApi.Control(new Control
{
Id = Guid.Parse("d9267ca6-d69b-4b7a-b482-c455f75a4408"),
Duration = 5000,
Intensity = (byte)Mathf.Clamp(damageNumber, 25, 100),
Type = ControlType.Shock
}));

FireAndForgetControl(_settingOnDamageDuration.Value, (byte)Mathf.Clamp(damageNumber, 1, 100), ControlType.Shock);
}

private void FireAndForgetControl(ushort duration, byte intensity, ControlType type) =>
LucTask.Run(() => SendControlToAll(duration, intensity, type));


private Task SendControlToAll(ushort duration, byte intensity, ControlType type)
{
var controls = _shockersList.Select(shocker =>
new Control
{
Id = shocker,
Duration = duration,
Intensity = intensity,
Type = type
});
return _openShockApi.Control(controls);
}

public void OnDeath()
{
LucTask.Run(() => _openShockApi.Control(new Control
{
Id = Guid.Parse("d9267ca6-d69b-4b7a-b482-c455f75a4408"),
Duration = 5000,
Intensity = 100,
Type = ControlType.Shock
}));
FireAndForgetControl(_settingOnDeathDuration.Value, _settingOnDeathIntensity.Value, ControlType.Shock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace OpenShock.Integrations.LethalCompany.OpenShockApi.Models;

public class ControlRequest
{
public IEnumerable<Control> Shocks { get; set; }
public IEnumerable<Control> Shocks { get; set; } = null!;
public string? CustomName { get; set; }
}
2 changes: 1 addition & 1 deletion Integrations.LethalCompany/OpenShockApi/OpenShockApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public OpenShockApi(Uri server, string apiToken)
_httpClient.DefaultRequestHeaders.Add("OpenShockToken", apiToken);
}

public async Task Control(params Control[] shocks)
public async Task Control(IEnumerable<Control> shocks)
{
Logger.LogInfo("Sending control request to OpenShock API");
var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/2/shockers/control")
Expand Down

0 comments on commit 4d8fbe8

Please sign in to comment.