commit 1
This commit is contained in:
parent
852d7a7dfd
commit
d27022c940
58
ProjectPlane/ProjectPlane/ExtentionPlane.cs
Normal file
58
ProjectPlane/ProjectPlane/ExtentionPlane.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectPlane
|
||||
{
|
||||
internal static class ExtentionPlane
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly char _separatorForObject = ':';
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
/// <param name="info"></param>
|
||||
/// <returns></returns>
|
||||
public static DrawingPlane CreateDrawingplane(this string info)
|
||||
{
|
||||
string[] strs = info.Split(_separatorForObject);
|
||||
if (strs.Length == 3)
|
||||
{
|
||||
return new DrawingPlane(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
|
||||
}
|
||||
if (strs.Length == 6)
|
||||
{
|
||||
return new DrawingWarPlane(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), Color.FromName(strs[2]),
|
||||
Color.FromName(strs[3]), Convert.ToBoolean(strs[4]),
|
||||
Convert.ToBoolean(strs[5]));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение данных для сохранения в файл
|
||||
/// </summary>
|
||||
/// <param name="drawingplane"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDataForSave(this DrawingPlane drawingplane)
|
||||
{
|
||||
var plane = drawingplane.Plane;
|
||||
var str =
|
||||
$"{plane.Speed}{_separatorForObject}{plane.Weight}{_separatorForObject}{plane.BodyColor.Name}";
|
||||
if (plane is not EntityWarPlane warPlane)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return
|
||||
$"{str}{_separatorForObject}{warPlane.DopColor.Name}{_separatorForObject}{warPlane.extraCell}" +
|
||||
$"{_separatorForObject}{warPlane.SuperTurbine}{_separatorForObject}";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -108,6 +108,31 @@ namespace ProjectPlane
|
||||
return new(_pictureWidth, _pictureHeight);
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение данных в виде строки
|
||||
/// </summary>
|
||||
/// <param name="sep"></param>
|
||||
/// <returns></returns>
|
||||
public string GetData(char separatorType, char separatorData)
|
||||
{
|
||||
string data = $"{_map.GetType().Name}{separatorType}";
|
||||
foreach (var car in _setPlanes.GetPlanes())
|
||||
{
|
||||
data += $"{car.GetInfo()}{separatorData}";
|
||||
}
|
||||
return data;
|
||||
}
|
||||
/// <summary>
|
||||
/// Загрузка списка из массива строк
|
||||
/// </summary>
|
||||
/// <param name="records"></param>
|
||||
public void LoadData(string[] records)
|
||||
{
|
||||
foreach (var rec in records)
|
||||
{
|
||||
_setPlanes.Insert(DrawingObject.Create(rec) as T);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
||||
/// </summary>
|
||||
private void Shaking()
|
||||
|
@ -1,4 +1,5 @@
|
||||
namespace ProjectPlane
|
||||
using System.Text;
|
||||
namespace ProjectPlane
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс для хранения коллекции карт
|
||||
@ -8,7 +9,7 @@
|
||||
/// <summary>
|
||||
/// Словарь (хранилище) с картами
|
||||
/// </summary>
|
||||
readonly Dictionary<string, MapWithSetPlanesGeneric<DrawingObject, AbstractMap>> _mapStorages;
|
||||
readonly Dictionary<string, MapWithSetPlanesGeneric<IDrawingObject, AbstractMap>> _mapStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий карт
|
||||
/// </summary>
|
||||
@ -26,9 +27,18 @@
|
||||
/// </summary>
|
||||
/// <param name="pictureWidth"></param>
|
||||
/// <param name="pictureHeight"></param>
|
||||
/// /// <summary>
|
||||
/// Разделитель для записи информации по элементу словаря в файл
|
||||
/// </summary>
|
||||
private readonly char separatorDict = '|';
|
||||
/// <summary>
|
||||
/// Разделитель для записей коллекции данных в файл
|
||||
/// </summary>
|
||||
private readonly char separatorData = ';';
|
||||
|
||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_mapStorages = new Dictionary<string, MapWithSetPlanesGeneric<DrawingObject, AbstractMap>>();
|
||||
_mapStorages = new Dictionary<string, MapWithSetPlanesGeneric<IDrawingObject, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
@ -40,7 +50,7 @@
|
||||
public void AddMap(string name, AbstractMap map)
|
||||
{
|
||||
if (!_mapStorages.ContainsKey(name))
|
||||
_mapStorages.Add(name, new MapWithSetPlanesGeneric<DrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages.Add(name, new MapWithSetPlanesGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление карты
|
||||
@ -56,7 +66,7 @@
|
||||
/// </summary>
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public MapWithSetPlanesGeneric<DrawingObject, AbstractMap> this[string ind]
|
||||
public MapWithSetPlanesGeneric<IDrawingObject, AbstractMap> this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
@ -64,5 +74,79 @@
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// <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);
|
||||
}
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
using (FileStream fs = new(filename, FileMode.Create))
|
||||
{
|
||||
WriteToFile($"MapsCollection{Environment.NewLine}", fs);
|
||||
foreach (var storage in _mapStorages)
|
||||
{
|
||||
WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Загрузка нформации по автомобилям на парковках из файла
|
||||
/// </summary>
|
||||
/// <param name="filename"></param>
|
||||
/// <returns></returns>
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string bufferTextFromFile = "";
|
||||
using (FileStream fs = new(filename, FileMode.Open))
|
||||
{
|
||||
byte[] b = new byte[fs.Length];
|
||||
UTF8Encoding temp = new(true);
|
||||
while (fs.Read(b, 0, b.Length) > 0)
|
||||
{
|
||||
bufferTextFromFile += temp.GetString(b);
|
||||
}
|
||||
}
|
||||
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (!strs[0].Contains("MapsCollection"))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
return false;
|
||||
}
|
||||
//очищаем записи
|
||||
_mapStorages.Clear();
|
||||
for (int i = 1; i < strs.Length; ++i)
|
||||
{
|
||||
var elem = strs[i].Split(separatorDict);
|
||||
AbstractMap map = null;
|
||||
switch (elem[1])
|
||||
{
|
||||
case "SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "SkyMap":
|
||||
map = new SkyMap();
|
||||
break;
|
||||
}
|
||||
_mapStorages.Add(elem[0], new MapWithSetPlanesGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user