Skip to content

Commit

Permalink
Commander Management Lottery Code Cleanup
Browse files Browse the repository at this point in the history
- Minor code cleanup in commander management
- Removal of OnGameInit calls which were not firing each round
  • Loading branch information
data-bomb committed Feb 13, 2024
1 parent 6902009 commit dc1427a
Showing 1 changed file with 39 additions and 104 deletions.
143 changes: 39 additions & 104 deletions Si_CommManagement/Si_CmdrMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ You should have received a copy of the GNU General Public License
using SilicaAdminMod;
using System.Linq;

[assembly: MelonInfo(typeof(CommanderManager), "Commander Management", "1.4.5", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(CommanderManager), "Commander Management", "1.4.6", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]
[assembly: MelonOptionalDependencies("Admin Mod")]

Expand Down Expand Up @@ -82,7 +82,6 @@ public String? Comments

static List<Player>[] commanderApplicants = null!;
static List<Player>? previousCommanders;
static bool bOnGameInitFired;

static Player?[]? teamswapCommanderChecks;
static Player[]? promotedCommanders;
Expand Down Expand Up @@ -143,8 +142,6 @@ public override void OnInitializeMelon()

CommanderManager.teamswapCommanderChecks = new Player[MaxTeams];
CommanderManager.promotedCommanders = new Player[MaxTeams];

bOnGameInitFired = false;
}
catch (Exception error)
{
Expand Down Expand Up @@ -201,125 +198,46 @@ public static void SendToRole(Player FormerCommander, MP_Strategy.ETeamRole role
GameMode.CurrentGameMode.SendRPCPacket(theRoleStream);
}

// may need to re-think this approach on preventing commander promotion

/*[HarmonyPatch(typeof(MP_Strategy), nameof(MP_Strategy.GetStrategyCommanderTeamSetup))]
private static class ApplyPatchCommanderTeamSetup
{
public static bool Prefix(MP_Strategy __instance, StrategyTeamSetup? __result, Player? __0)
{
try
{
if (__0 == null)
{
return true;
}
if (MasterBanList == null)
{
return true;
}
// check if player is allowed to be commander
long joiningPlayerSteamId = long.Parse(__0.ToString().Split('_')[1]);
BanEntry? banEntry = MasterBanList.Find(i => i.OffenderSteamId == joiningPlayerSteamId);
if (banEntry != null)
{
MelonLogger.Msg("Preventing " + banEntry.OffenderName + " from selecting commander.");
__0 = null;
__result = null;
return false;
}
}
catch (Exception error)
{
HelperMethods.PrintError(error, "Failed to run MP_Strategy::GetStrategyCommanderTeamSetup");
}
return true;
}
}*/

[HarmonyPatch(typeof(MusicJukeboxHandler), nameof(MusicJukeboxHandler.OnGameInit))]
private static class ApplyPatchOnGameInit
[HarmonyPatch(typeof(MusicJukeboxHandler), nameof(MusicJukeboxHandler.OnGameStarted))]
private static class ApplyPatchOnGameStarted
{
public static void Postfix(MusicJukeboxHandler __instance, GameMode __0)
{
try
{
if (commanderApplicants == null || teamswapCommanderChecks == null || previousCommanders == null)
if (commanderApplicants == null || previousCommanders == null || promotedCommanders == null)
{
return;
}

// prevent from running twice in one round switch cycle
if (!bOnGameInitFired)
{
bOnGameInitFired = true;
System.Random randomIndex = new System.Random();
Player? RemovePlayer = null;

int NumCommandersPastRound = 0;
for (int i = 0; i < MaxTeams; i++)
int NumCommandersPastRound = 0;
for (int i = 0; i < MaxTeams; i++)
{
if (commanderApplicants[i].Count > 0)
{
if (commanderApplicants[i].Count > 0)
{
MelonLogger.Msg("Clearing applicants from team index " + i.ToString());
commanderApplicants[i].Clear();

NumCommandersPastRound++;

// clear previous commander tracking status, if any
teamswapCommanderChecks[i] = null;
}
NumCommandersPastRound++;
}
}


// we want to remove the oldest commanders from the list
int NumCommandersToRemove = previousCommanders.Count - NumCommandersPastRound;
if (NumCommandersToRemove < 0)
for (int i = 0; i < MaxTeams; i++)
{
// clear previous commander tracking status, if any
if (teamswapCommanderChecks != null)
{
MelonLogger.Warning("Logic error. NumCommandersToRemove is " + NumCommandersToRemove.ToString());
NumCommandersPastRound = 0;
teamswapCommanderChecks[i] = null;
}

if (CommanderManager.previousCommanders.Count > NumCommandersToRemove)
if (Team.Teams[i] == null)
{
// remove the commanders from 2 rounds ago. first entry is the oldest.
for (int i = 0; i < NumCommandersToRemove; i++)
{
CommanderManager.previousCommanders.RemoveAt(i);
}
// account for transitions from 2 to 3 team rounds
commanderApplicants[i].Clear();
continue;
}
}
}
catch (Exception error)
{
HelperMethods.PrintError(error, "Failed to run MusicJukeboxHandler::OnGameInit");
}
}
}

[HarmonyPatch(typeof(MusicJukeboxHandler), nameof(MusicJukeboxHandler.OnGameStarted))]
private static class ApplyPatchOnGameStarted
{
public static void Postfix(MusicJukeboxHandler __instance, GameMode __0)
{
try
{
bOnGameInitFired = false;

if (commanderApplicants == null || previousCommanders == null || promotedCommanders == null)
{
return;
}

// *** TODO: need to account for if a player leaves the game within the 30 second window
System.Random randomIndex = new System.Random();
Player? RemovePlayer = null;

for (int i = 0; i < MaxTeams; i++)
{
if (Team.Teams[i] == null || commanderApplicants[i].Count == 0)
if (commanderApplicants[i].Count == 0)
{
continue;
}
Expand Down Expand Up @@ -373,6 +291,23 @@ public static void Postfix(MusicJukeboxHandler __instance, GameMode __0)
commanderApplicants[i].Clear();
MelonLogger.Msg("Clearing commander lottery for team " + Team.Teams[i].TeamName);
}

// we want to remove the oldest commanders from the list
int NumCommandersToRemove = previousCommanders.Count - NumCommandersPastRound;
if (NumCommandersToRemove < 0)
{
MelonLogger.Warning("Logic error. NumCommandersToRemove is " + NumCommandersToRemove.ToString());
NumCommandersPastRound = 0;
}

if (CommanderManager.previousCommanders.Count > NumCommandersToRemove)
{
// remove the commanders from 2 rounds ago. first entry is the oldest.
for (int i = 0; i < NumCommandersToRemove; i++)
{
CommanderManager.previousCommanders.RemoveAt(i);
}
}
}
catch (Exception error)
{
Expand Down

0 comments on commit dc1427a

Please sign in to comment.