- Timestamp:
- Sep 3, 2018, 7:11:10 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/PersistentData/Player.cs
r301 r324 3 3 using UnityEngine; 4 4 5 namespace AllocsFixes.PersistentData 6 { 7 [Serializable] 8 public class Player 9 { 10 private readonly string steamId; 11 private int entityId; 12 private string name; 13 private string ip; 14 private long totalPlayTime; 15 [OptionalField] 16 private DateTime 17 lastOnline; 18 private Inventory inventory; 19 [OptionalField] 20 private int 21 lastPositionX, lastPositionY, lastPositionZ; 22 [OptionalField][Obsolete("experience no longer available, use level and expToNextLevel instead")] 23 private uint experience; 24 [OptionalField] 25 private bool chatMuted; 26 [OptionalField] 27 private int maxChatLength; 28 [OptionalField] 29 private string chatColor; 30 [OptionalField] 31 private bool chatName; 32 [OptionalField] 33 private uint expToNextLevel; 34 [OptionalField] 35 private int level; 36 37 [NonSerialized] 38 private ClientInfo 39 clientInfo; 40 41 public string SteamID { 42 get { return steamId; } 43 } 44 45 public int EntityID { 46 get { return entityId; } 47 } 48 49 public string Name { 50 get { return name == null ? string.Empty : name; } 51 } 52 53 public string IP { 54 get { return ip == null ? string.Empty : ip; } 55 } 56 57 public Inventory Inventory { 58 get { 59 if (inventory == null) 60 inventory = new Inventory (); 61 return inventory; 62 } 63 } 64 65 public bool IsOnline { 66 get { return clientInfo != null; } 67 } 68 69 public ClientInfo ClientInfo { 70 get { return clientInfo; } 71 } 72 73 public EntityPlayer Entity { 74 get { 75 if (IsOnline) { 76 return GameManager.Instance.World.Players.dict [clientInfo.entityId]; 77 } else { 78 return null; 79 } 80 } 81 } 82 83 public long TotalPlayTime { 84 get { 85 if (IsOnline) { 86 return totalPlayTime + (long)(DateTime.Now - lastOnline).TotalSeconds; 87 } else { 88 return totalPlayTime; 89 } 90 } 91 } 92 93 public DateTime LastOnline { 94 get { 95 if (IsOnline) 96 return DateTime.Now; 97 else 98 return lastOnline; 99 } 100 } 101 102 public Vector3i LastPosition { 103 get { 104 if (IsOnline) 105 return new Vector3i (Entity.GetPosition ()); 106 else 107 return new Vector3i (lastPositionX, lastPositionY, lastPositionZ); 108 } 109 } 110 111 public bool LandProtectionActive { 112 get { 113 return GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance.GetPersistentPlayerList ().GetPlayerData (SteamID)); 114 } 115 } 116 117 public float LandProtectionMultiplier { 118 get { 119 return GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance.GetPersistentPlayerList ().GetPlayerData (SteamID)); 120 } 121 } 122 123 124 [Obsolete("Experience no longer available, use Level instead")] 125 public uint Experience { 126 get { 127 return 0; 128 } 129 } 130 131 public float Level { 132 get { 133 float expForNextLevel = (int)Math.Min ((Progression.BaseExpToLevel * Mathf.Pow (Progression.ExpMultiplier, level + 1)), int.MaxValue); 134 float fLevel = level + 1f - ((float)expToNextLevel / expForNextLevel); 135 return fLevel; 136 } 137 } 138 139 public bool IsChatMuted{ 140 get { 141 return chatMuted; 142 } 143 set { 144 chatMuted = value; 145 } 146 } 147 148 public int MaxChatLength { 149 get { 150 if (maxChatLength == 0 ) { 151 maxChatLength = 255; 152 } 153 return maxChatLength; 154 } 155 set { 156 maxChatLength = value; 157 } 158 } 159 160 public string ChatColor { 161 get { 162 if (chatColor == null || chatColor == "") { 163 chatColor = ""; 164 } 165 return chatColor; 166 } 167 168 set { 169 chatColor = value; 170 } 171 } 172 173 public bool ChatName { 174 get { 175 return chatName; 176 } 177 178 set { 179 chatName = value; 180 } 181 } 182 183 public void SetOffline () 184 { 185 if (clientInfo != null) { 186 Log.Out ("Player set to offline: " + steamId); 187 lastOnline = DateTime.Now; 188 try { 189 Vector3i lastPos = new Vector3i (Entity.GetPosition ()); 190 lastPositionX = lastPos.x; 191 lastPositionY = lastPos.y; 192 lastPositionZ = lastPos.z; 193 totalPlayTime += (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad); 194 } catch (NullReferenceException) { 195 Log.Out ("Entity not available. Something seems to be wrong here..."); 196 } 197 clientInfo = null; 198 } 199 } 200 201 public void SetOnline (ClientInfo ci) 202 { 203 Log.Out ("Player set to online: " + steamId); 204 clientInfo = ci; 205 entityId = ci.entityId; 206 name = ci.playerName; 207 ip = ci.ip; 208 lastOnline = DateTime.Now; 209 } 210 211 public void Update (PlayerDataFile _pdf) { 212 experience = 0; 213 expToNextLevel = _pdf.experience; 214 level = _pdf.level; 215 inventory.Update (_pdf); 216 } 217 218 public Player (string steamId) 219 { 220 this.steamId = steamId; 221 this.inventory = new Inventory (); 222 } 223 224 225 } 5 namespace AllocsFixes.PersistentData { 6 [Serializable] 7 public class Player { 8 private readonly string steamId; 9 private int entityId; 10 private string name; 11 private string ip; 12 private long totalPlayTime; 13 14 [OptionalField] private DateTime 15 lastOnline; 16 17 private Inventory inventory; 18 19 [OptionalField] private int 20 lastPositionX, lastPositionY, lastPositionZ; 21 22 [OptionalField] [Obsolete ("experience no longer available, use level and expToNextLevel instead")] 23 private uint experience; 24 25 [OptionalField] private bool chatMuted; 26 [OptionalField] private int maxChatLength; 27 [OptionalField] private string chatColor; 28 [OptionalField] private bool chatName; 29 [OptionalField] private uint expToNextLevel; 30 [OptionalField] private int level; 31 32 [NonSerialized] private ClientInfo 33 clientInfo; 34 35 public string SteamID { 36 get { return steamId; } 37 } 38 39 public int EntityID { 40 get { return entityId; } 41 } 42 43 public string Name { 44 get { return name == null ? string.Empty : name; } 45 } 46 47 public string IP { 48 get { return ip == null ? string.Empty : ip; } 49 } 50 51 public Inventory Inventory { 52 get { 53 if (inventory == null) 54 inventory = new Inventory (); 55 return inventory; 56 } 57 } 58 59 public bool IsOnline { 60 get { return clientInfo != null; } 61 } 62 63 public ClientInfo ClientInfo { 64 get { return clientInfo; } 65 } 66 67 public EntityPlayer Entity { 68 get { 69 if (IsOnline) { 70 return GameManager.Instance.World.Players.dict [clientInfo.entityId]; 71 } else { 72 return null; 73 } 74 } 75 } 76 77 public long TotalPlayTime { 78 get { 79 if (IsOnline) { 80 return totalPlayTime + (long) (DateTime.Now - lastOnline).TotalSeconds; 81 } else { 82 return totalPlayTime; 83 } 84 } 85 } 86 87 public DateTime LastOnline { 88 get { 89 if (IsOnline) 90 return DateTime.Now; 91 else 92 return lastOnline; 93 } 94 } 95 96 public Vector3i LastPosition { 97 get { 98 if (IsOnline) 99 return new Vector3i (Entity.GetPosition ()); 100 else 101 return new Vector3i (lastPositionX, lastPositionY, lastPositionZ); 102 } 103 } 104 105 public bool LandProtectionActive { 106 get { 107 return GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance 108 .GetPersistentPlayerList ().GetPlayerData (SteamID)); 109 } 110 } 111 112 public float LandProtectionMultiplier { 113 get { 114 return GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance 115 .GetPersistentPlayerList ().GetPlayerData (SteamID)); 116 } 117 } 118 119 120 [Obsolete ("Experience no longer available, use Level instead")] 121 public uint Experience { 122 get { return 0; } 123 } 124 125 public float Level { 126 get { 127 float expForNextLevel = 128 (int) Math.Min ((Progression.BaseExpToLevel * Mathf.Pow (Progression.ExpMultiplier, level + 1)), 129 int.MaxValue); 130 float fLevel = level + 1f - ((float) expToNextLevel / expForNextLevel); 131 return fLevel; 132 } 133 } 134 135 public bool IsChatMuted { 136 get { return chatMuted; } 137 set { chatMuted = value; } 138 } 139 140 public int MaxChatLength { 141 get { 142 if (maxChatLength == 0) { 143 maxChatLength = 255; 144 } 145 146 return maxChatLength; 147 } 148 set { maxChatLength = value; } 149 } 150 151 public string ChatColor { 152 get { 153 if (chatColor == null || chatColor == "") { 154 chatColor = ""; 155 } 156 157 return chatColor; 158 } 159 160 set { chatColor = value; } 161 } 162 163 public bool ChatName { 164 get { return chatName; } 165 166 set { chatName = value; } 167 } 168 169 public void SetOffline () { 170 if (clientInfo != null) { 171 Log.Out ("Player set to offline: " + steamId); 172 lastOnline = DateTime.Now; 173 try { 174 Vector3i lastPos = new Vector3i (Entity.GetPosition ()); 175 lastPositionX = lastPos.x; 176 lastPositionY = lastPos.y; 177 lastPositionZ = lastPos.z; 178 totalPlayTime += (long) (Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad); 179 } catch (NullReferenceException) { 180 Log.Out ("Entity not available. Something seems to be wrong here..."); 181 } 182 183 clientInfo = null; 184 } 185 } 186 187 public void SetOnline (ClientInfo ci) { 188 Log.Out ("Player set to online: " + steamId); 189 clientInfo = ci; 190 entityId = ci.entityId; 191 name = ci.playerName; 192 ip = ci.ip; 193 lastOnline = DateTime.Now; 194 } 195 196 public void Update (PlayerDataFile _pdf) { 197 UpdateProgression (_pdf); 198 inventory.Update (_pdf); 199 } 200 201 private void UpdateProgression (PlayerDataFile _pdf) { 202 if (_pdf.progressionData.Length > 0) { 203 using (PooledBinaryReader pbr = MemoryPools.poolBinaryReader.AllocSync (false)) { 204 pbr.SetBaseStream (_pdf.progressionData); 205 Progression p = Progression.Read (pbr, null); 206 expToNextLevel = (uint) p.ExpToNextLevel; 207 level = p.Level; 208 } 209 } 210 } 211 212 public Player (string steamId) { 213 this.steamId = steamId; 214 this.inventory = new Inventory (); 215 } 216 } 226 217 }
Note:
See TracChangeset
for help on using the changeset viewer.