Final changes

This commit is contained in:
KaseToatz1337
2024-11-11 11:28:17 +01:00
parent 4c01b5ae79
commit 83efd9de1f
5 changed files with 24 additions and 27 deletions

View File

@ -17,22 +17,19 @@ namespace Memory.Data
connection.Close();
}
// Check if the score is higher than 10th place
private static bool IsTop10(int points)
// Get the position of a certain score
public int GetScorePosition(int score)
{
using SQLiteConnection connection = new(URI);
connection.Open();
using SQLiteCommand command = new("SELECT Points FROM Scores ORDER BY Points DESC LIMIT 1 OFFSET 9", connection);
using SQLiteCommand command = new("SELECT COUNT(*) + 1 FROM Scores WHERE Points > @Score", connection);
command.Parameters.AddWithValue("@Score", score);
using SQLiteDataReader reader = command.ExecuteReader();
// If there aren't atleast 10 highscores, return true by default
if (!reader.Read())
{
connection.Close();
return true;
return 0;
}
int lowest = reader.GetInt32(0);
connection.Close();
return points > lowest;
return reader.GetInt32(0);
}
// Get the top 10 highscores
@ -41,7 +38,7 @@ namespace Memory.Data
List<Score> scores = [];
using SQLiteConnection connection = new(URI);
connection.Open();
using SQLiteCommand command = new("SELECT Name, Points FROM Scores ORDER BY Points DESC LIMIT 10", connection);
using SQLiteCommand command = new("SELECT Name, Points FROM Scores ORDER BY Points DESC", connection);
using SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
@ -52,20 +49,15 @@ namespace Memory.Data
}
// Write the new score to the database
public bool WriteScore(Score score)
public void WriteScore(Score score)
{
if (IsTop10(score.Points))
{
using SQLiteConnection connection = new(URI);
connection.Open();
using SQLiteCommand command = new("INSERT INTO Scores(Name, Points) VALUES(@Name, @Points)", connection);
command.Parameters.AddWithValue("@Name", score.Name);
command.Parameters.AddWithValue("@Points", score.Points);
command.ExecuteNonQuery();
connection.Close();
return true;
}
return false;
using SQLiteConnection connection = new(URI);
connection.Open();
using SQLiteCommand command = new("INSERT INTO Scores(Name, Points) VALUES(@Name, @Points)", connection);
command.Parameters.AddWithValue("@Name", score.Name);
command.Parameters.AddWithValue("@Points", score.Points);
command.ExecuteNonQuery();
connection.Close();
}
}
}