예제 #1
0
    public IEnumerator PostHighScoreRequest(string url, HSItem item)
    {
        string json = "{\"user\":\"" + item.user + "\",\"score\":" + item.score + "}";

        byte[] jsonToByte = new System.Text.UTF8Encoding().GetBytes(json);

        using (UnityWebRequest webRequest = UnityWebRequest.Post(webApiUrl, "POST"))
        {
            webRequest.uploadHandler   = (UploadHandler) new UploadHandlerRaw(jsonToByte);
            webRequest.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
            webRequest.SetRequestHeader("Content-Type", "application/json");

            yield return(webRequest.SendWebRequest());

            if (webRequest.isNetworkError || webRequest.isHttpError)
            {
                Debug.Log("ERROR: " + webRequest.error);
            }
            else
            {
                Debug.Log("Request sent");
                UpdateHighScore();
            }
        }
    }
예제 #2
0
    // =======================================================================
    // Add(), lägger till en person i highscore-listan.
    // =======================================================================
    void Add(int points)
    {
        // Skapa en temporär variabel av typen HSItem:
        HSItem temp = new HSItem(name, points);

        // Lägg till tmp i listan. Observera att följande Add()
        // tillhör klassen List (är alltså skapad av Microsoft).
        // Metoden har endast samma namn, som just denna Add():
        highscore.Add(temp);
        Sort(); // Sortera listan efter att vi har lagt till en person!

        // Är det för många i listan?
        if (highscore.Count > maxInList)
        {
            // Eftersom vi har lagt till endast en person nu, så betyder
            // det att det är en person för mycket. Index på personen
            // som är sist i listan, är samma som maxInList. Vi vill ju
            // att det högsta indexet ska vara maxInList-1. Allstå kan
            // vi bara ta bort elementet med index maxInList.
            // Exempel:
            // maxInList är 5, vi har 6 element i listan. Det sjätte
            // elementet har index 5. Vi gör highscore.RemoveAt(5):
            highscore.RemoveAt(maxInList);
        }
    }
예제 #3
0
        void Sort()
        {
            // Sort highscores from highest to lowest
            int max = hsItems.Count - 1;

            for (int i = 0; i < max; i++)
            {
                int nrLeft = max - i;
                for (int j = 0; j < nrLeft; j++)
                {
                    if (hsItems[j].Points < hsItems[j + 1].Points)
                    {
                        HSItem temp = hsItems[j + 1];
                        hsItems[j + 1] = hsItems[j];
                        hsItems[j]     = temp;
                    }
                }
            }

            // Remove excess entries
            if (hsItems.Count > maxInList)
            {
                for (int i = 0; i < hsItems.Count; i++)
                {
                    if (i >= maxInList)
                    {
                        hsItems.Remove(hsItems[i]);
                    }
                }
            }
        }
예제 #4
0
    // =======================================================================
    // LoadFromFile(), ladda från fil.
    // =======================================================================
    public void LoadFromFile(string filename)
    {
        StreamReader sr = new StreamReader(filename);
        string       row;

        while ((row = sr.ReadLine()) != null)
        {
            string[] words  = row.Split(':');
            int      points = Convert.ToInt32(words[1]);
            HSItem   temp   = new HSItem(words[0], points);
            highscore.Add(temp);
        }
        sr.Close();
    }
예제 #5
0
    // =======================================================================
    // Sort(),  metod som sorterar listan. Metoden
    // anropas av Add() när en ny person läggs till i
    // listan. Använder algoritmen bubblesort
    // =======================================================================
    void Sort()
    {
        int max = highscore.Count - 1;

        // Den yttre loopen, går igenom hela listan
        for (int i = 0; i < max; i++)
        {
            // Den inre, går igenom element för element
            int nrLeft = max - i; // För att se hur många som redan gåtts igenom
            for (int j = 0; j < nrLeft; j++)
            {
                if (highscore[j].Points < highscore[j + 1].Points) // Jämför elementen
                {
                    // Byt plats!
                    HSItem temp = highscore[j];
                    highscore[j]     = highscore[j + 1];
                    highscore[j + 1] = temp;
                }
            }
        }
    }
예제 #6
0
    // =======================================================================
    // LoadFromFile(), ladda från fil.
    // =======================================================================
    public void LoadFromFile(string filename)
    {
        StreamReader sr = new StreamReader(filename);

        // Läs in alla rader till string-objektet row och skriv ut dem:
        string row;

        while ((row = sr.ReadLine()) != null)
        {
            // skapa en vektor som innehåller namn och poäng,
            // words[0] blir namnet och words[1] är poängen:
            string[] words  = row.Split(':');
            int      points = Convert.ToInt32(words[1]);

            // Lägg till i listan:
            HSItem temp = new HSItem(words[0], points);
            highscore.Add(temp);
        }

        sr.Close(); // Stäng filen
    }
예제 #7
0
 public void AddHighScore(HSItem item)
 {
     StartCoroutine(PostHighScoreRequest(webApiUrl, item));
 }
예제 #8
0
 public void AssignHSItem(HSItem item)
 {
     userText.text  = item.user;
     scoreText.text = item.score.ToString();
 }