Exemple #1
0
        /// <summary>
        /// Subprogram to update leaderboard data
        /// </summary>
        /// <param name="newEntry">The entry to be added</param>
        public static void UpdateLeaderboardData(LeaderboardEntry newEntry)
        {
            //Determining position of entry and making adjustments as needed
            for (byte i = 0; i < leaderboard.Length; i++)
            {
                if (newEntry.Score > leaderboard[i].Score || (newEntry.Score == leaderboard[i].Score && newEntry.Time <= leaderboard[i].Time))
                {
                    for (byte adjustNo = (byte)(leaderboard.Length - 1); adjustNo > i; adjustNo--)
                    {
                        leaderboard[adjustNo]   = leaderboard[adjustNo - 1];
                        infoLocs[adjustNo, 1].X = infoLocs[adjustNo - 1, 1].X;
                        infoLocs[adjustNo, 2].X = infoLocs[adjustNo - 1, 2].X;
                        infoLocs[adjustNo, 3].X = infoLocs[adjustNo - 1, 3].X;
                    }

                    //Adding entry to leaderboard
                    leaderboard[i] = newEntry;
                    infoLocs[i, 1] = new Vector2(300 - infoFont.MeasureString(leaderboard[i].Name).X / 2, 165 + 50 * i);
                    infoLocs[i, 2] = new Vector2(500 - infoFont.MeasureString($"{leaderboard[i].Score}").X / 2, 165 + 50 * i);
                    infoLocs[i, 3] = new Vector2(700 - infoFont.MeasureString($"{leaderboard[i].Time}").X / 2, 165 + 50 * i);

                    //Breaking out of check loop
                    break;
                }
            }

            //Uploading data to file
            IO.UploadLeaderboardData(leaderboard);
        }
        /// <summary>
        /// Subprogram to load in leaderboard data
        /// </summary>
        private static void LoadLeaderboardData()
        {
            //Try-Catch block to handle file reading exceptions
            try
            {
                //Opening file
                inFile = File.OpenText(leaderboardDataPath);

                //Loading in leaderboard data
                LoadedLeaderboard = new LeaderboardEntry[5];
                for (int i = 0; i < LoadedLeaderboard.Length; i++)
                {
                    dataLine             = inFile.ReadLine();
                    dataLineParts        = dataLine.Split();
                    LoadedLeaderboard[i] = new LeaderboardEntry(dataLineParts[0], Convert.ToByte(dataLineParts[1]), Convert.ToDouble(dataLineParts[2]));
                }

                //Closing file
                inFile.Close();
            }
            catch (FileNotFoundException)
            {
                //Informing user that file was not found
                Console.WriteLine("Exception: File was not found");
            }
            catch (IndexOutOfRangeException)
            {
                //Informing user that index was out of range
                Console.WriteLine("Exception: Index was out of range");
            }
            catch (FormatException)
            {
                //Informing user that their data was read in via wrong format
                Console.WriteLine("Exception: Data was read in wrong format");
            }
            catch (EndOfStreamException)
            {
                //Informing user that file was attempted to be read past end of stream
                Console.WriteLine("Exception: Attempted to read past end of file");
            }
        }