Example #1
0
    public void SubmitConfirm_OnClick()
    {
        // Deactive input field and buttons until server response
        GameObject Background_Canvas = GameObject.Find("Background_Canvas");

        Set_InteractableSubmitScore(false);
        Set_Error(false);

        // Send high score to server
        string Name       = Background_Canvas.transform.GetChild(14).GetComponent <InputField>().text;
        int    High_Score = Data_Manager.Get_HighScore();

        // Check string name is null
        if (string.IsNullOrEmpty(Name.Replace(" ", "")))
        {
            // Name error
            Button_OnClick.Set_InteractableSubmitScore(true);
            Button_OnClick.Set_Error(true, "enter your name");
        }
        else if (Name.IndexOf("'") >= 0 || Name.IndexOf("|") >= 0)
        {
            // Correct name error
            Button_OnClick.Set_InteractableSubmitScore(true);
            Button_OnClick.Set_Error(true, "enter correct name");
        }
        else
        {
            GameObject.Find("Game_Controller").GetComponent <Submit_Score>().Post_Score(Name, High_Score);
        }
    }
    IEnumerator Post(SubmitInfo Info)
    {
        SubmitCallBack Call_Back;

        // Set server url and data to send
        string Submit_Url = Url + "/YourWebPage";

        // Post request to server
        var Request = new UnityWebRequest(Submit_Url, "POST");

        byte[] Body_Raw = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Info));
        Request.uploadHandler   = (UploadHandler) new UploadHandlerRaw(Body_Raw);
        Request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        Request.SetRequestHeader("Content-Type", "application/json");
        yield return(Request.Send());

        if (Request.responseCode == 200)
        {
            string Content = Request.downloadHandler.text;
            Call_Back = JsonConvert.DeserializeObject <SubmitCallBack>(Content);

            if (Call_Back.Your_CallBack == "Insert or Update your information")
            {
                // Save informations
                File_Manager.Save_Info();

                // Deactive submit score menu and active ten highscore
                Button_OnClick.Set_SubmitScoreMenu(false);
                Button_OnClick.Set_TopTenMenu(true);

                StartCoroutine(TopTen_Records(Info));
            }
            else if (Call_Back.Your_CallBack == "Name is taken")
            {
                // Server error
                Button_OnClick.Set_InteractableSubmitScore(true);
                Button_OnClick.Set_Error(true, "this name is taken");
            }
            else
            {
                // Server error
                Button_OnClick.Set_InteractableSubmitScore(true);
                Button_OnClick.Set_Error(true, "server error");
            }
        }
        else
        {
            // Server error
            Button_OnClick.Set_InteractableSubmitScore(true);
            Button_OnClick.Set_Error(true, "server error");
        }
    }
    IEnumerator TopTen_Records(SubmitInfo Info)
    {
        List <UserRecord> Records = new List <UserRecord>();

        // Set server url and data to send
        string Records_Url = Url + "/YourWebPage";

        // Post request to server
        var Request = new UnityWebRequest(Records_Url, "POST");

        byte[] Body_Raw = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Info));
        Request.uploadHandler   = (UploadHandler) new UploadHandlerRaw(Body_Raw);
        Request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        Request.SetRequestHeader("Content-Type", "application/json");
        yield return(Request.Send());

        if (Request.responseCode == 200)
        {
            string Content = Request.downloadHandler.text;
            Records = JsonConvert.DeserializeObject <List <UserRecord> >(Content);

            if (Records[0].Player_Information == "your server has error")
            {
                Button_OnClick.Set_RecordMessage("server error");
                Button_OnClick.Set_RecordLoading(false);
            }
            else if (Records[0].Player_Information == "There are no player yet")
            {
                Button_OnClick.Set_RecordMessage("list is empty!");
                Button_OnClick.Set_RecordLoading(false);
            }
            else
            {
                // Show records
                for (int i = 0; i < Records.Count - 1; i++)
                {
                    Button_OnClick.Set_RecordListView(i, (i + 1) + ". " + Records[i].Player_Information, Records[i].Player_HighScore);
                }

                // Set player record
                Button_OnClick.Set_RecordMessage(Records[Records.Count - 1].Player_Information + ": " + Records[Records.Count - 1].Player_HighScore);
            }
        }
        else
        {
            // Server error
            Button_OnClick.Set_RecordMessage("server error");
            Button_OnClick.Set_RecordLoading(false);
        }
    }
    void Set_GameOver()
    {
        if (Data_Manager.Get_HighScore() < Score)
        {
            Data_Manager.Set_HighScore(Score);
        }

        Txt_GameOverScore.text     = Score.ToString();
        Txt_GameOverHighsocre.text = Data_Manager.Get_HighScore().ToString();
        GameObject Background_Canvas = GameObject.Find("Background_Canvas");

        // Active game over menu
        Button_OnClick.Set_GameOverMenu(true);
        SceneManager.LoadScene("In_Game");
        // Enable animation
        Background_Canvas.GetComponent <Animator>().enabled = false;

        // Save file
        // File_Manager.Save_Info();
    }