source: binary-improvements2/7dtd-server-fixes/src/PersistentData/Player.cs@ 410

Last change on this file since 410 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 4.1 KB
RevLine 
[144]1using System;
2using UnityEngine;
3
[324]4namespace AllocsFixes.PersistentData {
[325]5 [Serializable]
6 public class Player {
[369]7 private readonly PlatformUserIdentifierAbs platformId;
[325]8 private int entityId;
9 private string name;
10 private string ip;
11 private long totalPlayTime;
[276]12
[369]13 private DateTime lastOnline;
[144]14
[325]15 private Inventory inventory;
[144]16
[369]17 private int lastPositionX, lastPositionY, lastPositionZ;
[144]18
[369]19 private bool chatMuted;
20 private int maxChatLength;
21 private string chatColor;
22 private bool chatName;
23 private uint expToNextLevel;
24 private int level;
[144]25
[325]26 [NonSerialized] private ClientInfo clientInfo;
[144]27
[369]28 public PlatformUserIdentifierAbs PlatformId => platformId;
[144]29
[369]30 public int EntityID => entityId;
[144]31
[369]32 public string Name => name ?? string.Empty;
[144]33
[369]34 public string IP => ip ?? string.Empty;
[144]35
[369]36 public Inventory Inventory => inventory ?? (inventory = new Inventory ());
[146]37
[369]38 public bool IsOnline => clientInfo != null;
[154]39
[369]40 public ClientInfo ClientInfo => clientInfo;
[253]41
[369]42 public EntityPlayer Entity => IsOnline ? GameManager.Instance.World.Players.dict [clientInfo.entityId] : null;
[253]43
[325]44 public long TotalPlayTime {
45 get {
46 if (IsOnline) {
47 return totalPlayTime + (long) (DateTime.Now - lastOnline).TotalSeconds;
48 }
[233]49
[325]50 return totalPlayTime;
51 }
52 }
[273]53
[369]54 public DateTime LastOnline => IsOnline ? DateTime.Now : lastOnline;
[273]55
[369]56 public Vector3i LastPosition => IsOnline ? new Vector3i (Entity.GetPosition ()) : new Vector3i (lastPositionX, lastPositionY, lastPositionZ);
[287]57
[369]58 public bool LandProtectionActive =>
59 GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance
60 .GetPersistentPlayerList ().GetPlayerData (PlatformId));
[287]61
[369]62 public float LandProtectionMultiplier =>
63 GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance
64 .GetPersistentPlayerList ().GetPlayerData (PlatformId));
[287]65
[325]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 }
[144]75
[325]76 public bool IsChatMuted {
[369]77 get => chatMuted;
78 set => chatMuted = value;
[325]79 }
[144]80
[325]81 public int MaxChatLength {
82 get {
83 if (maxChatLength == 0) {
84 maxChatLength = 255;
85 }
[324]86
[325]87 return maxChatLength;
88 }
[369]89 set => maxChatLength = value;
[325]90 }
[324]91
[325]92 public string ChatColor {
93 get {
[369]94 if (string.IsNullOrEmpty (chatColor)) {
[325]95 chatColor = "";
96 }
[324]97
[325]98 return chatColor;
99 }
[324]100
[369]101 set => chatColor = value;
[325]102 }
103
104 public bool ChatName {
[369]105 get => chatName;
106 set => chatName = value;
[325]107 }
108
[369]109 public Player (PlatformUserIdentifierAbs _platformId) {
110 platformId = _platformId;
[325]111 inventory = new Inventory ();
112 }
113
114 public void SetOffline () {
[326]115 if (clientInfo == null) {
116 return;
117 }
[325]118
[402]119 Log.Out ($"Player set to offline: {platformId}");
[326]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...");
[325]129 }
[326]130
131 clientInfo = null;
[325]132 }
133
[351]134 public void SetOnline (ClientInfo _ci) {
[402]135 Log.Out ($"Player set to online: {platformId}");
[351]136 clientInfo = _ci;
137 entityId = _ci.entityId;
138 name = _ci.playerName;
139 ip = _ci.ip;
[325]140 lastOnline = DateTime.Now;
141 }
[324]142
[325]143 public void Update (PlayerDataFile _pdf) {
144 UpdateProgression (_pdf);
145 inventory.Update (_pdf);
146 }
[324]147
[325]148 private void UpdateProgression (PlayerDataFile _pdf) {
[326]149 if (_pdf.progressionData.Length <= 0) {
150 return;
[325]151 }
[326]152
153 using (PooledBinaryReader pbr = MemoryPools.poolBinaryReader.AllocSync (false)) {
154 pbr.SetBaseStream (_pdf.progressionData);
[333]155 long posBefore = pbr.BaseStream.Position;
156 pbr.BaseStream.Position = 0;
[326]157 Progression p = Progression.Read (pbr, null);
[333]158 pbr.BaseStream.Position = posBefore;
[326]159 expToNextLevel = (uint) p.ExpToNextLevel;
160 level = p.Level;
161 }
[325]162 }
[369]163
[325]164 }
[324]165}
Note: See TracBrowser for help on using the repository browser.