150 lines
5.2 KiB
C#
150 lines
5.2 KiB
C#
using Dalamud.Game.Command;
|
|
using Dalamud.IoC;
|
|
using Dalamud.Plugin;
|
|
using System;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using Newtonsoft.Json;
|
|
using Dalamud.Interface.Windowing;
|
|
using Dalamud.Plugin.Services;
|
|
using DalamudShock.Windows;
|
|
// ReSharper disable RedundantAnonymousTypePropertyName
|
|
|
|
|
|
namespace DalamudShock
|
|
{
|
|
public sealed class Plugin : IDalamudPlugin
|
|
{
|
|
[PluginService] internal static IDalamudPluginInterface PluginInterface { get; private set; } = null!;
|
|
[PluginService] internal static IClientState ClientState { get; private set; } = null!;
|
|
[PluginService] internal static IFramework Framework { get; private set; } = null!;
|
|
[PluginService] internal static ICommandManager CommandManager { get; private set; } = null!;
|
|
[PluginService] internal static IPluginLog PluginLog { get; private set; } = null!;
|
|
|
|
|
|
private const string CommandName = "/zap";
|
|
public Configuration Configuration { get; init; }
|
|
public readonly WindowSystem WindowSystem = new("DalamudShock");
|
|
private ConfigWindow ConfigWindow { get; init; }
|
|
private MainWindow MainWindow { get; init; }
|
|
|
|
private uint lastHealth;
|
|
private DateTime lastShockTime = DateTime.MinValue;
|
|
|
|
public Plugin()
|
|
{
|
|
Configuration = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
|
|
|
|
ConfigWindow = new ConfigWindow(this);
|
|
MainWindow = new MainWindow(this);
|
|
|
|
WindowSystem.AddWindow(ConfigWindow);
|
|
WindowSystem.AddWindow(MainWindow);
|
|
|
|
CommandManager.AddHandler(CommandName, new CommandInfo(OnCommand)
|
|
{
|
|
HelpMessage = "A shockingly empty description :3"
|
|
});
|
|
|
|
PluginInterface.UiBuilder.Draw += DrawUI;
|
|
PluginInterface.UiBuilder.OpenConfigUi += ToggleConfigUI;
|
|
PluginInterface.UiBuilder.OpenMainUi += ToggleMainUI;
|
|
|
|
Framework.Update += OnFrameworkUpdate;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Framework.Update -= OnFrameworkUpdate;
|
|
WindowSystem.RemoveAllWindows();
|
|
ConfigWindow.Dispose();
|
|
MainWindow.Dispose();
|
|
CommandManager.RemoveHandler(CommandName);
|
|
}
|
|
|
|
private void OnCommand(string command, string args)
|
|
{
|
|
ToggleMainUI();
|
|
}
|
|
|
|
private void DrawUI() => WindowSystem.Draw();
|
|
|
|
public void ToggleConfigUI() => ConfigWindow.Toggle();
|
|
public void ToggleMainUI() => MainWindow.Toggle();
|
|
|
|
private void OnFrameworkUpdate(IFramework framework)
|
|
{
|
|
var player = ClientState.LocalPlayer;
|
|
|
|
if (player == null) return;
|
|
var currentHealth = player.CurrentHp;
|
|
|
|
if (currentHealth == lastHealth) return;
|
|
lastHealth = currentHealth;
|
|
|
|
if (currentHealth < player.MaxHp * 0.5)
|
|
{
|
|
if ((DateTime.Now - lastShockTime).TotalSeconds >= 15)
|
|
{
|
|
PluginLog.Information($"Player health changed to {currentHealth}, time conditions met. sending API request");
|
|
lastShockTime = DateTime.Now;
|
|
SendShockApiRequest();
|
|
}
|
|
}
|
|
}
|
|
|
|
private async void SendShockApiRequest()
|
|
{
|
|
if (string.IsNullOrEmpty(Configuration.openshockApiKey) || string.IsNullOrEmpty(Configuration.OpenshockShockerID))
|
|
{
|
|
PluginLog.Error("OpenShock API Key or Shocker ID is not configured.");
|
|
return;
|
|
}
|
|
|
|
var random = new Random();
|
|
int intensity = random.Next(0, Configuration.MaxValue);
|
|
int duration = random.Next(1000, 25000);
|
|
|
|
var requestData = new[]
|
|
{
|
|
new
|
|
{
|
|
id = Configuration.OpenshockShockerID,
|
|
type = "shock",
|
|
intensity = intensity,
|
|
duration = duration
|
|
}
|
|
};
|
|
|
|
var jsonContent = JsonConvert.SerializeObject(requestData);
|
|
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
|
|
|
using var client = new HttpClient();
|
|
client.DefaultRequestHeaders.Add("OpenShockToken", Configuration.openshockApiKey);
|
|
client.DefaultRequestHeaders.Add("User-Agent", "FFXIV:DalamudShock/0.2");
|
|
|
|
try
|
|
{
|
|
var response = await client.PostAsync("https://api.shocklink.net/1/shockers/control", content);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
PluginLog.Information("Shock sent successfully with intensity {0} and duration {1} ms.", intensity, duration);
|
|
}
|
|
else
|
|
{
|
|
var responseBody = await response.Content.ReadAsStringAsync();
|
|
PluginLog.Error($"Shock API request failed: {responseBody}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PluginLog.Error($"Error occurred while sending shock: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|