Skip to content

Commit 6e07f5f

Browse files
authored
bug: fix switching (#90)
1 parent c7b7c11 commit 6e07f5f

File tree

6 files changed

+66
-49
lines changed

6 files changed

+66
-49
lines changed

src/FiveStack.Commands/Knife.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,21 @@ public void OnSkipKnife(CCSPlayerController? player, CommandInfo? command)
4848

4949
match.knifeSystem.Skip();
5050
}
51+
52+
[ConsoleCommand(
53+
"api_knife_switch",
54+
"Should only be called by the API, this is so we know the api regonized the switch"
55+
)]
56+
[CommandHelper(whoCanExecute: CommandUsage.SERVER_ONLY)]
57+
public void OnApiSwitch(CCSPlayerController player, CommandInfo command)
58+
{
59+
MatchManager? match = _matchService.GetCurrentMatch();
60+
61+
if (match == null)
62+
{
63+
return;
64+
}
65+
66+
match.knifeSystem.ConfirmSwitch();
67+
}
5168
}

src/FiveStack.Entities/MatchLineUp.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public class MatchLineUp
55
public Guid id { get; set; } = Guid.Empty;
66
public string name { get; set; } = "";
77

8+
public string team { get; set; } = "";
89
public string coach_steam_id { get; set; } = "";
910

1011
public List<MatchMember> lineup_players { get; set; } = new List<MatchMember>();

src/FiveStack.Services/KnifeSystem.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ public void Stay(CCSPlayerController player)
120120

121121
public void Switch(CCSPlayerController player)
122122
{
123-
_logger.LogInformation("Knife round switching");
124-
125123
CsTeam winningTeam = GetWinningTeam() ?? CsTeam.None;
126124
MatchManager? match = _matchService.GetCurrentMatch();
127125

@@ -149,25 +147,7 @@ public void Switch(CCSPlayerController player)
149147
$"captain picked to {ChatColors.Red}swap {ChatColors.Default}sides"
150148
);
151149

152-
_gameServer.SendCommands(new[] { "mp_swapteams" });
153-
154-
var currentMap = _matchService.GetCurrentMatch()?.GetCurrentMap();
155-
156-
if (currentMap != null)
157-
{
158-
currentMap.lineup_1_side = currentMap.lineup_1_side == "CT" ? "T" : "CT";
159-
currentMap.lineup_2_side = currentMap.lineup_2_side == "CT" ? "T" : "CT";
160-
_matchService.GetCurrentMatch()?.SetupTeamNames();
161-
}
162-
163-
Server.NextFrame(() =>
164-
{
165-
_gameServer.SendCommands(new[] { "mp_restartgame 1" });
166-
});
167-
168150
_matchEvents.PublishGameEvent("switch", new Dictionary<string, object>());
169-
170-
match.UpdateMapStatus(eMapStatus.Live);
171151
}
172152

173153
public void Skip()
@@ -186,6 +166,12 @@ public void Skip()
186166
match.UpdateMapStatus(eMapStatus.Live);
187167
}
188168

169+
public void ConfirmSwitch()
170+
{
171+
_logger.LogInformation("Knife round confirming switch");
172+
_matchService.GetMatchFromApi(eMapStatus.Live);
173+
}
174+
189175
public CsTeam? GetWinningTeam()
190176
{
191177
return _winningTeam;

src/FiveStack.Services/MatchEvents.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Net.WebSockets;
22
using System.Text;
33
using System.Text.Json;
4-
using FiveStack.Entities;
54
using FiveStack.Enums;
65
using Microsoft.Extensions.Logging;
76

@@ -13,9 +12,13 @@ public class MatchEvents
1312
private ClientWebSocket? _webSocket;
1413
private CancellationTokenSource? _connectionCts;
1514
private bool _manualDisconnect = false;
16-
private Dictionary<Guid, EventData<Dictionary<string, object>>> _pendingMessages = new();
15+
private Dictionary<
16+
Guid,
17+
(EventData<Dictionary<string, object>> Event, DateTime Timestamp)
18+
> _pendingMessages = new();
1719
private System.Timers.Timer _retryTimer;
1820
private const int RETRY_INTERVAL_MS = 5000;
21+
private const int MESSAGE_RETRY_THRESHOLD_SECONDS = 10;
1922

2023
private readonly ILogger<MatchEvents> _logger;
2124
private readonly MatchService _matchService;
@@ -239,14 +242,20 @@ await _webSocket.CloseAsync(
239242

240243
private async Task RetryPendingMessages()
241244
{
242-
var messagesToRetry = _pendingMessages.ToList();
245+
var currentTime = DateTime.UtcNow;
246+
247+
var messagesToRetry = _pendingMessages
248+
.Where(m =>
249+
(currentTime - m.Value.Timestamp).TotalSeconds >= MESSAGE_RETRY_THRESHOLD_SECONDS
250+
)
251+
.ToList();
243252

244253
foreach (var message in messagesToRetry)
245254
{
246255
try
247256
{
248257
var jsonMessage = JsonSerializer.Serialize(
249-
new { @event = "events", data = message.Value }
258+
new { @event = "events", data = message.Value.Event }
250259
);
251260
var buffer = Encoding.UTF8.GetBytes(jsonMessage);
252261

@@ -259,7 +268,7 @@ await _webSocket.SendAsync(
259268
_connectionCts?.Token ?? CancellationToken.None
260269
);
261270

262-
_pendingMessages[message.Key] = message.Value;
271+
_pendingMessages[message.Key] = (message.Value.Event, currentTime);
263272
}
264273
}
265274
catch (Exception ex)
@@ -282,7 +291,7 @@ private async Task Publish<T>(Guid matchId, EventData<T> data)
282291

283292
if (data is EventData<Dictionary<string, object>> typedData)
284293
{
285-
_pendingMessages[data.messageId] = typedData;
294+
_pendingMessages[data.messageId] = (typedData, DateTime.UtcNow);
286295
}
287296

288297
try

src/FiveStack.Services/MatchManager.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void UpdateMapStatus(eMapStatus status)
240240
_currentMapStatus = status;
241241
}
242242

243-
public void SetupMatch(MatchData match)
243+
public void SetupMatch(MatchData match, eMapStatus? forceState = null)
244244
{
245245
_matchData = match;
246246

@@ -259,6 +259,11 @@ public void SetupMatch(MatchData match)
259259
return;
260260
}
261261

262+
if (forceState != null)
263+
{
264+
_currentMap.status = forceState.Value.ToString();
265+
}
266+
262267
_logger.LogInformation(
263268
$"Game State {_currentMap.status} on ({_currentMap.map.name}) / {Server.MapName}"
264269
);
@@ -286,16 +291,19 @@ public void SetupMatch(MatchData match)
286291

287292
SetupTeamNames();
288293

289-
if (MatchUtility.MapStatusStringToEnum(_currentMap.status) != _currentMapStatus)
290-
{
291-
UpdateMapStatus(MatchUtility.MapStatusStringToEnum(_currentMap.status));
292-
}
293-
294294
foreach (var player in MatchUtility.Players())
295295
{
296296
EnforceMemberTeam(player);
297297
}
298298

299+
Server.NextFrame(() =>
300+
{
301+
if (MatchUtility.MapStatusStringToEnum(_currentMap.status) != _currentMapStatus)
302+
{
303+
UpdateMapStatus(MatchUtility.MapStatusStringToEnum(_currentMap.status));
304+
}
305+
});
306+
299307
if (IsWarmup())
300308
{
301309
_gameServer.Message(HudDestination.Alert, "Received Match Data");
@@ -490,6 +498,13 @@ public async void EnforceMemberTeam(CCSPlayerController player, CsTeam? currentT
490498
MatchData? matchData = GetMatchData();
491499
MatchMap? currentMap = GetCurrentMap();
492500

501+
CsTeam lineup1StartingSide = TeamUtility.TeamStringToCsTeam(
502+
currentMap?.lineup_1_side ?? CsTeam.CounterTerrorist.ToString()
503+
);
504+
CsTeam lineup2StartingSide = TeamUtility.TeamStringToCsTeam(
505+
currentMap?.lineup_2_side ?? CsTeam.Terrorist.ToString()
506+
);
507+
493508
if (matchData == null || currentMap == null)
494509
{
495510
return;
@@ -536,20 +551,8 @@ public async void EnforceMemberTeam(CCSPlayerController player, CsTeam? currentT
536551
currentTeam = player.Team;
537552
}
538553

539-
CsTeam expectedTeam = CsTeam.None;
540-
541-
string lineupName =
542-
matchData.lineup_1_id == lineup_id
543-
? matchData.lineup_1.name
544-
: matchData.lineup_2.name;
545-
546-
foreach (var team in MatchUtility.Teams())
547-
{
548-
if (team.ClanTeamname == lineupName)
549-
{
550-
expectedTeam = TeamUtility.TeamNumToCSTeam(team.TeamNum);
551-
}
552-
}
554+
CsTeam expectedTeam =
555+
matchData.lineup_1_id == lineup_id ? lineup1StartingSide : lineup2StartingSide;
553556

554557
if (expectedTeam == CsTeam.None)
555558
{

src/FiveStack.Services/MatchService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json;
22
using CounterStrikeSharp.API;
33
using FiveStack.Entities;
4+
using FiveStack.Enums;
45
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.Logging;
67

@@ -55,7 +56,7 @@ var file in new[]
5556
}
5657
}
5758

58-
public async void GetMatchFromApi()
59+
public async void GetMatchFromApi(eMapStatus? forceState = null)
5960
{
6061
HttpClient httpClient = new HttpClient();
6162

@@ -101,14 +102,14 @@ public async void GetMatchFromApi()
101102

102103
if (_currentMatch?.GetMatchData()?.id == matchData.id)
103104
{
104-
_currentMatch.SetupMatch(matchData);
105+
_currentMatch.SetupMatch(matchData, forceState);
105106
return;
106107
}
107108

108109
_currentMatch =
109110
_serviceProvider.GetRequiredService(typeof(MatchManager)) as MatchManager;
110111

111-
_currentMatch!.SetupMatch(matchData);
112+
_currentMatch!.SetupMatch(matchData, forceState);
112113
});
113114
}
114115
catch (HttpRequestException ex)

0 commit comments

Comments
 (0)