PIbd-21_Kargin_D.E._AirFigh.../AirFighter/AirFighter/MapsCollection.cs
2022-11-13 03:20:01 +04:00

156 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
/// <summary>
/// Класс для хранения коллекции карт
/// </summary>
internal class MapsCollection
{
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
/// </summary>
public List<string> Keys => _mapStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Разделитель для записи информации по элементу словаря в файл
/// </summary>
private readonly char separatorDict = '|';
/// <summary>
/// Разделитель для записей коллекции данных в файл
/// </summary>
private readonly char separatorData = ';';
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
if (!string.IsNullOrEmpty(name) && map != null && !Keys.Contains(name))
{
_mapStorages.Add(name, new MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
if (!string.IsNullOrEmpty(name))
{
_mapStorages.Remove(name);
}
}
/// <summary>
/// Доступ к парковке
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap> this[string ind]
{
get
{
MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap> _map;
if (_mapStorages.TryGetValue(ind, out _map))
{
return _map;
}
else
return null;
}
}
/// <summary>
/// Сохранение информации по самолетам в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns></returns>
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = new(filename))
{
sw.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
return true;
}
/// <summary>
/// Загрузка нформации по самолетам на парковках из файла
/// </summary>
/// <param name = "filename" ></ param >
/// < returns ></ returns >
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
string bufferTextFromFile = "";
using (StreamReader sr = new(filename))
{
string checkMap = sr.ReadLine();
if (!checkMap.Contains("MapsCollection"))
{
return false;
}
bufferTextFromFile = sr.ReadLine();
_mapStorages.Clear();
while (bufferTextFromFile != null)
{
var strs = bufferTextFromFile.Split(separatorDict);
AbstractMap map = null;
switch (strs[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "StormMap":
map = new StormMap();
break;
}
_mapStorages.Add(strs[0], new MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[strs[0]].LoadData(strs[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
bufferTextFromFile = sr.ReadLine();
}
}
return true;
}
}
}