-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPlayerFinder.cs
More file actions
68 lines (55 loc) · 1.77 KB
/
Copy pathPlayerFinder.cs
File metadata and controls
68 lines (55 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using BBRAPIModules;
using System;
using System.Linq;
namespace PlayerFinder;
[Module("Library functions for finding players by partial names or SteamID", "1.0.0")]
public class PlayerFinder : BattleBitModule
{
public RunnerPlayer? ByExactName(string exactName, bool caseSensitive)
{
return this.Server.AllPlayers.FirstOrDefault(p => p.Name.Equals(exactName, caseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase));
}
public RunnerPlayer? ByNamePart(string namePart)
{
RunnerPlayer? exactMatch = this.ByExactName(namePart, true);
if (exactMatch != null)
{
return exactMatch;
}
exactMatch = this.ByExactName(namePart, false);
if (exactMatch != null)
{
return exactMatch;
}
RunnerPlayer[] playerList = this.AllByNamePart(namePart);
if (playerList.Length > 1)
{
throw new ManyPlayersMatchException(playerList);
}
if (playerList.Length == 0)
{
return null;
}
return playerList[0];
}
public RunnerPlayer? BySteamId(ulong steamId)
{
return this.Server.AllPlayers.FirstOrDefault(p => p.SteamID == steamId);
}
public RunnerPlayer[] AllByNamePart(string namePart)
{
return this.Server.AllPlayers.Where(p => p.Name.ToLower().Contains(namePart.ToLower())).ToArray();
}
}
public class ManyPlayersMatchException : Exception
{
public RunnerPlayer[] Players { get; }
public ManyPlayersMatchException(RunnerPlayer[] players)
{
this.Players = players;
}
public override string ToString()
{
return $"Multiple players match: {string.Join(", ", this.Players.Select(p => p.Name))}";
}
}