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

Last change on this file since 469 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
RevLine 
[253]1using System;
2using System.Collections.Generic;
3using System.Text.RegularExpressions;
4
[325]5namespace AllocsFixes.PersistentData {
[253]6 [Serializable]
7 public class Players {
[369]8 public readonly Dictionary<PlatformUserIdentifierAbs, Player> Dict = new Dictionary<PlatformUserIdentifierAbs, Player> ();
[253]9
[446]10 public int Count => Dict.Count;
[325]11
[446]12 public Player GetOrCreate (PlatformUserIdentifierAbs _internalId, PlatformUserIdentifierAbs _platformId, PlatformUserIdentifierAbs _crossPlatformId) {
13 if (_internalId == null) {
14 return null;
15 }
[325]16
[446]17 if (Dict.TryGetValue (_internalId, out Player pOld)) {
18 return pOld;
19 }
[325]20
[455]21 Log.Out ($"Created new player entry for ID: {_internalId}");
[446]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;
[253]30 }
[446]31
32 return Dict.TryGetValue (_internalId, out Player pOld) ? pOld : null;
[253]33 }
34
[446]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 }
[253]41
[446]42 return null;
43 }
44
45 public Player GetByString (string _nameOrId, bool _ignoreColorCodes) {
[369]46 if (string.IsNullOrEmpty (_nameOrId)) {
[253]47 return null;
48 }
49
[369]50 if (PlatformUserIdentifierAbs.TryFromCombinedString (_nameOrId, out PlatformUserIdentifierAbs userId)) {
[446]51 return GetByUserId (userId);
[325]52 }
[253]53
[369]54 if (int.TryParse (_nameOrId, out int entityId)) {
[446]55 foreach ((_, Player p) in Dict) {
56 if (p.IsOnline && p.EntityID == entityId) {
57 return p;
[253]58 }
59 }
60 }
[325]61
[446]62 foreach ((_, Player p) in Dict) {
63 string name = p.Name;
[325]64 if (_ignoreColorCodes) {
65 name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
66 }
67
[446]68 if (p.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
69 return p;
[325]70 }
71 }
72
[253]73 return null;
74 }
75 }
[325]76}
Note: See TracBrowser for help on using the repository browser.