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