[144] | 1 | using System;
|
---|
| 2 | using System.Runtime.Serialization;
|
---|
| 3 | using UnityEngine;
|
---|
| 4 |
|
---|
[324] | 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;
|
---|
[276] | 13 |
|
---|
[324] | 14 | [OptionalField] private DateTime
|
---|
| 15 | lastOnline;
|
---|
[144] | 16 |
|
---|
[324] | 17 | private Inventory inventory;
|
---|
[144] | 18 |
|
---|
[324] | 19 | [OptionalField] private int
|
---|
| 20 | lastPositionX, lastPositionY, lastPositionZ;
|
---|
[144] | 21 |
|
---|
[324] | 22 | [OptionalField] [Obsolete ("experience no longer available, use level and expToNextLevel instead")]
|
---|
| 23 | private uint experience;
|
---|
[144] | 24 |
|
---|
[324] | 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;
|
---|
[144] | 31 |
|
---|
[324] | 32 | [NonSerialized] private ClientInfo
|
---|
| 33 | clientInfo;
|
---|
[144] | 34 |
|
---|
[324] | 35 | public string SteamID {
|
---|
| 36 | get { return steamId; }
|
---|
| 37 | }
|
---|
[144] | 38 |
|
---|
[324] | 39 | public int EntityID {
|
---|
| 40 | get { return entityId; }
|
---|
| 41 | }
|
---|
[144] | 42 |
|
---|
[324] | 43 | public string Name {
|
---|
| 44 | get { return name == null ? string.Empty : name; }
|
---|
| 45 | }
|
---|
[144] | 46 |
|
---|
[324] | 47 | public string IP {
|
---|
| 48 | get { return ip == null ? string.Empty : ip; }
|
---|
| 49 | }
|
---|
[144] | 50 |
|
---|
[324] | 51 | public Inventory Inventory {
|
---|
| 52 | get {
|
---|
| 53 | if (inventory == null)
|
---|
| 54 | inventory = new Inventory ();
|
---|
| 55 | return inventory;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
[146] | 58 |
|
---|
[324] | 59 | public bool IsOnline {
|
---|
| 60 | get { return clientInfo != null; }
|
---|
| 61 | }
|
---|
[154] | 62 |
|
---|
[324] | 63 | public ClientInfo ClientInfo {
|
---|
| 64 | get { return clientInfo; }
|
---|
| 65 | }
|
---|
[253] | 66 |
|
---|
[324] | 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 | }
|
---|
[253] | 76 |
|
---|
[324] | 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 | }
|
---|
[276] | 86 |
|
---|
[324] | 87 | public DateTime LastOnline {
|
---|
| 88 | get {
|
---|
| 89 | if (IsOnline)
|
---|
| 90 | return DateTime.Now;
|
---|
| 91 | else
|
---|
| 92 | return lastOnline;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
[233] | 95 |
|
---|
[324] | 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 | }
|
---|
[233] | 104 |
|
---|
[324] | 105 | public bool LandProtectionActive {
|
---|
| 106 | get {
|
---|
| 107 | return GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance
|
---|
| 108 | .GetPersistentPlayerList ().GetPlayerData (SteamID));
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
[273] | 111 |
|
---|
[324] | 112 | public float LandProtectionMultiplier {
|
---|
| 113 | get {
|
---|
| 114 | return GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance
|
---|
| 115 | .GetPersistentPlayerList ().GetPlayerData (SteamID));
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
[273] | 118 |
|
---|
[287] | 119 |
|
---|
[324] | 120 | [Obsolete ("Experience no longer available, use Level instead")]
|
---|
| 121 | public uint Experience {
|
---|
| 122 | get { return 0; }
|
---|
| 123 | }
|
---|
[287] | 124 |
|
---|
[324] | 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 | }
|
---|
[287] | 134 |
|
---|
[324] | 135 | public bool IsChatMuted {
|
---|
| 136 | get { return chatMuted; }
|
---|
| 137 | set { chatMuted = value; }
|
---|
| 138 | }
|
---|
[287] | 139 |
|
---|
[324] | 140 | public int MaxChatLength {
|
---|
| 141 | get {
|
---|
| 142 | if (maxChatLength == 0) {
|
---|
| 143 | maxChatLength = 255;
|
---|
| 144 | }
|
---|
[144] | 145 |
|
---|
[324] | 146 | return maxChatLength;
|
---|
| 147 | }
|
---|
| 148 | set { maxChatLength = value; }
|
---|
| 149 | }
|
---|
[144] | 150 |
|
---|
[324] | 151 | public string ChatColor {
|
---|
| 152 | get {
|
---|
| 153 | if (chatColor == null || chatColor == "") {
|
---|
| 154 | chatColor = "";
|
---|
| 155 | }
|
---|
[233] | 156 |
|
---|
[324] | 157 | return chatColor;
|
---|
| 158 | }
|
---|
[144] | 159 |
|
---|
[324] | 160 | set { chatColor = value; }
|
---|
| 161 | }
|
---|
[144] | 162 |
|
---|
[324] | 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 | }
|
---|
| 217 | }
|
---|