public void WaitOtherPlayer(Connection connection, Connection.State state, bool cancelButton)
 {
     this.connection = connection;
     this.state = state;
     button1.Enabled = cancelButton;
     waitPlayer = true;
 }
示例#2
0
 private void bgw_DoWork(object sender, DoWorkEventArgs e)
 {
     try
     {
         while (connection.Connected() && !bgw.CancellationPending)
         {
             connection.Receive();
             MessageReceived();
         }
     }
     catch
     {
         Connection.State myState = connection.myState;
         if (myState == Connection.State.Playing || myState == Connection.State.PlayAgain)
         {
             Invoke((MethodInvoker)(() => gameForm.OnlinePlayerQuitted()));
         }
         Invoke((MethodInvoker)(() => PlayerDisconnected()));
     }
 }
示例#3
0
        void MessageReceived()
        {
            Connection.State myState = connection.myState;
            Connection.Info  info    = connection.receivedInfo;
            int nm = -1;

            if (connection.receivedNums.Count > 0)
            {
                nm = connection.receivedNums[0];
            }
            string data = connection.receivedData;

            if (info == Connection.Info.Idle)
            {
                if (myState == Connection.State.Playing || myState == Connection.State.PlayAgain)
                {
                    Invoke((MethodInvoker)(() => gameForm.OnlinePlayerQuitted()));
                }
                connection.otherPlayerState = Connection.State.Idle;
            }
            else if (info == Connection.Info.Waiting)
            {
                connection.otherPlayerState = Connection.State.Waiting;
                if (connection.myState == Connection.State.Waiting)
                {
                    progressForm.InstantClose();
                }
            }
            else if (info == Connection.Info.Disconnected)
            {
                if (myState == Connection.State.Playing || myState == Connection.State.PlayAgain)
                {
                    Invoke((MethodInvoker)(() => gameForm.OnlinePlayerQuitted()));
                }
                Invoke((MethodInvoker)(() => PlayerDisconnected()));
            }
            else if (info == Connection.Info.Playing)
            {
                connection.otherPlayerState = Connection.State.Playing;
            }
            else if (info == Connection.Info.PlayAgain)
            {
                connection.otherPlayerState = Connection.State.PlayAgain;
                if (connection.myState == Connection.State.PlayAgain)
                {
                    Invoke((MethodInvoker)(() => gameForm.NewGame()));
                }
                else
                {
                    Invoke((MethodInvoker)(() => gameForm.PlayAgainRequested()));
                }
            }
            else if (info == Connection.Info.PlayAt && myState == Connection.State.Playing)
            {
                Invoke((MethodInvoker)(() => gameForm.UpdateAll(nm, -1)));
            }
            else if (info == Connection.Info.Type)
            {
                Invoke((MethodInvoker)(() =>
                {
                    players[onlinePlayer].type = nm;
                    lblType[onlinePlayer].Text = playerType[nm];
                }));
            }
            else if (info == Connection.Info.Color)
            {
                Invoke((MethodInvoker)(() => ChangePlayerColor(onlinePlayer, Color.FromArgb(nm))));
            }
            else if (info == Connection.Info.Name)
            {
                Invoke((MethodInvoker)(() => tbxName[onlinePlayer].Text = data));
            }
            else if (info == Connection.Info.Image)
            {
                string fileName = "";
                if (nm != 0)
                {
                    fileName = connection.ReceiveFile(nm);
                }
                Invoke((MethodInvoker)(() => ChangePlayerImage(onlinePlayer, fileName)));
            }
        }