using System; using System.Runtime.Serialization; using UnityEngine; namespace AllocsFixes.PersistentData { [Serializable] public class Player { private readonly string steamId; private int entityId; private string name; private string ip; private long totalPlayTime; [OptionalField] private DateTime lastOnline; private Inventory inventory; [OptionalField] private int lastPositionX, lastPositionY, lastPositionZ; [NonSerialized] private ClientInfo clientInfo; public string SteamID { get { return steamId; } } public int EntityID { get { return entityId; } } public string Name { get { return name == null ? string.Empty : name; } } public string IP { get { return ip == null ? string.Empty : ip; } } public Inventory Inventory { get { if (inventory == null) inventory = new Inventory (); return inventory; } } public bool IsOnline { get { return clientInfo != null; } } public ClientInfo ClientInfo { get { return clientInfo; } } public EntityPlayer Entity { get { if (IsOnline) { return CommonMappingFunctions.GetEntityPlayer (clientInfo); } else { return null; } } } public long TotalPlayTime { get { if (IsOnline) { return totalPlayTime + (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad); } else { return totalPlayTime; } } } public DateTime LastOnline { get { if (IsOnline) return DateTime.Now; else return lastOnline; } } public Vector3i LastPosition { get { if (IsOnline) return new Vector3i (Entity.GetPosition ()); else return new Vector3i (lastPositionX, lastPositionY, lastPositionZ); } } public void SetOffline () { if (clientInfo != null) { Log.Out ("Player set to offline: " + steamId); lastOnline = DateTime.Now; try { Vector3i lastPos = new Vector3i (Entity.GetPosition ()); lastPositionX = lastPos.x; lastPositionY = lastPos.y; lastPositionZ = lastPos.z; totalPlayTime += (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad); } catch (NullReferenceException) { Log.Out ("Entity not available. Something seems to be wrong here..."); } clientInfo = null; } } public void SetOnline (ClientInfo ci) { Log.Out ("Player set to online: " + steamId); clientInfo = ci; entityId = CommonMappingFunctions.GetEntityID (ci); name = CommonMappingFunctions.GetPlayerName (ci); ip = ci.networkPlayer.ipAddress; } public Player (string steamId) { this.steamId = steamId; this.inventory = new Inventory (); } } }