-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathChannel.cs
177 lines (173 loc) · 6.24 KB
/
Channel.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Terraria;
namespace ChatAssistant
{
[Flags]
public enum ChannelFlags
{
None = 0,
Permanent = 1,
Passworded = 2,
Hidden = 4,
BlockGlobalChat = 8,
BlockJoinLeaveMsg = 16
}
public class ChannelTemplate
{
public String Name;
public String Password;
public bool Hidden;
public bool BlockGlobalChat;
public bool AnnounceChannelJoinLeave;
public ChannelFlags GetFlags()
{
ChannelFlags flags = (ChannelFlags)1;
if (this.Password.Length > 0)
flags |= ChannelFlags.Passworded;
if (this.Hidden)
flags |= ChannelFlags.Hidden;
if (this.BlockGlobalChat)
flags |= ChannelFlags.BlockGlobalChat;
if (!this.AnnounceChannelJoinLeave)
flags |= ChannelFlags.BlockJoinLeaveMsg;
return flags;
}
}
public class Channel
{
public int ID;
public String Name;
public ChannelFlags Flags;
public string Password;
public ChatMessage[] Log = new ChatMessage[100];
private int LogCounter = 0;
public List<int> Users = new List<int>();
public Channel(int id, string name) : this(id, name, 0, "") { }
public Channel(int id, string name, ChannelFlags flags, string password)
{
this.ID = id;
this.Name = name;
this.Flags = flags;
this.Password = password;
if (password != "")
this.Flags |= ChannelFlags.Passworded;
}
public void JoinChannel(CAPlayer player)
{
if (player == null)
return;
if (player.Channel >= 0 && player.Channel != this.ID && CAMain.Channels[player.Channel] != null)
CAMain.Channels[player.Channel].LeaveChannel(player);
player.Channel = this.ID;
if (!this.Users.Contains(player.Index))
{
this.Users.Add(player.Index);
CAMain.DisplayLog(player, 6);
if (!this.Flags.HasFlag(ChannelFlags.BlockJoinLeaveMsg))
NetMessage.SendData((int)PacketTypes.ChatText, -1, player.Index, String.Format("[Join] {0} has joined the channel", player.TSPlayer.Name), 255, Color.LightSalmon.R, Color.LightSalmon.G, Color.LightSalmon.B, this.ID + 1);
}
}
public void LeaveChannel(CAPlayer player)
{
this.Users.Remove(player.Index);
player.Channel = 0;
if (!this.Flags.HasFlag(ChannelFlags.Permanent) && this.Users.Count == 0)
DeleteChannel(this.ID);
else if (!this.Flags.HasFlag(ChannelFlags.BlockJoinLeaveMsg))
NetMessage.SendData((int)PacketTypes.ChatText, -1, player.Index, String.Format("[Leave] {0} has left the channel", player.TSPlayer.Name), 255, Color.LightSalmon.R, Color.LightSalmon.G, Color.LightSalmon.B, this.ID + 1);
}
public void AddToLog(ChatMessage msg)
{
this.Log[this.LogCounter] = msg;
this.LogCounter++;
if (this.LogCounter >= this.Log.Length)
this.LogCounter = 0;
}
public List<ChatMessage> GetLog(int len)
{
List<ChatMessage> ReturnList = new List<ChatMessage>();
int count = 0;
for (int i = 1; i < Log.Length; i++)
{
if (count >= len)
break;
int index = LogCounter - i;
if (index < 0)
index = Log.Length - 1 + index;
if (Log[index] != null)
{
ReturnList.Add(Log[index]);
count++;
}
}
//ReturnList.Reverse();
return ReturnList;
}
// ------------------ STATIC Methods ------------------------------------------
public static int CreateChannel(string name, string password = "")
{
int chID = GetEmpty();
if (chID == -1)
return -1;
CAMain.Channels[chID] = new Channel(chID, name, 0, password);
return chID;
}
public static void DeleteChannel(int id)
{
if (id > 0 && CAMain.Channels[id] != null)
{
if (CAMain.Channels[id].Flags.HasFlag(ChannelFlags.Permanent))
return;
foreach (int pID in CAMain.Channels[id].Users)
{
if (pID >= 0 && CAMain.PlayerList[pID] != null && CAMain.PlayerList[pID].Channel == id)
CAMain.PlayerList[pID].Channel = 0;
}
CAMain.Channels[id] = null;
}
}
public static int GetEmpty()
{
for (int i = 0; i < CAMain.Channels.Length; i++)
{
if (CAMain.Channels[i] == null)
return i;
}
return -1;
}
public static List<Channel> GetPermanent()
{
var ReturnList = new List<Channel>();
for (int i = 0; i < CAMain.Channels.Length; i++)
{
if (CAMain.Channels[i] != null && CAMain.Channels[i].Flags.HasFlag(ChannelFlags.Permanent))
ReturnList.Add(CAMain.Channels[i]);
}
return ReturnList;
}
public static List<Channel> GetNonHidden()
{
var ReturnList = new List<Channel>();
for (int i = 0; i < CAMain.Channels.Length; i++)
{
if (CAMain.Channels[i] != null && !CAMain.Channels[i].Flags.HasFlag(ChannelFlags.Hidden))
ReturnList.Add(CAMain.Channels[i]);
}
return ReturnList;
}
public static List<Channel> GetAll()
{
var ReturnList = new List<Channel>();
for (int i = 0; i < CAMain.Channels.Length; i++)
{
if (CAMain.Channels[i] != null)
ReturnList.Add(CAMain.Channels[i]);
}
return ReturnList;
}
}
}