2022-10-09 23:21:03 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-11-04 16:49:53 +04:00
|
|
|
|
using System.Diagnostics.Tracing;
|
2022-10-09 23:21:03 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Artilleries
|
|
|
|
|
{
|
|
|
|
|
internal class MapsCollection
|
|
|
|
|
{
|
2022-11-05 00:12:05 +04:00
|
|
|
|
readonly Dictionary<string, MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap>> _mapsStorage;
|
2022-10-09 23:21:03 +04:00
|
|
|
|
public List<string> Keys => _mapsStorage.Keys.ToList();
|
|
|
|
|
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
|
2022-11-04 16:49:53 +04:00
|
|
|
|
private readonly char separatorDict = '|';
|
|
|
|
|
private readonly char separatorData = ';';
|
|
|
|
|
|
2022-10-09 23:21:03 +04:00
|
|
|
|
public MapsCollection(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
2022-11-05 00:12:05 +04:00
|
|
|
|
_mapsStorage = new Dictionary<string, MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap>>();
|
2022-10-09 23:21:03 +04:00
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddMap(string name, AbstractMap map)
|
|
|
|
|
{
|
|
|
|
|
if (!_mapsStorage.ContainsKey(name))
|
|
|
|
|
{
|
2022-11-05 00:12:05 +04:00
|
|
|
|
_mapsStorage.Add(name, new MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
2022-10-09 23:21:03 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DelMap(string name)
|
|
|
|
|
{
|
|
|
|
|
if (_mapsStorage.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
_mapsStorage.Remove(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 00:12:05 +04:00
|
|
|
|
public MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> this[string index]
|
2022-10-09 23:21:03 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _mapsStorage.ContainsKey(index) ? _mapsStorage[index] : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-04 16:49:53 +04:00
|
|
|
|
|
2022-11-08 00:45:22 +04:00
|
|
|
|
public void SaveData(string filename)
|
2022-11-04 16:49:53 +04:00
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (FileStream fs = new FileStream(filename, FileMode.Create))
|
|
|
|
|
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
sw.WriteLine("MapsCollection");
|
|
|
|
|
foreach (var storage in _mapsStorage)
|
|
|
|
|
{
|
|
|
|
|
sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 00:45:22 +04:00
|
|
|
|
public void LoadData(string filename)
|
2022-11-04 16:49:53 +04:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2022-11-08 00:45:22 +04:00
|
|
|
|
throw new FileNotFoundException("Файл не найден");
|
2022-11-04 16:49:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (FileStream fs = new FileStream(filename, FileMode.Open))
|
|
|
|
|
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
string current_line = sr.ReadLine();
|
|
|
|
|
|
|
|
|
|
if (current_line == null || !current_line.Contains("MapsCollection"))
|
|
|
|
|
{
|
2022-11-08 00:45:22 +04:00
|
|
|
|
throw new FileFormatException("Неверный формат файла");
|
2022-11-04 16:49:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mapsStorage.Clear();
|
|
|
|
|
while ((current_line = sr.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
var elements = current_line.Split(separatorDict);
|
|
|
|
|
AbstractMap map = null;
|
|
|
|
|
|
|
|
|
|
switch (elements[1])
|
|
|
|
|
{
|
|
|
|
|
case "SimpleMap":
|
|
|
|
|
map = new SimpleMap();
|
|
|
|
|
break;
|
|
|
|
|
case "ForestMap":
|
|
|
|
|
map = new ForestMap();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 00:12:05 +04:00
|
|
|
|
_mapsStorage.Add(elements[0], new MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
2022-11-04 16:49:53 +04:00
|
|
|
|
_mapsStorage[elements[0]].LoadData(elements[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-09 23:21:03 +04:00
|
|
|
|
}
|
|
|
|
|
}
|