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) public int Compare(DrawningAirFighter? x, DrawningAirFighter? y)
{ {
if (x == null || x.EntityAirFighter == null) if (x == null || x.EntityAirFighter == null)
{
throw new ArgumentNullException(nameof(x)); throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityAirFighter == null) if (y == null || y.EntityAirFighter == null)
{
throw new ArgumentNullException(nameof(y)); 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) var BodyColorCompare = xEntitySailCatamaran.BodyColor.Name.CompareTo(yEntitySailCatamaran.BodyColor.Name);
return -1; if (BodyColorCompare != 0)
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)
{ {
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); var speedCompare = x.EntityAirFighter.Speed.CompareTo(y.EntityAirFighter.Speed);
if (speedCompare != 0) if (speedCompare != 0)
{
return speedCompare; return speedCompare;
}
return x.EntityAirFighter.Weight.CompareTo(y.EntityAirFighter.Weight); return x.EntityAirFighter.Weight.CompareTo(y.EntityAirFighter.Weight);
} }

View File

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