Exemple #1
0
    private void CheckConditions( )
    {
        if (!_playerLost)
        {
            if (IsWinCondition( ))
            {
                ClearPlanets( );

                WeaponManager.Instance.Clear( );

                OnWin?.Invoke( );
            }
            else if (IsLooseCondition( ))
            {
                _playerLost = true;
                ClearPlanets( );

                WeaponManager.Instance.Clear( );
                OnLoose?.Invoke( );
            }
        }
    }
Exemple #2
0
    public void ListenForData(string ip, int port, string flag, string peer)
    {
        Debug.Log("Listening Started....");

        Byte[] bytes = new Byte[1024];
        try
        {
            while (socketConnection.Connected)
            {
                try
                {
                    // Get a stream object for reading
                    using (NetworkStream stream = socketConnection.GetStream())
                    {
                        int length;
                        // Read incomming stream into byte arrary.
                        while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            var incommingData = new byte[length];
                            Array.Copy(bytes, 0, incommingData, 0, length);
                            string serverMessage = Encoding.UTF8.GetString(incommingData);

                            if (serverMessage.Contains("_PEER_REQ_"))
                            {
                                OnPeerRequest?.Invoke(serverMessage.Substring(10));
                            }
                            else if (serverMessage == "#PEER DECLINED")
                            {
                                OnPeerDeclined?.Invoke(serverMessage);
                            }
                            else if (serverMessage == "#WON")
                            {
                                OnWin?.Invoke(serverMessage);
                            }
                            else if (serverMessage == "#DRAW")
                            {
                                OnDraw?.Invoke(serverMessage);
                            }
                            else if (serverMessage == "#LOOSE")
                            {
                                OnLoose?.Invoke(serverMessage);
                            }
                            else if (serverMessage == " ")
                            {
                                Debug.Log(serverMessage + " --> " + socketConnection.Client.RemoteEndPoint);
                            }
                            else
                            {
                                try
                                {
                                    JSONObject js = new JSONObject(serverMessage);
                                    Debug.Log("JSON --- > " + js.ToString());

                                    if (js.HasField("IP"))
                                    {
                                        //Connect to GameServer
                                        OnGameServerCredentialsRecieved?.Invoke(js);
                                    }
                                    else if (js.HasField("_BOARD_"))
                                    {
                                        OnBoardData?.Invoke(js);
                                    }
                                }
                                catch (Exception e)
                                { Debug.Log("Exception: " + e); }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.Log("NETWORK STREAM READ : Exception");
                }
            }
        }
        catch (SocketException socketException)
        {
            Debug.Log("Socket exception: " + socketException);
        }

        if (OnListenDataEnded != null)
        {
            OnListenDataEnded("Listening Ended");
        }
    }
Exemple #3
0
 public static void Loose()
 {
     OnLoose?.Invoke();
 }