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

Last change on this file since 332 was 332, checked in by alloc, 5 years ago

*Latest optimizations

File size: 1.8 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<string, Player> Dict = new Dictionary<string, Player> (StringComparer.OrdinalIgnoreCase);
9
10                public Player this [string steamId, bool create] {
11                        get {
12                                if (string.IsNullOrEmpty (steamId)) {
13                                        return null;
14                                }
15
16                                if (Dict.ContainsKey (steamId)) {
17                                        return Dict [steamId];
18                                }
19
20                                if (!create || steamId.Length != 17) {
21                                        return null;
22                                }
23
24                                Log.Out ("Created new player entry for ID: " + steamId);
25                                Player p = new Player (steamId);
26                                Dict.Add (steamId, p);
27                                return p;
28                        }
29                }
30
31                public int Count {
32                        get { return Dict.Count; }
33                }
34
35//              public Player GetPlayerByNameOrId (string _nameOrId, bool _ignoreColorCodes)
36//              {
37//                      string sid = GetSteamID (_nameOrId, _ignoreColorCodes);
38//                      if (sid != null)
39//                              return this [sid];
40//                      else
41//                              return null;
42//              }
43
44                public string GetSteamID (string _nameOrId, bool _ignoreColorCodes) {
45                        if (_nameOrId == null || _nameOrId.Length == 0) {
46                                return null;
47                        }
48
49                        long tempLong;
50                        if (_nameOrId.Length == 17 && long.TryParse (_nameOrId, out tempLong)) {
51                                return _nameOrId;
52                        }
53
54                        int entityId;
55                        if (int.TryParse (_nameOrId, out entityId)) {
56                                foreach (KeyValuePair<string, Player> kvp in Dict) {
57                                        if (kvp.Value.IsOnline && kvp.Value.EntityID == entityId) {
58                                                return kvp.Key;
59                                        }
60                                }
61                        }
62
63                        foreach (KeyValuePair<string, Player> kvp in Dict) {
64                                string name = kvp.Value.Name;
65                                if (_ignoreColorCodes) {
66                                        name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
67                                }
68
69                                if (kvp.Value.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
70                                        return kvp.Key;
71                                }
72                        }
73
74                        return null;
75                }
76        }
77}
Note: See TracBrowser for help on using the repository browser.