source: binary-improvements/7dtd-server-fixes/src/PersistentData/Players.cs@ 483

Last change on this file since 483 was 455, checked in by alloc, 16 months ago

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text.RegularExpressions;
4
5namespace 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}
Note: See TracBrowser for help on using the repository browser.