source: binary-improvements/7dtd-server-fixes/src/PersistentData/Player.cs@ 294

Last change on this file since 294 was 287, checked in by alloc, 8 years ago

Updated core for Coppi's stuff

File size: 4.5 KB
Line 
1using System;
2using System.Runtime.Serialization;
3using UnityEngine;
4
5namespace 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)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad);
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 }
226}
Note: See TracBrowser for help on using the repository browser.