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