106 lines
3.5 KiB
C#
106 lines
3.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Warship
|
|
{
|
|
internal class MapsCollection
|
|
{
|
|
readonly Dictionary<string, MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>> _mapStorages;
|
|
|
|
public List<string> Keys => _mapStorages.Keys.ToList();
|
|
|
|
private readonly int _pictureWidth;
|
|
private readonly int _pictureHeight;
|
|
private readonly char separatorDict = '|';
|
|
private readonly char separatorData = ';';
|
|
|
|
public MapsCollection(int pictureWidth, int pictureHeight)
|
|
{
|
|
_mapStorages = new Dictionary<string, MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
|
|
public void 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}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void LoadData(string filename)
|
|
{
|
|
if (!File.Exists(filename))
|
|
{
|
|
throw new Exception("Файл не найден");
|
|
}
|
|
using (StreamReader sr = new(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 MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
|
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddMap(string name, AbstractMap map)
|
|
{
|
|
if (_mapStorages.ContainsKey(name))
|
|
{
|
|
MessageBox.Show("Карта уже существует");
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
_mapStorages.Add(name, new MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
|
}
|
|
}
|
|
|
|
public void DelMap(string name)
|
|
{
|
|
_mapStorages.Remove(name);
|
|
}
|
|
|
|
public MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap> this[string ind]
|
|
{
|
|
get
|
|
{
|
|
if (_mapStorages.ContainsKey(ind))
|
|
return _mapStorages[ind];
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|