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