Skip to content

Commit 0d4ee0e

Browse files
authored
Merge pull request #60 from DrMeepso/dev
Added `prev` command and ability to ban disconnected players
2 parents 261201b + cb70642 commit 0d4ee0e

File tree

4 files changed

+122
-6
lines changed

4 files changed

+122
-6
lines changed

Cove/Server/Actor/Player.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,38 @@ public WFPlayer(CSteamID id, string fisherName, SteamNetworkingIdentity identity
4646
this.identity = identity;
4747
}
4848
};
49+
50+
public enum PlayerState
51+
{
52+
InGame,
53+
Left,
54+
}
55+
56+
// Represents a player that was previously connected to the server
57+
public class PreviousPlayer
58+
{
59+
public CSteamID SteamId { get; set; }
60+
public string FisherID { get; set; }
61+
public string Username { get; set; }
62+
63+
public uint leftTimestamp { get; set; } = 0; // Timestamp when the player left, 0 if they are still connected
64+
public PlayerState State { get; set; } = PlayerState.InGame; // Current state of the player
65+
66+
public PreviousPlayer(CSteamID id, string fisherName, string FishID)
67+
{
68+
SteamId = id;
69+
FisherID = FishID;
70+
Username = fisherName;
71+
}
72+
73+
public static PreviousPlayer FromWFPlayer(WFPlayer player)
74+
{
75+
return new PreviousPlayer(
76+
player.SteamId,
77+
player.Username,
78+
player.FisherID
79+
);
80+
}
81+
}
82+
4983
}

Cove/Server/Server.Commands.cs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,33 @@ void RegisterDefaultCommands()
9191
// hacky fix,
9292
// Extract player name from the command message
9393
string playerIdent = string.Join(" ", args);
94-
// try find a user with the username first
95-
WFPlayer playerToBan = AllPlayers.ToList().Find(p => p.Username.Equals(playerIdent, StringComparison.OrdinalIgnoreCase));
96-
// if there is no player with the username try find someone with that fisher ID
94+
// try to find a user with the username first
95+
var playerToBan = AllPlayers.ToList().Find(p => p.Username.Equals(playerIdent, StringComparison.OrdinalIgnoreCase));
96+
// if there is no player with the username try to find someone with that fisher ID
9797
if (playerToBan == null)
9898
playerToBan = AllPlayers.ToList().Find(p => p.FisherID.Equals(playerIdent, StringComparison.OrdinalIgnoreCase));
99+
100+
// this could be programmed better, but it works
101+
if (playerToBan == null)
102+
{
103+
var previousPlayer = PreviousPlayers.ToList().Find(p => p.FisherID.Equals(playerIdent, StringComparison.OrdinalIgnoreCase));
104+
if (previousPlayer != null)
105+
{
106+
messagePlayer($"There is a previous player with that name, if you meant to ban them add a # before the ID: #{playerIdent}", player.SteamId);
107+
return;
108+
}
109+
110+
previousPlayer = PreviousPlayers.ToList().Find(p => $"#{p.FisherID}".Equals(playerIdent, StringComparison.OrdinalIgnoreCase));
111+
if (previousPlayer != null)
112+
{
113+
playerToBan = new WFPlayer(previousPlayer.SteamId, previousPlayer.Username, new SteamNetworkingIdentity())
114+
{
115+
FisherID = previousPlayer.FisherID,
116+
Username = previousPlayer.Username,
117+
};
118+
}
119+
}
120+
99121
if (playerToBan == null)
100122
{
101123
messagePlayer("Player not found!", player.SteamId);
@@ -114,6 +136,31 @@ void RegisterDefaultCommands()
114136
});
115137
SetCommandDescription("ban", "Bans a player from the server");
116138

139+
RegisterCommand("prev", (player, args) =>
140+
{
141+
if (!isPlayerAdmin(player.SteamId)) return;
142+
var sb = new StringBuilder();
143+
sb.AppendLine("Previous Players:");
144+
foreach (var prevPlayer in PreviousPlayers)
145+
{
146+
if (prevPlayer.State == PlayerState.InGame) continue;
147+
148+
// we dont want to show players that left more than 10 minutes ago
149+
if ((DateTime.UtcNow - DateTimeOffset.FromUnixTimeSeconds(prevPlayer.leftTimestamp).UtcDateTime)
150+
.TotalMinutes > 10)
151+
{
152+
continue;
153+
}
154+
155+
// get the time since the player left in a human readable format
156+
string timeLeft =
157+
$"{Math.Round((DateTime.UtcNow - DateTimeOffset.FromUnixTimeSeconds(prevPlayer.leftTimestamp).UtcDateTime).TotalMinutes)} minutes ago";
158+
sb.Append($"{prevPlayer.Username} ({prevPlayer.FisherID}) - Left: {timeLeft}\n");
159+
}
160+
messagePlayer(sb.ToString(), player.SteamId);
161+
});
162+
SetCommandDescription("prev", "Shows a list of previous players that were connected to the server");
163+
117164
}
118165

