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;
|
---|
15 | [OptionalField]
|
---|
16 | private DateTime
|
---|
17 | lastOnline;
|
---|
18 | private Inventory inventory;
|
---|
19 | [OptionalField]
|
---|
20 | private int
|
---|
21 | lastPositionX, lastPositionY, lastPositionZ;
|
---|
22 | [OptionalField]
|
---|
23 | private uint experience;
|
---|
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 {
|
---|
37 | get { return name == null ? string.Empty : name; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public string IP {
|
---|
41 | get { return ip == null ? string.Empty : ip; }
|
---|
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) {
|
---|
63 | return GameManager.Instance.World.Players.dict [clientInfo.entityId];
|
---|
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 |
|
---|
80 | public DateTime LastOnline {
|
---|
81 | get {
|
---|
82 | if (IsOnline)
|
---|
83 | return DateTime.Now;
|
---|
84 | else
|
---|
85 | return lastOnline;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
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 |
|
---|
98 | public bool LandProtectionActive {
|
---|
99 | get {
|
---|
100 | return GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance.GetPersistentPlayerList ().GetPlayerData (SteamID));
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public float LandProtectionMultiplier {
|
---|
105 | get {
|
---|
106 | return GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance.GetPersistentPlayerList ().GetPlayerData (SteamID));
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public uint Experience {
|
---|
111 | get {
|
---|
112 | return experience;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public float Level {
|
---|
117 | get {
|
---|
118 | float perc = (float)experience / 600000;
|
---|
119 | perc = Mathf.Sqrt (perc);
|
---|
120 | return Mathf.Clamp ((perc * 60) + 1, 1, 60);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void SetOffline ()
|
---|
125 | {
|
---|
126 | if (clientInfo != null) {
|
---|
127 | Log.Out ("Player set to offline: " + steamId);
|
---|
128 | lastOnline = DateTime.Now;
|
---|
129 | try {
|
---|
130 | Vector3i lastPos = new Vector3i (Entity.GetPosition ());
|
---|
131 | lastPositionX = lastPos.x;
|
---|
132 | lastPositionY = lastPos.y;
|
---|
133 | lastPositionZ = lastPos.z;
|
---|
134 | totalPlayTime += (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad);
|
---|
135 | } catch (NullReferenceException) {
|
---|
136 | Log.Out ("Entity not available. Something seems to be wrong here...");
|
---|
137 | }
|
---|
138 | clientInfo = null;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void SetOnline (ClientInfo ci)
|
---|
143 | {
|
---|
144 | Log.Out ("Player set to online: " + steamId);
|
---|
145 | clientInfo = ci;
|
---|
146 | entityId = ci.entityId;
|
---|
147 | name = ci.playerName;
|
---|
148 | ip = ci.ip;
|
---|
149 | lastOnline = DateTime.Now;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public void Update (PlayerDataFile _pdf) {
|
---|
153 | experience = _pdf.experience;
|
---|
154 | inventory.Update (_pdf);
|
---|
155 | }
|
---|
156 |
|
---|
157 | public Player (string steamId)
|
---|
158 | {
|
---|
159 | this.steamId = steamId;
|
---|
160 | this.inventory = new Inventory ();
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | }
|
---|
165 | }
|
---|