143 lines
5.1 KiB
C#
143 lines
5.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Catamaran
|
||
{
|
||
internal class MapsCollection
|
||
{
|
||
/// <summary>
|
||
/// Словарь (хранилище) с картами
|
||
/// </summary>
|
||
readonly Dictionary<string, MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>> _mapStorages;
|
||
|
||
/// <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>
|
||
///
|
||
private readonly char separatorDict = '|';
|
||
|
||
private readonly char separatorData = ';';
|
||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||
{
|
||
_mapStorages = new Dictionary<string, MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>>();
|
||
_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))
|
||
{
|
||
_mapStorages.Add(name, new MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Удаление карты
|
||
/// </summary>
|
||
/// <param name="name">Название карты</param>
|
||
public void DelMap(string name)
|
||
{
|
||
_mapStorages.Remove(name);
|
||
}
|
||
/// <summary>
|
||
/// Доступ к парковке
|
||
/// </summary>
|
||
/// <param name="ind"></param>
|
||
/// <returns></returns>
|
||
public MapWithSetBoatsGeneric<IDrawingObject, AbstractMap> this[string ind]
|
||
{
|
||
get
|
||
{
|
||
_mapStorages.TryGetValue(ind, out var map);
|
||
return map;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Метод записи информации в файл
|
||
/// </summary>
|
||
/// <param name="text">Строка, которую следует записать</param>
|
||
/// <param name="stream">Поток для записи</param>
|
||
|
||
public void 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)}");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Загрузка нформации про лодки в гавани из файла
|
||
/// </summary>
|
||
/// <param name="filename"></param>
|
||
/// <returns></returns>
|
||
|
||
public void LoadData(string filename)
|
||
{
|
||
if (!File.Exists(filename))
|
||
{
|
||
throw new Exception("Файл не найден");
|
||
}
|
||
using (StreamReader sr = new StreamReader(filename))
|
||
{
|
||
string str = "";
|
||
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
|
||
{
|
||
//если нет такой записи, то это не те данные
|
||
throw new Exception("Формат данных в файле не правильный");
|
||
}
|
||
//очищаем записи
|
||
_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));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|