Skip to content

Commit

Permalink
Implement ToN_HostChange OSC param.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisFeline committed Oct 18, 2024
1 parent b35faab commit c474b27
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ public int GetDeathID (string id) {
return Array.IndexOf(OSCDeathIDs, id) + 1;
}


/// <summary>
/// OSC Master Changed event for OSC.
/// </summary>
public bool OSCMasterChange { get; set; } = true;
/// <summary>
/// OSC Master Change event interval.
/// </summary>
public int OSCMasterChangeInterval { get; set; } = 200;

/// <summary>
/// Enables OSC chatbox messages.
/// </summary>
Expand Down
44 changes: 40 additions & 4 deletions Utils/LilOSC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ internal static class LilOSC {
const string ParamDeath = "ToN_DeathID";
const string ParamPages = "ToN_Pages";
const string ParamItemStatus = "ToN_ItemStatus";
const string ParamMaster = "ToN_MasterChange";

static readonly string[] ParamAll = [
ParamRoundType, ParamTerror1, ParamTerror2, ParamTerror3, ParamTPhase1, ParamTPhase2, ParamTPhase3,
ParamOptedIn, ParamSaboteur, ParamMap, ParamEncounter,
ParamTerrorColorH, ParamTerrorColorS, ParamTerrorColorV, ParamTerrorColorL,
ParamTerrorColorR, ParamTerrorColorG, ParamTerrorColorB,
ParamAlive, ParamReborn, ParamDamaged, ParamDeath, ParamPages, ParamItemStatus
ParamAlive, ParamReborn, ParamDamaged, ParamMaster, ParamDeath, ParamPages, ParamItemStatus
];
const string ParameterFileName = "osc_parameters.txt";
internal static void Initialize() {
Expand Down Expand Up @@ -186,6 +187,36 @@ private static void DeathTimer_Tick(object? sender, EventArgs e) {
}
#endregion

#region Host Change
private static Timer? HostTimer;
private static bool LastHost = false;
internal static void SendHostChange() {
if (LastHost || !MainWindow.Started || !Settings.Get.OSCMasterChange || !Settings.Get.OSCEnabled) return;

if (HostTimer == null) {
HostTimer = new Timer();
HostTimer.Tick += HostTimer_Tick;
HostTimer.Interval = Settings.Get.OSCMasterChangeInterval;
}

LastHost = true;
SendParam(ParamMaster, LastHost);

HostTimer.Stop();
if (Settings.Get.OSCMasterChangeInterval > 0) {
HostTimer.Interval = Settings.Get.OSCMasterChangeInterval;
HostTimer.Start();
} else LastHost = false;
}

private static void HostTimer_Tick(object? sender, EventArgs e) {
HostTimer?.Stop();

LastHost = false;
SendParam(ParamMaster, LastHost);
}
#endregion

private static Timer? DamageTimer;
private static int LastDamage = 0;
internal static void SetDamage(int damage) {
Expand All @@ -198,11 +229,16 @@ internal static void SetDamage(int damage) {
}

if (LastDamage != damage) {
DamageTimer.Stop();
DamageTimer.Start();

LastDamage = damage;
SendParam(ParamDamaged, damage); // change param to a const

DamageTimer.Stop();
if (Settings.Get.OSCDamagedInterval > 0) {
DamageTimer.Interval = Settings.Get.OSCDamagedInterval;
DamageTimer.Start();
} else {
LastDamage = 0;
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions Windows/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,9 @@ private bool HandleTerrorIndex(string line, DateTime timestamp, ToNLogContext co
const string STAT_STUN_LANDED = " landed a stun!";
const string STAT_STUN_TARGET = " was stunned.";
const string STAT_HIT = "Hit - ";

const string EVENT_MASTER_CHANGE = "[Behaviour] OnMasterClientSwitched";

private bool HandleStatCollection(string line, DateTime timestamp, ToNLogContext context) {
if (!context.IsRecent) return false;

Expand All @@ -893,6 +896,11 @@ private bool HandleStatCollection(string line, DateTime timestamp, ToNLogContext
}

if (Settings.Get.OSCEnabled) {
if (Settings.Get.OSCMasterChange && line.StartsWith(EVENT_MASTER_CHANGE)) {
LilOSC.SendHostChange();
return true;
}

bool isActivated = line.StartsWith("[UNSTABLE COIL] Activated!") || line.StartsWith("[EMERALD COIL] Activated!") || line.StartsWith("[CORKSCREW COIL] Activated!");
bool isDeactivated = line.StartsWith("[UNSTABLE COIL] Deactivated!") || line.StartsWith("[EMERALD COIL] Deactivated!") || line.StartsWith("[CORKSCREW COIL] Deactivated!");
if (isActivated || isDeactivated) {
Expand Down

0 comments on commit c474b27

Please sign in to comment.