Add ShockOnDamage toggle

This commit is contained in:
Mercury 2024-09-20 09:52:07 +02:00
parent 78b66c5023
commit 0dbb4aa734
3 changed files with 135 additions and 17 deletions

View file

@ -10,9 +10,13 @@ namespace DalamudShock
public string openshockApiKey { get; set; } = string.Empty;
public string OpenshockShockerID { get; set; } = string.Empty;
public string PiShockUser { get; set; } = string.Empty;
public int MaxValue { get; set; } = 100;
public int MaxDuration { get; set; } = 30;
public bool IsVibrationAllowed { get; set; } = false;
public bool UsePishock { get; set; } = false;
public bool ZapOnDamage { get; set; } = false;
public bool IsConfigWindowMovable { get; set; } = true;

View file

@ -27,7 +27,6 @@ namespace DalamudShock
public readonly WindowSystem WindowSystem = new("DalamudShock");
private ConfigWindow ConfigWindow { get; init; }
private MainWindow MainWindow { get; init; }
private uint lastHealth;
private DateTime lastShockTime = DateTime.MinValue;
@ -82,18 +81,53 @@ namespace DalamudShock
if (currentHealth == lastHealth) return;
lastHealth = currentHealth;
if (currentHealth < player.MaxHp * 0.5)
if (currentHealth != lastHealth)
{
if ((DateTime.Now - lastShockTime).TotalSeconds >= 15)
lastHealth = currentHealth;
if (Configuration.ZapOnDamage)
{
PluginLog.Information($"Player health changed to {currentHealth}, time conditions met. sending API request");
lastShockTime = DateTime.Now;
SendShockApiRequest();
if ((DateTime.Now - lastShockTime).TotalSeconds >= 15)
{
lastShockTime = DateTime.Now;
if (Configuration.UsePishock)
{
SendPiShockApiRequest();
}
else
{
SendOscApiRequest();
}
}
}
else
{
if (currentHealth < player.MaxHp * 0.5)
{
if ((DateTime.Now - lastShockTime).TotalSeconds >= 15)
{
PluginLog.Information($"Player health changed to {currentHealth}, time conditions met.");
lastShockTime = DateTime.Now;
if (Configuration.UsePishock)
{
PluginLog.Information("Using PiShock, triggering PiShock API request.");
SendPiShockApiRequest();
}
else
{
PluginLog.Information(
"Using default shock API, triggering standard shock API request.");
SendOscApiRequest();
}
}
}
}
}
}
private async void SendShockApiRequest()
private async void SendOscApiRequest()
{
if (string.IsNullOrEmpty(Configuration.openshockApiKey) || string.IsNullOrEmpty(Configuration.OpenshockShockerID))
{
@ -105,7 +139,7 @@ namespace DalamudShock
var random = new Random();
var intensity = random.Next(0, Configuration.MaxValue);
var duration = random.Next(1000, Configuration.MaxDuration);
var duration = random.Next(1000, Configuration.MaxDuration * 1000);
// Calculate the type based on weighted odds
var type = "shock";
@ -159,6 +193,71 @@ namespace DalamudShock
}
}
private async void SendPiShockApiRequest()
{
if (string.IsNullOrEmpty(Configuration.openshockApiKey) || string.IsNullOrEmpty(Configuration.OpenshockShockerID))
{
PluginLog.Error("Pishock API Key or ShareLink is not configured.");
return;
}
var isVibrationAllowed = Configuration.IsVibrationAllowed;
var random = new Random();
var intensity = random.Next(0, Configuration.MaxValue);
var duration = random.Next(1000, Configuration.MaxDuration);
var type = "0";
if (isVibrationAllowed)
{
var chance = random.Next(1, 11);
if (chance > 8)
{
type = "1";
}
}
var requestData = new[]
{
new
{
Username = Configuration.PiShockUser,
Name = "DalamudShock",
Code = Configuration.OpenshockShockerID,
Intensity = intensity,
Duration = duration,
Op= type
}
};
var jsonContent = JsonConvert.SerializeObject(requestData);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent",
"Dalamud/0.2 (Windows; U; Windows NT 10.4; x64) AppleWebKit/602.39 (KHTML, like Gecko)");
try
{
var response = await client.PostAsync("https://do.pishock.com/api/apioperate", content);
if (response.IsSuccessStatusCode)
{
PluginLog.Information("{0} sent successfully with intensity {1} and duration {2} ms.", type, 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}");
}
}
}
}

View file

@ -10,8 +10,7 @@ namespace DalamudShock.Windows
private Configuration Configuration;
public ConfigWindow(Plugin plugin) : base("DalamudShock Config###With a constant ID")
{
Flags = ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoScrollbar |
ImGuiWindowFlags.NoScrollWithMouse;
Flags = ImGuiWindowFlags.NoScrollWithMouse;
Size = new Vector2(600, 400);
SizeCondition = ImGuiCond.Always;
Configuration = plugin.Configuration;
@ -34,13 +33,13 @@ namespace DalamudShock.Windows
public override void Draw()
{
var apiKey = Configuration.openshockApiKey;
if (ImGui.InputText("OpenShock API Key", ref apiKey, 100))
if (ImGui.InputText("API Key", ref apiKey, 100))
{
Configuration.openshockApiKey = apiKey;
}
var shockerID = Configuration.OpenshockShockerID;
if (ImGui.InputText("OpenShock Shocker ID", ref shockerID, 100))
if (ImGui.InputText("Shocker ID or Share Code", ref shockerID, 100))
{
Configuration.OpenshockShockerID = shockerID;
}
@ -53,17 +52,33 @@ namespace DalamudShock.Windows
if (ImGui.SliderInt("Duration Cap (sec)", ref maxLength, 1, 30))
{
// Map the value from [1, 30] to [1000, 30000]
Configuration.MaxDuration = MapValue(maxLength, 1, 30, 1000, 30000);
}
int MapValue(int value, int fromMin, int fromMax, int toMin, int toMax)
{
return (int)((float)(value - fromMin) / (fromMax - fromMin) * (toMax - toMin) + toMin);
Configuration.MaxDuration = maxLength;
}
bool isVibrationAllowed = Configuration.IsVibrationAllowed;
if (ImGui.Checkbox("Allow Vibration", ref isVibrationAllowed))
{
Configuration.IsVibrationAllowed = isVibrationAllowed;
}
ImGui.SameLine();
bool usePsc = Configuration.UsePishock;
if (ImGui.Checkbox("Use PiShock API", ref usePsc))
{
Configuration.UsePishock = usePsc;
Configuration.IsVibrationAllowed = usePsc;
}
ImGui.Spacing();
if (usePsc)
{
string pishockUsername = Configuration.PiShockUser;
ImGui.InputText("PiShock Username", ref pishockUsername, 100);
Configuration.PiShockUser = pishockUsername;
}
ImGui.Spacing();
bool instantZap = Configuration.ZapOnDamage;
if (ImGui.Checkbox("Allow Vibration", ref instantZap))
{
Configuration.ZapOnDamage = instantZap;
}
ImGui.Spacing();
if (ImGui.Button("Save Configuration"))
{