Add comments

This commit is contained in:
KaseToatz1337
2024-11-07 10:18:20 +01:00
parent 32055107ab
commit 4c01b5ae79
5 changed files with 43 additions and 11 deletions

View File

@ -7,12 +7,24 @@ namespace Memory.Data
{
public const string URI = "Data Source=Scores.db;Version=3;";
private bool IsTop10(int points)
// Ensure the database and table exist upon ScoreHandler creation
public ScoreHandler()
{
using SQLiteConnection connection = new(URI);
connection.Open();
using SQLiteCommand command = new("CREATE TABLE IF NOT EXISTS Scores(ID INTEGER PRIMARY KEY, Name TEXT, Points INTEGER)", connection);
command.ExecuteNonQuery();
connection.Close();
}
// Check if the score is higher than 10th place
private static bool IsTop10(int points)
{
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 SQLiteDataReader reader = command.ExecuteReader();
// If there aren't atleast 10 highscores, return true by default
if (!reader.Read())
{
connection.Close();
@ -23,15 +35,7 @@ namespace Memory.Data
return points > lowest;
}
public ScoreHandler()
{
using SQLiteConnection connection = new(URI);
connection.Open();
using SQLiteCommand command = new("CREATE TABLE IF NOT EXISTS Scores(ID INTEGER PRIMARY KEY, Name TEXT, Points INTEGER)", connection);
command.ExecuteNonQuery();
connection.Close();
}
// Get the top 10 highscores
public List<Score> GetTopScores()
{
List<Score> scores = [];
@ -47,6 +51,7 @@ namespace Memory.Data
return scores;
}
// Write the new score to the database
public bool WriteScore(Score score)
{
if (IsTop10(score.Points))