"Если охотник не идет к зверю, то зверь идет ...." - Н. Ю.

This commit is contained in:
Artyom_Yashin 2023-12-19 18:46:53 +04:00
parent c225b1b450
commit fc9c2e7db3
9 changed files with 232 additions and 20 deletions

View File

@ -0,0 +1,59 @@
using ProjectAirBomber.DrawingObjects;
using ProjectAirBomber.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class DrawingPlaneEqutables : IEqualityComparer<DrawingPlane?>
{
public bool Equals(DrawingPlane? x, DrawingPlane? y)
{
if (x == null || x.EntityPlane == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityPlane == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityPlane.Speed != y.EntityPlane.Speed)
{
return false;
}
if (x.EntityPlane.Weight != y.EntityPlane.Weight)
{
return false;
}
if (x.EntityPlane.BodyColor != y.EntityPlane.BodyColor)
{
return false;
}
if (x is DrawingAirBomber && y is DrawingAirBomber)
{
EntityAirBomber EntityX = (EntityAirBomber)x.EntityPlane;
EntityAirBomber EntityY = (EntityAirBomber)y.EntityPlane;
if (EntityX.Bombs != EntityY.Bombs)
return false;
if (EntityX.Fuel != EntityY.Fuel)
return false;
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawingPlane? obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -32,6 +32,8 @@ namespace ProjectAirBomber
{
pictureBoxCollection = new PictureBox();
groupBoxTools = new GroupBox();
button2 = new Button();
button1 = new Button();
GroupBoxSets = new GroupBox();
buttonDelObject = new Button();
buttonAddObject = new Button();
@ -65,6 +67,8 @@ namespace ProjectAirBomber
// groupBoxTools
//
groupBoxTools.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
groupBoxTools.Controls.Add(button2);
groupBoxTools.Controls.Add(button1);
groupBoxTools.Controls.Add(GroupBoxSets);
groupBoxTools.Controls.Add(ButtonRefreshCollection);
groupBoxTools.Controls.Add(ButtonRemovePlane);
@ -77,6 +81,26 @@ namespace ProjectAirBomber
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// button2
//
button2.Location = new Point(12, 375);
button2.Name = "button2";
button2.Size = new Size(190, 29);
button2.TabIndex = 6;
button2.Text = "Сортировка по цвету";
button2.UseVisualStyleBackColor = true;
button2.Click += ButtonSortByColor_Click;
//
// button1
//
button1.Location = new Point(11, 340);
button1.Name = "button1";
button1.Size = new Size(190, 29);
button1.TabIndex = 5;
button1.Text = "Сортировка по типу";
button1.UseVisualStyleBackColor = true;
button1.Click += ButtonSortByType_Click;
//
// GroupBoxSets
//
GroupBoxSets.Anchor = AnchorStyles.Top | AnchorStyles.Right;
@ -150,14 +174,14 @@ namespace ProjectAirBomber
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(28, 435);
maskedTextBoxNumber.Location = new Point(27, 492);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(162, 27);
maskedTextBoxNumber.TabIndex = 1;
//
// ButtonAddPlane
//
ButtonAddPlane.Location = new Point(11, 338);
ButtonAddPlane.Location = new Point(11, 436);
ButtonAddPlane.Name = "ButtonAddPlane";
ButtonAddPlane.Size = new Size(190, 50);
ButtonAddPlane.TabIndex = 0;
@ -195,14 +219,14 @@ namespace ProjectAirBomber
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(224, 26);
SaveToolStripMenuItem.Size = new Size(166, 26);
SaveToolStripMenuItem.Text = "Сохранить";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(224, 26);
LoadToolStripMenuItem.Size = new Size(166, 26);
LoadToolStripMenuItem.Text = "Загрузить";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -247,5 +271,7 @@ namespace ProjectAirBomber
private ToolStripMenuItem ToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private Button button2;
private Button button1;
}
}

View File

@ -1,5 +1,6 @@
using System;
using AirBomber.Exceptions;
using AirBomber;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -40,7 +41,7 @@ namespace ProjectAirBomber
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
listBoxStorages.Items.Add(_storage.Keys[i].Name);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
@ -132,6 +133,11 @@ namespace ProjectAirBomber
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
}
catch (ArgumentException)
{
Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
});
form.AddEvent(planeDelegate);
}
@ -248,6 +254,25 @@ namespace ProjectAirBomber
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor());
private void ComparePlanes(IComparer<DrawingPlane?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowPlanes();
}
}
}

View File

