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

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

Fixed serialization of Player data

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> (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 (players.ContainsKey (steamId)) {
17 return players [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 players.Add (steamId, p);
27 return p;
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;
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 foreach (KeyValuePair<string, Player> kvp in players) {
68 string name = kvp.Value.Name;
69 if (_ignoreColorCodes) {
70 name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
71 }
72
73 if (kvp.Value.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
74 return kvp.Key;
75 }
76 }
77
78 return null;
79 }
80 }
81}
Note: See TracBrowser for help on using the repository browser.