Skip to content

Commit c459272

Browse files
committed
2 parents 20edfd4 + b961d48 commit c459272

File tree

3 files changed

+82
-74
lines changed

3 files changed

+82
-74
lines changed

TarkovMonitor/GameWatcher.cs

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ internal class GameWatcher
2626
public event EventHandler GroupDisbanded;
2727
public event EventHandler<GroupUserLeaveEventArgs> GroupUserLeave;
2828
public event EventHandler MapLoading;
29-
public event EventHandler<MatchingStartedEventArgs> MatchingStarted;
30-
public event EventHandler<MatchFoundEventArgs> MatchFound; // only fires on initial load into a raid
31-
public event EventHandler<MatchFoundEventArgs> MapLoaded; // fires on initial and subsequent loads into a raid
32-
public event EventHandler<MatchingCancelledEventArgs> MatchingAborted;
33-
public event EventHandler<RaidLoadedEventArgs> RaidLoaded;
29+
public event EventHandler<RaidInfoEventArgs> MatchingStarted;
30+
public event EventHandler<RaidInfoEventArgs> MatchFound; // only fires on initial load into a raid
31+
public event EventHandler<RaidInfoEventArgs> MapLoaded; // fires on initial and subsequent loads into a raid
32+
public event EventHandler<RaidInfoEventArgs> MatchingAborted;
33+
public event EventHandler<RaidInfoEventArgs> RaidCountdown;
34+
public event EventHandler<RaidInfoEventArgs> RaidStarted;
3435
public event EventHandler<RaidExitedEventArgs> RaidExited;
3536
public event EventHandler<TaskModifiedEventArgs> TaskModified;
3637
public event EventHandler<TaskEventArgs> TaskStarted;
@@ -98,21 +99,27 @@ private void ScreenshotWatcher_FolderCreated(object sender, FileSystemEventArgs
9899
}
99100
private void ScreenshotWatcher_Created(object sender, FileSystemEventArgs e)
100101
{
101-
var match = Regex.Match(e.Name, @"\d{4}-\d{2}-\d{2}\[\d{2}-\d{2}\]_(?<position>.+) \(\d\)\.png");
102-
if (!match.Success)
103-
{
104-
return;
105-
}
106-
var position = Regex.Match(match.Groups["position"].Value, @"(?<x>-?[\d.]+), (?<y>-?[\d.]+), (?<z>-?[\d.]+)_.*");
107-
if (!position.Success)
102+
try
108103
{
109-
return;
110-
}
111-
if (lastKnownMap == null)
104+
var match = Regex.Match(e.Name, @"\d{4}-\d{2}-\d{2}\[\d{2}-\d{2}\]_(?<position>.+) \(\d\)\.png");
105+
if (!match.Success)
106+
{
107+
return;
108+
}
109+
var position = Regex.Match(match.Groups["position"].Value, @"(?<x>-?[\d.]+), (?<y>-?[\d.]+), (?<z>-?[\d.]+)_.*");
110+
if (!position.Success)
111+
{
112+
return;
113+
}
114+
if (lastKnownMap == null)
115+
{
116+
return;
117+
}
118+
PlayerPosition?.Invoke(this, new(lastKnownMap, new Position(position.Groups["x"].Value, position.Groups["y"].Value, position.Groups["z"].Value)));
119+
} catch (Exception ex)
112120
{
113-
//return;
121+
ExceptionThrown?.Invoke(this, new ExceptionEventArgs(ex, $"parsing screenshot {e.Name}"));
114122
}
115-
PlayerPosition?.Invoke(this, new(lastKnownMap, new Position(position.Groups["x"].Value, position.Groups["y"].Value, position.Groups["z"].Value)));
116123
}
117124

