112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace SharpChat {
|
|
public class UsersContext {
|
|
[Flags]
|
|
public enum NameTarget {
|
|
None = 0,
|
|
UserName = 1,
|
|
NickName = 2,
|
|
UserAndNickName = UserName | NickName,
|
|
}
|
|
|
|
private readonly List<UserInfo> Users = new();
|
|
|
|
public int TotalCount { get; private set; }
|
|
|
|
public UserInfo[] All => Users.ToArray();
|
|
|
|
public UserInfo? Get(
|
|
long? userId = null,
|
|
string? name = null,
|
|
NameTarget nameTarget = NameTarget.UserName
|
|
) {
|
|
foreach(UserInfo info in Users) {
|
|
if(userId != null && info.UserId != userId)
|
|
continue;
|
|
if(name != null) {
|
|
// this could probably all fit in a single if condition, but it'd be massive and disgusting
|
|
// and require more thinking power than my goldfish brain can muster
|
|
bool match = false;
|
|
if(nameTarget.HasFlag(NameTarget.UserName) && info.UserName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
|
|
match = true;
|
|
else if(nameTarget.HasFlag(NameTarget.NickName) && info.NickName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
|
|
match = true;
|
|
if(!match)
|
|
continue;
|
|
}
|
|
|
|
return info;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public UserInfo[] GetMany(
|
|
long[]? userIds = null,
|
|
string[]? names = null,
|
|
NameTarget namesTarget = NameTarget.UserName,
|
|
int? minRank = null
|
|
) {
|
|
List<UserInfo> users = new();
|
|
|
|
foreach(UserInfo info in Users) {
|
|
if(minRank != null && info.Rank < minRank)
|
|
continue;
|
|
|
|
if(userIds != null && !userIds.Contains(info.UserId))
|
|
continue;
|
|
|
|
if(names?.Length > 0) {
|
|
bool match = false;
|
|
foreach(string name in names) {
|
|
if(namesTarget.HasFlag(NameTarget.UserName) && info.UserName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
|
|
match = true;
|
|
else if(namesTarget.HasFlag(NameTarget.NickName) && info.NickName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
|
|
match = true;
|
|
}
|
|
|
|
if(!match)
|
|
continue;
|
|
}
|
|
|
|
users.Add(info);
|
|
}
|
|
|
|
return users.ToArray();
|
|
}
|
|
|
|
public void Add(UserInfo info) {
|
|
if(Get(info.UserId, info.UserName) != null)
|
|
throw new ArgumentException("A user with that id and/or name has already been registred.", nameof(info));
|
|
|
|
Users.Add(info);
|
|
++TotalCount;
|
|
}
|
|
|
|
public void Remove(UserInfo info) {
|
|
if(!Users.Contains(info)) {
|
|
Remove(info.UserId);
|
|
return;
|
|
}
|
|
|
|
Users.Remove(info);
|
|
--TotalCount;
|
|
}
|
|
|
|
public void Remove(
|
|
long? userId = null,
|
|
string? name = null,
|
|
NameTarget nameTarget = NameTarget.UserName
|
|
) {
|
|
UserInfo? info = Get(userId, name, nameTarget);
|
|
if(info == null)
|
|
return;
|
|
|
|
Users.Remove(info);
|
|
--TotalCount;
|
|
}
|
|
}
|
|
}
|