forked from wfxjz/Nixware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeamSwitchManager.cs
107 lines (89 loc) · 3.26 KB
/
TeamSwitchManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using CounterStrikeSharp.API.Core;
using ChaseMod.Utils;
using CounterStrikeSharp.API.Modules.Utils;
using ChaseMod.Utils.Memory;
namespace ChaseMod;
internal class TeamSwitchManager
{
private bool _switchingTeams;
private int _terroristWinstreak;
private CsTeam _lastWinningTeam = CsTeam.Terrorist;
private bool _shouldSwitchTeams;
private readonly ChaseMod _plugin;
public TeamSwitchManager(ChaseMod chaseMod)
{
_plugin = chaseMod;
}
public void Start()
{
// TODO: Combine this _lastWinningTeam check logic in separate function since we're duplicating the logic atm.
_plugin.RegisterEventHandler<EventRoundEnd>((@event, info) =>
{
_lastWinningTeam = (CsTeam)@event.Winner;
_shouldSwitchTeams = false;
switch (_lastWinningTeam)
{
case CsTeam.CounterTerrorist:
ChaseModUtils.ChatAllPrefixed($"{ChatColors.Blue}CT {ChatColors.Grey}Win - Teams are being switched.");
_terroristWinstreak = 0;
_shouldSwitchTeams = true;
break;
case CsTeam.Terrorist:
_terroristWinstreak++;
if (_plugin.Config.MaxTerroristWinStreak > 0 && _terroristWinstreak >= _plugin.Config.MaxTerroristWinStreak)
{
ChaseModUtils.ChatAllPrefixed($"{ChatColors.Yellow}T {ChatColors.Grey}Win - Teams are being switched due to winstreak. ({_terroristWinstreak} wins in a row)");
_terroristWinstreak = 0;
_shouldSwitchTeams = true;
}
else
{
ChaseModUtils.ChatAllPrefixed($"{ChatColors.Yellow}T {ChatColors.Grey}Win");
}
break;
}
return HookResult.Continue;
});
_plugin.RegisterEventHandler<EventRoundPrestart>((@event, info) =>
{
if (_shouldSwitchTeams)
{
SwitchTeams();
}
return HookResult.Continue;
});
_plugin.RegisterEventHandler<EventPlayerTeam>((@event, info) =>
{
if (_switchingTeams)
{
info.DontBroadcast = true;
}
return HookResult.Continue;
}, HookMode.Pre);
_plugin.RegisterListener<Listeners.OnMapEnd>(() =>
{
_terroristWinstreak = 0;
});
}
private void SwitchTeams()
{
_switchingTeams = true;
CCSMatch.SwapTeamScores(
ChaseModUtils.GetGameRules()
);
foreach (var controller in ChaseModUtils.GetAllRealPlayers())
{
switch (controller.Team)
{
case CsTeam.CounterTerrorist:
controller.SwitchTeam(CsTeam.Terrorist);
break;
case CsTeam.Terrorist:
controller.SwitchTeam(CsTeam.CounterTerrorist);
break;
}
controller.RemoveAllItemsOnNextRoundReset = true;
}
_switchingTeams = false;
}
}