Skip to content

Commit

Permalink
make commands unified across the server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMeepso committed Dec 7, 2024
1 parent 3dab09b commit 89ea2df
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 305 deletions.
403 changes: 172 additions & 231 deletions Cove.ChatCommands/ChatCommands.cs

Large diffs are not rendered by default.

75 changes: 5 additions & 70 deletions Cove/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,77 +73,12 @@ void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e)
string input = Console.ReadLine();

Check warning on line 73 in Cove/Program.cs

View workflow job for this annotation

GitHub Actions / windows-build

Converting null literal or possible null value to non-nullable type.

Check warning on line 73 in Cove/Program.cs

View workflow job for this annotation

GitHub Actions / linux-build

Converting null literal or possible null value to non-nullable type.
string command = input.Split(' ')[0];

Check warning on line 74 in Cove/Program.cs

View workflow job for this annotation

GitHub Actions / windows-build

Dereference of a possibly null reference.

Check warning on line 74 in Cove/Program.cs

View workflow job for this annotation

GitHub Actions / linux-build

Dereference of a possibly null reference.

switch(command)
if (webfishingServer.DoseCommandExist(command))
{
case "exit":
Log("Server closed from console");
closeServer();
break;
case "say":
{
string message = input.Substring(command.Length + 1);
webfishingServer.messageGlobal($"Server: {message}");
Log($"Server: {message}");
}
break;
case "ban":
{
string id = input.Substring(command.Length + 1);
WFPlayer player = webfishingServer.AllPlayers.Find(p => p.Username.ToLower() == id.ToLower());
if (player != null)
{
if (webfishingServer.isPlayerBanned(player.SteamId))
{
Log($"Player {player.Username} is already banned!");
break;
} else
{
webfishingServer.banPlayer(player.SteamId, true);
}
Log($"Banned player {player.Username}");
}
else
{
Log("Player not found!");
}
}
break;
case "kick":
{
string id = input.Substring(command.Length + 1);
WFPlayer player = webfishingServer.AllPlayers.Find(p => p.Username.ToLower() == id.ToLower());
if (player != null)
{
webfishingServer.kickPlayer(player.SteamId);
Log($"Kicked player {player.Username}");
}
else
{
Log("Player not found!");
}
}
break;
case "players":
Log("Players:");
foreach (WFPlayer player in webfishingServer.AllPlayers)
{
Log(player.Username);
}
break;
case "help":
Log("Commands:");
Log("exit - Closes the application");
Log("say <message> - Sends a message to all players");
Log("ban <player> - Bans a player");
Log("kick <player> - Kicks a player");
Log("help - Shows this message");
Log("players - Lists all players");
Log("");
Log("players are the username of the player");
break;
default:
Log("Unknown command! Type 'help' for a list of commands.");
break;
string[] commandArgs = input.Split(' ').Skip(1).ToArray();
webfishingServer.InvokeCommand(webfishingServer.serverPlayer, command, commandArgs);
}
else
Log("Command not found!");

}
20 changes: 20 additions & 0 deletions Cove/Server/Plugins/CovePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,25 @@ public void SendPacketToAll(Dictionary<string, object> packet)
ParentServer.sendPacketToPlayers(packet);
}

public void RegisterCommand(string command, Action<WFPlayer, string[]> callback)
{
ParentServer.RegisterCommand(command, callback);
}

public void SetCommandDescription(string command, string description)
{
ParentServer.SetCommandDescription(command, description);
}

public void UnregisterCommand(string command)
{
ParentServer.UnregisterCommand(command);
}

public bool DoesCommandExist(string command)
{
return ParentServer.DoseCommandExist(command);
}

}
}
110 changes: 109 additions & 1 deletion Cove/Server/Server.Commands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Cove.Server.Actor;
using Steamworks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -8,8 +10,114 @@

