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

Last change on this file since 244 was 238, checked in by alloc, 9 years ago

Server fixes for A12

File size: 3.0 KB
RevLine 
[144]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;
[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;
[238]137 lastOnline = DateTime.Now;
[144]138 }
139
[233]140 public void Update (PlayerDataFile _pdf) {
141 experience = _pdf.experience;
142 inventory.Update (_pdf);
143 }
144
[144]145 public Player (string steamId)
146 {
147 this.steamId = steamId;
148 this.inventory = new Inventory ();
149 }
150
151
152 }
153}
Note: See TracBrowser for help on using the repository browser.