118125
public void Start()
@@ -196,7 +203,7 @@ internal void GameWatcher_NewLogData(object? sender, NewLogDataEventArgs e)
196203
// The map has been loaded and the game is searching for a match
197204
raidInfo = new()
198205
{
199-
MapLoadTime = float.Parse(Regex.Match(eventLine, @"LocationLoaded:[0-9.]+ real:(?<loadTime>[0-9.]+)").Groups["loadTime"].Value)
206+
MapLoadTime = float.Parse(Regex.Match(eventLine, @"LocationLoaded:[0-9.,]+ real:(?<loadTime>[0-9.,]+)").Groups["loadTime"].Value.Replace(",", "."))
200207
};
201208
MatchingStarted?.Invoke(this, new(raidInfo));
202209
}
@@ -206,8 +213,8 @@ internal void GameWatcher_NewLogData(object? sender, NewLogDataEventArgs e)
206213
// Just the queue time is available so far
207214
// Occurs on initial raid load and when the user cancels matching
208215
// Does not occur when the user re-connects to a raid in progress
209-
var queueTimeMatch = Regex.Match(eventLine, @"MatchingCompleted:[0-9.]+ real:(?<queueTime>[0-9.]+)");
210-
raidInfo.QueueTime = float.Parse(queueTimeMatch.Groups["queueTime"].Value);
216+
var queueTimeMatch = Regex.Match(eventLine, @"MatchingCompleted:[0-9.,]+ real:(?<queueTime>[0-9.,]+)");
217+
raidInfo.QueueTime = float.Parse(queueTimeMatch.Groups["queueTime"].Value.Replace(",", "."));
211218
}
212219
if (eventLine.Contains("application|TRACE-NetworkGameCreate profileStatus"))
213220
{
@@ -228,10 +235,7 @@ internal void GameWatcher_NewLogData(object? sender, NewLogDataEventArgs e)
228235
{
229236
// The raid start countdown begins. Only happens for PMCs.
230237
raidInfo.RaidType = RaidType.PMC;
231-
if (raidInfo.Online)
232-
{
233-
RaidLoaded?.Invoke(this, new(raidInfo));
234-
}
238+
RaidCountdown?.Invoke(this, new(raidInfo));
235239
}
236240
if (eventLine.Contains("application|GameStarted"))
237241
{
@@ -244,8 +248,9 @@ internal void GameWatcher_NewLogData(object? sender, NewLogDataEventArgs e)
244248
if (raidInfo.Online && raidInfo.RaidType != RaidType.PMC)
245249
{
246250
// We already raised the RaidLoaded event for PMC, so only raise here if not PMC
247-
RaidLoaded?.Invoke(this, new(raidInfo));
251+
//RaidStarted?.Invoke(this, new(raidInfo));
248252
}
253+
RaidStarted?.Invoke(this, new(raidInfo));
249254
raidInfo = new();
250255
}
251256
if (eventLine.Contains("application|Network game matching aborted") || eventLine.Contains("application|Network game matching cancelled"))
@@ -593,42 +598,14 @@ public override string ToString()
593598
return $"{this.PlayerInfo.Nickname} ({this.PlayerLoadout.Info.Side}, {this.PlayerLoadout.Info.Level})";
594599
}
595600
}
596-
public class MatchingStartedEventArgs : EventArgs
601+
public class RaidInfoEventArgs : EventArgs
597602
{
598-
public float MapLoadTime { get; set; }
599-
public MatchingStartedEventArgs(RaidInfo raidInfo)
600-
{
601-
MapLoadTime = raidInfo.MapLoadTime;
602-
}
603-
}
604-
public class MatchingCancelledEventArgs : MatchingStartedEventArgs
605-
{
606-
public float QueueTime { get; set; }
607-
public MatchingCancelledEventArgs(RaidInfo raidInfo) : base(raidInfo)
608-
{
609-
QueueTime = raidInfo.QueueTime;
610-
}
611-
}
612-
public class MatchFoundEventArgs : MatchingStartedEventArgs
613-
{
614-
public string Map { get; set; }
615-
public string RaidId { get; set; }
616-
public float QueueTime { get; set; }
617-
public MatchFoundEventArgs(RaidInfo raidInfo) : base(raidInfo)
603+
public RaidInfo RaidInfo { get; set; }
604+
public RaidInfoEventArgs(RaidInfo raidInfo)
618605
{
619-
Map = raidInfo.Map;
620-
RaidId = raidInfo.RaidId;
621-
QueueTime = raidInfo.QueueTime;
606+
RaidInfo = raidInfo;
622607
}
623608
}
624-
public class RaidLoadedEventArgs : MatchFoundEventArgs
625-
{
626-
public RaidType RaidType { get; set; }
627-
public RaidLoadedEventArgs(RaidInfo raidInfo) : base(raidInfo)
628-
{
629-
RaidType = raidInfo.RaidType;
630-
}
631-
}
632609
public class FleaSoldEventArgs : EventArgs
633610
{
634611
public string Buyer { get; set; }

TarkovMonitor/MainBlazorUI.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public MainBlazorUI()
4646
eft.FleaOfferExpired += Eft_FleaOfferExpired;
4747
eft.DebugMessage += Eft_DebugMessage;
4848
eft.ExceptionThrown += Eft_ExceptionThrown;
49-
eft.RaidLoaded += Eft_RaidLoaded;
49+
eft.RaidCountdown += Eft_RaidCountdown;
50+
eft.RaidStarted += Eft_RaidStart;
5051
eft.RaidExited += Eft_RaidExited;
5152
eft.TaskStarted += Eft_TaskStarted;
5253
eft.TaskFailed += Eft_TaskFailed;
@@ -69,6 +70,8 @@ public MainBlazorUI()
6970
UpdateCheck.NewVersion += UpdateCheck_NewVersion;
7071
UpdateCheck.Error += UpdateCheck_Error;
7172

73+
SocketClient.ExceptionThrown += SocketClient_ExceptionThrown;
74+
7275
// Update tarkov.dev Repository data
7376
UpdateItems();
7477
UpdateTasks();
@@ -94,6 +97,11 @@ public MainBlazorUI()
9497
blazorWebView1.WebView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
9598
}
9699

