[144] | 1 | using System;
|
---|
| 2 | using System.Runtime.Serialization;
|
---|
| 3 | using UnityEngine;
|
---|
| 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;
|
---|
[146] | 15 | [OptionalField]
|
---|
| 16 | private DateTime
|
---|
| 17 | lastOnline;
|
---|
[144] | 18 | private Inventory inventory;
|
---|
[154] | 19 | [OptionalField]
|
---|
| 20 | private int
|
---|
| 21 | lastPositionX, lastPositionY, lastPositionZ;
|
---|
[233] | 22 | [OptionalField]
|
---|
| 23 | private uint experience;
|
---|
[273] | 24 | [OptionalField]
|
---|
| 25 | private bool chatMuted;
|
---|
| 26 | [OptionalField]
|
---|
| 27 | private int maxChatLength;
|
---|
[144] | 28 | [NonSerialized]
|
---|
| 29 | private ClientInfo
|
---|
| 30 | clientInfo;
|
---|
| 31 |
|
---|
| 32 | public string SteamID {
|
---|
| 33 | get { return steamId; }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public int EntityID {
|
---|
| 37 | get { return entityId; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public string Name {
|
---|
[161] | 41 | get { return name == null ? string.Empty : name; }
|
---|
[144] | 42 | }
|
---|
| 43 |
|
---|
| 44 | public string IP {
|
---|
[161] | 45 | get { return ip == null ? string.Empty : ip; }
|
---|
[144] | 46 | }
|
---|
| 47 |
|
---|
| 48 | public Inventory Inventory {
|
---|
| 49 | get {
|
---|
| 50 | if (inventory == null)
|
---|
| 51 | inventory = new Inventory ();
|
---|
| 52 | return inventory;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public bool IsOnline {
|
---|
| 57 | get { return clientInfo != null; }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public ClientInfo ClientInfo {
|
---|
| 61 | get { return clientInfo; }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public EntityPlayer Entity {
|
---|
| 65 | get {
|
---|
| 66 | if (IsOnline) {
|
---|
[230] | 67 | return GameManager.Instance.World.Players.dict [clientInfo.entityId];
|
---|
[144] | 68 | } else {
|
---|
| 69 | return null;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public long TotalPlayTime {
|
---|
| 75 | get {
|
---|
| 76 | if (IsOnline) {
|
---|
| 77 | return totalPlayTime + (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad);
|
---|
| 78 | } else {
|
---|
| 79 | return totalPlayTime;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[146] | 84 | public DateTime LastOnline {
|
---|
| 85 | get {
|
---|
| 86 | if (IsOnline)
|
---|
| 87 | return DateTime.Now;
|
---|
| 88 | else
|
---|
| 89 | return lastOnline;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[154] | 93 | public Vector3i LastPosition {
|
---|
| 94 | get {
|
---|
| 95 | if (IsOnline)
|
---|
| 96 | return new Vector3i (Entity.GetPosition ());
|
---|
| 97 | else
|
---|
| 98 | return new Vector3i (lastPositionX, lastPositionY, lastPositionZ);
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[253] | 102 | public bool LandProtectionActive {
|
---|
| 103 | get {
|
---|
| 104 | return GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance.GetPersistentPlayerList ().GetPlayerData (SteamID));
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public float LandProtectionMultiplier {
|
---|
| 109 | get {
|
---|
| 110 | return GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance.GetPersistentPlayerList ().GetPlayerData (SteamID));
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[233] | 114 | public uint Experience {
|
---|
| 115 | get {
|
---|
| 116 | return experience;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | public float Level {
|
---|
| 121 | get {
|
---|
| 122 | float perc = (float)experience / 600000;
|
---|
| 123 | perc = Mathf.Sqrt (perc);
|
---|
| 124 | return Mathf.Clamp ((perc * 60) + 1, 1, 60);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[273] | 128 | public bool IsChatMuted{
|
---|
| 129 | get {
|
---|
| 130 | return chatMuted;
|
---|
| 131 | }
|
---|
| 132 | set {
|
---|
| 133 | chatMuted = value;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | public int MaxChatLength {
|
---|
| 138 | get {
|
---|
| 139 | if (maxChatLength == 0 ) {
|
---|
| 140 | maxChatLength = 255;
|
---|
| 141 | }
|
---|
| 142 | return maxChatLength;
|
---|
| 143 | }
|
---|
| 144 | set {
|
---|
| 145 | maxChatLength = value;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[144] | 149 | public void SetOffline ()
|
---|
| 150 | {
|
---|
[161] | 151 | if (clientInfo != null) {
|
---|
| 152 | Log.Out ("Player set to offline: " + steamId);
|
---|
| 153 | lastOnline = DateTime.Now;
|
---|
[198] | 154 | try {
|
---|
| 155 | Vector3i lastPos = new Vector3i (Entity.GetPosition ());
|
---|
| 156 | lastPositionX = lastPos.x;
|
---|
| 157 | lastPositionY = lastPos.y;
|
---|
| 158 | lastPositionZ = lastPos.z;
|
---|
| 159 | totalPlayTime += (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad);
|
---|
| 160 | } catch (NullReferenceException) {
|
---|
| 161 | Log.Out ("Entity not available. Something seems to be wrong here...");
|
---|
| 162 | }
|
---|
[161] | 163 | clientInfo = null;
|
---|
| 164 | }
|
---|
[144] | 165 | }
|
---|
| 166 |
|
---|
| 167 | public void SetOnline (ClientInfo ci)
|
---|
| 168 | {
|
---|
| 169 | Log.Out ("Player set to online: " + steamId);
|
---|
| 170 | clientInfo = ci;
|
---|
[230] | 171 | entityId = ci.entityId;
|
---|
| 172 | name = ci.playerName;
|
---|
[228] | 173 | ip = ci.ip;
|
---|
[238] | 174 | lastOnline = DateTime.Now;
|
---|
[144] | 175 | }
|
---|
| 176 |
|
---|
[233] | 177 | public void Update (PlayerDataFile _pdf) {
|
---|
| 178 | experience = _pdf.experience;
|
---|
| 179 | inventory.Update (_pdf);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[144] | 182 | public Player (string steamId)
|
---|
| 183 | {
|
---|
| 184 | this.steamId = steamId;
|
---|
| 185 | this.inventory = new Inventory ();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | }
|
---|
| 190 | }
|
---|