2021-01-29 15:29:50 +00:00
using OWML.Common ;
using QSB.Events ;
2021-01-30 10:09:27 +00:00
using QSB.Player ;
2021-01-26 17:08:02 +00:00
using QSB.Utility ;
using QSB.WorldSync ;
using System.Linq ;
2021-01-26 14:07:17 +00:00
namespace QSB.QuantumSync.Events
{
internal class QuantumAuthorityEvent : QSBEvent < QuantumAuthorityMessage >
{
public override EventType Type = > EventType . QuantumAuthority ;
public override void SetupListener ( ) = > GlobalMessenger < int , uint > . AddListener ( EventNames . QSBQuantumAuthority , Handler ) ;
public override void CloseListener ( ) = > GlobalMessenger < int , uint > . RemoveListener ( EventNames . QSBQuantumAuthority , Handler ) ;
private void Handler ( int objId , uint authorityOwner ) = > SendEvent ( CreateMessage ( objId , authorityOwner ) ) ;
private QuantumAuthorityMessage CreateMessage ( int objId , uint authorityOwner ) = > new QuantumAuthorityMessage
{
AboutId = LocalPlayerId ,
ObjectId = objId ,
AuthorityOwner = authorityOwner
} ;
2021-01-30 10:09:27 +00:00
public override bool CheckMessage ( bool isServer , QuantumAuthorityMessage message )
{
2021-02-09 11:01:21 +00:00
if ( ! QuantumManager . Instance . IsReady )
2021-02-09 10:03:13 +00:00
{
return false ;
}
2021-01-30 10:09:27 +00:00
var objects = QSBWorldSync . GetWorldObjects < IQSBQuantumObject > ( ) ;
var obj = objects . ToList ( ) [ message . ObjectId ] ;
2021-02-09 09:27:52 +00:00
// Deciding if to change the object's owner
// Message
// | = 0 | > 0 |
// = 0 | Yes*| Yes |
// > 0 | Yes | No |
// Obj
// *Doesn't change anything,
// so can be yes or no
2021-01-30 10:09:27 +00:00
return obj . ControllingPlayer = = 0 | | message . AuthorityOwner = = 0 ;
}
public override void OnReceiveLocal ( bool server , QuantumAuthorityMessage message )
{
var objects = QSBWorldSync . GetWorldObjects < IQSBQuantumObject > ( ) ;
var obj = objects . ToList ( ) [ message . ObjectId ] ;
obj . ControllingPlayer = message . AuthorityOwner ;
}
2021-01-26 14:07:17 +00:00
public override void OnReceiveRemote ( bool server , QuantumAuthorityMessage message )
{
2021-01-26 17:08:02 +00:00
var objects = QSBWorldSync . GetWorldObjects < IQSBQuantumObject > ( ) ;
2021-01-26 23:41:53 +00:00
var obj = objects . ToList ( ) [ message . ObjectId ] ;
2021-01-29 15:29:50 +00:00
if ( obj . ControllingPlayer ! = 0 & & message . AuthorityOwner ! = 0 )
{
2021-02-08 20:04:14 +00:00
DebugLog . ToConsole ( $"Warning - object {(obj as IWorldObject).Name} already has owner {obj.ControllingPlayer}, but trying to be replaced by {message.AuthorityOwner}!" , MessageType . Warning ) ;
2021-01-29 15:29:50 +00:00
}
2021-01-26 17:08:02 +00:00
obj . ControllingPlayer = message . AuthorityOwner ;
2021-01-30 10:09:27 +00:00
if ( obj . ControllingPlayer = = 0 & & obj . IsEnabled )
{
// object has no owner, but is still active for this player. request ownership
2021-02-13 10:11:02 +00:00
QSBEventManager . FireEvent ( EventNames . QSBQuantumAuthority , message . ObjectId , QSBPlayerManager . LocalPlayerId ) ;
2021-01-30 10:09:27 +00:00
}
2021-01-26 14:07:17 +00:00
}
}
}