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

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

Fixed serialization of Player data

File size: 1.9 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 {
[327]8 private readonly Dictionary<string, Player> players = 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
16 if (players.ContainsKey (steamId)) {
[253]17 return players [steamId];
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);
26 players.Add (steamId, p);
27 return p;
[253]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;
[325]56 }
[253]57
[326]58 int entityId;
[325]59 if (int.TryParse (_nameOrId, out entityId)) {
[253]60 foreach (KeyValuePair<string, Player> kvp in players) {
[325]61 if (kvp.Value.IsOnline && kvp.Value.EntityID == entityId) {
[253]62 return kvp.Key;
63 }
64 }
65 }
[325]66
67 foreach (KeyValuePair<string, Player> kvp in players) {
[326]68 string name = kvp.Value.Name;
[325]69 if (_ignoreColorCodes) {
70 name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
71 }
72
[326]73 if (kvp.Value.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
[325]74 return kvp.Key;
75 }
76 }
77
[253]78 return null;
79 }
80 }
[325]81}
Note: See TracBrowser for help on using the repository browser.