Fixed all requirements
This commit is contained in:
@ -7,6 +7,22 @@ namespace Memory.Data
|
||||
{
|
||||
public const string URI = "Data Source=Scores.db;Version=3;";
|
||||
|
||||
private 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 (!reader.Read())
|
||||
{
|
||||
connection.Close();
|
||||
return true;
|
||||
}
|
||||
int lowest = reader.GetInt32(0);
|
||||
connection.Close();
|
||||
return points > lowest;
|
||||
}
|
||||
|
||||
public ScoreHandler()
|
||||
{
|
||||
using SQLiteConnection connection = new(URI);
|
||||
@ -22,26 +38,29 @@ namespace Memory.Data
|
||||
using SQLiteConnection connection = new(URI);
|
||||
connection.Open();
|
||||
using SQLiteCommand command = new("SELECT Name, Points FROM Scores ORDER BY Points DESC LIMIT 10", connection);
|
||||
using (SQLiteDataReader reader = command.ExecuteReader())
|
||||
using SQLiteDataReader reader = command.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
scores.Add(new(reader.GetString(0), reader.GetInt32(1)));
|
||||
}
|
||||
scores.Add(new(reader.GetString(0), reader.GetInt32(1)));
|
||||
}
|
||||
connection.Close();
|
||||
return scores;
|
||||
}
|
||||
|
||||
public void WriteScore(Score score)
|
||||
public bool WriteScore(Score score)
|
||||
{
|
||||
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();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user