Beispiel #1
0
    static public JSONScores getJSONScores()
    {
        try
        {
            string filePath = Path.Combine(Application.persistentDataPath, "Levels/scores");
            if (File.Exists(filePath))
            {
                string     dataAsJson = File.ReadAllText(filePath);
                JSONScores loadedData = JsonUtility.FromJson <JSONScores>(dataAsJson);

                Debug.Log(dataAsJson);
                Debug.Log(JsonUtility.ToJson(loadedData));
                return(loadedData);
            }
            JSONScores result = new JSONScores();
            result.scores = new List <JSONScore>();
            return(result);
        }
        catch (Exception e)
        {
            Debug.LogException(e);
            Debug.Log("Can't find files");
            return(null);
        }
    }
Beispiel #2
0
 static public bool setJSONScores(JSONScores jSONScores)
 {
     try
     {
         string filePath = Path.Combine(Application.persistentDataPath, "Levels/scores");
         File.WriteAllText(filePath, JsonUtility.ToJson(jSONScores));
         return(true);
     }
     catch (Exception e)
     {
         Debug.LogException(e);
         Debug.Log("Can't find files");
         return(false);
     }
 }
Beispiel #3
0
 static public JSONScore getJSONScore(long id)
 {
     try
     {
         string filePath = Path.Combine(Application.persistentDataPath, "Levels/scores");
         if (File.Exists(filePath))
         {
             string     dataAsJson = File.ReadAllText(filePath);
             JSONScores loadedData = JsonUtility.FromJson <JSONScores>(dataAsJson);
             JSONScore  cur        = loadedData.scores.Find(x => x.id == id);
             Debug.Log(dataAsJson);
             Debug.Log(JsonUtility.ToJson(loadedData));
             return(cur);
         }
         return(null);
     }
     catch (Exception e)
     {
         Debug.LogException(e);
         Debug.Log("Can't find files");
         return(null);
     }
 }
Beispiel #4
0
    static public bool addScore(List <JSONScore> jSONScores)
    {
        bool scored = false;

        try
        {
            JSONScores tmp = getJSONScores();
            foreach (var scr in jSONScores)
            {
                JSONScore cur = tmp.scores.Find(x => x.id == scr.id);
                if (cur != null)
                {
                    if (cur.points <= scr.points && cur.seconds >= scr.seconds)
                    {
                        cur.points  = scr.points;
                        cur.seconds = scr.seconds;
                        scored      = true;
                    }
                }
                else
                {
                    tmp.scores.Add(scr);
                    scored = true;
                }
            }

            string filePath = Path.Combine(Application.persistentDataPath, "Levels/scores");
            File.WriteAllText(filePath, JsonUtility.ToJson(tmp));
            return(scored);
        }
        catch (Exception e)
        {
            Debug.LogException(e);
            Debug.Log("Can't find files");
            return(false);
        }
    }