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

Last change on this file since 387 was 383, checked in by alloc, 2 years ago

Fixed a bunch of warnings

File size: 1.6 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
[369]10 public Player this [PlatformUserIdentifierAbs _platformId, bool _create] {
[253]11 get {
[369]12 if (_platformId == null) {
[276]13 return null;
[325]14 }
15
[369]16 if (Dict.TryGetValue (_platformId, out Player pOld)) {
17 return pOld;
[253]18 }
[325]19
[369]20 if (!_create) {
[326]21 return null;
[325]22 }
23
[369]24 Log.Out ("Created new player entry for ID: " + _platformId);
25 Player p = new Player (_platformId);
26 Dict.Add (_platformId, p);
[326]27 return p;
[253]28 }
29 }
30
[369]31 public int Count => Dict.Count;
[253]32
[369]33 public PlatformUserIdentifierAbs GetSteamID (string _nameOrId, bool _ignoreColorCodes) {
34 if (string.IsNullOrEmpty (_nameOrId)) {
[253]35 return null;
36 }
37
[369]38 if (PlatformUserIdentifierAbs.TryFromCombinedString (_nameOrId, out PlatformUserIdentifierAbs userId)) {
39 return userId;
[325]40 }
[253]41
[369]42 if (int.TryParse (_nameOrId, out int entityId)) {
43 foreach (KeyValuePair<PlatformUserIdentifierAbs, Player> kvp in Dict) {
[325]44 if (kvp.Value.IsOnline && kvp.Value.EntityID == entityId) {
[253]45 return kvp.Key;
46 }
47 }
48 }
[325]49
[369]50 foreach (KeyValuePair<PlatformUserIdentifierAbs, Player> kvp in Dict) {
[326]51 string name = kvp.Value.Name;
[325]52 if (_ignoreColorCodes) {
53 name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
54 }
55
[326]56 if (kvp.Value.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
[325]57 return kvp.Key;
58 }
59 }
60
[253]61 return null;
62 }
63 }
[325]64}
Note: See TracBrowser for help on using the repository browser.