[253] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Text.RegularExpressions;
|
---|
| 4 |
|
---|
[325] | 5 | namespace AllocsFixes.PersistentData {
|
---|
[253] | 6 | [Serializable]
|
---|
| 7 | public class Players {
|
---|
[332] | 8 | public readonly Dictionary<string, Player> Dict = new Dictionary<string, Player> (StringComparer.OrdinalIgnoreCase);
|
---|
[253] | 9 |
|
---|
[351] | 10 | public Player this [string _steamId, bool _create] {
|
---|
[253] | 11 | get {
|
---|
[351] | 12 | if (string.IsNullOrEmpty (_steamId)) {
|
---|
[276] | 13 | return null;
|
---|
[325] | 14 | }
|
---|
| 15 |
|
---|
[351] | 16 | if (Dict.ContainsKey (_steamId)) {
|
---|
| 17 | return Dict [_steamId];
|
---|
[253] | 18 | }
|
---|
[325] | 19 |
|
---|
[351] | 20 | if (!_create || _steamId.Length != 17) {
|
---|
[326] | 21 | return null;
|
---|
[325] | 22 | }
|
---|
| 23 |
|
---|
[351] | 24 | Log.Out ("Created new player entry for ID: " + _steamId);
|
---|
| 25 | Player p = new Player (_steamId);
|
---|
| 26 | Dict.Add (_steamId, p);
|
---|
[326] | 27 | return p;
|
---|
[253] | 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public int Count {
|
---|
[332] | 32 | get { return Dict.Count; }
|
---|
[253] | 33 | }
|
---|
| 34 |
|
---|
| 35 | // public Player GetPlayerByNameOrId (string _nameOrId, bool _ignoreColorCodes)
|
---|
| 36 | // {
|
---|
| 37 | // string sid = GetSteamID (_nameOrId, _ignoreColorCodes);
|
---|
| 38 | // if (sid != null)
|
---|
| 39 | // return this [sid];
|
---|
| 40 | // else
|
---|
| 41 | // return null;
|
---|
| 42 | // }
|
---|
| 43 |
|
---|
| 44 | public string GetSteamID (string _nameOrId, bool _ignoreColorCodes) {
|
---|
| 45 | if (_nameOrId == null || _nameOrId.Length == 0) {
|
---|
| 46 | return null;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | long tempLong;
|
---|
| 50 | if (_nameOrId.Length == 17 && long.TryParse (_nameOrId, out tempLong)) {
|
---|
| 51 | return _nameOrId;
|
---|
[325] | 52 | }
|
---|
[253] | 53 |
|
---|
[326] | 54 | int entityId;
|
---|
[325] | 55 | if (int.TryParse (_nameOrId, out entityId)) {
|
---|
[332] | 56 | foreach (KeyValuePair<string, Player> kvp in Dict) {
|
---|
[325] | 57 | if (kvp.Value.IsOnline && kvp.Value.EntityID == entityId) {
|
---|
[253] | 58 | return kvp.Key;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
[325] | 62 |
|
---|
[332] | 63 | foreach (KeyValuePair<string, Player> kvp in Dict) {
|
---|
[326] | 64 | string name = kvp.Value.Name;
|
---|
[325] | 65 | if (_ignoreColorCodes) {
|
---|
| 66 | name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[326] | 69 | if (kvp.Value.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
|
---|
[325] | 70 | return kvp.Key;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[253] | 74 | return null;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
[325] | 77 | }
|
---|