2019-04-24 17:17:40 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Mirror;
|
2019-04-24 21:02:39 +00:00
|
|
|
|
using SanAndreasUnity.Behaviours;
|
2019-04-24 23:00:31 +00:00
|
|
|
|
using SanAndreasUnity.Utilities;
|
2019-05-05 21:12:42 +00:00
|
|
|
|
using System.Linq;
|
2019-04-24 17:17:40 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Net
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class Player : NetworkBehaviour
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
static List<Player> s_allPlayers = new List<Player>();
|
|
|
|
|
public static Player[] AllPlayers { get { return s_allPlayers.ToArray(); } }
|
2019-05-05 21:12:42 +00:00
|
|
|
|
public static IEnumerable<Player> AllPlayersEnumerable { get { return s_allPlayers; } }
|
2019-04-24 17:17:40 +00:00
|
|
|
|
|
2019-04-24 21:02:39 +00:00
|
|
|
|
/// <summary>Local player.</summary>
|
|
|
|
|
public static Player Local { get; private set; }
|
|
|
|
|
|
2019-04-24 23:00:31 +00:00
|
|
|
|
public static event System.Action<Player> onStart = delegate {};
|
|
|
|
|
|
2019-04-24 22:04:14 +00:00
|
|
|
|
[SyncVar(hook=nameof(OnOwnedGameObjectChanged))] GameObject m_ownedGameObject;
|
|
|
|
|
Ped m_ownedPed;
|
2019-04-24 21:02:39 +00:00
|
|
|
|
//public GameObject OwnedGameObject { get { return m_ownedGameObject; } internal set { m_ownedGameObject = value; } }
|
2019-04-24 22:04:14 +00:00
|
|
|
|
public Ped OwnedPed { get { return m_ownedPed; } internal set { m_ownedPed = value; m_ownedGameObject = value != null ? value.gameObject : null; } }
|
2019-04-24 17:39:38 +00:00
|
|
|
|
|
2019-07-09 00:37:49 +00:00
|
|
|
|
public string DescriptionForLogging => "(netId=" + this.netId + ", addr=" + (this.connectionToClient != null ? this.connectionToClient.address : "") + ")";
|
|
|
|
|
|
2019-04-24 17:17:40 +00:00
|
|
|
|
|
2019-05-05 21:12:42 +00:00
|
|
|
|
|
|
|
|
|
public static Player GetOwningPlayer(Ped ped)
|
|
|
|
|
{
|
|
|
|
|
if (null == ped)
|
|
|
|
|
return null;
|
|
|
|
|
return AllPlayersEnumerable.FirstOrDefault(p => p.OwnedPed == ped);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 17:17:40 +00:00
|
|
|
|
void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
s_allPlayers.Add(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
s_allPlayers.Remove(this);
|
2019-04-28 14:03:54 +00:00
|
|
|
|
|
|
|
|
|
// kill player's ped
|
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
{
|
|
|
|
|
if (this.OwnedPed)
|
|
|
|
|
Destroy(this.OwnedPed.gameObject);
|
|
|
|
|
}
|
2019-04-28 14:09:58 +00:00
|
|
|
|
|
2019-04-24 17:17:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 22:04:14 +00:00
|
|
|
|
public override void OnStartClient()
|
|
|
|
|
{
|
|
|
|
|
base.OnStartClient();
|
|
|
|
|
|
|
|
|
|
if (this.isServer)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_ownedPed = m_ownedGameObject != null ? m_ownedGameObject.GetComponent<Ped>() : null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 21:02:39 +00:00
|
|
|
|
public override void OnStartLocalPlayer()
|
|
|
|
|
{
|
|
|
|
|
base.OnStartLocalPlayer();
|
|
|
|
|
Local = this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 17:17:40 +00:00
|
|
|
|
void Start()
|
|
|
|
|
{
|
2019-04-28 14:24:33 +00:00
|
|
|
|
// log some info
|
2019-07-09 00:46:45 +00:00
|
|
|
|
if (!this.isLocalPlayer)
|
2019-07-09 00:37:49 +00:00
|
|
|
|
Debug.LogFormat("Player {0} connected, time: {1}", this.DescriptionForLogging, F.CurrentDateForLogging);
|
2019-04-28 14:24:33 +00:00
|
|
|
|
|
2019-04-24 23:00:31 +00:00
|
|
|
|
F.InvokeEventExceptionSafe(onStart, this);
|
2019-04-24 17:17:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 14:27:35 +00:00
|
|
|
|
public override void OnNetworkDestroy()
|
|
|
|
|
{
|
|
|
|
|
base.OnNetworkDestroy();
|
|
|
|
|
|
|
|
|
|
// log some info about this
|
2019-07-09 00:46:45 +00:00
|
|
|
|
if (!this.isLocalPlayer)
|
2019-07-09 00:37:49 +00:00
|
|
|
|
Debug.LogFormat("Player {0} disconnected, time: {1}", this.DescriptionForLogging, F.CurrentDateForLogging);
|
2019-04-28 14:27:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 22:04:14 +00:00
|
|
|
|
void OnOwnedGameObjectChanged(GameObject newGo)
|
|
|
|
|
{
|
2019-04-25 23:57:20 +00:00
|
|
|
|
Debug.LogFormat("Owned game object changed for player (net id {0})", this.netId);
|
|
|
|
|
|
2019-04-24 22:04:14 +00:00
|
|
|
|
if (this.isServer)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_ownedGameObject = newGo;
|
|
|
|
|
|
|
|
|
|
m_ownedPed = m_ownedGameObject != null ? m_ownedGameObject.GetComponent<Ped>() : null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 00:28:09 +00:00
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Telepathy does not detect dead connections, so we'll have to detect them ourselves
|
|
|
|
|
if (NetStatus.IsServer && !this.isLocalPlayer)
|
|
|
|
|
{
|
|
|
|
|
if (Time.time - this.connectionToClient.lastMessageTime > 6f)
|
|
|
|
|
{
|
|
|
|
|
// disconnect client
|
2019-07-09 00:41:08 +00:00
|
|
|
|
Debug.LogFormat("Detected dead connection for player {0}", this.DescriptionForLogging);
|
2019-07-09 00:28:09 +00:00
|
|
|
|
this.connectionToClient.Disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 17:17:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|