source: binary-improvements/7dtd-server-fixes/src/CommonMappingFunctions.cs@ 110

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

fixes

File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using ManagedSteam.SteamTypes;
4
5public class CommonMappingFunctions
6{
7 public static ConnectionManager GetConnectionManager ()
8 {
9 return ConnectionManager.Instance;
10 }
11
12 public static GameManager GetGameManager ()
13 {
14 return GetConnectionManager ().gameManager;
15 }
16
17 public static string GetPlayerName (ClientInfo _ci)
18 {
19 try {
20 int entityId = GetConnectionManager ().mapClientToEntity [_ci.clientId];
21 return GetGameManager ().World.playerEntities.dict [entityId].EntityName;
22 } catch (Exception e) {
23 Log.Out ("Error getting player name for ClientInfo: " + e);
24 }
25 return null;
26 }
27
28 public static EntityPlayer GetEntityPlayer (ClientInfo _ci)
29 {
30 try {
31 int entityId = GetConnectionManager ().mapClientToEntity [_ci.clientId];
32 return GetGameManager ().World.playerEntities.dict [entityId];
33 } catch (Exception e) {
34 Log.Out ("Error getting entity player for ClientInfo: " + e);
35 }
36 return null;
37 }
38
39 public static string GetSteamID (ClientInfo _ci)
40 {
41 return SingletonMonoBehaviour<Authenticator>.Instance.GetPlayerId (GetPlayerName (_ci));
42 }
43
44 public static int GetClientID (ClientInfo _ci)
45 {
46 if (_ci != null) {
47 if (GetConnectionManager ().connectedClients.ContainsKey (_ci.clientId))
48 return _ci.clientId;
49 }
50 return -1;
51 }
52
53 public static int GetEntityID (ClientInfo _ci)
54 {
55 try {
56 ConnectionManager cm = GetConnectionManager ();
57
58 if (cm.mapClientToEntity.ContainsKey (_ci.clientId))
59 return cm.mapClientToEntity [_ci.clientId];
60 else
61 return -1;
62 } catch (Exception e) {
63 Log.Out ("Error getting entity ID for ClientInfo: " + e);
64 }
65 return -1;
66 }
67
68 public static ClientInfo GetClientInfoFromEntityID (int _entityId)
69 {
70 try {
71 ConnectionManager cm = GetConnectionManager ();
72
73 if (cm.mapClientToEntity.ContainsValue (_entityId)) {
74 foreach (KeyValuePair<int, int> kvp in cm.mapClientToEntity) {
75 if (kvp.Value == _entityId) {
76 return cm.connectedClients [kvp.Key];
77 }
78 }
79 }
80
81 return null;
82 } catch (Exception e) {
83 Log.Out ("Error getting ClientInfo for entity ID: " + e);
84 }
85 return null;
86 }
87
88 public static ClientInfo GetClientInfoFromClientID (int _clientId)
89 {
90 try {
91 ConnectionManager cm = GetConnectionManager ();
92
93 if (cm.connectedClients.ContainsKey (_clientId))
94 return cm.connectedClients [_clientId];
95 else
96 return null;
97 } catch (Exception e) {
98 Log.Out ("Error getting ClientInfo for client ID: " + e);
99 }
100 return null;
101 }
102
103 public static ClientInfo GetClientInfoFromPlayerName (string _playerName)
104 {
105 try {
106 ConnectionManager cm = GetConnectionManager ();
107
108 _playerName = _playerName.ToLower ();
109 foreach (ClientInfo ci in cm.connectedClients.Values) {
110 if (GetPlayerName (ci).ToLower ().Equals (_playerName)) {
111 return ci;
112 }
113 }
114 } catch (Exception e) {
115 Log.Out ("Error getting ClientInfo for player name: " + e);
116 }
117 return null;
118 }
119
120 public static ClientInfo GetClientInfoFromNameOrID (string _nameOrId)
121 {
122 try {
123 int entityId = -1;
124 if (int.TryParse (_nameOrId, out entityId)) {
125 ClientInfo ci = GetClientInfoFromEntityID (entityId);
126 if (ci != null)
127 return ci;
128 }
129
130 return GetClientInfoFromPlayerName (_nameOrId);
131 } catch (Exception e) {
132 Log.Out ("Error getting ClientInfo for entity ID or player name: " + e);
133 }
134 return null;
135 }
136
137 public static ClientInfo GetClientInfoFromSteamID (string _steamId)
138 {
139 try {
140 Dictionary<string, object> uToID = Authenticator.Instance.usersToIDs;
141 foreach (KeyValuePair<string, object> kvp in uToID) {
142 string curId = string.Empty + ((SteamID)kvp.Value).AsUInt64;
143 if (curId.Equals (_steamId)) {
144 return GetClientInfoFromPlayerName (kvp.Key);
145 }
146 }
147 } catch (Exception e) {
148 Log.Out ("Error getting ClientInfo for steam ID: " + e);
149 }
150 return null;
151 }
152
153}
154
Note: See TracBrowser for help on using the repository browser.