84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace SharpChat {
|
|
public class ChatChannel {
|
|
public string Name { get; set; }
|
|
public string Password { get; set; } = string.Empty;
|
|
public bool IsTemporary { get; set; } = false;
|
|
public int Rank { get; set; } = 0;
|
|
public ChatUser Owner { get; set; } = null;
|
|
|
|
private List<ChatUser> Users { get; } = new();
|
|
|
|
public bool HasPassword
|
|
=> !string.IsNullOrWhiteSpace(Password);
|
|
|
|
public ChatChannel() { }
|
|
|
|
public ChatChannel(string name) {
|
|
Name = name;
|
|
}
|
|
|
|
public bool HasUser(ChatUser user) {
|
|
return Users.Contains(user);
|
|
}
|
|
|
|
public void UserJoin(ChatUser user) {
|
|
if(!user.InChannel(this)) {
|
|
// Remove this, a different means for this should be established for V1 compat.
|
|
user.Channel?.UserLeave(user);
|
|
user.JoinChannel(this);
|
|
}
|
|
|
|
if(!HasUser(user))
|
|
Users.Add(user);
|
|
}
|
|
|
|
public void UserLeave(ChatUser user) {
|
|
Users.Remove(user);
|
|
|
|
if(user.InChannel(this))
|
|
user.LeaveChannel(this);
|
|
}
|
|
|
|
public IEnumerable<ChatUser> GetUsers(IEnumerable<ChatUser> exclude = null) {
|
|
IEnumerable<ChatUser> users = Users.OrderByDescending(x => x.Rank);
|
|
|
|
if(exclude != null)
|
|
users = users.Except(exclude);
|
|
|
|
return users.ToList();
|
|
}
|
|
|
|
public string Pack() {
|
|
StringBuilder sb = new();
|
|
|
|
sb.Append(Name);
|
|
sb.Append('\t');
|
|
sb.Append(string.IsNullOrEmpty(Password) ? '0' : '1');
|
|
sb.Append('\t');
|
|
sb.Append(IsTemporary ? '1' : '0');
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public bool NameEquals(string name) {
|
|
return string.Equals(name, Name, StringComparison.InvariantCultureIgnoreCase);
|
|
}
|
|
|
|
public override int GetHashCode() {
|
|
return Name.GetHashCode();
|
|
}
|
|
|
|
public static bool CheckName(string name) {
|
|
return !string.IsNullOrWhiteSpace(name) && name.All(CheckNameChar);
|
|
}
|
|
|
|
public static bool CheckNameChar(char c) {
|
|
return char.IsLetter(c) || char.IsNumber(c) || c == '-' || c == '_';
|
|
}
|
|
}
|
|
}
|