Skip to content

Commit

Permalink
Fixed error when player leaves, fixed null chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMeepso committed Nov 28, 2024
1 parent 596dbd9 commit 177f83a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 47 deletions.
2 changes: 2 additions & 0 deletions Cove/Server/Actor/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class WFActor
public int despawnTime = -1;
public bool despawn = true;

public CSteamID owner = new CSteamID(0); // 0 is the server

public WFActor(long ID, string Type, Vector3 entPos, Vector3 entRot = null)

Check warning on line 40 in Cove/Server/Actor/Actor.cs

View workflow job for this annotation

GitHub Actions / windows-build

Cannot convert null literal to non-nullable reference type.

Check warning on line 40 in Cove/Server/Actor/Actor.cs

View workflow job for this annotation

GitHub Actions / linux-build

Cannot convert null literal to non-nullable reference type.
{
InstanceID = ID;
Expand Down
2 changes: 2 additions & 0 deletions Cove/Server/Actor/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public WFPlayer(CSteamID id, string fisherName) : base(0, "player", Vector3.zero
FisherID = randomID;
Username = fisherName;

owner = id;

pos = new Vector3(0, 0, 0);
despawn = false; // players down despawn!
}
Expand Down
15 changes: 3 additions & 12 deletions Cove/Server/Server.Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ You may obtain a copy of the License at
limitations under the License.
*/


using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -25,37 +24,29 @@ namespace Cove.Server
{
public partial class CoveServer
{
// purely for debug
// purely for debug, yes i know its 100% fucked
public static void printStringDict(Dictionary<string, object> obj, string sub = "")
{
foreach (var kvp in obj)
{
if (kvp.Value is Dictionary<string, object>)
{
printStringDict((Dictionary<string, object>)kvp.Value, sub + "." + kvp.Key);
}
else if (kvp.Value is Dictionary<int, object>)
{
printArray((Dictionary<int, object>)kvp.Value, sub + "." + kvp.Key);
} else {
else
Console.WriteLine($"{sub} {kvp.Key}: {kvp.Value}");
}
}
}
public static void printArray(Dictionary<int, object> obj, string sub = "")
{
foreach (var kvp in obj)
{
if (kvp.Value is Dictionary<string, object>)
{
printStringDict((Dictionary<string, object>)kvp.Value, sub + "." + kvp.Key);
}
else if (kvp.Value is Dictionary<int, object>)
{
printArray((Dictionary<int, object>)kvp.Value, sub + "." + kvp.Key);
} else {
else
Console.WriteLine($"{sub} {kvp.Key}: {kvp.Value}");
}
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions Cove/Server/Server.Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ You may obtain a copy of the License at
limitations under the License.
*/


using Steamworks;
using Cove.GodotFormat;
using Cove.Server.Actor;
using Cove.Server.Utils;
using System.Reflection;

namespace Cove.Server
{
Expand Down Expand Up @@ -56,10 +54,9 @@ void OnNetworkPacket(byte[] packet, CSteamID sender)
hostPacket["type"] = "recieve_host";
hostPacket["host_id"] = SteamUser.GetSteamID().m_SteamID.ToString();
sendPacketToPlayers(hostPacket);

if (isPlayerAdmin(sender))
{
messagePlayer("You're an admin on this server!", sender);
}

Thread ChalkInformer = new Thread(() => SendStagedChalkPackets(sender));
ChalkInformer.Start(); // send the player all the chalk data
Expand Down Expand Up @@ -90,13 +87,17 @@ void OnNetworkPacket(byte[] packet, CSteamID sender)
{
WFPlayer thisPlayer = AllPlayers.Find(p => p.SteamId.m_SteamID == sender.m_SteamID);
if (thisPlayer == null)
{
Console.WriteLine("No fisher found for player instance!");
}
else
{
thisPlayer.InstanceID = actorID;
allActors.Add(thisPlayer); // add the player to the actor list
}

} else {
WFActor cActor = new WFActor(actorID, type, Vector3.zero, Vector3.zero);
cActor.owner = sender;
allActors.Add(cActor);
}

}
Expand Down
3 changes: 3 additions & 0 deletions Cove/Server/Server.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public void spawnRainCloud()
sendPacketToPlayers(rainSpawnPacket); // spawn the rain!
RainCloud cloud = new RainCloud(IId, pos);
cloud.despawn = true;

serverOwnedInstances.Add(cloud);
allActors.Add(cloud);
}

public WFActor spawnFish(string fishType = "fish_spawn")
Expand Down Expand Up @@ -130,6 +132,7 @@ public WFActor spawnGenericActor(string type, Vector3 pos = null)

WFActor actor = new WFActor(IId, type, pos);
serverOwnedInstances.Add(actor);
allActors.Add(actor);

instanceSpacePrams["actor_type"] = type;
instanceSpacePrams["at"] = pos;
Expand Down
45 changes: 16 additions & 29 deletions Cove/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ You may obtain a copy of the License at
limitations under the License.
*/


using Steamworks;
using Cove.Server.Plugins;
using Cove.GodotFormat;
using Cove.Server.Actor;
using Cove.Server.Utils;
using Microsoft.Extensions.Hosting;
using Cove.Server.HostedServices;
using Microsoft.Extensions.Logging;
using System.Reflection;
using Vector3 = Cove.GodotFormat.Vector3;

namespace Cove.Server
{
Expand Down Expand Up @@ -52,6 +50,7 @@ public partial class CoveServer

public List<WFPlayer> AllPlayers = new();
public List<WFActor> serverOwnedInstances = new();
public List<WFActor> allActors = new();

Thread cbThread;
Thread networkThread;
Expand All @@ -65,15 +64,13 @@ public partial class CoveServer

public void Init()
{

cbThread = new(runSteamworksUpdate);
networkThread = new(RunNetwork);

Console.WriteLine("Loading world!");
string worldFile = $"{AppDomain.CurrentDomain.BaseDirectory}worlds/main_zone.tscn";
if (!File.Exists(worldFile))
{

Console.WriteLine("-- ERROR --");
Console.WriteLine("main_zone.tscn is missing!");
Console.WriteLine("please put a world file in the /worlds folder so the server may load it!");
Expand Down Expand Up @@ -287,11 +284,9 @@ public void Init()
WFPlayer newPlayer = new WFPlayer(userChanged, Username);
AllPlayers.Add(newPlayer);

//Console.WriteLine($"{Username} has been assigned the fisherID: {newPlayer.FisherID}");

foreach (PluginInstance plugin in loadedPlugins)
foreach (PluginInstance p in loadedPlugins)
{
plugin.plugin.onPlayerJoin(newPlayer);
p.plugin.onPlayerJoin(newPlayer);
}

// check if the player is banned
Expand All @@ -310,25 +305,18 @@ public void Init()

if (stateChange.HasFlag(EChatMemberStateChange.k_EChatMemberStateChangeLeft) || stateChange.HasFlag(EChatMemberStateChange.k_EChatMemberStateChangeDisconnected))
{

string Username = SteamFriends.GetFriendPersonaName(userChanged);

Console.WriteLine($"{Username} [{userChanged.m_SteamID}] has left the game!");
updatePlayercount();

foreach (var player in AllPlayers)
WFPlayer leavingPlayer = AllPlayers.Find(p => p.SteamId.m_SteamID == userChanged.m_SteamID);
foreach (PluginInstance plugin in loadedPlugins)
{
if (player.SteamId.m_SteamID == userChanged.m_SteamID)
{

foreach (PluginInstance plugin in loadedPlugins)
{
plugin.plugin.onPlayerLeave(player);
}

AllPlayers.Remove(player);
}
plugin.plugin.onPlayerLeave(leavingPlayer);
}
AllPlayers.Remove(leavingPlayer);
allActors.RemoveAll(a => a.owner.m_SteamID == userChanged.m_SteamID);
}
});

Expand Down Expand Up @@ -363,25 +351,17 @@ public void Init()
private bool getBoolFromString(string str)
{
if (str.ToLower() == "true")
{
return true;
}
else if (str.ToLower() == "false")
{
return false;
}
else
{
return false;
}
}

void runSteamworksUpdate()
{
while (true)
{
SteamAPI.RunCallbacks();
}
}

void RunNetwork()
Expand Down Expand Up @@ -423,6 +403,13 @@ void OnPlayerChat(string message, CSteamID id)
{

WFPlayer sender = AllPlayers.Find(p => p.SteamId == id);
if (sender == null)
{
Console.WriteLine($"[UNKNOWN] {id}: {message}");
// should probbaly kick the player here
return;
}

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

foreach (PluginInstance plugin in loadedPlugins)
Expand Down

0 comments on commit 177f83a

Please sign in to comment.