private void CommandRecieved(object sender, CommandEventArgs e) { if (e.Command.SenderName != client.Username) { //Recieving a chat message if (e.Command.CommandType == CommandType.Message) { rtbChat.AppendText(e.Command.SenderName.ToString() + ": " + e.Command.Data); } //Notify of user connecting if (e.Command.CommandType == CommandType.UserConnected) { clientList.Add(e.Command.Data); string username = e.Command.Data.Split(':')[2]; rtbChat.AppendText(username + " has connected." + Environment.NewLine); lstUsers.Items.Add(username); } //Notify of user disconnecting if (e.Command.CommandType == CommandType.UserDisconnected) { string username = e.Command.Data.Split(':')[2]; for (int i = 0; i < lstUsers.Items.Count; i++) { if ((string)lstUsers.Items[i] == username) { lstUsers.Items.RemoveAt(i); break; } } for (int i = 0; i < clientList.Count; i++) { if (clientList[i] == e.Command.Data) { clientList.RemoveAt(i); } } rtbChat.AppendText(username + " has disconnected." + Environment.NewLine); } } //Update client list when recieved - Outside self message check to enable server to inform client of it's existence if (e.Command.CommandType == CommandType.ClientListRequest) { string[] clients = Array.ConvertAll(e.Command.Data.Split(','), p => p.Trim()); lstUsers.Items.Clear(); for (int i = 0; i < clients.Length; i++) { if (clients[i] != "") { string username = clients[i].Split(':')[2]; lstUsers.Items.Add(username); clientList.Add(clients[i]); } } } if (e.Command.CommandType == CommandType.ChallengeRequest) { if (activeChallenge == false) { string data = e.Command.Data; DialogResult dr = MessageBox.Show(e.Command.SenderName + " has challenged you!" + Environment.NewLine + "Wins: " + int.Parse(data.Split(':')[0]) + Environment.NewLine + "Losses: " + int.Parse(data.Split(':')[1]) + Environment.NewLine + "Do You accept?", "You have been challenged!", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { activeChallenge = true; //Accept the request Command cmd = new Command(CommandType.ChallengeResponse, e.Command.SenderIP, "true"); cmd.TargetPort = e.Command.SenderPort; cmd.SenderIP = client.IP; cmd.SenderName = client.Username; cmd.SenderPort = client.Port; client.SendCommand(cmd); } else { //Reject the request Command cmd = new Command(CommandType.ChallengeResponse, e.Command.SenderIP, "false"); cmd.TargetPort = e.Command.SenderPort; cmd.SenderIP = client.IP; cmd.SenderName = client.Username; cmd.SenderPort = client.Port; client.SendCommand(cmd); } } } if (e.Command.CommandType == CommandType.ChallengeResponse) { if (e.Command.Data.ToString().ToLower() == "true") { //Challenge Accepted Command cmd = new Command(CommandType.GameStartRequest, client.ServerIP, e.Command.SenderIP + ":" + e.Command.SenderPort); cmd.TargetPort = client.ServerPort; cmd.SenderIP = client.IP; cmd.SenderPort = client.Port; cmd.SenderName = client.Username; client.SendCommand(cmd); } else { //challenge Rejected activeChallenge = false; rtbChat.AppendText("Your game challenge was rejected." + Environment.NewLine); } } if (e.Command.CommandType == CommandType.GameIDInform) { activeGameID = int.Parse(e.Command.Data); if (InvokeRequired) { BeginInvoke(new MethodInvoker(delegate { gameForm = new BattleshipGameForm(ref client, activeGameID); gameForm.Show(); gameForm.FormClosed += new FormClosedEventHandler(GameForm_Closed); })); } else { gameForm = new BattleshipGameForm(ref client, activeGameID); gameForm.Show(); gameForm.FormClosed += new FormClosedEventHandler(GameForm_Closed); } } if (e.Command.CommandType == CommandType.UserDataInform) { ConfirmChallenge(e.Command); } }
private void CommandRecieved(object sender, CommandEventArgs e) { if (e.Command.SenderName != client.Username) { //Recieving a chat message if (e.Command.CommandType == CommandType.Message) { rtbChat.AppendText(e.Command.SenderName.ToString() + ": " + e.Command.Data); } //Notify of user connecting if (e.Command.CommandType == CommandType.UserConnected) { clientList.Add(e.Command.Data); string username = e.Command.Data.Split(':')[2]; rtbChat.AppendText(i18n.GetText("userHasConnected", username)); lstUsers.Items.Add(username); } //Notify of user disconnecting if (e.Command.CommandType == CommandType.UserDisconnected) { string username = e.Command.Data.Split(':')[2]; for (int i = 0; i < lstUsers.Items.Count; i++) { if ((string)lstUsers.Items[i] == username) { lstUsers.Items.RemoveAt(i); break; } } for (int i = 0; i < clientList.Count; i++) { if (clientList[i] == e.Command.Data) { clientList.RemoveAt(i); } } rtbChat.AppendText(i18n.GetText("userHasDisconected", username)); } } //Update client list when recieved - Outside self message check to enable server to inform client of it's existence if (e.Command.CommandType == CommandType.ClientListRequest) { string[] clients = Array.ConvertAll(e.Command.Data.Split(','), p => p.Trim()); lstUsers.Items.Clear(); for (int i = 0; i < clients.Length; i++) { if (clients[i] != "") { string username = clients[i].Split(':')[2]; lstUsers.Items.Add(username); clientList.Add(clients[i]); } } } if (e.Command.CommandType == CommandType.ChallengeRequest) { if (activeChallenge == false) { string data = e.Command.Data; DialogResult dr = MessageBox.Show(i18n.GetText("ChallengeRequest", e.Command.SenderName, data.Split(':')[0], data.Split(':')[1]), i18n.GetText("ChallengeRequestTitle"), MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { activeChallenge = true; //Accept the request Command cmd = new Command(CommandType.ChallengeResponse, e.Command.SenderIP, "true"); cmd.TargetPort = e.Command.SenderPort; cmd.SenderIP = client.IP; cmd.SenderName = client.Username; cmd.SenderPort = client.Port; client.SendCommand(cmd); } else { //Reject the request Command cmd = new Command(CommandType.ChallengeResponse, e.Command.SenderIP, "false"); cmd.TargetPort = e.Command.SenderPort; cmd.SenderIP = client.IP; cmd.SenderName = client.Username; cmd.SenderPort = client.Port; client.SendCommand(cmd); } } } if (e.Command.CommandType == CommandType.ChallengeResponse) { if (e.Command.Data.ToString().ToLower() == "true") { //Challenge Accepted Command cmd = new Command(CommandType.GameStartRequest, client.ServerIP, e.Command.SenderIP + ":" + e.Command.SenderPort); cmd.TargetPort = client.ServerPort; cmd.SenderIP = client.IP; cmd.SenderPort = client.Port; cmd.SenderName = client.Username; client.SendCommand(cmd); } else { //challenge Rejected activeChallenge = false; rtbChat.AppendText(i18n.GetText("ChallengeRejected")); } } if (e.Command.CommandType == CommandType.GameIDInform) { activeGameID = int.Parse(e.Command.Data); if (InvokeRequired) { BeginInvoke(new MethodInvoker(delegate { gameForm = new BattleshipGameForm(ref client, activeGameID); gameForm.Show(); gameForm.FormClosed += new FormClosedEventHandler(GameForm_Closed); })); } else { gameForm = new BattleshipGameForm(ref client, activeGameID); gameForm.Show(); gameForm.FormClosed += new FormClosedEventHandler(GameForm_Closed); } } if (e.Command.CommandType == CommandType.UserDataInform) { ConfirmChallenge(e.Command); } }