119166
public void RegisterCommand(string command, Action<WFPlayer, string[]> cb)

Cove/Server/Server.Punish.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ public bool isPlayerBanned(CSteamID id)
6464
private void writeToBansFile(CSteamID id)
6565
{
6666
string fileDir = $"{AppDomain.CurrentDomain.BaseDirectory}bans.txt";
67-
WFPlayer player = AllPlayers.Find(p => p.SteamId == id);
68-
File.AppendAllLines(fileDir, [$"{id.m_SteamID} #{player.Username}"]);
67+
PreviousPlayer player = PreviousPlayers.Find(p => p.SteamId == id);
68+
string username = player != null ? player.Username : "Unknown";
69+
File.AppendAllLines(fileDir, [$"{id.m_SteamID} #{username}"]);
6970
}
7071

7172
public void kickPlayer(CSteamID id)

Cove/Server/Server.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public partial class CoveServer
8282

8383
public List<string> WantedTags = new();
8484

85+
public List<PreviousPlayer> PreviousPlayers = new();
86+
8587
public void Init()
8688
{
8789
cbThread = new(runSteamworksUpdate);
@@ -346,6 +348,7 @@ public void Init()
346348
CSteamID userMakingChange = new CSteamID(param.m_ulSteamIDMakingChange);
347349

348350
EChatMemberStateChange stateChange = (EChatMemberStateChange)param.m_rgfChatMemberStateChange;
351+
// Player joined the lobby
349352
if (stateChange.HasFlag(EChatMemberStateChange.k_EChatMemberStateChangeEntered))
350353
{
351354
string Username = SteamFriends.GetFriendPersonaName(userChanged);
@@ -356,6 +359,7 @@ public void Init()
356359
connectionsQueued.Add(userChanged);
357360
}
358361

362+
// Player left the lobby
359363
if (stateChange.HasFlag(EChatMemberStateChange.k_EChatMemberStateChangeLeft) || stateChange.HasFlag(EChatMemberStateChange.k_EChatMemberStateChangeDisconnected))
360364
{
361365
string Username = SteamFriends.GetFriendPersonaName(userChanged);
@@ -367,7 +371,7 @@ public void Init()
367371
// if player is in AllPlayers, remove them
368372
if (!connectionsQueued.Contains(userChanged))
369373
{
370-
WFPlayer player = AllPlayers.Find(p => p.SteamId == userChanged);
374+
var player = AllPlayers.Find(p => p.SteamId == userChanged);
371375
if (player != null)
372376
{
373377
AllPlayers.Remove(player);
@@ -385,6 +389,21 @@ public void Init()
385389
// tell all plugins that the player left
386390
loadedPlugins.ForEach(plugin => plugin.plugin.onPlayerLeave(player));
387391

392+
// find the previous player and update their state
393+
var previousPlayer = PreviousPlayers.Find(p => p.SteamId == player.SteamId);
394+
if (previousPlayer != null)
395+
{
396+
previousPlayer.leftTimestamp = (uint)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
397+
previousPlayer.State = PlayerState.Left;
398+
}
399+
else
400+
{
401+
// if the player is not in the previous players list, add them
402+
var p = PreviousPlayer.FromWFPlayer(player);
403+
PreviousPlayers.Add(p);
404+
p.leftTimestamp = (uint)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
405+
p.State = PlayerState.Left;
406+
}
388407
}
389408
}
390409
}
@@ -467,6 +486,21 @@ out chatEntryType
467486
// make the player a wfplayer
468487
WFPlayer player = new WFPlayer(userId, SteamFriends.GetFriendPersonaName(userId), new SteamNetworkingIdentity());
469488
AllPlayers.Add(player);
489+
490+
// if there is already a player with the same FisherID, remove them from the previous players list to prevent duplicates
491+
var sharedIDPrev = PreviousPlayers.Find(p => p.FisherID == player.FisherID);
492+
if (sharedIDPrev != null)
493+
{
494+
PreviousPlayers.Remove(sharedIDPrev); // remove the previous player with the same FisherID
495+
}
496+
497+
var prev = PreviousPlayers.Find(p => p.SteamId == userId);
498+
if (prev != null)
499+
{
500+
PreviousPlayers.Remove(prev); // remove the previous player if they are already in the list
501+
}
502+
503+
PreviousPlayers.Add( PreviousPlayer.FromWFPlayer(player) ); // add the player to the previous players list
470504

471505
Dictionary<string, object> joinedPacket = new();
472506
joinedPacket["type"] = "user_joined_weblobby";

0 commit comments

Comments
 (0)