Skip to content

Commit

Permalink
Changes Timers for AFK Mod
Browse files Browse the repository at this point in the history
- Changes away from System.Timers for the AFK mod
  • Loading branch information
data-bomb committed Mar 20, 2024
1 parent 4384dab commit ae876f7
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions Si_AFKManager/Si_AFK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ You should have received a copy of the GNU General Public License
using HarmonyLib;
using MelonLoader;
using Si_AFKManager;
using System.Timers;
using System.Collections.Generic;
using System.Linq;
using SilicaAdminMod;
using System;
using System.Collections;
using UnityEngine;

[assembly: MelonInfo(typeof(AwayFromKeyboard), "AFK Manager", "1.2.6", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(AwayFromKeyboard), "AFK Manager", "1.2.7", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]
[assembly: MelonOptionalDependencies("Admin Mod")]

Expand All @@ -59,10 +59,9 @@ public uint Minutes
}
}

static Timer afkTimer = null!;
static float Timer_AFKCheck = HelperMethods.Timer_Inactive;
static bool AdminModAvailable = false;
static List<AFKCount> AFKTracker = null!;
static bool oneMinuteCheckTime;
static bool skippedFirstCheck;

static MelonPreferences_Category _modCategory = null!;
Expand All @@ -78,6 +77,8 @@ public override void OnInitializeMelon()

public override void OnLateInitializeMelon()
{
HelperMethods.StartTimer(Timer_AFKCheck);

Check failure on line 80 in Si_AFKManager/Si_AFK.cs

View workflow job for this annotation

GitHub Actions / build (Si_AFKManager)

Argument 1 must be passed with the 'ref' keyword

Check failure on line 80 in Si_AFKManager/Si_AFK.cs

View workflow job for this annotation

GitHub Actions / build (Si_AFKManager)

Argument 1 must be passed with the 'ref' keyword

Check failure on line 80 in Si_AFKManager/Si_AFK.cs

View workflow job for this annotation

GitHub Actions / build (Si_AFKManager)

Argument 1 must be passed with the 'ref' keyword

Check failure on line 80 in Si_AFKManager/Si_AFK.cs

View workflow job for this annotation

GitHub Actions / build (Si_AFKManager)

Argument 1 must be passed with the 'ref' keyword

AdminModAvailable = RegisteredMelons.Any(m => m.Info.Name == "Admin Mod");

AFKTracker = new List<AFKCount>();
Expand All @@ -87,12 +88,6 @@ public override void OnLateInitializeMelon()
HelperMethods.RegisterAdminCommand("kick", kickCallback, Power.Kick);
HelperMethods.RegisterAdminCommand("afk", afkCallback, Power.Kick);

double interval = 60000.0f;
afkTimer = new System.Timers.Timer(interval);
afkTimer.Elapsed += new ElapsedEventHandler(TimerCallbackOneMinute);
afkTimer.AutoReset = true;
afkTimer.Enabled = true;

#if NET6_0
bool QListLoaded = RegisteredMelons.Any(m => m.Info.Name == "QList");
if (!QListLoaded)
Expand Down Expand Up @@ -234,11 +229,6 @@ public static void Command_AFK(Player callerPlayer, String args)
}
}

private static void TimerCallbackOneMinute(object? source, ElapsedEventArgs e)
{
oneMinuteCheckTime = true;
}

#if NET6_0
[HarmonyPatch(typeof(MusicJukeboxHandler), nameof(MusicJukeboxHandler.Update))]
#else
Expand All @@ -256,11 +246,22 @@ private static void Postfix(MusicJukeboxHandler __instance)
}

// check if timer expired while the game is in-progress
if (GameMode.CurrentGameMode.GameOngoing == true && oneMinuteCheckTime == true && skippedFirstCheck == true)
Timer_AFKCheck += Time.deltaTime;
if (Timer_AFKCheck >= 60.0f)
{
//MelonLogger.Msg("AFK Timer fired");
Timer_AFKCheck = 0.0f;

oneMinuteCheckTime = false;
if (!GameMode.CurrentGameMode.GameOngoing)
{
return;
}

// skip the first timer expiration so we're at least a minute into the round
if (!skippedFirstCheck)
{
skippedFirstCheck = true;
return;
}

// track if any players need to be removed from the AFKTracker list after we've finished iterating
// we can't kick inside the foreach Players iterator because it modifies the list
Expand Down Expand Up @@ -334,13 +335,6 @@ private static void Postfix(MusicJukeboxHandler __instance)
AFKTracker.RemoveAt(indexToKick);
}
}

// skip the first timer expiration so we're at least a minute into the round
if (GameMode.CurrentGameMode.GameOngoing == true && oneMinuteCheckTime == true && skippedFirstCheck == false)
{
oneMinuteCheckTime = false;
skippedFirstCheck = true;
}
}
catch (Exception exception)
{
Expand Down

0 comments on commit ae876f7

Please sign in to comment.