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

Last change on this file since 442 was 369, checked in by alloc, 3 years ago

Preparations for A20 release
Changes usage of "SteamID" to "UserID" in console commands
Also changes a bunch of the WebAPI stuff to show / use UserIDs

File size: 4.1 KB
Line 
1using System;
2using UnityEngine;
3
4namespace AllocsFixes.PersistentData {
5 [Serializable]
6 public class Player {
7 private readonly PlatformUserIdentifierAbs platformId;
8 private int entityId;
9 private string name;
10 private string ip;
11 private long totalPlayTime;
12
13 private DateTime lastOnline;
14
15 private Inventory inventory;
16
17 private int lastPositionX, lastPositionY, lastPositionZ;
18
19 private bool chatMuted;
20 private int maxChatLength;
21 private string chatColor;
22 private bool chatName;
23 private uint expToNextLevel;
24 private int level;
25
26 [NonSerialized] private ClientInfo clientInfo;
27
28 public PlatformUserIdentifierAbs PlatformId => platformId;
29
30 public int EntityID => entityId;
31
32 public string Name => name ?? string.Empty;
33
34 public string IP => ip ?? string.Empty;
35
36 public Inventory Inventory => inventory ?? (inventory = new Inventory ());
37
38 public bool IsOnline => clientInfo != null;
39
40 public ClientInfo ClientInfo => clientInfo;
41
42 public EntityPlayer Entity => IsOnline ? GameManager.Instance.World.Players.dict [clientInfo.entityId] : null;
43
44 public long TotalPlayTime {
45 get {
46 if (IsOnline) {
47 return totalPlayTime + (long) (DateTime.Now - lastOnline).TotalSeconds;
48 }
49
50 return totalPlayTime;
51 }
52 }
53
54 public DateTime LastOnline => IsOnline ? DateTime.Now : lastOnline;
55
56 public Vector3i LastPosition => IsOnline ? new Vector3i (Entity.GetPosition ()) : new Vector3i (lastPositionX, lastPositionY, lastPositionZ);
57
58 public bool LandProtectionActive =>
59 GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance
60 .GetPersistentPlayerList ().GetPlayerData (PlatformId));
61
62 public float LandProtectionMultiplier =>
63 GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance
64 .GetPersistentPlayerList ().GetPlayerData (PlatformId));
65
66 public float Level {
67 get {
68 float expForNextLevel =
69 (int) Math.Min (Progression.BaseExpToLevel * Mathf.Pow (Progression.ExpMultiplier, level + 1),
70 int.MaxValue);
71 float fLevel = level + 1f - expToNextLevel / expForNextLevel;
72 return fLevel;
73 }
74 }
75
76 public bool IsChatMuted {
77 get => chatMuted;
78 set => chatMuted = value;
79 }
80
81 public int MaxChatLength {
82 get {
83 if (maxChatLength == 0) {
84 maxChatLength = 255;
85 }
86
87 return maxChatLength;
88 }
89 set => maxChatLength = value;
90 }
91
92 public string ChatColor {
93 get {
94 if (string.IsNullOrEmpty (chatColor)) {
95 chatColor = "";
96 }
97
98 return chatColor;
99 }
100
101 set => chatColor = value;
102 }
103
104 public bool ChatName {
105 get => chatName;
106 set => chatName = value;
107 }
108
109 public Player (PlatformUserIdentifierAbs _platformId) {
110 platformId = _platformId;
111 inventory = new Inventory ();
112 }
113
114 public void SetOffline () {
115 if (clientInfo == null) {
116 return;
117 }
118
119 Log.Out ("Player set to offline: " + platformId);
120 lastOnline = DateTime.Now;
121 try {
122 Vector3i lastPos = new Vector3i (Entity.GetPosition ());
123 lastPositionX = lastPos.x;
124 lastPositionY = lastPos.y;
125 lastPositionZ = lastPos.z;
126 totalPlayTime += (long) (Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad);
127 } catch (NullReferenceException) {
128 Log.Out ("Entity not available. Something seems to be wrong here...");
129 }
130
131 clientInfo = null;
132 }
133
134 public void SetOnline (ClientInfo _ci) {
135 Log.Out ("Player set to online: " + platformId);
136 clientInfo = _ci;
137 entityId = _ci.entityId;
138 name = _ci.playerName;
139 ip = _ci.ip;
140 lastOnline = DateTime.Now;
141 }
142
143 public void Update (PlayerDataFile _pdf) {
144 UpdateProgression (_pdf);
145 inventory.Update (_pdf);
146 }
147
148 private void UpdateProgression (PlayerDataFile _pdf) {
149 if (_pdf.progressionData.Length <= 0) {
150 return;
151 }
152
153 using (PooledBinaryReader pbr = MemoryPools.poolBinaryReader.AllocSync (false)) {
154 pbr.SetBaseStream (_pdf.progressionData);
155 long posBefore = pbr.BaseStream.Position;
156 pbr.BaseStream.Position = 0;
157 Progression p = Progression.Read (pbr, null);
158 pbr.BaseStream.Position = posBefore;
159 expToNextLevel = (uint) p.ExpToNextLevel;
160 level = p.Level;
161 }
162 }
163
164 }
165}
Note: See TracBrowser for help on using the repository browser.