1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text.RegularExpressions;
|
---|
4 |
|
---|
5 | namespace AllocsFixes.PersistentData {
|
---|
6 | [Serializable]
|
---|
7 | public class Players {
|
---|
8 | public readonly Dictionary<PlatformUserIdentifierAbs, Player> Dict = new Dictionary<PlatformUserIdentifierAbs, Player> ();
|
---|
9 |
|
---|
10 | public int Count => Dict.Count;
|
---|
11 |
|
---|
12 | public Player GetOrCreate (PlatformUserIdentifierAbs _internalId, PlatformUserIdentifierAbs _platformId, PlatformUserIdentifierAbs _crossPlatformId) {
|
---|
13 | if (_internalId == null) {
|
---|
14 | return null;
|
---|
15 | }
|
---|
16 |
|
---|
17 | if (Dict.TryGetValue (_internalId, out Player pOld)) {
|
---|
18 | return pOld;
|
---|
19 | }
|
---|
20 |
|
---|
21 | Log.Out ("Created new player entry for ID: " + _internalId);
|
---|
22 | Player p = new Player (_internalId, _platformId, _crossPlatformId);
|
---|
23 | Dict.Add (_internalId, p);
|
---|
24 | return p;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public Player GetByInternalId (PlatformUserIdentifierAbs _internalId) {
|
---|
28 | if (_internalId == null) {
|
---|
29 | return null;
|
---|
30 | }
|
---|
31 |
|
---|
32 | return Dict.TryGetValue (_internalId, out Player pOld) ? pOld : null;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public Player GetByUserId (PlatformUserIdentifierAbs _userId) {
|
---|
36 | foreach ((_, Player p) in Dict) {
|
---|
37 | if (p.PlatformId.Equals (_userId) || p.CrossPlatformId.Equals (_userId)) {
|
---|
38 | return p;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | return null;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public Player GetByString (string _nameOrId, bool _ignoreColorCodes) {
|
---|
46 | if (string.IsNullOrEmpty (_nameOrId)) {
|
---|
47 | return null;
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (PlatformUserIdentifierAbs.TryFromCombinedString (_nameOrId, out PlatformUserIdentifierAbs userId)) {
|
---|
51 | return GetByUserId (userId);
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (int.TryParse (_nameOrId, out int entityId)) {
|
---|
55 | foreach ((_, Player p) in Dict) {
|
---|
56 | if (p.IsOnline && p.EntityID == entityId) {
|
---|
57 | return p;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | foreach ((_, Player p) in Dict) {
|
---|
63 | string name = p.Name;
|
---|
64 | if (_ignoreColorCodes) {
|
---|
65 | name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (p.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
|
---|
69 | return p;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | return null;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|