Ejemplo n.º 1
0
        private void frmLanGame_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            frmMain.localpc.DisconnectUDP();

            if (frmMain.localpc.Function == 1)
            {
                frmGameOptions frm = new frmGameOptions();
                frm.ShowDialog();
                lblstatus.Text = "Đang tìm kiếm đối thủ...";
                this.ActiveListenerBroadcast = true;
                tRec = new Thread(new ThreadStart(ListenForRequestBroadcast));
                tRec.IsBackground = true;
                tRec.Start();
            }
            else
            {
                btnSanSang.Enabled     = true;
                lblstatus.Text         = "Đang tiến hành kết nối máy chủ...";
                this.ActiveListenerUDP = true;
                tRec = new Thread(new ThreadStart(ListenForRequestUDP));
                tRec.IsBackground = true;
                tRec.Start();
            }
            ucMyProfile.LoadProfile(frmMain.localpc.Profile);
        }
Ejemplo n.º 2
0
        void ListenForRequestTCP()
        {
            while (ActiveListenerTCP)
            {
                try
                {
                    string message = frmMain.localpc.ReceiveTCPData();
                    if (message != "")
                    {
                        string strType, strItem;
                        frmMain.localpc.AnalysisReceiveTCPString(message, out strType, out strItem);

                        switch (strType.ToUpper())
                        {
                        case "ASKFILE":
                            frmMain.localpc.SendTCPData("FILE", "");
                            //frmMain.localpc.SendFileTCP();
                            //Thread tsend = new Thread(new ThreadStart(frmMain.localpc.SendFileTCP));
                            //tsend.Start();
                            break;

                        case "FILE":
                            ActiveListenerTCP = false;
                            //Thread treceive = new Thread(new ThreadStart(frmMain.localpc.ReceiveFileTCP));
                            //treceive.Start();
                            //frmMain.localpc.ReceiveFileTCP();
                            ActiveListenerTCP = true;
                            break;

                        case "SENDED":
                            if (frmMain.localpc.Function == 1)     //Server
                            {
                                ucOppProfile.LoadProfile(frmMain.localpc.ParnerName);
                                frmMain.localpc.SendTCPData("OPTION", iSide + ":" + iTotalTimer + ":" + iMoveTimer + ":" + iExtraTimer + ":" + iUndo);
                            }
                            else
                            {
                                frmMain.localpc.SendTCPData("ASKFILE", "");
                            }
                            break;

                        case "OPTION":
                            string[] arrOption = strItem.Split(':');
                            iSide = int.Parse(arrOption[0]);
                            if (iSide == 0)
                            {
                                iSide = 1;
                            }
                            else
                            {
                                iSide = 0;
                            }

                            iTotalTimer = int.Parse(arrOption[1]);
                            iMoveTimer  = int.Parse(arrOption[2]);
                            iExtraTimer = int.Parse(arrOption[3]);
                            iUndo       = int.Parse(arrOption[4]);
                            frmGameOptions frm = new frmGameOptions();
                            frm.ShowDialog();
                            if (frmGameOptions.bEditOption == false)
                            {
                                btnPlay.Enabled = true;
                            }
                            break;

                        case "ENDOPTION":
                            lblstatus.Text       = "Cấu hình đã xong, Xin nhấn Bắt đầu để chơi.";
                            this.btnPlay.Enabled = true;
                            break;

                        case "MOVE":
                            Board.MakeMove(strItem);
                            break;

                        case "CHAT":
                            this.lstChatBox.Items.Add(frmMain.localpc.ParnerName + ": " + strItem);
                            lstChatBox.TopIndex = lstChatBox.Items.Count - 1;
                            break;

                        case "LOSE":
                            MessageBox.Show("Chúc mừng bạn đã thắng " + frmMain.localpc.ParnerName);
                            panel1.Enabled = false;
                            break;

                        case "DRAW":
                            DialogResult drdraw = MessageBox.Show(frmMain.localpc.ParnerName + " xin Hòa. Bạn đồng ý không ?", "XIN HÒA", MessageBoxButtons.OKCancel);
                            if (drdraw == DialogResult.OK)
                            {
                                frmMain.localpc.SendTCPData("DRAWOK", "");
                                MessageBox.Show("Bạn đã Hòa với " + frmMain.localpc.ParnerName + ". Cố gắng thêm nhé.");
                                panel1.Enabled = false;
                            }
                            else
                            {
                                frmMain.localpc.SendTCPData("DRAWCANCEL", "");
                            }
                            break;

                        case "DRAWOK":
                            MessageBox.Show("Bạn đã Hòa với " + frmMain.localpc.ParnerName + ". Cố gắng thêm nhé.");
                            panel1.Enabled = false;
                            break;

                        case "DRAWCANCEL":
                            MessageBox.Show(frmMain.localpc.ParnerName + " không đồng ý HÒA.");
                            break;

                        case "UNDO":
                            DialogResult dr = MessageBox.Show(frmMain.localpc.ParnerName + " xin Đi Lại. Bạn đồng ý không ?", "XIN ĐI LẠI", MessageBoxButtons.OKCancel);
                            if (dr == DialogResult.OK)
                            {
                                frmMain.localpc.SendTCPData("UNDOOK", "");
                                Board.UnDoMove();
                                UcMovesHistory1.LoadMovesHistory(Board.stkWhiteMoves, Board.stkBlackMoves);
                            }
                            else
                            {
                                frmMain.localpc.SendTCPData("UNDOCANCEL", "");
                            }
                            break;

                        case "UNDOOK":
                            Board.UnDoMove();
                            break;

                        case "UNDOCANCEL":
                            MessageBox.Show(frmMain.localpc.ParnerName + " không đồng ý cho bạn đi lại.");
                            break;

                        case "NEWGAME":
                            DialogResult drnewgame = MessageBox.Show(frmMain.localpc.ParnerName + " mời bạn chơi lại. Bạn đồng ý không ?", "CHƠI LẠI", MessageBoxButtons.OKCancel);
                            if (drnewgame == DialogResult.OK)
                            {
                                frmMain.localpc.SendTCPData("NEWGAMEOK", "");
                            }
                            else
                            {
                                frmMain.localpc.SendTCPData("NEWGAMECANCEL", "");
                            }
                            break;

                        case "NEWGAMEOK":
                            frmGameOptions frmgameoption = new frmGameOptions();
                            frmgameoption.ShowDialog();
                            if (frmGameOptions.bEditOption == false)
                            {
                                btnPlay.Enabled = true;
                                panel1.Enabled  = true;
                            }
                            break;

                        case "NEWGAMECANCEL":
                            MessageBox.Show(frmMain.localpc.ParnerName + " đã từ chối Chơi Lại với bạn.", "Chơi Lại");
                            break;
                        }
                    }
                }
                catch
                {
                    if (frmMain.localpc.ParnerName != null)
                    {
                        MessageBox.Show("Máy bạn đã mất kết nối với " + frmMain.localpc.ParnerName, "Mất kết nối");
                    }
                    else
                    {
                        MessageBox.Show("Máy bạn đã mất kết nối với máy chủ", "Mất kết nối");
                    }

                    this.ActiveListenerTCP = false;
                    this.Close();
                }
            }
        }