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

Last change on this file since 214 was 202, checked in by alloc, 10 years ago

Server fixes

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