-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTeamLimit.cs
102 lines (84 loc) · 3.92 KB
/
TeamLimit.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
using BattleBitAPI.Common;
using BattleBitAPI.Features;
using BBRAPIModules;
using Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BBRModules
{
[Module("A module made to handle team limits and team swapping.", "1.0.0")]
[RequireModule(typeof(PlaceholderLib))]
public class TeamLimit : BattleBitModule
{
public static TeamLimitConfiguration Configuration { get; set; }
public PlaceholderLib PlaceholderLib = null!;
public override async Task<bool> OnPlayerRequestingToChangeTeam(RunnerPlayer player, Team requestedTeam)
{
if (Configuration.DenyAllTeamSwapping)
{
string message = new PlaceholderLib(Configuration.NoSwappingMessage)
.Build();
player.SayToChat(message);
return false;
}
int teamCount = GetTeamPlayerCount(requestedTeam);
int playersOnline = GetPlayersOnline();
int extraPlayers = GetExtraPlayerCount();
if (teamCount >= ((int)(playersOnline / 2) + extraPlayers))
{
string message = new PlaceholderLib(Configuration.TeamFullMessage).AddParam("maxPlayers", teamCount + extraPlayers)
.Build();
player.SayToChat(message);
return false;
}
return true;
}
[CommandCallback("balance", Description = "Force balance the teams.", Permissions = new[] { "TeamLimit.Balance" })]
public void BalanceCommand(RunnerPlayer player)
{
}
private Team GetBiggestTeam()
{
int teamACount = Server.AllTeamAPlayers.Count();
int teamBCount = Server.AllTeamBPlayers.Count();
return teamACount > teamBCount ? Team.TeamA : Team.TeamB;
}
private Team GetSmallestTeam()
{
int teamACount = Server.AllTeamAPlayers.Count();
int teamBCount = Server.AllTeamBPlayers.Count();
return teamACount > teamBCount ? Team.TeamB : Team.TeamA;
}
private int GetExtraPlayerCount()
{
string mapSize = Server.MapSize.ToString().Substring(1);
if (!Configuration.ForMapSizes.ContainsKey(mapSize) || Configuration.ForMapSizes[mapSize] == -1)
return Configuration.ExtraPlayerCount;
return Configuration.ForMapSizes[mapSize];
}
private int GetTeamPlayerCount(Team team) => team == Team.TeamA ? Server.AllTeamAPlayers.Count() : Server.AllTeamBPlayers.Count();
private int GetPlayersOnline() => Server.AllPlayers.Count();
}
public class TeamLimitConfiguration : ModuleConfiguration
{
// Do not allow anyone to swap, whatsoever.
public bool DenyAllTeamSwapping { get; set; } = false;
// How many players over the limit (half the number of players online) would you allow? (e.g. player count is 32, limit is 2, so team limit is now 34)
public int ExtraPlayerCount { get; set; } = 2;
// The message to send when a player tries to swap to a team that is full (DenyAllTeamSwapping is false)
public string TeamFullMessage { get; set; } = "{#ffaaaa}[SERVER]{/} You cannot swap teams as it is full! (Limit: {#ffaaaa}{maxPlayers}{/})";
// The message to send when a player tries to change teams (DenyAllTeamSwapping is true)
public string NoSwappingMessage { get; set; } = "{#ffaaaa}[SERVER]{/} Swapping teams is disabled on this server.";
public Dictionary<string, int> ForMapSizes { get; set; } = new()
{
// Any number that isn't -1 will set the extra player count for that map size. Not add, set. 0 means they can only swap teams if it is unbalanced.
{"127vs127", 4},
{"64vs64", 4},
{"32vs32", -1},
{"16vs16", 0},
{"8vs8", 0},
};
}
}