/// <summary> /// Return the highscore for the required level /// </summary> /// <param name="map"></param> /// <returns></returns> public ScoreLine GetBestScoreForLevel(String map, ScoreType scoreType) { try { ScoreLine[] result = null; if (save.ScoresBylevel[scoreType].TryGetValue(map, out result)) { return(result[0]); } else { save.AddScore(map, scoreType, ScoreLine.GetDefaultScoreLine(1)); return(GetBestScoreForLevel(map, scoreType)); } } catch (IndexOutOfRangeException) { this.save = new SaveData(); this.Save(); //This exception is throwed only if you have an old savegame //For now I just reset scores and options throw new Exception(LocalizedStrings.GetString("CorruptedSavegame")); } }
/// <summary> /// Add a new score to the level scoreboard /// </summary> /// <param name="map">map name</param> /// <param name="scoreType"></param> /// <param name="newScoreLine"></param> /// <returns>The rank of the score</returns> public Int32 AddScore(String map, ScoreType scoreType, ScoreLine newScoreLine) { int rank = ScoreLineNumber + 1; bool replace = false; ScoreLine[] scoresForThisLevel = null; //Access to the score for this level and this mode SerializableDictionary <String, ScoreLine[]> score = null; if (ScoresBylevel.TryGetValue(scoreType, out score)) { if (score.TryGetValue(map, out scoresForThisLevel)) { for (int scoreLineIndex = 0; scoreLineIndex < scoresForThisLevel.Length; scoreLineIndex++) { ScoreLine currentLine = scoresForThisLevel[scoreLineIndex]; //Look if scores in the top ScoreLineNumber lines if (newScoreLine.Score > currentLine.Score) { replace = true; } else { if (newScoreLine.Score == currentLine.Score) { if (newScoreLine.Date > currentLine.Date) { replace = true; } } } if (replace) { rank = currentLine.Rank; //HACK Avoid corrupted savegames if (rank == -1) { rank = 20; } newScoreLine.Rank = rank; ScoreLine last = null; ScoreLine toReplace = newScoreLine; //Move down new lines for (int i = scoreLineIndex; i < scoresForThisLevel.Length - 1; i++) { last = scoresForThisLevel[i]; scoresForThisLevel[i] = toReplace; toReplace = last; toReplace.Rank += 1; } break; } } } else { //Create scores data for this map scoresForThisLevel = new ScoreLine[ScoreLineNumber]; for (int i = 0; i < scoresForThisLevel.Length; i++) { scoresForThisLevel[i] = ScoreLine.GetDefaultScoreLine(i + 1); } //Player has the best score scoresForThisLevel[0] = newScoreLine; ScoresBylevel[scoreType].Add(map, scoresForThisLevel); } } return(rank); }