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

155 lines
5.4 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>
private static void WriteToFile(string text, FileStream stream)
{
byte[] info = new UTF8Encoding(true).GetBytes(text);
stream.Write(info, 0, info.Length);
}
/// <summary>
/// Сохранение информации про лодки м в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns></returns>
public bool SaveData(string filename)
{
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)}");
}
}
return true;
}
/// <summary>
/// Загрузка нформации про лодки в гавани из файла
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
2022-11-21 09:47:05 +04:00
2022-11-20 23:55:50 +04:00
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new StreamReader(filename))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
//очищаем записи
_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));
}
}
return true;
}
2022-11-06 23:39:21 +04:00
}
}