no idea speedrun
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user