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

Last change on this file since 454 was 447, checked in by alloc, 17 months ago

24_28_41

File size: 4.4 KB
RevLine 
[144]1using System;
2using UnityEngine;
3
[324]4namespace AllocsFixes.PersistentData {
[325]5 [Serializable]
6 public class Player {
[447]7 private readonly PlatformUserIdentifierAbs internalId;
8 private readonly PlatformUserIdentifierAbs platformId;
9 private readonly PlatformUserIdentifierAbs crossPlatformId;
10
[325]11 private int entityId;
12 private string name;
13 private string ip;
14 private long totalPlayTime;
[276]15
[369]16 private DateTime lastOnline;
[144]17
[325]18 private Inventory inventory;
[144]19
[369]20 private int lastPositionX, lastPositionY, lastPositionZ;
[144]21
[369]22 private bool chatMuted;
23 private int maxChatLength;
24 private string chatColor;
25 private bool chatName;
26 private uint expToNextLevel;
27 private int level;
[144]28
[325]29 [NonSerialized] private ClientInfo clientInfo;
[144]30
[447]31 public PlatformUserIdentifierAbs InternalId => internalId;
32 public PlatformUserIdentifierAbs PlatformId => platformId;
33 public PlatformUserIdentifierAbs CrossPlatformId => crossPlatformId;
[144]34
[369]35 public int EntityID => entityId;
[144]36
[369]37 public string Name => name ?? string.Empty;
[144]38
[369]39 public string IP => ip ?? string.Empty;
[144]40
[446]41 public Inventory Inventory => inventory ??= new Inventory ();
[146]42
[369]43 public bool IsOnline => clientInfo != null;
[154]44
[369]45 public ClientInfo ClientInfo => clientInfo;
[253]46
[369]47 public EntityPlayer Entity => IsOnline ? GameManager.Instance.World.Players.dict [clientInfo.entityId] : null;
[253]48
[325]49 public long TotalPlayTime {
50 get {
51 if (IsOnline) {
52 return totalPlayTime + (long) (DateTime.Now - lastOnline).TotalSeconds;
53 }
[233]54
[325]55 return totalPlayTime;
56 }
57 }
[273]58
[369]59 public DateTime LastOnline => IsOnline ? DateTime.Now : lastOnline;
[273]60
[369]61 public Vector3i LastPosition => IsOnline ? new Vector3i (Entity.GetPosition ()) : new Vector3i (lastPositionX, lastPositionY, lastPositionZ);
[287]62
[369]63 public bool LandProtectionActive =>
64 GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance
[446]65 .GetPersistentPlayerList ().GetPlayerData (InternalId));
[287]66
[369]67 public float LandProtectionMultiplier =>
68 GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance
[446]69 .GetPersistentPlayerList ().GetPlayerData (InternalId));
[287]70
[325]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 }
[144]80
[325]81 public bool IsChatMuted {
[369]82 get => chatMuted;
83 set => chatMuted = value;
[325]84 }
[144]85
[325]86 public int MaxChatLength {
87 get {
88 if (maxChatLength == 0) {
89 maxChatLength = 255;
90 }
[324]91
[325]92 return maxChatLength;
93 }
[369]94 set => maxChatLength = value;
[325]95 }
[324]96
[325]97 public string ChatColor {
98 get {
[369]99 if (string.IsNullOrEmpty (chatColor)) {
[325]100 chatColor = "";
101 }
[324]102
[325]103 return chatColor;
104 }
[324]105
[369]106 set => chatColor = value;
[325]107 }
108
109 public bool ChatName {
[369]110 get => chatName;
111 set => chatName = value;
[325]112 }
113
[446]114 public Player (PlatformUserIdentifierAbs _internalId, PlatformUserIdentifierAbs _platformId, PlatformUserIdentifierAbs _crossPlatformId) {
[447]115 internalId = _internalId;
116 platformId = _platformId;
117 crossPlatformId = _crossPlatformId;
[325]118 inventory = new Inventory ();
119 }
120
121 public void SetOffline () {
[326]122 if (clientInfo == null) {
123 return;
124 }
[325]125
[446]126 Log.Out ("Player set to offline: " + InternalId);
[326]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...");
[325]136 }
[326]137
138 clientInfo = null;
[325]139 }
140
[351]141 public void SetOnline (ClientInfo _ci) {
[446]142 Log.Out ("Player set to online: " + InternalId);
[351]143 clientInfo = _ci;
144 entityId = _ci.entityId;
145 name = _ci.playerName;
146 ip = _ci.ip;
[325]147 lastOnline = DateTime.Now;
148 }
[324]149
[443]150 public void Update (ClientInfo _clientInfo, PlayerDataFile _pdf) {
151 UpdateProgression (_clientInfo, _pdf);
[325]152 inventory.Update (_pdf);
153 }
[324]154
[443]155 private void UpdateProgression (ClientInfo _clientInfo, PlayerDataFile _pdf) {
[326]156 if (_pdf.progressionData.Length <= 0) {
157 return;
[325]158 }
[326]159
[443]160 if (!GameManager.Instance.World.Entities.dict.TryGetValue (_clientInfo.entityId, out Entity entity)) {
161 return;
[326]162 }
[443]163
164 if (entity is not EntityPlayer ep) {
165 return;
166 }
167
168 expToNextLevel = (uint)ep.Progression.ExpToNextLevel;
169 level = ep.Progression.Level;
[325]170 }
[369]171
[325]172 }
[324]173}
Note: See TracBrowser for help on using the repository browser.