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

Last change on this file since 325 was 325, checked in by alloc, 8 years ago

Code style cleanup (mostly whitespace changes, enforcing braces, using cleanup)

File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text.RegularExpressions;
4
5namespace AllocsFixes.PersistentData {
6 [Serializable]
7 public class Players {
8 private readonly Dictionary<string, Player> players = new Dictionary<string, Player> ();
9
10 public Player this [string steamId, bool create] {
11 get {
12 if (string.IsNullOrEmpty (steamId)) {
13 return null;
14 }
15
16 if (players.ContainsKey (steamId)) {
17 return players [steamId];
18 }
19
20 if (create && steamId != null && steamId.Length == 17) {
21 Log.Out ("Created new player entry for ID: " + steamId);
22 Player p = new Player (steamId);
23 players.Add (steamId, p);
24 return p;
25 }
26
27 return null;
28 }
29 }
30
31 public List<string> SteamIDs {
32 get { return new List<string> (players.Keys); }
33 }
34
35 public int Count {
36 get { return players.Count; }
37 }
38
39// public Player GetPlayerByNameOrId (string _nameOrId, bool _ignoreColorCodes)
40// {
41// string sid = GetSteamID (_nameOrId, _ignoreColorCodes);
42// if (sid != null)
43// return this [sid];
44// else
45// return null;
46// }
47
48 public string GetSteamID (string _nameOrId, bool _ignoreColorCodes) {
49 if (_nameOrId == null || _nameOrId.Length == 0) {
50 return null;
51 }
52
53 long tempLong;
54 if (_nameOrId.Length == 17 && long.TryParse (_nameOrId, out tempLong)) {
55 return _nameOrId;
56 }
57
58 int entityId = -1;
59 if (int.TryParse (_nameOrId, out entityId)) {
60 foreach (KeyValuePair<string, Player> kvp in players) {
61 if (kvp.Value.IsOnline && kvp.Value.EntityID == entityId) {
62 return kvp.Key;
63 }
64 }
65 }
66
67 _nameOrId = _nameOrId.ToLower ();
68 foreach (KeyValuePair<string, Player> kvp in players) {
69 string name = kvp.Value.Name.ToLower ();
70 if (_ignoreColorCodes) {
71 name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
72 }
73
74 if (kvp.Value.IsOnline && name.Equals (_nameOrId)) {
75 return kvp.Key;
76 }
77 }
78
79 return null;
80 }
81 }
82}
Note: See TracBrowser for help on using the repository browser.