PIbd_23_Kislitsa_E.D_AirFig.../AirFighter/AirplanesGenericStorage.cs

144 lines
5.2 KiB
C#
Raw Permalink Normal View History

2023-10-27 00:17:04 +04:00
using ProjectAirFighter.DrawningObjects;
using ProjectAirFighter.Generics;
using ProjectAirFighter.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-11-24 22:30:21 +04:00
using System.IO;
2023-10-27 00:17:04 +04:00
namespace ProjectAirFighter.Generics
{
internal class AirplanesGenericStorage
{
readonly Dictionary<string, AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>> _airplaneStorages;
public List<string> Keys => _airplaneStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
2023-11-24 22:30:21 +04:00
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
2023-10-27 00:17:04 +04:00
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
{
_airplaneStorages = new Dictionary<string, AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
2023-11-24 22:30:21 +04:00
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplaneStorages)
{
StringBuilder records = new();
foreach (DrawningAirplane? elem in record.Value.GetAirplanes)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
2023-12-08 22:44:38 +04:00
throw new Exception("Невалиданя операция, нет данных для сохранения");
2023-11-24 22:30:21 +04:00
}
string toWrite = $"AirplaneStorage{Environment.NewLine}{data}";
var strs = toWrite.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
using (StreamWriter sw = new(filename))
{
foreach (var str in strs)
{
sw.WriteLine(str);
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
2023-12-08 22:44:38 +04:00
throw new IOException("Файл не найден");
2023-11-24 22:30:21 +04:00
}
using (StreamReader sr = new(filename))
{
string str = sr.ReadLine();
var strs = str.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
2023-12-08 22:44:38 +04:00
throw new IOException("Нет данных для загрузки");
2023-11-24 22:30:21 +04:00
}
if (!strs[0].StartsWith("AirplaneStorage"))
{
2023-12-08 22:44:38 +04:00
throw new IOException("Неверный формат данных");
2023-11-24 22:30:21 +04:00
}
_airplaneStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = sr.ReadLine();
continue;
}
AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningAirplane? airplane =
elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight);
if (airplane != null)
{
if (!(collection + airplane))
{
return false;
}
}
}
_airplaneStorages.Add(record[0], collection);
2023-10-27 00:17:04 +04:00
2023-11-24 22:30:21 +04:00
str = sr.ReadLine();
} while (str != null);
}
return true;
}
2023-10-27 00:17:04 +04:00
public void AddSet(string name)
{
_airplaneStorages.Add(name, new AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_airplaneStorages.ContainsKey(name))
return;
_airplaneStorages.Remove(name);
}
public AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>? this[string ind]
{
get
{
if (_airplaneStorages.ContainsKey(ind))
return _airplaneStorages[ind];
return null;
}
}
}
}