using System; using System.Collections.Generic; using ManagedSteam.SteamTypes; public class CommonMappingFunctions { public static ConnectionManager GetConnectionManager () { return ConnectionManager.Instance; } public static GameManager GetGameManager () { return GetConnectionManager ().gameManager; } public static string GetPlayerName (ClientInfo _ci) { try { int entityId = GetConnectionManager ().mapClientToEntity [_ci.clientId]; return GetGameManager ().World.playerEntities.dict [entityId].EntityName; } catch (Exception e) { Log.Out ("Error getting player name for ClientInfo: " + e); } return null; } public static EntityPlayer GetEntityPlayer (ClientInfo _ci) { try { int entityId = GetConnectionManager ().mapClientToEntity [_ci.clientId]; return GetGameManager ().World.playerEntities.dict [entityId]; } catch (Exception e) { Log.Out ("Error getting entity player for ClientInfo: " + e); } return null; } public static string GetSteamID (ClientInfo _ci) { return SingletonMonoBehaviour.Instance.GetPlayerId (GetPlayerName (_ci)); } public static int GetClientID (ClientInfo _ci) { if (_ci != null) return _ci.clientId; else return -1; } public static int GetEntityID (ClientInfo _ci) { try { ConnectionManager cm = GetConnectionManager (); if (cm.mapClientToEntity.ContainsKey (_ci.clientId)) return cm.mapClientToEntity [_ci.clientId]; else return -1; } catch (Exception e) { Log.Out ("Error getting entity ID for ClientInfo: " + e); } return -1; } public static ClientInfo GetClientInfoFromEntityID (int _entityId) { try { ConnectionManager cm = GetConnectionManager (); if (cm.mapClientToEntity.ContainsValue (_entityId)) { foreach (KeyValuePair kvp in cm.mapClientToEntity) { if (kvp.Value == _entityId) { return cm.connectedClients [kvp.Key]; } } } return null; } catch (Exception e) { Log.Out ("Error getting ClientInfo for entity ID: " + e); } return null; } public static ClientInfo GetClientInfoFromClientID (int _clientId) { try { ConnectionManager cm = GetConnectionManager (); if (cm.connectedClients.ContainsKey (_clientId)) return cm.connectedClients [_clientId]; else return null; } catch (Exception e) { Log.Out ("Error getting ClientInfo for client ID: " + e); } return null; } public static ClientInfo GetClientInfoFromPlayerName (string _playerName) { try { ConnectionManager cm = GetConnectionManager (); _playerName = _playerName.ToLower (); foreach (ClientInfo ci in cm.connectedClients.Values) { if (GetPlayerName (ci).ToLower ().Equals (_playerName)) { return ci; } } } catch (Exception e) { Log.Out ("Error getting ClientInfo for player name: " + e); } return null; } public static ClientInfo GetClientInfoFromNameOrID (string _nameOrId) { try { int entityId = -1; if (int.TryParse (_nameOrId, out entityId)) { ClientInfo ci = GetClientInfoFromEntityID (entityId); if (ci != null) return ci; } return GetClientInfoFromPlayerName (_nameOrId); } catch (Exception e) { Log.Out ("Error getting ClientInfo for entity ID or player name: " + e); } return null; } public static ClientInfo GetClientInfoFromSteamID (string _steamId) { try { Dictionary uToID = Authenticator.Instance.usersToIDs; foreach (KeyValuePair kvp in uToID) { string curId = string.Empty + ((SteamID)kvp.Value).AsUInt64; if (curId.Equals (_steamId)) { return GetClientInfoFromPlayerName (kvp.Key); } } } catch (Exception e) { Log.Out ("Error getting ClientInfo for steam ID: " + e); } return null; } }