It's working
This commit is contained in:
parent
71b107cc4e
commit
02e8de2a67
1 changed files with 126 additions and 10 deletions
|
|
@ -26,7 +26,13 @@ public static class HighscoreProcessing
|
|||
/// <param name="filePath">Path to write to</param>
|
||||
public static void WriteOutputFileAndPrint(GameScore[] highscore, string filePath)
|
||||
{
|
||||
// TODO
|
||||
string[] lines = new string[highscore.Length + 1];
|
||||
lines[0] = "Score;Date;Player";
|
||||
for (int i = 0; i < highscore.Length; i++)
|
||||
{
|
||||
lines[i + 1] = $"{highscore[i].Score};{highscore[i].Date:dd.MM.yyyy};{highscore[i].Player.NickName} (#{highscore[i].Player.Id})";
|
||||
}
|
||||
File.WriteAllLines(filePath, lines);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -37,8 +43,36 @@ public static class HighscoreProcessing
|
|||
/// <returns>Array of parsed items; empty array if file does not exist or is empty</returns>
|
||||
public static GameScore[] LoadScoresFromFile(string filePath)
|
||||
{
|
||||
// TODO
|
||||
return [];
|
||||
if (!Path.Exists(filePath))
|
||||
{
|
||||
return Array.Empty<GameScore>();
|
||||
}
|
||||
|
||||
string[] loadedScores = File.ReadAllLines(filePath);
|
||||
if (loadedScores.Length <= 1)
|
||||
{
|
||||
return Array.Empty<GameScore>();
|
||||
}
|
||||
loadedScores = loadedScores[1..];
|
||||
GameScore?[] gameScores = new GameScore[loadedScores.Length];
|
||||
for (int i = 0; i < loadedScores.Length; i++)
|
||||
{
|
||||
if (TryParseGameScore(loadedScores[i], out GameScore? gameScore))
|
||||
{
|
||||
if(gameScore!.Player.Id < 0 || gameScore.Score < 0)
|
||||
{
|
||||
gameScores[i] = null;
|
||||
continue;
|
||||
}
|
||||
gameScores[i] = gameScore!;
|
||||
}
|
||||
else
|
||||
{
|
||||
gameScores[i] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return TrimArray(gameScores);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -49,11 +83,28 @@ public static class HighscoreProcessing
|
|||
/// <returns>True if parsed successfully; false otherwise</returns>
|
||||
public static bool TryParseGameScore(string line, out GameScore? gameScore)
|
||||
{
|
||||
// TODO
|
||||
gameScore = null;
|
||||
|
||||
string[] parts = line.Split(Separator);
|
||||
if (parts.Length != 4)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!int.TryParse(parts[0], out int id) || !DateTime.TryParse(parts[2], out DateTime date) ||
|
||||
!int.TryParse(parts[3], out int score))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(parts[1] == string.Empty || score < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
gameScore = new GameScore(new Player(id, parts[1]), score, date);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Based on the provided full set of game scores for each user the best score is determined.
|
||||
/// If a user has two or more entries with the same score the oldest (earliest) entry is considered to
|
||||
|
|
@ -63,8 +114,35 @@ public static class HighscoreProcessing
|
|||
/// <returns>An array containing the highest score of each user, based on passed scores</returns>
|
||||
public static GameScore[] FindBestScorePerUser(GameScore[] allScores)
|
||||
{
|
||||
// TODO
|
||||
return [];
|
||||
GameScore[] sortedScored = allScores;
|
||||
SortGameScores(sortedScored);
|
||||
sortedScored = TrimScorePerUser(sortedScored);
|
||||
return sortedScored;
|
||||
}
|
||||
|
||||
private static GameScore[] TrimScorePerUser(GameScore?[] scores)
|
||||
{
|
||||
for (int i = 0; i < scores.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < scores.Length; j++)
|
||||
{
|
||||
if (scores[i]?.Player.Id == scores[j]?.Player.Id && i != j)
|
||||
{
|
||||
if(scores[i] == null || scores[j] == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (scores[i]!.Score < scores[j]!.Score ||
|
||||
(scores[i]!.Score == scores[j]!.Score && scores[i]!.Date > scores[j]!.Date))
|
||||
{
|
||||
(scores[i], scores[j]) = (scores[j], scores[i]);
|
||||
}
|
||||
scores[j] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return TrimArray(scores);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -75,7 +153,20 @@ public static class HighscoreProcessing
|
|||
/// <param name="scores">Scores to sort</param>
|
||||
public static void SortGameScores(GameScore[] scores)
|
||||
{
|
||||
// TODO
|
||||
for (int i = 0; i < scores.Length; i++)
|
||||
{
|
||||
for (int j = i + 1; j < scores.Length; j++)
|
||||
{
|
||||
if (scores[i].Score < scores[j].Score)
|
||||
{
|
||||
(scores[i], scores[j]) = (scores[j], scores[i]);
|
||||
}
|
||||
else if (scores[i].Score == scores[j].Score && scores[i].Date > scores[j].Date)
|
||||
{
|
||||
(scores[i], scores[j]) = (scores[j], scores[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -85,7 +176,32 @@ public static class HighscoreProcessing
|
|||
/// <returns>A new array containing only the non-null entries from the passed array</returns>
|
||||
public static GameScore[] TrimArray(GameScore?[] arrayToTrim)
|
||||
{
|
||||
// TODO
|
||||
return [];
|
||||
if (arrayToTrim.Length == 0)
|
||||
{
|
||||
return Array.Empty<GameScore>();
|
||||
}
|
||||
int index = 0;
|
||||
foreach (var VARIABLE in arrayToTrim)
|
||||
{
|
||||
if (VARIABLE != null)
|
||||
{
|
||||
index++;
|
||||
}
|
||||
}
|
||||
if (index == 0)
|
||||
{
|
||||
return Array.Empty<GameScore>();
|
||||
}
|
||||
|
||||
var trimmedArray = new GameScore[index];
|
||||
index = 0;
|
||||
for (int i = 0; i < arrayToTrim.Length; i++)
|
||||
{
|
||||
if (arrayToTrim[i] != null)
|
||||
{
|
||||
trimmedArray[index++] = arrayToTrim[i]!;
|
||||
}
|
||||
}
|
||||
return trimmedArray;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue