| 1 | using System; | 
|---|
| 2 | using UnityEngine; | 
|---|
| 3 |  | 
|---|
| 4 | namespace AllocsFixes.PersistentData { | 
|---|
| 5 | [Serializable] | 
|---|
| 6 | public class Player { | 
|---|
| 7 | private int entityId; | 
|---|
| 8 | private string name; | 
|---|
| 9 | private string ip; | 
|---|
| 10 | private long totalPlayTime; | 
|---|
| 11 |  | 
|---|
| 12 | private DateTime lastOnline; | 
|---|
| 13 |  | 
|---|
| 14 | private Inventory inventory; | 
|---|
| 15 |  | 
|---|
| 16 | private int lastPositionX, lastPositionY, lastPositionZ; | 
|---|
| 17 |  | 
|---|
| 18 | private bool chatMuted; | 
|---|
| 19 | private int maxChatLength; | 
|---|
| 20 | private string chatColor; | 
|---|
| 21 | private bool chatName; | 
|---|
| 22 | private uint expToNextLevel; | 
|---|
| 23 | private int level; | 
|---|
| 24 |  | 
|---|
| 25 | [NonSerialized] private ClientInfo clientInfo; | 
|---|
| 26 |  | 
|---|
| 27 | public PlatformUserIdentifierAbs InternalId { get; } | 
|---|
| 28 | public PlatformUserIdentifierAbs PlatformId { get; } | 
|---|
| 29 | public PlatformUserIdentifierAbs CrossPlatformId { get; } | 
|---|
| 30 |  | 
|---|
| 31 | public int EntityID => entityId; | 
|---|
| 32 |  | 
|---|
| 33 | public string Name => name ?? string.Empty; | 
|---|
| 34 |  | 
|---|
| 35 | public string IP => ip ?? string.Empty; | 
|---|
| 36 |  | 
|---|
| 37 | public Inventory Inventory => inventory ??= new Inventory (); | 
|---|
| 38 |  | 
|---|
| 39 | public bool IsOnline => clientInfo != null; | 
|---|
| 40 |  | 
|---|
| 41 | public ClientInfo ClientInfo => clientInfo; | 
|---|
| 42 |  | 
|---|
| 43 | public EntityPlayer Entity => IsOnline ? GameManager.Instance.World.Players.dict [clientInfo.entityId] : null; | 
|---|
| 44 |  | 
|---|
| 45 | public long TotalPlayTime { | 
|---|
| 46 | get { | 
|---|
| 47 | if (IsOnline) { | 
|---|
| 48 | return totalPlayTime + (long) (DateTime.Now - lastOnline).TotalSeconds; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | return totalPlayTime; | 
|---|
| 52 | } | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | public DateTime LastOnline => IsOnline ? DateTime.Now : lastOnline; | 
|---|
| 56 |  | 
|---|
| 57 | public Vector3i LastPosition => IsOnline ? new Vector3i (Entity.GetPosition ()) : new Vector3i (lastPositionX, lastPositionY, lastPositionZ); | 
|---|
| 58 |  | 
|---|
| 59 | public bool LandProtectionActive => | 
|---|
| 60 | GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance | 
|---|
| 61 | .GetPersistentPlayerList ().GetPlayerData (InternalId)); | 
|---|
| 62 |  | 
|---|
| 63 | public float LandProtectionMultiplier => | 
|---|
| 64 | GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance | 
|---|
| 65 | .GetPersistentPlayerList ().GetPlayerData (InternalId)); | 
|---|
| 66 |  | 
|---|
| 67 | public float Level { | 
|---|
| 68 | get { | 
|---|
| 69 | float expForNextLevel = | 
|---|
| 70 | (int) Math.Min (Progression.BaseExpToLevel * Mathf.Pow (Progression.ExpMultiplier, level + 1), | 
|---|
| 71 | int.MaxValue); | 
|---|
| 72 | float fLevel = level + 1f - expToNextLevel / expForNextLevel; | 
|---|
| 73 | return fLevel; | 
|---|
| 74 | } | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | public bool IsChatMuted { | 
|---|
| 78 | get => chatMuted; | 
|---|
| 79 | set => chatMuted = value; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | public int MaxChatLength { | 
|---|
| 83 | get { | 
|---|
| 84 | if (maxChatLength == 0) { | 
|---|
| 85 | maxChatLength = 255; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | return maxChatLength; | 
|---|
| 89 | } | 
|---|
| 90 | set => maxChatLength = value; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | public string ChatColor { | 
|---|
| 94 | get { | 
|---|
| 95 | if (string.IsNullOrEmpty (chatColor)) { | 
|---|
| 96 | chatColor = ""; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | return chatColor; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | set => chatColor = value; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | public bool ChatName { | 
|---|
| 106 | get => chatName; | 
|---|
| 107 | set => chatName = value; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | public Player (PlatformUserIdentifierAbs _internalId, PlatformUserIdentifierAbs _platformId, PlatformUserIdentifierAbs _crossPlatformId) { | 
|---|
| 111 | InternalId = _internalId; | 
|---|
| 112 | PlatformId = _platformId; | 
|---|
| 113 | CrossPlatformId = _crossPlatformId; | 
|---|
| 114 | inventory = new Inventory (); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | public void SetOffline () { | 
|---|
| 118 | if (clientInfo == null) { | 
|---|
| 119 | return; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | Log.Out ("Player set to offline: " + InternalId); | 
|---|
| 123 | lastOnline = DateTime.Now; | 
|---|
| 124 | try { | 
|---|
| 125 | Vector3i lastPos = new Vector3i (Entity.GetPosition ()); | 
|---|
| 126 | lastPositionX = lastPos.x; | 
|---|
| 127 | lastPositionY = lastPos.y; | 
|---|
| 128 | lastPositionZ = lastPos.z; | 
|---|
| 129 | totalPlayTime += (long) (Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad); | 
|---|
| 130 | } catch (NullReferenceException) { | 
|---|
| 131 | Log.Out ("Entity not available. Something seems to be wrong here..."); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | clientInfo = null; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | public void SetOnline (ClientInfo _ci) { | 
|---|
| 138 | Log.Out ("Player set to online: " + InternalId); | 
|---|
| 139 | clientInfo = _ci; | 
|---|
| 140 | entityId = _ci.entityId; | 
|---|
| 141 | name = _ci.playerName; | 
|---|
| 142 | ip = _ci.ip; | 
|---|
| 143 | lastOnline = DateTime.Now; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | public void Update (ClientInfo _clientInfo, PlayerDataFile _pdf) { | 
|---|
| 147 | UpdateProgression (_clientInfo, _pdf); | 
|---|
| 148 | inventory.Update (_pdf); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | private void UpdateProgression (ClientInfo _clientInfo, PlayerDataFile _pdf) { | 
|---|
| 152 | if (_pdf.progressionData.Length <= 0) { | 
|---|
| 153 | return; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | if (!GameManager.Instance.World.Entities.dict.TryGetValue (_clientInfo.entityId, out Entity entity)) { | 
|---|
| 157 | return; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | if (entity is not EntityPlayer ep) { | 
|---|
| 161 | return; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | expToNextLevel = (uint)ep.Progression.ExpToNextLevel; | 
|---|
| 165 | level = ep.Progression.Level; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | } | 
|---|
| 169 | } | 
|---|