2023-02-10 06:07:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat {
|
|
|
|
|
public class ChatChannel : IPacketTarget {
|
|
|
|
|
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;
|
|
|
|
|
|
2023-02-07 15:01:56 +00:00
|
|
|
|
private List<ChatUser> Users { get; } = new();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
public bool HasPassword
|
|
|
|
|
=> !string.IsNullOrWhiteSpace(Password);
|
|
|
|
|
|
|
|
|
|
public string TargetName => Name;
|
|
|
|
|
|
2023-02-08 03:32:12 +00:00
|
|
|
|
public ChatChannel() { }
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
public ChatChannel(string name) {
|
|
|
|
|
Name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasUser(ChatUser user) {
|
2023-02-10 06:07:59 +00:00
|
|
|
|
return Users.Contains(user);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UserJoin(ChatUser user) {
|
2023-02-07 15:01:56 +00:00
|
|
|
|
if(!user.InChannel(this)) {
|
2022-08-30 15:00:58 +00:00
|
|
|
|
// Remove this, a different means for this should be established for V1 compat.
|
|
|
|
|
user.Channel?.UserLeave(user);
|
|
|
|
|
user.JoinChannel(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
if(!HasUser(user))
|
|
|
|
|
Users.Add(user);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UserLeave(ChatUser user) {
|
2023-02-10 06:07:59 +00:00
|
|
|
|
Users.Remove(user);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-07 15:01:56 +00:00
|
|
|
|
if(user.InChannel(this))
|
2022-08-30 15:00:58 +00:00
|
|
|
|
user.LeaveChannel(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Send(IServerPacket packet) {
|
2023-02-10 06:07:59 +00:00
|
|
|
|
foreach(ChatUser user in Users)
|
|
|
|
|
user.Send(packet);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ChatUser> GetUsers(IEnumerable<ChatUser> exclude = null) {
|
2023-02-10 06:07:59 +00:00
|
|
|
|
IEnumerable<ChatUser> users = Users.OrderByDescending(x => x.Rank);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
if(exclude != null)
|
|
|
|
|
users = users.Except(exclude);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
return users.ToList();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Pack() {
|
2023-02-07 15:01:56 +00:00
|
|
|
|
StringBuilder sb = new();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
sb.Append(Name);
|
|
|
|
|
sb.Append('\t');
|
|
|
|
|
sb.Append(string.IsNullOrEmpty(Password) ? '0' : '1');
|
|
|
|
|
sb.Append('\t');
|
|
|
|
|
sb.Append(IsTemporary ? '1' : '0');
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2023-02-10 06:07:59 +00:00
|
|
|
|
|
|
|
|
|
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 == '-';
|
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|