public void Receive(string code, Socket source)
{
if (!Owner.EverReceived)
{
Owner.EverReceived = true;
}
Owner.AllReceived += "%%Receive " + System.DateTime.Now.ToShortTimeString() + Environment.NewLine + code + Environment.NewLine;
if (RcvBuf != "")
{
if (code[0] == '☻')
{
Owner.AllReceived += "%%Dispose " + System.DateTime.Now.ToShortTimeString() + Environment.NewLine + RcvBuf + Environment.NewLine; // for debug
RcvBuf = "";
Receive(code, source);
return;
}
if (code.Contains("☂"))
{
RcvBuf += code;
string Buf = RcvBuf;
RcvBuf = "";
Owner.AllReceived += "%%Combine " + System.DateTime.Now.ToShortTimeString() + Environment.NewLine + Buf + Environment.NewLine; // for debug
Receive(Buf, source);
return;
}
}
if (code.Contains("☂☻"))
{
int posEmoji = code.IndexOf("☂☻");
string code1 = code.Substring(0, posEmoji + 1);
string code2 = code.Substring(posEmoji + 1);
Owner.AllReceived += "%%SplitTo " + System.DateTime.Now.ToShortTimeString() + Environment.NewLine + code1 + " AND " + code2 + Environment.NewLine; // for debug
Receive(code1, source);
Receive(code2, source);
return;
}
if (!code.Contains("☂"))
{
Owner.AllReceived += "%%SaveToBuf " + System.DateTime.Now.ToShortTimeString() + Environment.NewLine + code + Environment.NewLine; // for debug
RcvBuf += code;
return;
}
if ((Owner.MyAccessCharacter == AccessCharater.Host || Owner.MyAccessCharacter == AccessCharater.HostWatcher) && (!code.Contains("☻DisConnect#") && (!code.Contains("☻MyName#"))))
{
Owner.MessageRecieved = code;
Owner.MessageRecievedSource = source;
}
code = code.Trim('☂', '☻');
bool NotDirect = code.Contains('☺');
code = code.Trim('☺');
if (Owner.PointedOutCardPic != null)
{
Owner.PointedOutCardPic.Invalidate();
Owner.PointedOutCardPic = null;
}
Owner.AllReceived += "%%ReceiveComplete " + System.DateTime.Now.ToShortTimeString() + Environment.NewLine + code + Environment.NewLine; // for debug
string type;
List<string> contents = new List<string>();
string text;
//Dividing
int pos = 0;
int pos2;
pos = code.IndexOf("#", pos);
pos2 = code.IndexOf("#", pos + 1);
if (pos == -1 || pos2 == -1)
{
return;
}
type = code.Substring(0, pos);
string contents_temp = code.Substring(pos + 1, pos2 - pos - 1);
contents.AddRange(contents_temp.Split(new string[] { ";" }, StringSplitOptions.None));
text = code.Substring(pos2 + 1);
switch (type)
{
case "MyName":
if (Owner.MyAccessCharacter == AccessCharater.Host)
{
if (((ServerSocket)Owner.socket).communicateSockets.Count > 1)
{
break;
}
Owner.RivalName = contents[2];
Owner.ClientsNames.Add(Owner.socket.communicateSocket, contents[2]);
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##连接成功,对手为:" + contents[2] + "(版本" + contents[0] + ")");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##Connected. Your opponent is: " + contents[2] + "(Ver." + contents[0] + ")");
}
}
else if (Owner.MyAccessCharacter == AccessCharater.HostWatcher)
{
//已废弃
if (((ServerSocket)Owner.socket).communicateSockets.Count > 2)
{
break;
}
Owner.ClientsNames.Add(((ServerSocket)Owner.socket).communicateSockets.Last(), contents[2]);
if (((ServerSocket)Owner.socket).communicateSockets.Count == 1)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##玩家1:" + contents[2] + " 加入对战。(版本" + contents[0] + ")" + Environment.NewLine + "请告知玩家2在连接到您时输入端口号:" + Owner.connection.Portusing.ToString() + "。");
}
else if (((ServerSocket)Owner.socket).communicateSockets.Count == 2)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##玩家2:" + contents[2] + " 加入对战。(版本" + contents[0] + ")" + Environment.NewLine + "可以开始游戏。对战双方为:" + Owner.ClientsNames[Owner.socket.communicateSocket] + "(近侧) 与 " + contents[2] + "(远侧)。");
Owner.socket.Send("☻SetPlayer2Telling1#" + contents[2] + "#" + "☂", ((ServerSocket)Owner.socket).communicateSockets[0], true);
}
}
else
{
Owner.RivalName = contents[2];
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##连接成功,对手为:" + contents[2] + "(版本" + contents[0] + ")");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##Connected. Your opponent is: " + contents[2] + "(Ver." + contents[0] + ")");
}
}
if (double.Parse(contents[0]) > Owner.Version)
{
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + "##您的版本较低,可能导致游戏无法正常进行,请尽快下载更新。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + "##Please update your program before the game.");
}
}
break;
case "SetWatcher":
if (Owner.AskIfWatch(contents))
{
Owner.MyAccessCharacter = AccessCharater.Watcher;
Owner.SetToWatchingMode();
Owner.WatcherName = Owner.PlayerName;
Owner.PlayerName = contents[2];
Owner.RivalName = contents[3];
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##加入观战,对战双方为:" + contents[2] + "(近侧) 与 " + contents[3] + "(远侧)。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##You are watching the game of" + contents[2] + "(near) and " + contents[3] + "(far).");
}
if (double.Parse(contents[0]) > Owner.Version)
{
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + "##您的版本较低,可能导致游戏无法正常进行,请尽快下载更新。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + "##Please update your program before the game.");
}
}
}
else
{
Owner.connection.Disconnect();
}
break;
case "GetWatcher":
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + contents[0] + "加入观战。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + contents[0] + " entered as a watcher.");
}
if (Owner.MyAccessCharacter == AccessCharater.Host || Owner.MyAccessCharacter == AccessCharater.HostWatcher)
{
Owner.ClientsNames.Add(((ServerSocket)Owner.socket).communicateSockets.Last(), contents[0]);
}
break;
case "WatcherLeave":
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + contents[0] + "退出观战。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + contents[0] + " left.");
}
break;
case "SetPlayer1":
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##您已连接到中继主机,正在等待对手加入。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##You are connected to a server. Waiting for another player...");
}
if (double.Parse(contents[0]) > Owner.Version)
{
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + "##您的版本较低,可能导致游戏无法正常进行,请尽快下载更新。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + "##Please update your program before the game.");
}
}
break;
case "SetPlayer2Telling1":
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##对手:" + contents[0] + " 已连接到中继主机。可以开始游戏。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##Your opponent ( " + contents[0] + " ) is connected to the server. You may start the game.");
}
Owner.RivalName = contents[0];
Owner.RivalConnected = true;
break;
case "SetPlayer2":
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##您已连接到中继主机,对手为:" + contents[2] + "。可以开始游戏。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##You are connected to a server. Your opponent is " + contents[2] + ". You may start the game.");
}
Owner.RivalName = contents[2];
Owner.RivalConnected = true;
if (double.Parse(contents[0]) > Owner.Version)
{
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + "##您的版本较低,可能导致游戏无法正常进行,请尽快下载更新。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + "##Please update your program before the game.");
}
}
break;
case "PointOut":
try
{
if (Owner.WatchingMode && !NotDirect)
{
switch (contents[0])
{
case "SenderFront":
Owner.PointedOutCardPic = Owner.FrontFieldCardPics[Convert.ToInt32(contents[1])];
break;
case "SenderBack":
Owner.PointedOutCardPic = Owner.BackFieldCardPics[Convert.ToInt32(contents[1])];
break;
case "ReceiverFront":
Owner.PointedOutCardPic = Owner.RivalFrontFieldCardPics[Convert.ToInt32(contents[1])];
break;
case "ReceiverBack":
Owner.PointedOutCardPic = Owner.RivalBackFieldCardPics[Convert.ToInt32(contents[1])];
break;
}
}
else
{
switch (contents[0])
{
case "SenderFront":
Owner.PointedOutCardPic = Owner.RivalFrontFieldCardPics[Convert.ToInt32(contents[1])];
break;
case "SenderBack":
Owner.PointedOutCardPic = Owner.RivalBackFieldCardPics[Convert.ToInt32(contents[1])];
break;
case "ReceiverFront":
Owner.PointedOutCardPic = Owner.FrontFieldCardPics[Convert.ToInt32(contents[1])];
break;
case "ReceiverBack":
Owner.PointedOutCardPic = Owner.BackFieldCardPics[Convert.ToInt32(contents[1])];
break;
}
}
}
catch
{
Owner.UpdateGetMsgTextBox(text);
break;
}
Owner.BorderPainter = Owner.PointedOutCardPic.CreateGraphics();
Rectangle rect = new Rectangle(Owner.PointedOutCardPic.ClientRectangle.X, Owner.PointedOutCardPic.ClientRectangle.Y,
Owner.PointedOutCardPic.ClientRectangle.X + Owner.PointedOutCardPic.ClientRectangle.Width - 1,
Owner.PointedOutCardPic.ClientRectangle.Y + Owner.PointedOutCardPic.ClientRectangle.Height - 1);
Owner.BorderPainter.DrawRectangle(new Pen(Color.White, 6), rect);
Owner.UpdateGetMsgTextBox(text);
break;
case "DisConnect":
if (Owner.MyAccessCharacter == AccessCharater.Host)
{
if (source.Equals(Owner.socket.communicateSocket))
{
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##对手 " + Owner.RivalName + " 断开连接。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + Owner.RivalName + " disconnected.");
}
Owner.connection.Disconnect();
}
else
{
string name = "";
if (Owner.ClientsNames.TryGetValue(source, out name))
{
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + name + "退出观战。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + name + " left the room.");
}
Owner.ClientsNames.Remove(source);
Owner.connection.Disconnect(source);
SendSecret("WatcherLeave", new List<string>() { name }, "");
}
}
}
else if (Owner.MyAccessCharacter == AccessCharater.Player)
{
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##对手 " + Owner.RivalName + " 断开连接。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + Owner.RivalName + " disconnected.");
}
Owner.connection.Disconnect();
}
else if (Owner.MyAccessCharacter == AccessCharater.HostWatcher)
{
//废弃
if (source.Equals(Owner.socket.communicateSocket) || source.Equals(((ServerSocket)Owner.socket).communicateSockets[1]))
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##玩家 " + Owner.RivalName + " 断开连接。");
Owner.connection.Disconnect();
}
else
{
string name = "";
if (Owner.ClientsNames.TryGetValue(source, out name))
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + name + "退出观战。");
Owner.ClientsNames.Remove(source);
Owner.connection.Disconnect(source);
SendSecret("WatcherLeave", new List<string>() { name }, "");
}
}
}
else
{
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##对战双方连接中断。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##One of the players disconnected.");
}
Owner.connection.Disconnect();
}
break;
case "WatcherText":
Owner.UpdateGetMsgTextBox(text);
break;
case "AskSpeVersion":
if (!Owner.WatchingMode)
{
SendSecret("TellSpeVersion", null, "");
}
break;
case "TellSpeVersion":
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##玩家 " + contents[0] + " 的版本号为:" + contents[1]);
break;
case "UseJudge":
if (Owner.Language == Language.Chinese)
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + contents[0] + " 开启裁判功能。");
}
else
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + contents[0] + " enabled Judge mode.");
}
break;
case "PushHistory":
if (contents[0] == Owner.PlayerName)
{
if (Owner.Language == Language.Chinese)
{
if (DialogResult.Yes == MessageBox.Show("收到推送的历史,是否接收?", "推送历史", MessageBoxButtons.YesNo))
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + contents[0] + " 接收历史。");
Owner.SetGameOn();
Owner.Player = new User(contents[1].Replace(";", ";"), Owner);
Owner.Renew();
Send("Update", "");
}
}
else
{
if (DialogResult.Yes == MessageBox.Show("You received a history status, do you want to load it? ", "History", MessageBoxButtons.YesNo))
{
Owner.UpdateGetMsgTextBox(Environment.NewLine + Environment.NewLine + "##" + contents[0] + " load a history status.");
Owner.SetGameOn();
Owner.Player = new User(contents[1].Replace(";", ";"), Owner);
Owner.Renew();
Send("Update", "");
}
}
}
break;
default:
//Decoding
if (Owner.MyAccessCharacter == AccessCharater.Watcher && (!NotDirect))
{
Owner.Player = new User(contents_temp, Owner);
if (Owner.SpeVer.Contains("Judge"))
{
Owner.historychecker.AddHistory(Owner.PlayerName, DateTime.Now.ToShortTimeString(), type, new User(contents_temp, Owner));
}
Owner.Renew();
}
else if (Owner.MyAccessCharacter == AccessCharater.HostWatcher)
{
if (source.Equals(((ServerSocket)Owner.socket).communicateSockets[0]))
{
Owner.Player = new User(contents_temp, Owner);
if (Owner.SpeVer.Contains("Judge"))
{
Owner.historychecker.AddHistory(Owner.PlayerName, DateTime.Now.ToShortTimeString(), type, new User(contents_temp, Owner));
}
Owner.Renew();
}
else if (source.Equals(((ServerSocket)Owner.socket).communicateSockets[1]))
{
Owner.Rival = new User(contents_temp, Owner);
if (Owner.SpeVer.Contains("Judge"))
{
Owner.historychecker.AddHistory(Owner.RivalName, DateTime.Now.ToShortTimeString(), type, new User(contents_temp, Owner));
}
Owner.RivalRenew();
}
}
else
{
if (Owner.RivalName == "")
{
int pos3 = text.IndexOf(" ");
Owner.RivalName = text.Substring(0, pos3).Replace(Environment.NewLine, "");
}
Owner.Rival = new User(contents_temp, Owner);
if (Owner.SpeVer.Contains("Judge"))
{
Owner.historychecker.AddHistory(Owner.RivalName, DateTime.Now.ToShortTimeString(), type, new User(contents_temp, Owner));
}
Owner.RivalRenew();
}
//Display
if (text != "")
{
Owner.UpdateGetMsgTextBox(text);
}
break;
}
}