This commit is contained in:
Whoisthatjulia 2023-12-29 20:42:01 +04:00
parent faa1e41ee0
commit ff8cbd3ea6
3 changed files with 57 additions and 35 deletions

View File

@ -3,68 +3,82 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using AirFighter.DrawningObjects;
using AirFighter.MovementStrategy;
using AirFighter.Generics;
using System.IO;
using AirFighter.Exceptions;
using AirFighter.Drawnings;
using AirFighter.MovementStrategy;
using AirFighter.DrawningObjects;
using AirFighter.Exceptions;
using AirFighter.Exceptions;
namespace AirFighter.Generics namespace AirFighter.Generics
{ {
internal class AirFighterGenericStorage internal class AirFighterGenericStorage
{ {
readonly Dictionary<AirFighterCollectionInfo, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>> _fighterStorages; readonly Dictionary<AirFighterCollectionInfo, AirFighterGenericCollection<DrawningAirFighter,
DrawningObjectAirFighter>> _lincornStorages;
public List<AirFighterCollectionInfo> Keys => _fighterStorages.Keys.ToList(); public List<AirFighterCollectionInfo> Keys => _lincornStorages.Keys.ToList();
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public AirFighterGenericStorage(int pictureWidth, int pictureHeight) public AirFighterGenericStorage(int pictureWidth, int pictureHeight)
{ {
_fighterStorages = new Dictionary<AirFighterCollectionInfo, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>>(); _lincornStorages = new Dictionary<AirFighterCollectionInfo,
AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name) public void AddSet(string name)
{ {
// TODO Прописать логику для добавления // TODO Прописать логику для добавления
if (!_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) if (!_lincornStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty)))
{ {
var fighterCollection = new AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>(_pictureWidth, _pictureHeight); var lincornCollection = new AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>(_pictureWidth, _pictureHeight);
_fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), fighterCollection); _lincornStorages.Add(new AirFighterCollectionInfo(name, string.Empty), lincornCollection);
} }
} }
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name) public void DelSet(string name)
{ {
// TODO Прописать логику для удаления // TODO Прописать логику для удаления
if (_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) if (_lincornStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty)))
{ {
_fighterStorages.Remove(new AirFighterCollectionInfo(name, string.Empty)); _lincornStorages.Remove(new AirFighterCollectionInfo(name, string.Empty));
} }
} }
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>? this[string ind] public AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>? this[string ind]
{ {
get get
{ {
AirFighterCollectionInfo indObj = new AirFighterCollectionInfo(ind, string.Empty); AirFighterCollectionInfo indObj = new AirFighterCollectionInfo(ind, string.Empty);
// TODO Продумать логику получения набора // TODO Продумать логику получения набора
if (_fighterStorages.ContainsKey(indObj)) if (_lincornStorages.ContainsKey(indObj))
{ {
return _fighterStorages[indObj]; return _lincornStorages[indObj];
} }
return null; return null;
} }
} }
/// <summary>
private static readonly char _separatorForKeyValue = '|'; /// Сохранение информации по установкам в хранилище в файл
/// </summary>
private readonly char _separatorRecords = ';'; /// <param name="filename">Путь и имя файла</param>
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
private static readonly char _separatorForObject = ':';
public void SaveData(string filename) public void SaveData(string filename)
{ {
if (File.Exists(filename)) if (File.Exists(filename))
@ -72,7 +86,7 @@ namespace AirFighter.Generics
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<AirFighterCollectionInfo, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>> record in _fighterStorages) foreach (KeyValuePair<AirFighterCollectionInfo, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>> record in _lincornStorages)
{ {
StringBuilder records = new(); StringBuilder records = new();
foreach (DrawningAirFighter? elem in record.Value.GetAirFighter) foreach (DrawningAirFighter? elem in record.Value.GetAirFighter)
@ -88,15 +102,22 @@ namespace AirFighter.Generics
using (StreamWriter writer = new StreamWriter(filename)) using (StreamWriter writer = new StreamWriter(filename))
{ {
writer.Write($"fighterStorage{Environment.NewLine}{data}"); writer.Write($"lincornStorage{Environment.NewLine}{data}");
} }
} }
// <summary>
/// Загрузка информации по установкам в хранилище из файла
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
public void LoadData(string filename) public void LoadData(string filename)
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
{ {
throw new Exception("Файл не найден"); throw new Exception("Файл не найден");
} }
using (StreamReader reader = new StreamReader(filename)) using (StreamReader reader = new StreamReader(filename))
{ {
string cheker = reader.ReadLine(); string cheker = reader.ReadLine();
@ -104,11 +125,11 @@ namespace AirFighter.Generics
{ {
throw new Exception("Нет данных для загрузки"); throw new Exception("Нет данных для загрузки");
} }
if (!cheker.StartsWith("fighterStorage")) if (!cheker.StartsWith("lincornStorage"))
{ {
throw new Exception("Неверный формат ввода"); throw new Exception("Неверный формат ввода");
} }
_fighterStorages.Clear(); _lincornStorages.Clear();
string strs; string strs;
bool firstinit = true; bool firstinit = true;
while ((strs = reader.ReadLine()) != null) while ((strs = reader.ReadLine()) != null)
@ -126,11 +147,11 @@ namespace AirFighter.Generics
AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter> collection = new(_pictureWidth, _pictureHeight); AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter> collection = new(_pictureWidth, _pictureHeight);
foreach (string data in strs.Split(_separatorForKeyValue)[1].Split(_separatorRecords)) foreach (string data in strs.Split(_separatorForKeyValue)[1].Split(_separatorRecords))
{ {
DrawningAirFighter? fighter = DrawningAirFighter? lincorn =
data?.CreateDrawningAirFighter(_separatorForObject, _pictureWidth, _pictureHeight); data?.CreateDrawningAirFighter(_separatorForObject, _pictureWidth, _pictureHeight);
if (fighter != null) if (lincorn != null)
{ {
try { _ = collection + fighter; } try { _ = collection + lincorn; }
catch (AirFighterNotFoundException e) catch (AirFighterNotFoundException e)
{ {
throw e; throw e;
@ -141,9 +162,10 @@ namespace AirFighter.Generics
} }
} }
} }
_fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), collection); _lincornStorages.Add(new AirFighterCollectionInfo(name, string.Empty), collection);
} }
} }
} }
} }
} }

View File

@ -38,7 +38,7 @@ namespace AirFighter
listBoxStorage.Items.Clear(); listBoxStorage.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++) for (int i = 0; i < _storage.Keys.Count; i++)
{ {
listBoxStorage.Items.Add(_storage.Keys[i]); listBoxStorage.Items.Add(_storage.Keys[i].Name);
} }
if (listBoxStorage.Items.Count > 0 && (index == -1 || index if (listBoxStorage.Items.Count > 0 && (index == -1 || index
>= listBoxStorage.Items.Count)) >= listBoxStorage.Items.Count))

View File

@ -14,7 +14,7 @@
], ],
"Enrich": [ "FromLogContext", "WithShipName", "WithThreadId" ], "Enrich": [ "FromLogContext", "WithShipName", "WithThreadId" ],
"Properties": { "Properties": {
"Application": "ContainerShip" "Application": "AirFighter"
} }
} }
} }