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

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

fixes

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