[107] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using ManagedSteam.SteamTypes;
|
---|
| 4 |
|
---|
| 5 | public 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 | return _ci.clientId;
|
---|
| 48 | else
|
---|
| 49 | return -1;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public static int GetEntityID (ClientInfo _ci)
|
---|
| 53 | {
|
---|
| 54 | try {
|
---|
| 55 | ConnectionManager cm = GetConnectionManager ();
|
---|
| 56 |
|
---|
| 57 | if (cm.mapClientToEntity.ContainsKey (_ci.clientId))
|
---|
| 58 | return cm.mapClientToEntity [_ci.clientId];
|
---|
| 59 | else
|
---|
| 60 | return -1;
|
---|
| 61 | } catch (Exception e) {
|
---|
| 62 | Log.Out ("Error getting entity ID for ClientInfo: " + e);
|
---|
| 63 | }
|
---|
| 64 | return -1;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public static ClientInfo GetClientInfoFromEntityID (int _entityId)
|
---|
| 68 | {
|
---|
| 69 | try {
|
---|
| 70 | ConnectionManager cm = GetConnectionManager ();
|
---|
| 71 |
|
---|
| 72 | if (cm.mapClientToEntity.ContainsValue (_entityId)) {
|
---|
| 73 | foreach (KeyValuePair<int, int> kvp in cm.mapClientToEntity) {
|
---|
| 74 | if (kvp.Value == _entityId) {
|
---|
| 75 | return cm.connectedClients [kvp.Key];
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | return null;
|
---|
| 81 | } catch (Exception e) {
|
---|
| 82 | Log.Out ("Error getting ClientInfo for entity ID: " + e);
|
---|
| 83 | }
|
---|
| 84 | return null;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public static ClientInfo GetClientInfoFromClientID (int _clientId)
|
---|
| 88 | {
|
---|
| 89 | try {
|
---|
| 90 | ConnectionManager cm = GetConnectionManager ();
|
---|
| 91 |
|
---|
| 92 | if (cm.connectedClients.ContainsKey (_clientId))
|
---|
| 93 | return cm.connectedClients [_clientId];
|
---|
| 94 | else
|
---|
| 95 | return null;
|
---|
| 96 | } catch (Exception e) {
|
---|
| 97 | Log.Out ("Error getting ClientInfo for client ID: " + e);
|
---|
| 98 | }
|
---|
| 99 | return null;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public static ClientInfo GetClientInfoFromPlayerName (string _playerName)
|
---|
| 103 | {
|
---|
| 104 | try {
|
---|
| 105 | ConnectionManager cm = GetConnectionManager ();
|
---|
| 106 |
|
---|
| 107 | _playerName = _playerName.ToLower ();
|
---|
| 108 | foreach (ClientInfo ci in cm.connectedClients.Values) {
|
---|
| 109 | if (GetPlayerName (ci).ToLower ().Equals (_playerName)) {
|
---|
| 110 | return ci;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | } catch (Exception e) {
|
---|
| 114 | Log.Out ("Error getting ClientInfo for player name: " + e);
|
---|
| 115 | }
|
---|
| 116 | return null;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public static ClientInfo GetClientInfoFromNameOrID (string _nameOrId)
|
---|
| 120 | {
|
---|
| 121 | try {
|
---|
| 122 | int entityId = -1;
|
---|
| 123 | if (int.TryParse (_nameOrId, out entityId)) {
|
---|
| 124 | ClientInfo ci = GetClientInfoFromEntityID (entityId);
|
---|
| 125 | if (ci != null)
|
---|
| 126 | return ci;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | return GetClientInfoFromPlayerName (_nameOrId);
|
---|
| 130 | } catch (Exception e) {
|
---|
| 131 | Log.Out ("Error getting ClientInfo for entity ID or player name: " + e);
|
---|
| 132 | }
|
---|
| 133 | return null;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | public static ClientInfo GetClientInfoFromSteamID (string _steamId)
|
---|
| 137 | {
|
---|
| 138 | try {
|
---|
| 139 | Dictionary<string, object> uToID = Authenticator.Instance.usersToIDs;
|
---|
| 140 | foreach (KeyValuePair<string, object> kvp in uToID) {
|
---|
| 141 | string curId = string.Empty + ((SteamID)kvp.Value).AsUInt64;
|
---|
| 142 | if (curId.Equals (_steamId)) {
|
---|
| 143 | return GetClientInfoFromPlayerName (kvp.Key);
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | } catch (Exception e) {
|
---|
| 147 | Log.Out ("Error getting ClientInfo for steam ID: " + e);
|
---|
| 148 | }
|
---|
| 149 | return null;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | }
|
---|
| 153 |
|
---|