|
7 | 7 | using PugSharp.ApiStats;
|
8 | 8 | using PugSharp.Match.Contract;
|
9 | 9 | using PugSharp.Server.Contract;
|
| 10 | +using PugSharp.Shared; |
10 | 11 | using PugSharp.Translation;
|
11 | 12 | using PugSharp.Translation.Properties;
|
12 | 13 |
|
@@ -85,7 +86,7 @@ private void Initialize(MatchInfo matchInfo)
|
85 | 86 | throw new NotSupportedException("Initialize can onyl be called once!");
|
86 | 87 | }
|
87 | 88 |
|
88 |
| - if (matchInfo.Config.Maplist.Length < matchInfo.Config.NumMaps) |
| 89 | + if (matchInfo.Config.Maplist.Count < matchInfo.Config.NumMaps) |
89 | 90 | {
|
90 | 91 | throw new NotSupportedException($"Can not create Match without the required number of maps! At lease {matchInfo.Config.NumMaps} are required!");
|
91 | 92 | }
|
@@ -129,12 +130,17 @@ private void InitializeStateMachine()
|
129 | 130 | .PermitDynamicIf(MatchCommand.LoadMatch, () => HasRestoredMatch() ? MatchState.RestoreMatch : MatchState.WaitingForPlayersConnectedReady);
|
130 | 131 |
|
131 | 132 | _MatchStateMachine.Configure(MatchState.WaitingForPlayersConnectedReady)
|
132 |
| - .PermitDynamicIf(MatchCommand.PlayerReady, () => HasRestoredMatch() ? MatchState.MatchRunning : MatchState.MapVote, AllPlayersAreReady) |
| 133 | + .PermitDynamicIf(MatchCommand.PlayerReady, () => HasRestoredMatch() ? MatchState.MatchRunning : MatchState.DefineTeams, AllPlayersAreReady) |
133 | 134 | .OnEntry(StartWarmup)
|
134 | 135 | .OnEntry(SetAllPlayersNotReady)
|
135 | 136 | .OnEntry(StartReadyReminder)
|
136 | 137 | .OnExit(StopReadyReminder);
|
137 | 138 |
|
| 139 | + _MatchStateMachine.Configure(MatchState.DefineTeams) |
| 140 | + .Permit(MatchCommand.TeamsDefined, MatchState.MapVote) |
| 141 | + .OnEntry(ContinueIfDefault) |
| 142 | + .OnEntry(ScrambleTeams); |
| 143 | + |
138 | 144 | _MatchStateMachine.Configure(MatchState.MapVote)
|
139 | 145 | .PermitReentryIf(MatchCommand.VoteMap, MapIsNotSelected)
|
140 | 146 | .PermitIf(MatchCommand.VoteMap, MatchState.TeamVote, MapIsSelected)
|
@@ -200,6 +206,30 @@ private void InitializeStateMachine()
|
200 | 206 | _MatchStateMachine.Fire(MatchCommand.LoadMatch);
|
201 | 207 | }
|
202 | 208 |
|
| 209 | + private void ScrambleTeams() |
| 210 | + { |
| 211 | + if (MatchInfo.Config.TeamMode == Config.TeamMode.Scramble) |
| 212 | + { |
| 213 | + var randomizedPlayers = AllMatchPlayers.Randomize().ToList(); |
| 214 | + |
| 215 | + MatchInfo.MatchTeam1.Players.Clear(); |
| 216 | + MatchInfo.MatchTeam1.Players.AddRange(randomizedPlayers.Take(randomizedPlayers.Count.Half())); |
| 217 | + |
| 218 | + MatchInfo.MatchTeam2.Players.Clear(); |
| 219 | + MatchInfo.MatchTeam2.Players.AddRange(randomizedPlayers.Skip(randomizedPlayers.Count.Half())); |
| 220 | + |
| 221 | + TryFireState(MatchCommand.TeamsDefined); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + private void ContinueIfDefault() |
| 226 | + { |
| 227 | + if (MatchInfo.Config.TeamMode == Config.TeamMode.Default) |
| 228 | + { |
| 229 | + TryFireState(MatchCommand.TeamsDefined); |
| 230 | + } |
| 231 | + } |
| 232 | + |
203 | 233 | private void StartWarmup()
|
204 | 234 | {
|
205 | 235 | _CsServer.LoadAndExecuteConfig("warmup.cfg");
|
@@ -664,7 +694,7 @@ private void SendRemainingMapsToVotingTeam()
|
664 | 694 | }
|
665 | 695 |
|
666 | 696 | // If only one map is configured
|
667 |
| - if (MatchInfo.Config.Maplist.Length == 1) |
| 697 | + if (MatchInfo.Config.Maplist.Count == 1) |
668 | 698 | {
|
669 | 699 | _MapsToSelect = MatchInfo.Config.Maplist.Select(x => new Vote(x)).ToList();
|
670 | 700 | TryFireState(MatchCommand.VoteMap);
|
@@ -900,12 +930,18 @@ private MatchPlayer GetMatchPlayer(ulong steamID)
|
900 | 930 |
|
901 | 931 | public bool TryAddPlayer(IPlayer player)
|
902 | 932 | {
|
| 933 | + if (!PlayerBelongsToMatch(player.SteamID)) |
| 934 | + { |
| 935 | + _Logger.LogInformation("Player with steam id {steamId} is no member of this match!", player.SteamID); |
| 936 | + return false; |
| 937 | + } |
| 938 | + |
903 | 939 | var isTeam1 = MatchInfo.Config.Team1.Players.ContainsKey(player.SteamID);
|
904 | 940 | var isTeam2 = !isTeam1 && MatchInfo.Config.Team2.Players.ContainsKey(player.SteamID);
|
905 | 941 | if (!isTeam1 && !isTeam2)
|
906 | 942 | {
|
907 |
| - _Logger.LogInformation("Player with steam id {steamId} is no member of this match!", player.SteamID); |
908 |
| - return false; |
| 943 | + // if no team is configured add player to team with less players |
| 944 | + isTeam1 = MatchInfo.MatchTeam1.Players.Count < MatchInfo.MatchTeam2.Players.Count; |
909 | 945 | }
|
910 | 946 |
|
911 | 947 | var team = isTeam1 ? MatchInfo.MatchTeam1 : MatchInfo.MatchTeam2;
|
@@ -1205,6 +1241,12 @@ public void CompleteMap(int tPoints, int ctPoints)
|
1205 | 1241 |
|
1206 | 1242 | public bool PlayerBelongsToMatch(ulong steamId)
|
1207 | 1243 | {
|
| 1244 | + if (MatchInfo.Config.Team1.Players.Count == 0 && MatchInfo.Config.Team2.Players.Count == 0) |
| 1245 | + { |
| 1246 | + // Allow matches without player configuration wait for the first 10 players |
| 1247 | + return true; |
| 1248 | + } |
| 1249 | + |
1208 | 1250 | return MatchInfo.Config.Team1.Players.Any(x => x.Key.Equals(steamId))
|
1209 | 1251 | || MatchInfo.Config.Team2.Players.Any(x => x.Key.Equals(steamId));
|
1210 | 1252 | }
|
|
0 commit comments