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

Last change on this file since 327 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

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