Пример #1
0
        public void loadFromFile()
        {
            fileReader = new StreamReader(filePath); //Open file
            string jsonString = fileReader.ReadLine(); //file contains one line only by design (ends with \n) - json formatting
            fileReader.Close(); //Close file - avoid file exceptions
            fileReader.Dispose();
            if (jsonString != null && jsonString.Length > 0) //If string exist
            {
                JObject jsonObject = JObject.Parse(jsonString); //Parse string into jsonobject for easier manipulation
                JArray users = jsonObject.GetValue("users") as JArray; //Get user array from jsonobject
                if (users != null)
                {
                    foreach (JObject u in users) //Add users to users list
                    {
                        User user = new User(u.GetValue("username").ToString());
                        JArray score = u.GetValue("scoreboard") as JArray;
                        user.loadScoreboardArray(score.ToObject<int[]>());
                        bool userExcist = false; //If user exist - refresh scoreboard
                        foreach (User ue in this.users)
                        {
                            if (ue.getUsername().CompareTo(user.getUsername()) == 0)
                            {
                                userExcist = true;
                                ue.loadScoreboardArray(score.ToObject<int[]>());
                                break;
                            }
                        }
                        if (!userExcist) //If user doesent exist - add
                        {
                            this.users.Add(user);
                        }

                    }
                }
            }
        }