Skip to content

Add Player Leave Event #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Uchu.Char/Handlers/CharacterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using RakDotNet;
using Uchu.Core;
using Uchu.Core.Client;
using Uchu.World;

namespace Uchu.Char.Handlers
{
Expand Down Expand Up @@ -69,7 +70,17 @@ private static async Task SendCharacterList(IRakConnection connection, long user
[PacketHandler]
public async Task CharacterList(CharacterListRequest packet, IRakConnection connection)
{
// Remove the player from the zone.
var session = UchuServer.SessionCache.GetSession(connection.EndPoint);
if (UchuServer is WorldUchuServer worldUchuServer) {
var player = worldUchuServer.FindPlayer(connection);
if (player != null)
{
await player.DestroyAsync();
}
}

// Send the character list.
await SendCharacterList(connection, session.UserId);
}

Expand Down
1 change: 1 addition & 0 deletions Uchu.Char/Uchu.Char.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<ProjectReference Include="..\Uchu.Core\Uchu.Core.csproj" />
<ProjectReference Include="..\Uchu.World\Uchu.World.csproj" />
</ItemGroup>

</Project>
16 changes: 12 additions & 4 deletions Uchu.World/Objects/GameObjects/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ internal Player()
{
Connection.Disconnected += async reason =>
{
Logger.Information($"{this} left: {reason}.");
await GetComponent<SaveComponent>().SaveAsync(false);
Connection = default;
Destroy(this);
await DestroyAsync();
};

Listen(OnPositionUpdate, UpdatePhysics);
Expand Down Expand Up @@ -117,6 +114,17 @@ internal Player()
});
}

/// <summary>
/// Destroys the player.
/// </summary>
public async Task DestroyAsync(CloseReason? reason = CloseReason.ClientDisconnect)
{
Logger.Information($"{this} left: {reason}.");
await GetComponent<SaveComponent>().SaveAsync(false);
Connection = default;
Destroy(this);
}

/// <summary>
/// Constructs the player, settings spawn parameters and masks
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions Uchu.World/Objects/Zone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class Zone : Object

// Events
public Event<Player> OnPlayerLoad { get; }
public Event<Player> OnPlayerLeave { get; }
public Event<Object> OnObject { get; }
public Event OnTick { get; }
public Event<Player, string> OnChatMessage { get; }
Expand All @@ -98,6 +99,7 @@ public Zone(ZoneInfo zoneInfo, UchuServer uchuServer, ushort instanceId = defaul
EarlyPhysics = new Event();
LatePhysics = new Event();
OnPlayerLoad = new Event<Player>();
OnPlayerLeave = new Event<Player>();
OnObject = new Event<Object>();
OnTick = new Event();
OnChatMessage = new Event<Player, string>();
Expand Down Expand Up @@ -401,6 +403,12 @@ internal void UnregisterObject(Object obj)

ManagedObjects.Remove(obj);

// Invoke the player left event if the object is an event.
if (obj is Player player)
{
OnPlayerLeave.Invoke(player);
}

if (obj is GameObject gameObject)
{
if ((gameObject.Id.Flags & ObjectIdFlags.Spawned) != 0)
Expand Down