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

Last change on this file since 357 was 351, checked in by alloc, 6 years ago

Fixed game version compatibility of GamePrefs
Code style cleanup (mostly argument names)

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.