@ -0,0 +1,32 @@
using ProjectAirBomber.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class PlaneCompareByColor : IComparer<DrawingPlane?>
{
public int Compare(DrawingPlane? x, DrawingPlane? y)
{
if (x == null || x.EntityPlane == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityPlane == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityPlane.BodyColor.Name != y.EntityPlane.BodyColor.Name)
{
return x.EntityPlane.BodyColor.Name.CompareTo(y.EntityPlane.BodyColor.Name);
}
var speedCompare = x.EntityPlane.Speed.CompareTo(y.EntityPlane.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityPlane.Weight.CompareTo(y.EntityPlane.Weight);
}
}
}

View File

@ -0,0 +1,32 @@
using ProjectAirBomber.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class PlaneCompareByType : IComparer<DrawingPlane?>
{
public int Compare(DrawingPlane? x, DrawingPlane? y)
{
if (x == null || x.EntityPlane == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityPlane == null)
throw new ArgumentNullException(nameof(y));
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityPlane.Speed.CompareTo(y.EntityPlane.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityPlane.Weight.CompareTo(y.EntityPlane.Weight);
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class PlanesCollectionInfo : IEquatable<PlanesCollectionInfo?>
{
public string Name { get; private set; }
public string Description { get; private set; }
public PlanesCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(PlanesCollectionInfo? other)
{
return Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -1,6 +1,7 @@
using ProjectAirBomber.Generics;
using ProjectAirBomber.DrawingObjects;
using ProjectAirBomber.MovementStrategy;
using AirBomber;
using System;
using System.Collections.Generic;
using System.Linq;
@ -34,6 +35,8 @@ namespace ProjectAirBomber.Generics
/// Набор объектов
/// </summary>
private readonly SetGeneric<T> _collection;
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary>
/// Получение объектов коллекции
/// </summary>
@ -61,7 +64,7 @@ namespace ProjectAirBomber.Generics
{
if (obj == null || collect == null)
return false;
collect._collection.Insert(obj);
collect._collection.Insert(obj, new DrawingPlaneEqutables());
return true;
}
/// <summary>

View File

@ -31,14 +31,14 @@ namespace ProjectAirBomber.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> record in _planeStorage)
foreach (KeyValuePair<PlanesCollectionInfo, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> record in _planeStorage)
{
StringBuilder records = new();
foreach (DrawingPlane? elem in record.Value.GetPlanes)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
@ -92,22 +92,22 @@ namespace ProjectAirBomber.Generics
}
}
}
_planeStorage.Add(record[0], collection);
_planeStorage.Add(new PlanesCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine();
} while (str != null);
}
return;
}
readonly Dictionary<string, PlanesGenericCollection<DrawingPlane,
readonly Dictionary<PlanesCollectionInfo, PlanesGenericCollection<DrawingPlane,
DrawingObjectPlane>> _planeStorage;
public List<string> Keys => _planeStorage.Keys.ToList();
public List<PlanesCollectionInfo> Keys => _planeStorage.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public PlanesGenericStorage(int pictureWidth, int pictureHeight)
{
_planeStorage = new Dictionary<string, PlanesGenericCollection<DrawingPlane,
_planeStorage = new Dictionary<PlanesCollectionInfo, PlanesGenericCollection<DrawingPlane,
DrawingObjectPlane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
@ -115,24 +115,25 @@ namespace ProjectAirBomber.Generics
public void AddSet(string name)
{
_planeStorage.Add(name,
_planeStorage.Add(new PlanesCollectionInfo(name, string.Empty),
new PlanesGenericCollection<DrawingPlane,
DrawingObjectPlane>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_planeStorage.ContainsKey(name))
if (!_planeStorage.ContainsKey(new PlanesCollectionInfo(name, string.Empty)))
return;
_planeStorage.Remove(name);
_planeStorage.Remove(new PlanesCollectionInfo(name, string.Empty));
}
public PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>?this[string ind]
{
get
{
if(_planeStorage.ContainsKey(ind))
return _planeStorage[ind];
PlanesCollectionInfo indObj = new PlanesCollectionInfo(ind, string.Empty);
if(_planeStorage.ContainsKey(indObj))
return _planeStorage[indObj];
return null;
}
}

View File

@ -24,19 +24,26 @@ namespace ProjectAirBomber.Generics
_places = new List<T?>(count);
}
public void Insert(T plane)
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public void Insert(T plane, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
Insert(plane, 0);
Insert(plane, 0, equal);
}
public void Insert(T plane, int position)
public void Insert(T plane, int position, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count))
throw new Exception("Неверная позиция для вставки");
if (equal != null)
{
if (_places.Contains(plane, equal))
throw new ArgumentException(nameof(plane));
}
_places.Insert(position, plane);
}
public void Remove(int position)