This commit is contained in:
Whoisthatjulia 2023-12-29 21:02:13 +04:00
parent ff8cbd3ea6
commit b1be9c302d
2 changed files with 55 additions and 69 deletions

View File

@ -14,35 +14,42 @@ namespace AirFighter
public int Compare(DrawningAirFighter? x, DrawningAirFighter? y)
{
if (x == null || x.EntityAirFighter == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityAirFighter == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntityAirFighter.BodyColor.Name != y.EntityAirFighter.BodyColor.Name)
var bodyColorCompare = x.EntityAirFighter.BodyColor.Name.CompareTo(y.EntityAirFighter.BodyColor.Name);
if (bodyColorCompare != 0)
{
return x.EntityAirFighter.BodyColor.Name.CompareTo(y.EntityAirFighter.BodyColor.Name);
return bodyColorCompare;
}
if (x.GetType().Name != y.GetType().Name)
if (x.EntityAirFighter is EntityAirFighterMilitary xEntitySailCatamaran && y.EntityAirFighter is EntityAirFighterMilitary yEntitySailCatamaran)
{
if (x is EntityAirFighter)
return -1;
else
return 1;
}
if (x.GetType().Name == y.GetType().Name && x is DrawningAirFighterMilitary)
{
EntityAirFighterMilitary EntityX = (EntityAirFighterMilitary)x.EntityAirFighter;
EntityAirFighterMilitary EntityY = (EntityAirFighterMilitary)y.EntityAirFighter;
if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name)
var BodyColorCompare = xEntitySailCatamaran.BodyColor.Name.CompareTo(yEntitySailCatamaran.BodyColor.Name);
if (BodyColorCompare != 0)
{
return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name);
return BodyColorCompare;
}
var AdditionalColorCompare = xEntitySailCatamaran.AdditionalColor.Name.CompareTo(yEntitySailCatamaran.AdditionalColor.Name);
if (AdditionalColorCompare != 0)
{
return AdditionalColorCompare;
}
}
var speedCompare = x.EntityAirFighter.Speed.CompareTo(y.EntityAirFighter.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityAirFighter.Weight.CompareTo(y.EntityAirFighter.Weight);
}

View File

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