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

Last change on this file since 208 was 198, checked in by alloc, 10 years ago

fixes

File size: 2.6 KB
Line 
1using System;
2using System.Runtime.Serialization;
3using UnityEngine;
4
5namespace 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 [NonSerialized]
23 private ClientInfo
24 clientInfo;
25
26 public string SteamID {
27 get { return steamId; }
28 }
29
30 public int EntityID {
31 get { return entityId; }
32 }
33
34 public string Name {
35 get { return name == null ? string.Empty : name; }
36 }
37
38 public string IP {
39 get { return ip == null ? string.Empty : ip; }
40 }
41
42 public Inventory Inventory {
43 get {
44 if (inventory == null)
45 inventory = new Inventory ();
46 return inventory;
47 }
48 }
49
50 public bool IsOnline {
51 get { return clientInfo != null; }
52 }
53
54 public ClientInfo ClientInfo {
55 get { return clientInfo; }
56 }
57
58 public EntityPlayer Entity {
59 get {
60 if (IsOnline) {
61 return CommonMappingFunctions.GetEntityPlayer (clientInfo);
62 } else {
63 return null;
64 }
65 }
66 }
67
68 public long TotalPlayTime {
69 get {
70 if (IsOnline) {
71 return totalPlayTime + (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad);
72 } else {
73 return totalPlayTime;
74 }
75 }
76 }
77
78 public DateTime LastOnline {
79 get {
80 if (IsOnline)
81 return DateTime.Now;
82 else
83 return lastOnline;
84 }
85 }
86
87 public Vector3i LastPosition {
88 get {
89 if (IsOnline)
90 return new Vector3i (Entity.GetPosition ());
91 else
92 return new Vector3i (lastPositionX, lastPositionY, lastPositionZ);
93 }
94 }
95
96 public void SetOffline ()
97 {
98 if (clientInfo != null) {
99 Log.Out ("Player set to offline: " + steamId);
100 lastOnline = DateTime.Now;
101 try {
102 Vector3i lastPos = new Vector3i (Entity.GetPosition ());
103 lastPositionX = lastPos.x;
104 lastPositionY = lastPos.y;
105 lastPositionZ = lastPos.z;
106 totalPlayTime += (long)(Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad);
107 } catch (NullReferenceException) {
108 Log.Out ("Entity not available. Something seems to be wrong here...");
109 }
110 clientInfo = null;
111 }
112 }
113
114 public void SetOnline (ClientInfo ci)
115 {
116 Log.Out ("Player set to online: " + steamId);
117 clientInfo = ci;
118 entityId = CommonMappingFunctions.GetEntityID (ci);
119 name = CommonMappingFunctions.GetPlayerName (ci);
120 ip = ci.networkPlayer.ipAddress;
121 }
122
123 public Player (string steamId)
124 {
125 this.steamId = steamId;
126 this.inventory = new Inventory ();
127 }
128
129
130 }
131}
Note: See TracBrowser for help on using the repository browser.