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

Last change on this file since 458 was 455, checked in by alloc, 16 months ago

25_30_44

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