[144] | 1 | using System;
|
---|
| 2 | using System.Runtime.Serialization;
|
---|
| 3 | using UnityEngine;
|
---|
| 4 |
|
---|
| 5 | namespace AllocsFixes.PersistentData
|
---|
| 6 | {
|
---|
| 7 | [Serializable]
|
---|
| 8 | public class Player
|
---|
| 9 | {
|
---|
| 10 | private readonly string steamId;
|
---|
| 11 | private int entityId;
|
---|
| 12 | private string name;
|
---|
| 13 | private string ip;
|
---|
| 14 | private long totalPlayTime;
|
---|
[146] | 15 | [OptionalField]
|
---|
| 16 | private DateTime
|
---|
| 17 | lastOnline;
|
---|
[144] | 18 | private Inventory inventory;
|
---|
[154] | 19 | [OptionalField]
|
---|
| 20 | private int
|
---|
| 21 | lastPositionX, lastPositionY, lastPositionZ;
|
---|
[233] | 22 | [OptionalField]
|
---|
| 23 | private uint experience;
|
---|
[144] | 24 | [NonSerialized]
|
---|
| 25 | private ClientInfo
|
---|
| 26 | clientInfo;
|
---|
| 27 |
|
---|
| 28 | public string SteamID {
|
---|
| 29 | get { return steamId; }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | public int EntityID {
|
---|
| 33 | get { return entityId; }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public string Name {
|
---|
[161] | 37 | get { return name == null ? string.Empty : name; }
|
---|
[144] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public string IP {
|
---|
[161] | 41 | get { return ip == null ? string.Empty : ip; }
|
---|
[144] | 42 | }
|
---|
| 43 |
|
---|
| 44 | public Inventory Inventory {
|
---|
| 45 | get {
|
---|
| 46 | if (inventory == null)
|
---|
| 47 | inventory = new Inventory ();
|
---|
| 48 | return inventory;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public bool IsOnline {
|
---|
| 53 | get { return clientInfo != null; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public ClientInfo ClientInfo {
|
---|
| 57 | get { return clientInfo; }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public EntityPlayer Entity {
|
---|
| 61 | get {
|
---|
| 62 | if (IsOnline) {
|
---|
[230] | 63 | return GameManager.Instance.World.Players.dict [clientInfo.entityId];
|
---|
[144] | 64 | } else {
|
---|
| 65 | return null;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public long TotalPlayTime {
|
---|
| 71 | get {
|
---|
| 72 | if (IsOnline) {
|
---|
| 73 | return totalPlayTime + (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad);
|
---|
| 74 | } else {
|
---|
| 75 | return totalPlayTime;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[146] | 80 | public DateTime LastOnline {
|
---|
| 81 | get {
|
---|
| 82 | if (IsOnline)
|
---|
| 83 | return DateTime.Now;
|
---|
| 84 | else
|
---|
| 85 | return lastOnline;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[154] | 89 | public Vector3i LastPosition {
|
---|
| 90 | get {
|
---|
| 91 | if (IsOnline)
|
---|
| 92 | return new Vector3i (Entity.GetPosition ());
|
---|
| 93 | else
|
---|
| 94 | return new Vector3i (lastPositionX, lastPositionY, lastPositionZ);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[233] | 98 | public uint Experience {
|
---|
| 99 | get {
|
---|
| 100 | return experience;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | public float Level {
|
---|
| 105 | get {
|
---|
| 106 | float perc = (float)experience / 600000;
|
---|
| 107 | perc = Mathf.Sqrt (perc);
|
---|
| 108 | return Mathf.Clamp ((perc * 60) + 1, 1, 60);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[144] | 112 | public void SetOffline ()
|
---|
| 113 | {
|
---|
[161] | 114 | if (clientInfo != null) {
|
---|
| 115 | Log.Out ("Player set to offline: " + steamId);
|
---|
| 116 | lastOnline = DateTime.Now;
|
---|
[198] | 117 | try {
|
---|
| 118 | Vector3i lastPos = new Vector3i (Entity.GetPosition ());
|
---|
| 119 | lastPositionX = lastPos.x;
|
---|
| 120 | lastPositionY = lastPos.y;
|
---|
| 121 | lastPositionZ = lastPos.z;
|
---|
| 122 | totalPlayTime += (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad);
|
---|
| 123 | } catch (NullReferenceException) {
|
---|
| 124 | Log.Out ("Entity not available. Something seems to be wrong here...");
|
---|
| 125 | }
|
---|
[161] | 126 | clientInfo = null;
|
---|
| 127 | }
|
---|
[144] | 128 | }
|
---|
| 129 |
|
---|
| 130 | public void SetOnline (ClientInfo ci)
|
---|
| 131 | {
|
---|
| 132 | Log.Out ("Player set to online: " + steamId);
|
---|
| 133 | clientInfo = ci;
|
---|
[230] | 134 | entityId = ci.entityId;
|
---|
| 135 | name = ci.playerName;
|
---|
[228] | 136 | ip = ci.ip;
|
---|
[144] | 137 | }
|
---|
| 138 |
|
---|
[233] | 139 | public void Update (PlayerDataFile _pdf) {
|
---|
| 140 | experience = _pdf.experience;
|
---|
| 141 | inventory.Update (_pdf);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[144] | 144 | public Player (string steamId)
|
---|
| 145 | {
|
---|
| 146 | this.steamId = steamId;
|
---|
| 147 | this.inventory = new Inventory ();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | }
|
---|
| 152 | }
|
---|