[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 {
|
---|
[369] | 8 | public readonly Dictionary<PlatformUserIdentifierAbs, Player> Dict = new Dictionary<PlatformUserIdentifierAbs, Player> ();
|
---|
[253] | 9 |
|
---|
[369] | 10 | public Player this [PlatformUserIdentifierAbs _platformId, bool _create] {
|
---|
[253] | 11 | get {
|
---|
[369] | 12 | if (_platformId == null) {
|
---|
[276] | 13 | return null;
|
---|
[325] | 14 | }
|
---|
| 15 |
|
---|
[369] | 16 | if (Dict.TryGetValue (_platformId, out Player pOld)) {
|
---|
| 17 | return pOld;
|
---|
[253] | 18 | }
|
---|
[325] | 19 |
|
---|
[369] | 20 | if (!_create) {
|
---|
[326] | 21 | return null;
|
---|
[325] | 22 | }
|
---|
| 23 |
|
---|
[369] | 24 | Log.Out ("Created new player entry for ID: " + _platformId);
|
---|
| 25 | Player p = new Player (_platformId);
|
---|
| 26 | Dict.Add (_platformId, p);
|
---|
[326] | 27 | return p;
|
---|
[253] | 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[369] | 31 | public int Count => Dict.Count;
|
---|
[253] | 32 |
|
---|
[369] | 33 | public PlatformUserIdentifierAbs GetSteamID (string _nameOrId, bool _ignoreColorCodes) {
|
---|
| 34 | if (string.IsNullOrEmpty (_nameOrId)) {
|
---|
[253] | 35 | return null;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[369] | 38 | if (PlatformUserIdentifierAbs.TryFromCombinedString (_nameOrId, out PlatformUserIdentifierAbs userId)) {
|
---|
| 39 | return userId;
|
---|
[325] | 40 | }
|
---|
[253] | 41 |
|
---|
[369] | 42 | if (int.TryParse (_nameOrId, out int entityId)) {
|
---|
[391] | 43 | foreach ((PlatformUserIdentifierAbs iUserId, Player player) in Dict) {
|
---|
| 44 | if (player.IsOnline && player.EntityID == entityId) {
|
---|
| 45 | return iUserId;
|
---|
[253] | 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
[325] | 49 |
|
---|
[391] | 50 | foreach ((PlatformUserIdentifierAbs iUserId, Player player) in Dict) {
|
---|
| 51 | string name = player.Name;
|
---|
[325] | 52 | if (_ignoreColorCodes) {
|
---|
| 53 | name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[391] | 56 | if (player.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
|
---|
| 57 | return iUserId;
|
---|
[325] | 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[253] | 61 | return null;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
[325] | 64 | }
|
---|