Example #1
0
    bool sendData(JengaMatchMakerMessage data, JengaUser u = null)
    {
        if (udp == null)
        {
            return(false);
        }

        try {
            string json        = JsonUtility.ToJson(data);
            byte[] send_buffer = System.Text.Encoding.ASCII.GetBytes(json);

            IPEndPoint e = ep;
            if (u != null)
            {
                e = new IPEndPoint(IPAddress.Parse(u.ip), u.port);
            }

            int res = udp.Send(send_buffer, send_buffer.Length, e);
            if (res != send_buffer.Length)
            {
                return(false);
            }

            return(true);
        } catch (System.Exception e) {
            Debug.LogError(e.Message);
            return(false);
        }
    }
Example #2
0
    public void cancelMatch()
    {
        panelWaiting.SetActive(false);
        JengaMatchMakerMessage jmmm = new JengaMatchMakerMessage();

        jmmm.cmd = "cancel";
        sendData(jmmm, userSelected);
    }
Example #3
0
    public void rejectMatch()
    {
        JengaMatchMakerMessage jmmm = new JengaMatchMakerMessage();

        jmmm.cmd = "reject";
        sendData(jmmm, userSelected);
        panelInvitation.SetActive(false);
    }
Example #4
0
    public void Leave()
    {
        JengaMatchMakerMessage jmmm = new JengaMatchMakerMessage();

        jmmm.cmd       = "leave";
        jmmm.user      = new JengaUser();
        jmmm.user.name = inputName.text;
        jmmm.user.type = InteractionType;
        sendData(jmmm);
    }
Example #5
0
 public void acceptMatch()
 {
     if (!loading)
     {
         JengaMatchMakerMessage jmmm = new JengaMatchMakerMessage();
         jmmm.cmd  = "accept";
         jmmm.user = localUser;
         sendData(jmmm, userSelected);
     }
 }
Example #6
0
    void sendKeepAlive()
    {
        if (loading)
        {
            return;
        }

        JengaMatchMakerMessage jmmm = new JengaMatchMakerMessage();

        jmmm.cmd = "keepAlive";
        sendData(jmmm);
    }
Example #7
0
    public void Login()
    {
        PlayerPrefs.SetString("userName", inputName.text);

        JengaMatchMakerMessage jmmm = new JengaMatchMakerMessage();

        jmmm.cmd       = "join";
        localUser      = new JengaUser();
        localUser      = new JengaUser();
        localUser.name = inputName.text;
        localUser.type = InteractionType;
        jmmm.user      = localUser;
        sendData(jmmm);
        InvokeRepeating("sendKeepAlive", 1, 10);
    }
Example #8
0
    public void startMatch()
    {
        JengaUserButton[] jubs = usersContainer.gameObject.GetComponentsInChildren <JengaUserButton>();
        foreach (JengaUserButton j in jubs)
        {
            Toggle t = j.gameObject.GetComponent <Toggle>();
            if (t.isOn)
            {
                userSelected = j.user;
                panelWaiting.SetActive(true);
                labelWaiting.text = "Waiting the answer from " + userSelected.name + "\n" + userSelected.type;

                JengaMatchMakerMessage jmmm = new JengaMatchMakerMessage();
                jmmm.cmd       = "playInvitation";
                jmmm.user      = new JengaUser();
                jmmm.user.name = inputName.text;
                jmmm.user.type = InteractionType;
                sendData(jmmm, userSelected);
                PlayerPrefs.SetInt("Invited", 0);
                break;
            }
        }
    }
Example #9
0
    // Update is called once per frame
    void Update()
    {
        // Check if loading new scene
        if (loading)
        {
            if (!IsInvoking("loadJengaGame"))
            {
                Invoke("loadJengaGame", 1.0f);
            }
            return;
        }

        // Get the current users list
        JengaUserButton[] jubs = usersContainer.gameObject.GetComponentsInChildren <JengaUserButton>();

        // if udp is not working properly, then not continue
        if ((udp == null) || (ep == null) || (udp.Client == null))
        {
            return;
        }

        // set a very low timeout to avoid waiting time in update
        udp.Client.ReceiveTimeout = 5;
        try {
            // try to load 20 packets to avoid acumulation of the information
            for (int i = 0; i < 20; i++)
            {
                if (udp.Client.Available <= 0)
                {
                    break;
                }

                IPEndPoint ep2   = new IPEndPoint(IPAddress.Any, 0);
                byte[]     bytes = udp.Receive(ref ep2);

                string msg = System.Text.Encoding.ASCII.GetString(bytes);
                JengaMatchMakerMessage jmmm = JsonUtility.FromJson <JengaMatchMakerMessage>(msg);

                if (jmmm.cmd == "userList")
                {
                    ToggleGroup tg = usersContainer.GetComponent <ToggleGroup>();
                    foreach (JengaUser u in jmmm.users)
                    {
                        // Search the user
                        JengaUserButton ju = null;
                        foreach (JengaUserButton j in jubs)
                        {
                            if ((j.user.ip == u.ip) && (j.user.port == u.port))
                            {
                                ju = j;
                                break;
                            }
                        }

                        // Instantiate new user button
                        if (ju == null)
                        {
                            GameObject button = Instantiate(userButton, usersContainer.transform);
                            ju = button.GetComponent <JengaUserButton>();
                            Toggle t = button.GetComponent <Toggle>();
                            t.group = tg;
                        }

                        // Set user data to the button
                        ju.setUser(u);
                    }
                }

                // Invitation to play received
                else if (jmmm.cmd == "playInvitation")
                {
                    Debug.Log("Play invitation");
                    panelInvitation.SetActive(true);
                    userSelected         = jmmm.user;
                    userSelected.ip      = ep2.Address.ToString();
                    userSelected.port    = ep2.Port;
                    labelInvitation.text = "Do you want to play with " + userSelected.name + "?\n" + userSelected.type;
                    PlayerPrefs.SetInt("Invited", 1);
                }

                // Invitation to play accepted
                else if (jmmm.cmd == "accept")
                {
                    acceptMatch();
                    acceptedMatch();
                }

                // Invitation rejected
                else if (jmmm.cmd == "reject")
                {
                    panelWaiting.SetActive(false);
                }

                // Invitation canceled
                else if (jmmm.cmd == "cancel")
                {
                    panelInvitation.SetActive(false);
                }
            }
        } catch (SocketException e) {
            // if (e.SocketErrorCode != SocketError.TimedOut)
            //     udp = null;
        }

        // Clean old users
        foreach (JengaUserButton j in jubs)
        {
            if ((Time.time - j.user.lastUpdate) > 15.0f)
            {
                Destroy(j.gameObject, 0.3f);
            }
        }
    }