world object auth

This commit is contained in:
JohnCorby 2022-08-15 22:53:46 -07:00
parent eebd36d11d
commit fcfac90a2a
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,15 @@
using QSB.WorldSync;
namespace QSB.AuthoritySync;
/// <summary>
/// a world object that has an owner
/// </summary>
public interface IAuthWorldObject : IWorldObject
{
public uint Owner { get; set; }
/// <summary>
/// can the world object have authority
/// </summary>
public bool CanOwn { get; }
}

View File

@ -0,0 +1,45 @@
using QSB.Messaging;
using QSB.Player;
namespace QSB.AuthoritySync;
/// <summary>
/// request ownership of a world object
/// </summary>
public class WorldObjectAuthMessage : QSBWorldObjectMessage<IAuthWorldObject, uint>
{
public WorldObjectAuthMessage(uint owner) : base(owner) { }
public override bool ShouldReceive
{
get
{
if (!base.ShouldReceive)
{
return false;
}
// Deciding if to change the object's owner
// Message
// | = 0 | > 0 |
// = 0 | No | Yes |
// > 0 | Yes | No |
// if Obj==Message then No
// Obj
return (WorldObject.Owner == 0 || Data == 0) && WorldObject.Owner != Data;
}
}
public override void OnReceiveLocal() => WorldObject.Owner = Data;
public override void OnReceiveRemote()
{
WorldObject.Owner = Data;
if (WorldObject.Owner == 0 && WorldObject.CanOwn)
{
// object has no owner, but is still active for this player. request ownership
WorldObject.SendMessage(new WorldObjectAuthMessage(QSBPlayerManager.LocalPlayerId));
}
}
}