Pibd-22_Presnyakova.V.V_Cat.../Catamaran/MapsCollection.cs

144 lines
5.0 KiB
C#
Raw Normal View History

2022-11-06 23:39:21 +04:00
using System;
using System.Collections.Generic;
2022-11-20 23:55:50 +04:00
using System.IO;
2022-11-06 23:39:21 +04:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class MapsCollection
{
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
2022-11-20 23:55:50 +04:00
readonly Dictionary<string, MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>> _mapStorages;
2022-11-06 23:39:21 +04:00
/// <summary>
/// Возвращение списка названий карт
/// </summary>
public List<string> Keys => _mapStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
2022-11-20 23:55:50 +04:00
///
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
2022-11-06 23:39:21 +04:00
public MapsCollection(int pictureWidth, int pictureHeight)
{
2022-11-20 23:55:50 +04:00
_mapStorages = new Dictionary<string, MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>>();
2022-11-06 23:39:21 +04:00
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
if (!_mapStorages.ContainsKey(name))
{
2022-11-20 23:55:50 +04:00
_mapStorages.Add(name, new MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
2022-11-06 23:39:21 +04:00
}
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
_mapStorages.Remove(name);
}
/// <summary>
/// Доступ к парковке
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
2022-11-20 23:55:50 +04:00
public MapWithSetBoatsGeneric<IDrawingObject, AbstractMap> this[string ind]
2022-11-06 23:39:21 +04:00
{
get
{
_mapStorages.TryGetValue(ind, out var map);
return map;
}
}
2022-11-20 23:55:50 +04:00
/// <summary>
/// Метод записи информации в файл
/// </summary>
/// <param name="text">Строка, которую следует записать</param>
/// <param name="stream">Поток для записи</param>
2022-12-03 20:28:56 +04:00
2022-11-21 11:19:10 +04:00
public void SaveData(string filename)
2022-11-20 23:55:50 +04:00
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = new StreamWriter(filename))
{
sw.WriteLine("MapsCollection");
foreach (var storage in _mapStorages)
{
sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}");
}
}
}
/// <summary>
/// Загрузка нформации про лодки в гавани из файла
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
2022-11-21 09:47:05 +04:00
2022-11-21 11:19:10 +04:00
public void LoadData(string filename)
2022-11-20 23:55:50 +04:00
{
if (!File.Exists(filename))
{
2022-12-04 00:11:52 +04:00
throw new FileNotFoundException(filename);
2022-11-20 23:55:50 +04:00
}
using (StreamReader sr = new StreamReader(filename))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
2022-12-04 00:11:52 +04:00
throw new FormatException(str);
2022-11-20 23:55:50 +04:00
}
//очищаем записи
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "SecondMap":
map = new SecondMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, (char)StringSplitOptions.RemoveEmptyEntries));
}
}
}
2022-11-06 23:39:21 +04:00
}
}