Skip to content

Commit dbfd30e

Browse files
Refactor the ranks in GameLobbyBase class (CnCNet#713)
* Refactoring the ranks from private constants to enum * Replace `enum` with `class` to reduce `(int)` calls * Move ranks to `GameLobbyBase` * Replace enum with struct to reduce `(int)` and `(Rank)` casts * Replace struct with record * Make `rank` field private
1 parent 7222dfe commit dbfd30e

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ namespace DTAClient.DXGUI.Multiplayer.GameLobby
2626
/// </summary>
2727
public abstract class GameLobbyBase : INItializableWindow
2828
{
29+
protected record Rank
30+
{
31+
private readonly int rank;
32+
33+
public static readonly Rank None = 0;
34+
public static readonly Rank Easy = 1;
35+
public static readonly Rank Medium = 2;
36+
public static readonly Rank Hard = 3;
37+
38+
private Rank(int rank) => this.rank = rank;
39+
40+
public static implicit operator int(Rank value) => value.rank;
41+
42+
public static implicit operator Rank(int value) => new Rank(value);
43+
}
44+
2945
protected const int MAX_PLAYER_COUNT = 8;
3046
protected const int PLAYER_OPTION_VERTICAL_MARGIN = 12;
3147
protected const int PLAYER_OPTION_HORIZONTAL_MARGIN = 3;
@@ -37,11 +53,6 @@ public abstract class GameLobbyBase : INItializableWindow
3753

3854
private readonly string FavoriteMapsLabel = "Favorite Maps".L10N("Client:Main:FavoriteMaps");
3955

40-
private const int RANK_NONE = 0;
41-
private const int RANK_EASY = 1;
42-
private const int RANK_MEDIUM = 2;
43-
private const int RANK_HARD = 3;
44-
4556
/// <summary>
4657
/// Creates a new instance of the game lobby base.
4758
/// </summary>
@@ -2348,27 +2359,27 @@ protected GameType GetGameType()
23482359
return GameType.TeamGame;
23492360
}
23502361

2351-
protected int GetRank()
2362+
protected Rank GetRank()
23522363
{
23532364
if (GameMode == null || Map == null)
2354-
return RANK_NONE;
2365+
return Rank.None;
23552366

23562367
foreach (GameLobbyCheckBox checkBox in CheckBoxes)
23572368
{
23582369
if ((checkBox.MapScoringMode == CheckBoxMapScoringMode.DenyWhenChecked && checkBox.Checked) ||
23592370
(checkBox.MapScoringMode == CheckBoxMapScoringMode.DenyWhenUnchecked && !checkBox.Checked))
23602371
{
2361-
return RANK_NONE;
2372+
return Rank.None;
23622373
}
23632374
}
23642375

23652376
PlayerInfo localPlayer = Players.Find(p => p.Name == ProgramConstants.PLAYERNAME);
23662377

23672378
if (localPlayer == null)
2368-
return RANK_NONE;
2379+
return Rank.None;
23692380

23702381
if (IsPlayerSpectator(localPlayer))
2371-
return RANK_NONE;
2382+
return Rank.None;
23722383

23732384
// These variables are used by both the skirmish and multiplayer code paths
23742385
int[] teamMemberCounts = new int[5];
@@ -2394,50 +2405,50 @@ protected int GetRank()
23942405
if (isMultiplayer)
23952406
{
23962407
if (Players.Count == 1)
2397-
return RANK_NONE;
2408+
return Rank.None;
23982409

23992410
// PvP stars for 2-player and 3-player maps
24002411
if (Map.MaxPlayers <= 3)
24012412
{
24022413
List<PlayerInfo> filteredPlayers = Players.Where(p => !IsPlayerSpectator(p)).ToList();
24032414

24042415
if (AIPlayers.Count > 0)
2405-
return RANK_NONE;
2416+
return Rank.None;
24062417

24072418
if (filteredPlayers.Count != Map.MaxPlayers)
2408-
return RANK_NONE;
2419+
return Rank.None;
24092420

24102421
int localTeamIndex = localPlayer.TeamId;
24112422
if (localTeamIndex > 0 && filteredPlayers.Count(p => p.TeamId == localTeamIndex) > 1)
2412-
return RANK_NONE;
2423+
return Rank.None;
24132424

2414-
return RANK_HARD;
2425+
return Rank.Hard;
24152426
}
24162427

24172428
// Coop stars for maps with 4 or more players
24182429
// See the code in StatisticsManager.GetRankForCoopMatch for the conditions
24192430

24202431
if (Players.Find(p => IsPlayerSpectator(p)) != null)
2421-
return RANK_NONE;
2432+
return Rank.None;
24222433

24232434
if (AIPlayers.Count == 0)
2424-
return RANK_NONE;
2435+
return Rank.None;
24252436

24262437
if (Players.Find(p => p.TeamId != localPlayer.TeamId) != null)
2427-
return RANK_NONE;
2438+
return Rank.None;
24282439

24292440
if (Players.Find(p => p.TeamId == 0) != null)
2430-
return RANK_NONE;
2441+
return Rank.None;
24312442

24322443
if (AIPlayers.Find(p => p.TeamId == 0) != null)
2433-
return RANK_NONE;
2444+
return Rank.None;
24342445

24352446
teamMemberCounts[localPlayer.TeamId] += Players.Count;
24362447

24372448
if (lowestEnemyAILevel < highestAllyAILevel)
24382449
{
24392450
// Check that the player's AI allies aren't stronger
2440-
return RANK_NONE;
2451+
return Rank.None;
24412452
}
24422453

24432454
// Check that all teams have at least as many players
@@ -2452,7 +2463,7 @@ protected int GetRank()
24522463
if (teamMemberCounts[i] > 0)
24532464
{
24542465
if (teamMemberCounts[i] < allyCount)
2455-
return RANK_NONE;
2466+
return Rank.None;
24562467
}
24572468
}
24582469

@@ -2464,14 +2475,14 @@ protected int GetRank()
24642475
// *********
24652476

24662477
if (AIPlayers.Count != Map.MaxPlayers - 1)
2467-
return RANK_NONE;
2478+
return Rank.None;
24682479

24692480
teamMemberCounts[localPlayer.TeamId]++;
24702481

24712482
if (lowestEnemyAILevel < highestAllyAILevel)
24722483
{
24732484
// Check that the player's AI allies aren't stronger
2474-
return RANK_NONE;
2485+
return Rank.None;
24752486
}
24762487

24772488
if (localPlayer.TeamId > 0)
@@ -2488,7 +2499,7 @@ protected int GetRank()
24882499
if (teamMemberCounts[i] > 0)
24892500
{
24902501
if (teamMemberCounts[i] < allyCount)
2491-
return RANK_NONE;
2502+
return Rank.None;
24922503
}
24932504
}
24942505

@@ -2507,7 +2518,7 @@ protected int GetRank()
25072518
}
25082519

25092520
if (!pass)
2510-
return RANK_NONE;
2521+
return Rank.None;
25112522
}
25122523

25132524
return lowestEnemyAILevel + 1;

0 commit comments

Comments
 (0)