2022-11-02 22:04:03 +04:00
|
|
|
|
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>
|
2022-11-13 03:20:01 +04:00
|
|
|
|
readonly Dictionary<string, MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap>> _mapStorages;
|
2022-11-02 22:04:03 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий карт
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<string> Keys => _mapStorages.Keys.ToList();
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ширина окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
/// <summary>
|
2022-11-13 03:20:01 +04:00
|
|
|
|
/// Разделитель для записи информации по элементу словаря в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly char separatorDict = '|';
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly char separatorData = ';';
|
|
|
|
|
/// <summary>
|
2022-11-02 22:04:03 +04:00
|
|
|
|
/// Высота окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pictureWidth"></param>
|
|
|
|
|
/// <param name="pictureHeight"></param>
|
|
|
|
|
public MapsCollection(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
2022-11-13 03:20:01 +04:00
|
|
|
|
_mapStorages = new Dictionary<string, MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap>>();
|
2022-11-02 22:04:03 +04:00
|
|
|
|
_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))
|
|
|
|
|
{
|
2022-11-13 03:20:01 +04:00
|
|
|
|
_mapStorages.Add(name, new MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
2022-11-02 22:04:03 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <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>
|
2022-11-13 03:20:01 +04:00
|
|
|
|
public MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap> this[string ind]
|
2022-11-02 22:04:03 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2022-11-13 03:20:01 +04:00
|
|
|
|
MapWithSetAirFightersGeneric<IDrawningObject, AbstractMap> _map;
|
2022-11-02 22:04:03 +04:00
|
|
|
|
if (_mapStorages.TryGetValue(ind, out _map))
|
|
|
|
|
{
|
|
|
|
|
return _map;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-13 03:20:01 +04:00
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2022-11-02 22:04:03 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-13 03:20:01 +04:00
|
|
|
|
|