Example #1
0
 private void CountDown_Tick(object sender, EventArgs e)
 {
     ncountdown--;
     textBox1.Text = "" + ncountdown;
     SendUpdateMesageToAllConnectedclients("/Countdown " + ncountdown); //sends the countdown of the server game starting to players. (syncrosation)
     if (ncountdown == 0)
     {
         CountDown.Stop();
         PlayerGo.Start();
     }
 }
    private IEnumerator PlayOneStep()
    {
        int   nStep      = 500;
        float timeToMove = 0.6f;
        int   nColors    = 3;

        // Calculate moving for all spheres and player
        for (int j = 0; j < nStep; j++)
        {
            CalculateMovingSpheres();
            CalculateMovingPlayer();
            CheckColorCount(nColors);

            yield return(new WaitForSeconds(timeToMove));

            // Invoke player and spheres for moving
            PlayerGo.Invoke(playerMoveSide, playerSwap);

            for (int i = 0; i < sphereList.Count; i++)
            {
                SphereGo.Invoke(sphereList[i], sphereMoveSide[i], sphereSwap[i], i);
            }
        }
    }
Example #3
0
        private void OnTimedEvent_PeriodicCommunicationActivityCheck(Object myObject, EventArgs myEventArgs) //when something is sent.
        {
            int iIndex;

            iIndex = GetnextAvailable_ConnectionArray_Entry();
            if (-1 != iIndex)
            {
                try
                {
                    m_Connection_Array[iIndex].ClientSpecific_Socket = m_ListenSocket.Accept(); // Accept a connection (if pending) and assign a new socket to it (AcceptSocket)
                    m_Connection_Array[iIndex].bInUse = true;
                    m_Connection_Array[iIndex].ClientSpecific_Socket.Blocking = false;          // Make the new socket operate in non-blocking mode
                    m_iNumberOfConnectedClients++;
                    NumberOfClients_textBox.Text = System.Convert.ToString(m_iNumberOfConnectedClients);
                    Status_textBox.Text          = "A new client connected";
                    SendUpdateMesageToAllConnectedclients(empty);
                }
                catch (SocketException se)                              // Handle socket-related exception
                {                                                       // If an exception occurs, display an error message
                    if (10053 == se.ErrorCode || 10054 == se.ErrorCode) // Remote end closed the connection
                    {
                        CloseConnection(iIndex);
                    }
                    else if (10035 != se.ErrorCode)
                    { // Ignore error messages relating to normal behaviour of non-blocking sockets
                        MessageBox.Show(se.Message);
                    }
                }
                catch // Silently handle any other exception
                {
                }
            }

            // Second, check for received messages on each connected socket
            for (iIndex = 0; iIndex < m_iMaxConnections; iIndex++)
            {
                if (true == m_Connection_Array[iIndex].bInUse)
                {
                    try
                    {
                        EndPoint localEndPoint = (EndPoint)m_LocalIPEndPoint;
                        byte[]   ReceiveBuffer = new byte[1024];
                        int      iReceiveByteCount;
                        iReceiveByteCount = m_Connection_Array[iIndex].ClientSpecific_Socket.ReceiveFrom(ReceiveBuffer, ref localEndPoint);

                        if (0 < iReceiveByteCount)
                        {   // Copy the number of bytes received, from the message buffer to the text control
                            szReceivedMessage = Encoding.ASCII.GetString(ReceiveBuffer, 0, iReceiveByteCount);

                            string diceposition = szReceivedMessage;

                            int Dice    = diceposition.IndexOf(" ");
                            int positon = diceposition.IndexOf(" ", Dice + 1);

                            try {
                                if (Dice != positon && Dice >= 0)
                                {
                                    diceposition = diceposition.Remove(Dice + 1, positon - Dice);
                                }
                            }
                            catch { }

                            if (diceposition.Contains("/Dice"))                                                                                                    // if the message includes dice then then the following commands
                            {
                                string userName = listBox1.Items[iIndex].ToString();                                                                               // gets the username of the player which has sent it.
                                Message_textBox.AppendText("\n" + "ServerLOG:" + userName + " Has rolled the dice and got : (" + diceposition.Substring(5) + ")"); //shows what dice the player has rolled keeps as log in the server.
                                userName += " Has Rolled and Achived " + diceposition.Substring(5);                                                                //creates string to send back to all users.
                                SendUpdateMesageToAllConnectedclients("/DIC" + userName);                                                                          // send a small command so it can divide the and take it as dice. (username includes all lines)
                            }

                            if (szReceivedMessage.Contains("/STOP")) //if the clients reach end point then it will stop all the timers.
                            {
                                ncountdown = 5;
                                nPlayergo  = 0;
                                counterr   = 5;
                                counter.Stop();
                                PlayerGo.Stop();
                                CountDown.Stop();
                                return;
                            }

                            if ("/User:"******"/Mesg:" == szReceivedMessage.Substring(0, 6))                                    // when a player messages it will take start with /mesg
                            {
                                string userName = listBox1.Items[iIndex].ToString();                                   //gets players name
                                Message_textBox.AppendText("\n " + userName + " : " + szReceivedMessage.Substring(6)); //keeps message as log into host richtextbox.


                                userName += " : " + szReceivedMessage.Substring(6);      //uses string to send backk to all clients connected.
                                SendUpdateMesageToAllConnectedclients("MSG" + userName); //uses string to send backk to all clients connected.
                            }

                            //synchronisation

                            else if (szReceivedMessage.Contains("/P1")) //if the player 1 has moved location then it will send this command including new position example /P1056;
                            {
                                if (diceposition.StartsWith("/P"))      //somestimes commands are connected so if it starts with it then send it like this.
                                {
                                    SendUpdateMesageToAllConnectedclients(szReceivedMessage);
                                }
                                else //sometimes it will be at the end so we will remove the first commands which is normaly the dice.
                                {
                                    SendUpdateMesageToAllConnectedclients(szReceivedMessage.Substring(8));
                                }
                            }
                            else if (szReceivedMessage.Contains("/P2")) //if the player 1 has moved location then it will send this command including new position example /P1056;
                            {
                                if (diceposition.StartsWith("/P"))
                                {
                                    SendUpdateMesageToAllConnectedclients(szReceivedMessage);
                                }
                                else //sometimes it will be at the end so we will remove the first commands which is normaly the dice.
                                {
                                    SendUpdateMesageToAllConnectedclients(szReceivedMessage.Substring(8));
                                }
                            }

                            else if (szReceivedMessage.Contains("/P3")) //if the player 1 has moved location then it will send this command including new position example /P1056;
                            {
                                if (diceposition.StartsWith("/P"))
                                {
                                    SendUpdateMesageToAllConnectedclients(szReceivedMessage);
                                }
                                else //sometimes it will be at the end so we will remove the first commands which is normaly the dice.
                                {
                                    SendUpdateMesageToAllConnectedclients(szReceivedMessage.Substring(8));
                                }
                            }

                            else if (szReceivedMessage.Contains("/P4")) //if the player 1 has moved location then it will send this command including new position example /P1056;
                            {
                                if (diceposition.StartsWith("/P"))
                                {
                                    SendUpdateMesageToAllConnectedclients(szReceivedMessage);
                                }
                                else //sometimes it will be at the end so we will remove the first commands which is normaly the dice.
                                {
                                    SendUpdateMesageToAllConnectedclients(szReceivedMessage.Substring(8));
                                }
                            }

                            else if ("/iInx:" == szReceivedMessage.Substring(0, 6)) //sends the user their player number to keep synchronisation
                            {
                                String userName = "******" + (iIndex + 1);               // iIndex starts with 0 so we allways add 1;
                                SendUpdateMesageToAllConnectedclients(userName);    // iIndex starts with 0 so we allways add 1;
                                timer1.Start();                                     //if they want they can make it send every time but this is disabled atm.
                            }
                        }
                    }
                    catch (SocketException se)                              // Handle socket-related exception
                    {                                                       // If an exception occurs, display an error message
                        if (10053 == se.ErrorCode || 10054 == se.ErrorCode) // Remote end closed the connection
                        {
                            CloseConnection(iIndex);
                        }
                        else if (10035 != se.ErrorCode)
                        { // Ignore error messages relating to normal behaviour of non-blocking sockets
                            MessageBox.Show(se.Message);
                        }
                    }
                    catch // Silently handle any other exception
                    {
                        Message_textBox.AppendText(szReceivedMessage);
                    }
                }
            }
        }
Example #4
0
 public void AddPlayerGo(PlayerGo p)
 {
     _playerGos.Add(p);
     Debug.Log("player go count = " + _playerGos.Count);
 }
Example #5
0
 public void RemovePlayer()
 {
     _player = null;
 }
Example #6
0
 public void SetPlayer(PlayerGo player)
 {
     _player = player;
 }