no idea speedrun

This commit is contained in:
2024-10-23 23:10:30 +02:00
parent e2c9116575
commit a2726c715c
6 changed files with 61 additions and 55 deletions
+13 -7
View File
@@ -5,28 +5,33 @@ using Snake.Model;
namespace Snake.Persistance;
public class SnakeLevelLoader{
#region Fields
private readonly string SaveFileName;
private List<StoredSnakeLevel>? _stored_levels;
#endregion
public readonly string SaveFileName;
#region Properties
public ReadOnlyCollection<StoredSnakeLevel> StoredLevels => _stored_levels?.AsReadOnly()
?? throw new InvalidOperationException("Can not query stored levels before loading the data");
#endregion
#region Constructors
public SnakeLevelLoader(string saveFileName){
SaveFileName = saveFileName;
if(!File.Exists(SaveFileName)){
throw new FileNotFoundException("Save file not found", SaveFileName);
}
}
#endregion
#region Methods
public async Task ReadAllDataAsync()
{
string file_content = await File.ReadAllTextAsync(SaveFileName);
_stored_levels = JsonSerializer.Deserialize<List<StoredSnakeLevel>>(file_content, new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower })
?? throw new JsonException("Failed to deserialize stored levels");
await Task.Delay(2000);
}
private List<StoredSnakeLevel>? _stored_levels;
public ReadOnlyCollection<StoredSnakeLevel> StoredLevels => _stored_levels?.AsReadOnly()
?? throw new InvalidOperationException("Can not query stored levels before loading the data");
public SnakeLevel LoadLevel(StoredSnakeLevel level){
return new SnakeLevel(level.Size,
level.Obstacles.Select(a => new Point(a[0], a[1])),
@@ -34,4 +39,5 @@ public class SnakeLevelLoader{
level.NewEggRound,
level.EggLimit);
}
#endregion
}