100+
private void SocketClient_ExceptionThrown(object? sender, ExceptionEventArgs e)
101+
{
102+
messageLog.AddMessage($"Error {e.Context}: {e.Exception.Message}\n{e.Exception.StackTrace}", "exception");
103+
}
104+
97105
protected override void OnShown(EventArgs e)
98106
{
99107
base.OnShown(e);
@@ -105,13 +113,13 @@ protected override void OnShown(EventArgs e)
105113
}
106114
}
107115

108-
private void Eft_MapLoaded(object? sender, MatchFoundEventArgs e)
116+
private void Eft_MapLoaded(object? sender, RaidInfoEventArgs e)
109117
{
110118
if (!Properties.Settings.Default.autoNavigateMap)
111119
{
112120
return;
113121
}
114-
var map = TarkovDev.Maps.Find(m => m.nameId == e.Map);
122+
var map = TarkovDev.Maps.Find(m => m.nameId == e.RaidInfo.Map);
115123
if (map == null)
116124
{
117125
return;
@@ -286,16 +294,16 @@ private async Task InitializeProgress()
286294
}
287295
}
288296

289-
private void Eft_MatchFound(object? sender, MatchFoundEventArgs e)
297+
private void Eft_MatchFound(object? sender, RaidInfoEventArgs e)
290298
{
291299
if (Properties.Settings.Default.matchFoundAlert)
292300
{
293301
PlaySoundFromResource(Properties.Resources.match_found);
294302
}
295-
var mapName = e.Map;
303+
var mapName = e.RaidInfo.Map;
296304
var map = TarkovDev.Maps.Find(m => m.nameId == mapName);
297305
if (map != null) mapName = map.name;
298-
messageLog.AddMessage($"Matching complete on {mapName} after {e.QueueTime} seconds");
306+
messageLog.AddMessage($"Matching complete on {mapName} after {e.RaidInfo.QueueTime} seconds");
299307
}
300308

301309
private void Eft_NewLogData(object? sender, NewLogDataEventArgs e)
@@ -446,15 +454,23 @@ private void Eft_ExceptionThrown(object? sender, ExceptionEventArgs e)
446454
messageLog.AddMessage($"Error {e.Context}: {e.Exception.Message}\n{e.Exception.StackTrace}", "exception");
447455
}
448456

