Final changes
This commit is contained in:
@ -102,7 +102,7 @@ namespace Memory.Cmd
|
|||||||
// Print the players score to the console then loop over highscores to print those aswell
|
// Print the players score to the console then loop over highscores to print those aswell
|
||||||
Console.WriteLine($"Your score: {game.Scoring.Points}\nTop 10 Highscores:");
|
Console.WriteLine($"Your score: {game.Scoring.Points}\nTop 10 Highscores:");
|
||||||
List<Score> highscores = game.ScoreHandler.GetTopScores();
|
List<Score> highscores = game.ScoreHandler.GetTopScores();
|
||||||
for (int i = 0; i < highscores.Count; i++)
|
for (int i = 0; i < Math.Min(Game.LEADERBOARDSIZE, highscores.Count); i++)
|
||||||
{
|
{
|
||||||
Score score = highscores[i];
|
Score score = highscores[i];
|
||||||
Console.WriteLine($"{i+1}. {score.Name}: {score.Points}");
|
Console.WriteLine($"{i+1}. {score.Name}: {score.Points}");
|
||||||
|
@ -17,22 +17,19 @@ namespace Memory.Data
|
|||||||
connection.Close();
|
connection.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the score is higher than 10th place
|
// Get the position of a certain score
|
||||||
private static bool IsTop10(int points)
|
public int GetScorePosition(int score)
|
||||||
{
|
{
|
||||||
using SQLiteConnection connection = new(URI);
|
using SQLiteConnection connection = new(URI);
|
||||||
connection.Open();
|
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();
|
using SQLiteDataReader reader = command.ExecuteReader();
|
||||||
// If there aren't atleast 10 highscores, return true by default
|
|
||||||
if (!reader.Read())
|
if (!reader.Read())
|
||||||
{
|
{
|
||||||
connection.Close();
|
return 0;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
int lowest = reader.GetInt32(0);
|
return reader.GetInt32(0);
|
||||||
connection.Close();
|
|
||||||
return points > lowest;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the top 10 highscores
|
// Get the top 10 highscores
|
||||||
@ -41,7 +38,7 @@ namespace Memory.Data
|
|||||||
List<Score> scores = [];
|
List<Score> scores = [];
|
||||||
using SQLiteConnection connection = new(URI);
|
using SQLiteConnection connection = new(URI);
|
||||||
connection.Open();
|
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();
|
using SQLiteDataReader reader = command.ExecuteReader();
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
@ -52,9 +49,7 @@ namespace Memory.Data
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write the new score to the database
|
// 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);
|
using SQLiteConnection connection = new(URI);
|
||||||
connection.Open();
|
connection.Open();
|
||||||
@ -63,9 +58,6 @@ namespace Memory.Data
|
|||||||
command.Parameters.AddWithValue("@Points", score.Points);
|
command.Parameters.AddWithValue("@Points", score.Points);
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
connection.Close();
|
connection.Close();
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ namespace Memory.Gui
|
|||||||
Highscores.Children.Clear();
|
Highscores.Children.Clear();
|
||||||
OwnScore.Content = $"Your score: {game!.Scoring.Points}";
|
OwnScore.Content = $"Your score: {game!.Scoring.Points}";
|
||||||
List<Score> highscores = game.ScoreHandler.GetTopScores();
|
List<Score> highscores = game.ScoreHandler.GetTopScores();
|
||||||
for (int i = 0; i < highscores.Count; i++)
|
for (int i = 0; i < Math.Min(Game.LEADERBOARDSIZE, highscores.Count); i++)
|
||||||
{
|
{
|
||||||
Score score = highscores[i];
|
Score score = highscores[i];
|
||||||
Label label = new()
|
Label label = new()
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
public const int MINPOINTS = 10;
|
public const int MINPOINTS = 10;
|
||||||
public const int MAXTIME = 10000;
|
public const int MAXTIME = 10000;
|
||||||
public const int STARTSCORE = 100;
|
public const int STARTSCORE = 100;
|
||||||
|
public const int LEADERBOARDSIZE = 10;
|
||||||
|
|
||||||
public int DeckSize { get; } = deckSize;
|
public int DeckSize { get; } = deckSize;
|
||||||
public int GridSize { get; } = GetGridSize(deckSize).Rows;
|
public int GridSize { get; } = GetGridSize(deckSize).Rows;
|
||||||
@ -93,6 +94,8 @@
|
|||||||
if (IsFinished())
|
if (IsFinished())
|
||||||
{
|
{
|
||||||
CalculateScore(DateTimeOffset.Now.ToUnixTimeMilliseconds());
|
CalculateScore(DateTimeOffset.Now.ToUnixTimeMilliseconds());
|
||||||
|
if (ScoreHandler.GetScorePosition(Scoring.Points) <= LEADERBOARDSIZE)
|
||||||
|
{
|
||||||
ScoreHandler.WriteScore(Scoring);
|
ScoreHandler.WriteScore(Scoring);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,3 +104,4 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
{
|
{
|
||||||
public interface IScoreHandler
|
public interface IScoreHandler
|
||||||
{
|
{
|
||||||
public bool WriteScore(Score score);
|
public int GetScorePosition(int score);
|
||||||
|
public void WriteScore(Score score);
|
||||||
public List<Score> GetTopScores();
|
public List<Score> GetTopScores();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user