source: binary-improvements2/7dtd-server-fixes/src/PersistentData/Players.cs@ 402

Last change on this file since 402 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 1.6 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 Player this [PlatformUserIdentifierAbs _platformId, bool _create] {
11 get {
12 if (_platformId == null) {
13 return null;
14 }
15
16 if (Dict.TryGetValue (_platformId, out Player pOld)) {
17 return pOld;
18 }
19
20 if (!_create) {
21 return null;
22 }
23
24 Log.Out ($"Created new player entry for ID: {_platformId}");
25 Player p = new Player (_platformId);
26 Dict.Add (_platformId, p);
27 return p;
28 }
29 }
30
31 public int Count => Dict.Count;
32
33 public PlatformUserIdentifierAbs GetSteamID (string _nameOrId, bool _ignoreColorCodes) {
34 if (string.IsNullOrEmpty (_nameOrId)) {
35 return null;
36 }
37
38 if (PlatformUserIdentifierAbs.TryFromCombinedString (_nameOrId, out PlatformUserIdentifierAbs userId)) {
39 return userId;
40 }
41
42 if (int.TryParse (_nameOrId, out int entityId)) {
43 foreach ((PlatformUserIdentifierAbs iUserId, Player player) in Dict) {
44 if (player.IsOnline && player.EntityID == entityId) {
45 return iUserId;
46 }
47 }
48 }
49
50 foreach ((PlatformUserIdentifierAbs iUserId, Player player) in Dict) {
51 string name = player.Name;
52 if (_ignoreColorCodes) {
53 name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
54 }
55
56 if (player.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
57 return iUserId;
58 }
59 }
60
61 return null;
62 }
63 }
64}
Note: See TracBrowser for help on using the repository browser.