/
AccountExt.cs
159 lines (134 loc) · 3.79 KB
/
AccountExt.cs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#region Header
// _,-'/-'/
// . __,-; ,'( '/
// \. `-.__`-._`:_,-._ _ , . ``
// `:-._,------' ` _,`--` -: `_ , ` ,' :
// `---..__,,--' (C) 2023 ` -'. -'
// # Vita-Nex [http://core.vita-nex.com] #
// {o)xxx|===============- # -===============|xxx(o}
// # #
#endregion
#region References
using System;
using System.Collections.Generic;
using System.Linq;
using Server.Accounting;
#endregion
namespace Server
{
public static class AccountExtUtility
{
public static bool IsOnline(this IAccount acc)
{
return FindMobiles(acc, m => m.IsOnline()).Any();
}
public static Mobile GetOnlineMobile(this IAccount acc)
{
return FindMobiles(acc, m => m.IsOnline()).FirstOrDefault();
}
public static TMob GetOnlineMobile<TMob>(this IAccount acc)
where TMob : Mobile
{
return FindMobiles<TMob>(acc, m => m.IsOnline()).FirstOrDefault();
}
public static Mobile[] GetMobiles(this IAccount acc)
{
return GetMobiles(acc, null);
}
public static Mobile[] GetMobiles(this IAccount acc, Func<Mobile, bool> predicate)
{
return FindMobiles(acc, predicate).ToArray();
}
public static IEnumerable<Mobile> FindMobiles(this IAccount acc)
{
return FindMobiles(acc, null);
}
public static IEnumerable<Mobile> FindMobiles(this IAccount acc, Func<Mobile, bool> predicate)
{
if (acc == null)
{
yield break;
}
for (var i = 0; i < acc.Length; i++)
{
if (acc[i] != null && (predicate == null || predicate(acc[i])))
{
yield return acc[i];
}
}
}
public static TMob[] GetMobiles<TMob>(this IAccount acc)
where TMob : Mobile
{
return GetMobiles<TMob>(acc, null);
}
public static TMob[] GetMobiles<TMob>(this IAccount acc, Func<TMob, bool> predicate)
where TMob : Mobile
{
return FindMobiles(acc, predicate).ToArray();
}
public static IEnumerable<TMob> FindMobiles<TMob>(this IAccount acc)
where TMob : Mobile
{
return FindMobiles<TMob>(acc, null);
}
public static IEnumerable<TMob> FindMobiles<TMob>(this IAccount acc, Func<TMob, bool> predicate)
where TMob : Mobile
{
if (acc == null)
{
yield break;
}
for (var i = 0; i < acc.Length; i++)
{
if (acc[i] is TMob && (predicate == null || predicate((TMob)acc[i])))
{
yield return (TMob)acc[i];
}
}
}
public static Account[] GetSharedAccounts(this IAccount acc)
{
return GetSharedAccounts(acc as Account);
}
public static Account[] GetSharedAccounts(this Account acc)
{
return FindSharedAccounts(acc).ToArray();
}
public static IEnumerable<Account> FindSharedAccounts(this IAccount acc)
{
return FindSharedAccounts(acc as Account);
}
public static IEnumerable<Account> FindSharedAccounts(this Account acc)
{
if (acc == null)
{
yield break;
}
foreach (var a in Accounts.GetAccounts().AsParallel().OfType<Account>().Where(a => IsSharedWith(acc, a)))
{
yield return a;
}
}
public static bool IsSharedWith(this IAccount acc, IAccount a)
{
return IsSharedWith(acc as Account, a as Account);
}
public static bool IsSharedWith(this Account acc, Account a)
{
return acc != null && a != null && (acc == a || acc.LoginIPs.Any(a.LoginIPs.Contains));
}
public static bool CheckAccount(this Mobile a, Mobile b)
{
return a != null && b != null && (a == b || a.Account == b.Account);
}
public static bool CheckAccount(this Mobile a, IAccount b)
{
return a != null && b != null && a.Account == b;
}
public static bool CheckAccount(this IAccount a, Mobile b)
{
return a != null && b != null && a == b.Account;
}
}
}