namespace Cove.Server
{

public class RegisteredCommand
{
public string Command;
public string Description;
public Action<WFPlayer, string[]> Callback;
public RegisteredCommand(string command, string description, Action<WFPlayer, string[]> callback)
{
Command = command.ToLower(); // make sure its lower case to not mess anything up
Description = description;
Callback = callback;
}

public void Invoke(WFPlayer player, string[] args)
{
Callback(player, args);
}

}

public partial class CoveServer
{
List<RegisteredCommand> Commands = [];

void RegisterDefaultCommands()
{
RegisterCommand("help", (player, args) =>
{
var sb = new StringBuilder();
sb.AppendLine("Commands:");
foreach (var cmd in Commands)
{
sb.AppendLine($"{cmd.Command} - {cmd.Description}");
}
messagePlayer(sb.ToString(), player.SteamId);
});
SetCommandDescription("help", "Shows all commands");

RegisterCommand("exit", (player, args) =>
{
// make sure the player is the host
if (player.SteamId != serverPlayer.SteamId)
{
messagePlayer("You are not the host!", player.SteamId);
return;
}
messagePlayer("Server is shutting down!", player.SteamId);

Dictionary<string, object> closePacket = new();
closePacket["type"] = "server_close";

loadedPlugins.ForEach(plugin => plugin.plugin.onEnd()); // tell all plugins that the server is closing!

disconnectAllPlayers();
SteamMatchmaking.LeaveLobby(SteamLobby);
SteamAPI.Shutdown();
Environment.Exit(0);
});
SetCommandDescription("exit", "Shuts down the server (host only)");
}

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

if (Commands.Any(c => c.Command == command))
{
Log($"Command '{command}' is already registerd!");
return;
}

Commands.Add(new RegisteredCommand(command, "", cb));

}

public void UnregisterCommand(string command)
{
Commands.RemoveAll(c => c.Command == command);
}

public void SetCommandDescription(string command, string description)
{
var cmd = Commands.Find(c => c.Command == command);
if (cmd == null)
{
Log($"Command '{command}' not found!");
return;
}
cmd.Description = description;
}

public void InvokeCommand(WFPlayer player, string command, string[] args)
{
var cmd = Commands.Find(c => c.Command == command);
if (cmd == null)
{
Log($"Command '{command}' not found!");
return;
}
cmd.Invoke(player, args);
}

public bool DoseCommandExist(string command)
{
var cmd = Commands.Find(c => c.Command == command);
if (cmd == null)
return false;

return true;
}
}
}
2 changes: 0 additions & 2 deletions Cove/Server/Server.Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ void OnNetworkPacket(byte[] packet, CSteamID sender)
string type = (string)((Dictionary<string, object>)packetInfo["params"])["actor_type"];
long actorID = (long)((Dictionary<string, object>)packetInfo["params"])["actor_id"];

Log($"Player spawned in a {type} actor");

// all actor types that should not be spawned by anyone but the server!
if (type == "fish_spawn_alien" || type == "fish_spawn" || type == "raincloud")
{
Expand Down
14 changes: 13 additions & 1 deletion Cove/Server/Server.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void readAdmins()
}
}

public void spawnRainCloud()
public WFActor spawnRainCloud()
{
Random rand = new Random();
Dictionary<string, object> rainSpawnPacket = new Dictionary<string, object>();
Expand Down Expand Up @@ -76,6 +76,8 @@ public void spawnRainCloud()
serverOwnedInstances.Add(cloud);

allActors.Add(cloud);

return cloud;
}

public WFActor spawnFish(string fishType = "fish_spawn")
Expand Down Expand Up @@ -274,6 +276,12 @@ public void messagePlayer(string msg, CSteamID id, string color = "ffffff")
chatPacket["zone"] = "main_zone";
chatPacket["zone_owner"] = 1;

if (id.m_SteamID == SteamUser.GetSteamID().m_SteamID)
{
Log($"{msg}");
return;
}

sendPacketToPlayer(chatPacket, id);
}

Expand Down Expand Up @@ -308,6 +316,10 @@ public void runActorReady(WFActor instance)

public bool isPlayerAdmin(CSteamID id)
{
// if the server is being checked, they are an admin
if (id.m_SteamID == serverPlayer.SteamId.m_SteamID)
return true;

string adminSteamID = Admins.Find(a => long.Parse(a) == (long)id.m_SteamID);
return adminSteamID is string;
}
Expand Down
22 changes: 22 additions & 0 deletions Cove/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public partial class CoveServer
public List<WFActor> serverOwnedInstances = new();
public List<WFActor> allActors = new();

public WFPlayer serverPlayer;

Thread cbThread;
Thread networkThread;

Expand Down Expand Up @@ -187,6 +189,8 @@ public void Init()
readAdmins();
Log("Setup finished, starting server!");

RegisterDefaultCommands(); // register the default commands

if (Directory.Exists($"{AppDomain.CurrentDomain.BaseDirectory}plugins"))
{
loadAllPlugins();
Expand All @@ -204,6 +208,8 @@ public void Init()
return;
}

serverPlayer = new WFPlayer(SteamUser.GetSteamID(), SteamFriends.GetPersonaName());

// thread for running steamworks callbacks
cbThread.IsBackground = true;
cbThread.Start();
Expand Down Expand Up @@ -433,6 +439,22 @@ void OnPlayerChat(string message, CSteamID id)

Log($"[{sender.FisherID}] {sender.Username}: {message}");

// check if the first char is !, if so its a command
if (message.StartsWith("!"))
{
string command = message.Split(' ')[0].Substring(1);
string[] args = message.Split(' ').Skip(1).ToArray();
if (DoseCommandExist(command))
{
InvokeCommand(sender, command, args);
}
else
{
messagePlayer("Command not found!", sender.SteamId);
Log("Command not found!");
}
}

foreach (PluginInstance plugin in loadedPlugins)
{
plugin.plugin.onChatMessage(sender, message);
Expand Down

0 comments on commit 89ea2df

Please sign in to comment.