449-
private async void Eft_RaidLoaded(object? sender, RaidLoadedEventArgs e)
457+
private static void Eft_RaidCountdown(object? sender, RaidInfoEventArgs e)
450458
{
451459
if (Properties.Settings.Default.raidStartAlert) PlaySoundFromResource(Properties.Resources.raid_starting);
452-
var mapName = e.Map;
460+
}
461+
462+
private async void Eft_RaidStart(object? sender, RaidInfoEventArgs e)
463+
{
464+
if (e.RaidInfo.RaidType != RaidType.PMC || e.RaidInfo.QueueTime == 0)
465+
{
466+
if (Properties.Settings.Default.raidStartAlert) PlaySoundFromResource(Properties.Resources.raid_starting);
467+
}
468+
var mapName = e.RaidInfo.Map;
453469
var map = TarkovDev.Maps.Find(m => m.nameId == mapName);
454470
if (map != null) mapName = map.name;
455-
if (e.RaidType != RaidType.Unknown)
471+
if (e.RaidInfo.RaidType != RaidType.Unknown)
456472
{
457-
messageLog.AddMessage($"Starting {e.RaidType} raid on {mapName}");
473+
messageLog.AddMessage($"Starting {e.RaidInfo.RaidType} raid on {mapName}");
458474
}
459475
else
460476
{
@@ -464,13 +480,13 @@ private async void Eft_RaidLoaded(object? sender, RaidLoadedEventArgs e)
464480
{
465481
return;
466482
}
467-
if (e.QueueTime == 0 || e.RaidType == RaidType.Unknown)
483+
if (!e.RaidInfo.Online || e.RaidInfo.QueueTime == 0 || e.RaidInfo.RaidType == RaidType.Unknown)
468484
{
469485
return;
470486
}
471487
try
472488
{
473-
await TarkovDev.PostQueueTime(e.Map, (int)Math.Round(e.QueueTime), e.RaidType.ToString().ToLower());
489+
await TarkovDev.PostQueueTime(e.RaidInfo.Map, (int)Math.Round(e.RaidInfo.QueueTime), e.RaidInfo.RaidType.ToString().ToLower());
474490
}
475491
catch (Exception ex)
476492
{

TarkovMonitor/SocketClient.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using System.Net.WebSockets;
1+
using MudBlazor;
2+
using System.Net.WebSockets;
23
using System.Text.Json.Nodes;
34
using Websocket.Client;
45

56
namespace TarkovMonitor
67
{
78
internal static class SocketClient
89
{
10+
public static event EventHandler<ExceptionEventArgs> ExceptionThrown;
911
private static readonly string wsUrl = "wss://socket.tarkov.dev";
1012
private static WebsocketClient? socket;
1113

@@ -19,7 +21,7 @@ private static async Task Connect()
1921
{
2022
if (socket != null && socket.IsRunning)
2123
{
22-
socket.Stop(WebSocketCloseStatus.NormalClosure, null);
24+
await socket.Stop(WebSocketCloseStatus.NormalClosure, null);
2325
}
2426
var remoteId = Properties.Settings.Default.remoteId;
2527
if (remoteId == null || remoteId == "")
@@ -68,7 +70,7 @@ public static async Task Send(JsonObject message)
6870
return;
6971
}
7072
message["sessionID"] = remoteid;
71-
socket.Send(message.ToJsonString());
73+
await Task.Run(() => socket.Send(message.ToJsonString()));
7274
}
7375

7476
public static async Task UpdatePlayerPosition(PlayerPositionEventArgs e)
@@ -93,7 +95,13 @@ public static async Task UpdatePlayerPosition(PlayerPositionEventArgs e)
9395
}
9496
}
9597
};
96-
await Send(payload);
98+
try
99+
{
100+
await Send(payload);
101+
} catch (Exception ex)
102+
{
103+
ExceptionThrown?.Invoke(payload, new(ex, "updating player position"));
104+
}
97105
}
98106

99107
public static async Task NavigateToMap(TarkovDev.Map map)
@@ -107,7 +115,14 @@ public static async Task NavigateToMap(TarkovDev.Map map)
107115
["value"] = map.normalizedName
108116
}
109117
};
110-
await Send(payload);
118+
try
119+
{
120+
await Send(payload);
121+
}
122+
catch (Exception ex)
123+
{
124+
ExceptionThrown?.Invoke(payload, new(ex, $"navigating to map {map.name}"));
125+
}
111126
}
112127
}
113128
}

0 commit comments

Comments
 (0)