-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
884 lines (827 loc) · 40.4 KB
/
Copy pathProgram.cs
File metadata and controls
884 lines (827 loc) · 40.4 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*
* F1RPC - A simple, but effective, Discord RPC program for EA's F1 23, with the optional feature of submitting race results via Discord Webhook requests.
*
* Author: Plushie (fretfxul)
* GitHub: https://github.com/xKaelyn/F1RPC
*
* License: MIT License
*
* Please refer to the GitHub repository for detailed documentation and contributing guidelines.
*
* Created by Plushie (fretfxul), 2023.
*/
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using CSharpDiscordWebhook.NET.Discord;
using F1RPC.Configuration;
using F1Sharp;
using F1Sharp.Packets;
using NetDiscordRpc;
using NetDiscordRpc.RPC;
using Newtonsoft.Json;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
namespace F1RPC
{
public class F1RPC
{
public DiscordRPC? discord { get; private set; }
public DiscordWebhook? webhook { get; private set; } = new DiscordWebhook();
public static ConfigJson Config { get; private set; } = new ConfigJson();
public string? projectDirectory { get; set; }
public bool? isRunningOnMacOS { get; set; }
public Version? versionId { get; set; } = Assembly.GetExecutingAssembly().GetName().Version;
static void Main(string[] args)
{
F1RPC f1 = new() { isRunningOnMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) };
f1.projectDirectory =
f1.isRunningOnMacOS == true
? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
: Directory.GetCurrentDirectory();
Log.Logger = new LoggerConfiguration()
.WriteTo
.Console(
theme: SystemConsoleTheme.Literate,
restrictedToMinimumLevel: LogEventLevel.Information
)
.WriteTo
.File(
$"{f1.projectDirectory}/logs/F1RPC.log",
outputTemplate: "{Timestamp:dd MMM yyyy - hh:mm:ss tt} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: LogEventLevel.Information
)
.MinimumLevel
.Information()
.CreateLogger();
Log.Information($"F1RPC | Version {f1.versionId}");
Log.Information("Program booting..");
try
{
f1.Initialize().GetAwaiter().GetResult();
}
catch (Exception ex)
{
if (ex.Message == "Address already in use")
{
Log.Fatal(
$"Port {Config.Port} is already in use. Please either close the program using it and try again or choose another port in the Configuration.json file."
);
}
else
{
Log.Fatal($"Exception occured: {ex.Message}");
}
}
}
public async Task Initialize()
{
ConfigJson configJson = new();
int port = 0;
string json = "";
bool webhookEnabled = false;
// Check to see if it's running on MacOS as the path is handled differently.
// For some reason, when running a MacOS program, the working directory is the root of the drive.
// Not the program directory.
if (isRunningOnMacOS == true)
{
string? projectDirectory = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location
);
json = await File.ReadAllTextAsync(
$"{projectDirectory}/assets/config/Configuration.json"
)
.ConfigureAwait(false);
string configPath = string.Format(
$"{projectDirectory}/assets/config/Configuration.json"
);
using FileStream fs = File.OpenRead(configPath);
Config = JsonConvert.DeserializeObject<ConfigJson>(json);
}
// Otherwise, use the previous code.
else
{
json = await File.ReadAllTextAsync("assets/config/Configuration.json")
.ConfigureAwait(false);
using FileStream fs = File.OpenRead("assets/config/Configuration.json");
Config = JsonConvert.DeserializeObject<ConfigJson>(json);
}
configJson = JsonConvert.DeserializeObject<ConfigJson>(json);
port = configJson.Port;
Log.Information("Logger initialized.");
Log.Information(
"If you have any problems, please raise a issue on GitHub and upload your log file found in the logs folder."
);
// Check if the Discord App ID is set in the configuration file
// If not, throw an error and exit the program as it's required.
if (configJson.AppId == "YOUR_APP_ID_HERE")
{
Log.Error("Please set your Discord App ID in assets/config/Configuration.json");
return;
}
// Check if the Discord Webhook URL is set in the configuration file
// If not, throw a warning and disable the webhook feature.
// Optional feature, so it's not required.
if (configJson.WebhookUrl == "YOUR_WEBHOOK_URL_HERE")
{
Log.Warning(
"If you wish to use the Discord Webhook feature, make sure to set your webhook URL in assets/config/Configuration.json."
);
webhookEnabled = false;
}
else
{
Log.Information(
"Discord Webhook feature enabled - final race classification data will be sent to the webhook."
);
webhook.Uri = new Uri(configJson.WebhookUrl);
webhookEnabled = true;
}
// Create a new instance of DiscordRPC with the Discord App ID from the configuration file
discord = new DiscordRPC(configJson.AppId);
// Let's actually bring the Discord client online
discord.Initialize();
Log.Information("DiscordRPC initialized.");
Log.Information("Program initialized. Setting up client..");
// Create a new instance of the TelemetryClient class with the specified port
TelemetryClient client = new(port);
Log.Information($"Client initialized. Listening on port {port}.");
Log.Information("Waiting for data..");
// Variables for storing various data
int teamId = 0;
string playerName = "";
string teamName = "";
var track = "";
var currentTrackId = "";
int lapNumber = 0;
int totalLaps = 0;
int formulaType = 0;
string sessionType = "";
int penalties = 0;
int totalWarnings = 0;
int cornerCuttingWarnings = 0;
int safetyCars = 0;
int virtualSafetyCars = 0;
int redFlags = 0;
int playerIndex = 0;
int currentPosition = 0;
int totalParticipants = 0;
string playerPlatform = "";
double raceCompletion = 0.0;
int lobbyPlayerCount = 0;
int finalPosition = 0;
int gridPosition = 0;
int finalPoints = 0;
int finalResultStatus = 0;
int weatherId = 0;
string weatherConditions = "";
int numPitStops = 0;
int speedTrapFastestDriverIdx = 0;
float speedTrapFastestSpeedKmh = 0.0f;
List<dynamic> penaltiesList = new();
float fastestLapTime = 0.0f;
int fastestLapDriverIdx = 0;
string fastestLapDriver = "";
int penaltyDriverIdx = 0;
string penaltyDriverName = "";
bool networkGame = false;
var button = new NetDiscordRpc.RPC.Button[]
{
new() { Label = "Powered by F1RPC", Url = "https://github.com/xKaelyn/F1RPC" }
};
// When first booting system, reset the status by showing a "in menu" presence
resetStatus(discord);
// Subscribe to the events for receiving data
client.OnLapDataReceive += Client_OnLapDataReceive;
client.OnSessionDataReceive += (packet) =>
Client_OnSessionDataReceive(packet, discord, teamName);
client.OnParticipantsDataReceive += Client_OnParticipantsDataReceive;
client.OnLobbyInfoDataReceive += (packet) =>
Client_OnLobbyInfoDataReceive(packet, discord);
client.OnFinalClassificationDataReceive += async (packet) =>
await Client_OnFinalClassificationDataReceiveAsync(packet, discord);
client.OnEventDetailsReceive += Client_OnEventDetailsReceive;
// Method for when receiving event details - used for getting fastest lap and speed trap data
void Client_OnEventDetailsReceive(EventPacket packet)
{
fastestLapTime = packet.eventDetails.fastestLap.lapTime;
fastestLapDriverIdx = packet.eventDetails.fastestLap.vehicleIdx;
speedTrapFastestDriverIdx = packet.eventDetails.sppedTrap.vehicleIdx;
speedTrapFastestSpeedKmh = packet.eventDetails.sppedTrap.speed;
}
// Method for when receiving lap data
// This method is triggered when lap data is received from the telemetry client.
void Client_OnLapDataReceive(LapDataPacket packet)
{
playerIndex = packet.header.playerCarIndex;
lapNumber = packet.lapData[playerIndex].currentLapNum;
currentPosition = packet.lapData[playerIndex].carPosition;
totalWarnings = packet.lapData[playerIndex].totalWarnings;
numPitStops = packet.lapData[playerIndex].numPitStops;
// Calculate the race completion percentage based on the current lap number and total laps
raceCompletion =
lapNumber == 1
? 0.0
: Math.Round((lapNumber - 1) / (double)(totalLaps - 1) * 100, 2);
}
// Method for when recieving lobby info
// Lobby data is received twice a second, until the game begins and the lobby is destroyed.
void Client_OnLobbyInfoDataReceive(LobbyInfoPacket packet, DiscordRPC discord)
{
lobbyPlayerCount = packet.numPlayers;
// Variable to change "player" to "players" if more than 1 player is in the lobby - no need to while loop this as it's only modified each time the data is received
string playerText = "";
if (lobbyPlayerCount > 1)
{
playerText = "players";
}
if (lobbyPlayerCount < 1)
{
playerText = "player";
}
// Set presence to "Waiting in the lobby with x other players", -1 because the player itself is not counted
discord.SetPresence(
new RichPresence
{
Details = $"In a multiplayer lobby | Using {playerPlatform}",
State = $"With {lobbyPlayerCount - 1} other {playerText}",
Assets = new Assets
{
LargeImageKey = $"f1_23_logo",
LargeImageText = $"F1 23"
},
Buttons = button
}
);
}
// Method for when recieving final classification data - used for getting final position
// Final Classification data is only received once once the final scoreboard is shown to the player.
// Method is async to allow for a Task.Delay at the end of the method as we need to assume the user is in the menus - we don't have a way of knowing if they are or not.
async Task Client_OnFinalClassificationDataReceiveAsync(
FinalClassificationPacket packet,
DiscordRPC discord
)
{
finalPosition = packet.classificationData[playerIndex].position;
gridPosition = packet.classificationData[playerIndex].gridPosition;
finalPoints = packet.classificationData[playerIndex].points;
finalResultStatus = (int)packet.classificationData[playerIndex].resultStatus;
// To-Do: Add session best lap time to presence
// If user finished a session, but it's not a race
if (sessionType is not "Race" and not "Race 2" and not "Race 3")
{
discord.SetPresence(
new RichPresence
{
Details = $"Session Completed | Track: {track}",
State = $"Racing for {teamName} | Using {playerPlatform}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
}
// If user finished the race
if (finalResultStatus == 3)
{
if (sessionType is not "Race" and not "Race 2" and not "Race 3")
{
discord.SetPresence(
new RichPresence
{
Details = $"Session Completed | Track: {track}",
State = $"Racing for {teamName} | Using {playerPlatform}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
}
else
{
discord.SetPresence(
new RichPresence
{
Details =
$"Finished: P{finalPosition} / P{totalParticipants} | Started: P{gridPosition} | Track: {track}",
State =
$"Racing for {teamName} | {finalPoints} points earned | Using {playerPlatform}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
// Check if webhook feature is enabled
if (webhookEnabled == true)
{
// Capitalize the first letter of the player name if it's not a network game
// As otherwise, driver names are all uppercase - e.g RICCIARDO, VERSTAPPEN etc.
string playerNameValue = !networkGame
? char.ToUpperInvariant(playerName[0])
+ playerName[1..].ToLowerInvariant()
: playerName;
DiscordMessage message = new();
DiscordEmbed embed =
new()
{
Author = new EmbedAuthor
{
Name = "F1RPC",
Url = new Uri("https://github.com/xkaelyn/f1rpc"),
},
Title = "Race Finished",
Thumbnail = new EmbedMedia
{
Url = new Uri(
$"https://raw.githubusercontent.com/xKaelyn/F1RPC/master/assets/images/{currentTrackId}.png"
)
},
Url = new Uri("https://github.com/xkaelyn/f1rpc"),
Color = GetEmbedColorByPosition(finalPosition),
Fields = new List<EmbedField>()
{
new() { Name = "Date & Time", Value = $"{DateTime.Now}", },
new() { Name = "Driver", Value = playerNameValue, },
new() { Name = "Track", Value = track },
new() { Name = "Team", Value = teamName },
new()
{
Name = "Virtual Safety Cars",
Value = $"{virtualSafetyCars}",
Inline = true
},
new()
{
Name = "Safety Cars",
Value = $"{safetyCars}",
Inline = true
},
new()
{
Name = "Red Flags",
Value = $"{redFlags}",
Inline = true
},
new()
{
Name = "Penalties",
Value = $"{penalties}",
Inline = true
},
new()
{
Name = "Warnings",
Value = $"{totalWarnings}",
Inline = true
},
new()
{
Name = "Corner Cut Warnings",
Value = $"{cornerCuttingWarnings}",
Inline = true
},
new()
{
Name = "Starting Position",
Value = $"P{gridPosition}",
Inline = true
},
new()
{
Name = "Final Position",
Value = $"P{finalPosition}",
Inline = true
},
new()
{
Name = "Position Change",
Value =
$"{(gridPosition < finalPosition ? "-" : "+ ")}{Math.Abs(gridPosition - finalPosition)}",
Inline = true
},
new()
{
Name = "Number of Pit Stops",
Value = $"{numPitStops}",
Inline = true
},
new()
{
Name = "Championship Points Earned",
Value = $"{finalPoints}",
Inline = true
}
},
Footer = new EmbedFooter
{
Text = $"xKaelyn/F1RPC ~ Version {versionId}"
}
};
message.Embeds.Add(embed);
try
{
if (webhook != null)
{
await webhook.SendAsync(message);
}
else
{
Log.Fatal("Webhook is null.");
}
}
catch (Exception ex)
{
Log.Fatal($"Error sending webhook: {ex}");
}
}
}
}
// If user did not finish the race
if (finalResultStatus == 4)
{
discord.SetPresence(
new RichPresence
{
Details = $"DNF | Started: P{gridPosition} | Track: {track}",
State = $"Racing for {teamName} | Using {playerPlatform}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
}
// If user was disqualified
if (finalResultStatus == 5)
{
discord.SetPresence(
new RichPresence
{
Details = $"Disqualified | Started: P{gridPosition} | Track: {track}",
State = $"Racing for {teamName} | Using {playerPlatform}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
}
// If user was not classified
if (finalResultStatus == 6)
{
discord.SetPresence(
new RichPresence
{
Details = $"Not Classified | Track: {track}",
State = $"Racing for {teamName} | Using {playerPlatform}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
}
// If user retired
if (finalResultStatus == 7)
{
discord.SetPresence(
new RichPresence
{
Details = $"Retired | Started: P{gridPosition} | Track: {track}",
State = $"Racing for {teamName} | Using {playerPlatform}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
}
// Wait 15 seconds
await Task.Delay(15000);
// Assume the player is in the menus - no way of actually knowing if they are or not
resetStatus(discord);
}
// Method for when recieving participants data - used for getting team name
// Participant data is received once every 5 seconds.
void Client_OnParticipantsDataReceive(ParticipantsPacket packet)
{
playerIndex = packet.header.playerCarIndex;
totalParticipants = packet.numActiveCars;
teamId = (int)packet.participants[playerIndex].teamId;
penaltyDriverName = new string(packet.participants[penaltyDriverIdx].name);
playerName =
playerIndex >= 0 && playerIndex < packet.participants.Length
? new string(packet.participants[playerIndex].name)
: "Unknown";
fastestLapDriver =
fastestLapDriverIdx >= 0 && fastestLapDriverIdx < packet.participants.Length
? new string(packet.participants[fastestLapDriverIdx].name)
: "Unknown";
var playerPlatformInt = (int)packet.participants[playerIndex].platform;
playerPlatform = playerPlatformInt switch
{
1 => "PC (Steam)",
6 => "PC (Origin/EA)",
3 => "PlayStation",
4 => "Xbox",
_ => "Unknown",
};
teamName = GetTeamNameFromId(teamId);
}
// Method for getting team name from team id (as F1 uses integers)
string GetTeamNameFromId(int teamId)
{
List<dynamic> teamlist =
new()
{
new { GameId = 0, Name = "Mercedes-AMG Petronas F1 Team" },
new { GameId = 1, Name = "Scuderia Ferrari" },
new { GameId = 2, Name = "Oracle Red Bull Racing" },
new { GameId = 3, Name = "Williams Racing" },
new { GameId = 4, Name = "Aston Martin Aramco Cognizant F1 Team" },
new { GameId = 5, Name = "BWT Alpine F1 Team" },
new { GameId = 6, Name = "Scuderia AlphaTauri" },
new { GameId = 7, Name = "Haas F1 Team" },
new { GameId = 8, Name = "McLaren F1 Team" },
new { GameId = 9, Name = "Alfa Romeo F1 Team ORLEN" },
new { GameId = 85, Name = "Mercedes (2020)" },
new { GameId = 86, Name = "Ferrari (2020)" },
new { GameId = 87, Name = "Red Bull (2020)" },
new { GameId = 88, Name = "Williams (2020)" },
new { GameId = 89, Name = "Racing Point (2020)" },
new { GameId = 90, Name = "Renault (2020)" },
new { GameId = 91, Name = "AlphaTauri (2020)" },
new { GameId = 92, Name = "Haas (2020)" },
new { GameId = 93, Name = "McLaren (2020)" },
new { GameId = 94, Name = "Alfa Romeo (2020)" },
new { GameId = 95, Name = "Aston Martin DB11 V12" },
new { GameId = 96, Name = "Aston Martin Vantage F1 Edition" },
new { GameId = 97, Name = "Aston Martin Vantage Safety Car" },
new { GameId = 98, Name = "Ferrari F8 Tributo" },
new { GameId = 99, Name = "Ferrari Roma" },
new { GameId = 100, Name = "McLaren 7205" },
new { GameId = 101, Name = "McLaren Artura" },
new { GameId = 102, Name = "Mercedes AMG GT Black Series Safety Car" },
new { GameId = 103, Name = "Mercedes AMG GTR Pro" },
new { GameId = 104, Name = "F1 Custom Team" },
new { GameId = 106, Name = "Prema (2021)" },
new { GameId = 107, Name = "Uni-Virtuosi (2021)" },
new { GameId = 108, Name = "Carlin (2021)" },
new { GameId = 109, Name = "Hitech (2021)" },
new { GameId = 110, Name = "Art GP (2021)" },
new { GameId = 111, Name = "MP Motorsport (2021)" },
new { GameId = 112, Name = "Charouz (2021)" },
new { GameId = 113, Name = "Dams (2021)" },
new { GameId = 114, Name = "Campos (2021)" },
new { GameId = 115, Name = "BWT (2021)" },
new { GameId = 116, Name = "Trident (2021)" },
new { GameId = 117, Name = "Mercedes AMG GT Black Series" },
new { GameId = 118, Name = "Mercedes (2022)" },
new { GameId = 119, Name = "Ferrari (2022)" },
new { GameId = 120, Name = "Red Bull Racing (2022)" },
new { GameId = 121, Name = "Williams (2022)" },
new { GameId = 122, Name = "Aston Martin (2022)" },
new { GameId = 123, Name = "Alpine (2022)" },
new { GameId = 124, Name = "AlphaTauri (2022)" },
new { GameId = 125, Name = "Haas (2022)" },
new { GameId = 126, Name = "McLaren (2022)" },
new { GameId = 127, Name = "Alfa Romeo (2022)" },
new { GameId = 128, Name = "Konnersport (2022)" },
new { GameId = 129, Name = "Konnersport" },
new { GameId = 130, Name = "Prema (2022)" },
new { GameId = 131, Name = "Uni-Virtuosi (2022)" },
new { GameId = 132, Name = "Carlin (2022)" },
new { GameId = 133, Name = "MP Motorsport (2022)" },
new { GameId = 134, Name = "Charouz (2022)" },
new { GameId = 135, Name = "Dams (2022)" },
new { GameId = 136, Name = "Campos (2022)" },
new { GameId = 137, Name = "Van Amersfoort Racing (2022)" },
new { GameId = 138, Name = "Trident (2022)" },
new { GameId = 139, Name = "Hitech (2022)" },
new { GameId = 140, Name = "Art GP (2022)" },
new { GameId = 143, Name = "ART Grand Prix (2023)" },
new { GameId = 144, Name = "Campos Racing (2023)" },
new { GameId = 145, Name = "Carlin (2023)" },
new { GameId = 146, Name = "PHM Racing (2023)" },
new { GameId = 147, Name = "DAMS (2023)" },
new { GameId = 148, Name = "Hitech (2023)" },
new { GameId = 149, Name = "MP Motorsport (2023)" },
new { GameId = 150, Name = "Prema (2023)" },
new { GameId = 151, Name = "Trident (2023)" },
new { GameId = 152, Name = "Van Amersfoort Racing (2023)" },
new { GameId = 153, Name = "Uni-Virtuosi (2023)" }
};
var team = teamlist.FirstOrDefault(t => t.GameId == teamId);
return team != null ? (string)team.Name : "Unknown";
}
// Method for getting weather condition from weather id (as F1 uses integers)
string GetWeatherConditions(int weatherId) =>
weatherId switch
{
0 => "Clear",
1 => "Light Cloud",
2 => "Overcast",
3 => "Light Rain",
4 => "Heavy Rain",
5 => "Storm",
_ => throw new Exception("Unknown weather ID")
};
// Method for when receiving session data
// Session data is received twice a second until the session is destroyed. It only contains data about the ongoing session.
void Client_OnSessionDataReceive(
SessionPacket packet,
DiscordRPC discord,
string teamName
)
{
formulaType = (int)packet.formula;
totalLaps = packet.totalLaps;
weatherId = (int)packet.weather;
currentTrackId = packet.trackId.ToString().ToLower();
virtualSafetyCars = packet.numVirtualSafetyCarPeriods;
safetyCars = packet.numSafetyCarPeriods;
redFlags = packet.numRedFlagPeriods;
networkGame = packet.networkGame != 0;
weatherConditions = GetWeatherConditions(weatherId);
// Case switch for checking track id, setting track name and setting track image
track = (int)packet.trackId switch
{
0 => "Australia: Melbourne",
1 => "France: Le Castellet",
2 => "China: Shanghai",
3 => "Bahrain: Sakhir",
4 => "Spain: Barcelona-Catalunya",
5 => "Monaco",
6 => "Canada: Montreal",
7 => "UK: Silverstone",
8 => "Germany: Hockenheim",
9 => "Hungary: Budapest",
10 => "Belgium: Spa-Francorchamps",
11 => "Italy: Monza",
12 => "Singapore",
13 => "Japan: Suzuka",
14 => "Abu Dhabi: Yas Marina",
15 => "USA (Texas): COTA",
16 => "Brazil: Sao Paolo",
17 => "Austria: Spielberg",
18 => "Russia: Sochi",
19 => "Mexico",
20 => "Azerbaijan: Baku",
21 => "Bahrain: Sakhir (Short)",
22 => "UK: Silverstone (Short)",
23 => "USA (Texas): COTA (Short)",
24 => "Japan: Suzuka (Short)",
25 => "Vietnam: Hanoi",
26 => "Netherlands: Zandvoort",
27 => "Italy: Imola",
28 => "Portugal: Portimao",
29 => "Saudi Arabia: Jeddah",
30 => "USA (Florida): Miami",
31 => "USA (Nevada): Las Vegas",
32 => "Qatar: Losail",
_ => throw new Exception("Unknown track ID")
};
// Case Switch for Packet Session Type
sessionType = (int)packet.sessionType switch
{
0 => "Unknown",
1 => "Practice 1",
2 => "Practice 2",
3 => "Practice 3",
4 => "Short Practice",
5 => "Qualifying 1",
6 => "Qualifying 2",
7 => "Qualifying 3",
8 => "Short Qualifying",
9 => "One-Shot Qualifying",
10 => "Race",
11 => "Race 2",
12 => "Race 3",
13 => "Time Trial",
_ => throw new Exception("Unknown session type")
};
// Practice / Qualifying
if ((int)packet.sessionType is >= 1 and <= 9)
{
discord.SetPresence(
new RichPresence
{
Details = $"{sessionType} - {track}",
State = $"Racing for {teamName} | Conditions: {weatherConditions}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
}
// Race
if ((int)packet.sessionType is >= 10 and <= 12)
{
discord.SetPresence(
new RichPresence
{
Details =
$"{sessionType} - {track} | Lap {lapNumber} / {totalLaps} | {raceCompletion}% complete",
State =
$"Racing for {teamName} | P{currentPosition} / P{totalParticipants} | Conditions: {weatherConditions}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
}
// Time Trial
if ((int)packet.sessionType == 13)
{
discord.SetPresence(
new RichPresence
{
Details = $"{sessionType} - {track}",
State = $"Using {playerPlatform}",
Assets = new Assets
{
LargeImageKey = $"{currentTrackId}",
LargeImageText = $"{track}"
},
Buttons = button
}
);
}
}
// Method for resetting status - checks if F1 23 is open before executing SetPresence
void resetStatus(DiscordRPC discord)
{
Log.Information($"Connected. Updating status..");
discord.SetPresence(
new RichPresence
{
Details = "Idle",
Assets = new Assets
{
LargeImageKey = "f1_23_logo",
LargeImageText = "F1 23"
},
Timestamps = new Timestamps(DateTime.UtcNow),
Buttons = button
}
);
Log.Information($"Updated Discord Status: {discord.CurrentPresence.Details}");
}
await Task.Delay(-1).ConfigureAwait(false);
}
private static DiscordColor GetEmbedColorByPosition(int finalPosition)
{
if (finalPosition == 1)
{
DiscordColor color = new(Color.Gold);
return color;
}
if (finalPosition == 2)
{
DiscordColor color = new(Color.Silver);
return color;
}
if (finalPosition == 3)
{
DiscordColor color = new(Color.SaddleBrown);
return color;
}
if (finalPosition is >= 4 and <= 10)
{
var color = new DiscordColor(Color.Cyan);
return color;
}
else
{
var color = new DiscordColor(Color.White);
return color;
}
}
}
}