Skip to content

Commit 5c6d775

Browse files
committed
Added prev command to list all players in the server in the last 10 minutes.
Allow for banning disconnected players with there fisherID
1 parent 95de840 commit 5c6d775

File tree

4 files changed

+120
-5
lines changed

4 files changed

+120
-5
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ 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);
67+
PreviousPlayer player = PreviousPlayers.Find(p => p.SteamId == id);
6868
File.AppendAllLines(fileDir, [$"{id.m_SteamID} #{player.Username}"]);
6969
}
7070

Cove/Server/Server.cs

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

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

86+
public List<PreviousPlayer> PreviousPlayers = new();
87+
8688
public void Init()
8789
{
8890
cbThread = new(runSteamworksUpdate);
@@ -347,6 +349,7 @@ public void Init()
347349
CSteamID userMakingChange = new CSteamID(param.m_ulSteamIDMakingChange);
348350

349351
EChatMemberStateChange stateChange = (EChatMemberStateChange)param.m_rgfChatMemberStateChange;
352+
// Player joined the lobby
350353
if (stateChange.HasFlag(EChatMemberStateChange.k_EChatMemberStateChangeEntered))
351354
{
352355
string Username = SteamFriends.GetFriendPersonaName(userChanged);
@@ -357,6 +360,7 @@ public void Init()
357360
connectionsQueued.Add(userChanged);
358361
}
359362

363+
// Player left the lobby
360364
if (stateChange.HasFlag(EChatMemberStateChange.k_EChatMemberStateChangeLeft) || stateChange.HasFlag(EChatMemberStateChange.k_EChatMemberStateChangeDisconnected))
361365
{
362366
string Username = SteamFriends.GetFriendPersonaName(userChanged);
@@ -368,7 +372,7 @@ public void Init()
368372
// if player is in AllPlayers, remove them
369373
if (!connectionsQueued.Contains(userChanged))
370374
{
371-
WFPlayer player = AllPlayers.Find(p => p.SteamId == userChanged);
375+
var player = AllPlayers.Find(p => p.SteamId == userChanged);
372376
if (player != null)
373377
{
374378
AllPlayers.Remove(player);
@@ -386,6 +390,21 @@ public void Init()
386390
// tell all plugins that the player left
387391
loadedPlugins.ForEach(plugin => plugin.plugin.onPlayerLeave(player));
388392

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

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

0 commit comments

Comments
 (0)