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

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

*Latest optimizations

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