From 176d02b1f8f9af62d60e69c3913547b70d429e6a Mon Sep 17 00:00:00 2001 From: xom9kxom9k Date: Sun, 5 May 2024 12:00:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AntiAircraftGun/AntiAircraftGun.csproj | 6 -- .../AbstractCompany.cs | 8 ++- .../CollectionGenericObjects/CarBase.cs | 30 +++++---- .../ListGenericObjects.cs | 1 - .../MassiveGenericObjects.cs | 59 ++++++++---------- .../Exceptions/CollectionOverflowException.cs | 1 + .../Exceptions/ObjectNotFoundException.cs | 1 + .../PositionOutOfCollectionException.cs | 2 +- AntiAircraftGun/FormArmoredCarCollection.cs | 61 +++++++++++-------- AntiAircraftGun/Program.cs | 42 +++++++------ AntiAircraftGun/serilog.config | 13 ---- AntiAircraftGun/serilog.json | 15 +++++ AntiAircraftGun/serilogConfig.json | 25 -------- 13 files changed, 126 insertions(+), 138 deletions(-) delete mode 100644 AntiAircraftGun/serilog.config create mode 100644 AntiAircraftGun/serilog.json delete mode 100644 AntiAircraftGun/serilogConfig.json diff --git a/AntiAircraftGun/AntiAircraftGun.csproj b/AntiAircraftGun/AntiAircraftGun.csproj index 95199bf..f3c591c 100644 --- a/AntiAircraftGun/AntiAircraftGun.csproj +++ b/AntiAircraftGun/AntiAircraftGun.csproj @@ -30,10 +30,4 @@ - - - Always - - - \ No newline at end of file diff --git a/AntiAircraftGun/CollectionGenericObjects/AbstractCompany.cs b/AntiAircraftGun/CollectionGenericObjects/AbstractCompany.cs index af9fd2a..702b156 100644 --- a/AntiAircraftGun/CollectionGenericObjects/AbstractCompany.cs +++ b/AntiAircraftGun/CollectionGenericObjects/AbstractCompany.cs @@ -98,8 +98,12 @@ public abstract class AbstractCompany SetObjectsPosition(); for (int i = 0; i < (_collection?.Count ?? 0); ++i) { - DrawningArmoredCar? obj = _collection?.Get(i); - obj?.DrawTransport(graphics); + try + { + DrawningArmoredCar? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + catch (Exception) { } } return bitmap; diff --git a/AntiAircraftGun/CollectionGenericObjects/CarBase.cs b/AntiAircraftGun/CollectionGenericObjects/CarBase.cs index 4fe96ec..6cb943c 100644 --- a/AntiAircraftGun/CollectionGenericObjects/CarBase.cs +++ b/AntiAircraftGun/CollectionGenericObjects/CarBase.cs @@ -39,26 +39,30 @@ public class CarBase : AbstractCompany /// protected override void SetObjectsPosition() { - int nowWidth = 0; - int nowHeight = 0; + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = width - 1; + int curHeight = 0; for (int i = 0; i < (_collection?.Count ?? 0); i++) { - if (nowHeight > _pictureHeight / _placeSizeHeight) + try { - return; + _collection?.Get(i)?.SetPictureSize(_pictureWidth, _pictureHeight); + _collection?.Get(i)?.SetPosition(_placeSizeWidth * curWidth, curHeight * _placeSizeHeight + 4); } - if (_collection?.Get(i) != null) - { - _collection?.Get(i)?.SetPictureSize(_pictureWidth , _pictureHeight); - _collection?.Get(i)?.SetPosition(_placeSizeWidth * nowWidth + 10, nowHeight * _placeSizeHeight * 2 ); - } - - if (nowWidth < _pictureWidth / _placeSizeWidth - 1) nowWidth++; + catch (Exception) { } + if (curWidth > 0) + curWidth--; else { - nowWidth = 0; - nowHeight++; + curWidth = width - 1; + curHeight++; + } + if (curHeight > height) + { + return; } } } diff --git a/AntiAircraftGun/CollectionGenericObjects/ListGenericObjects.cs b/AntiAircraftGun/CollectionGenericObjects/ListGenericObjects.cs index bf88dff..ee171c0 100644 --- a/AntiAircraftGun/CollectionGenericObjects/ListGenericObjects.cs +++ b/AntiAircraftGun/CollectionGenericObjects/ListGenericObjects.cs @@ -1,6 +1,5 @@ using AntiAircraftGun.CollectionGenereticObject; using AntiAircraftGun.Exceptions; -using Exceptions; namespace AntiAircraftGun.CollectionGenericObjects; /// diff --git a/AntiAircraftGun/CollectionGenericObjects/MassiveGenericObjects.cs b/AntiAircraftGun/CollectionGenericObjects/MassiveGenericObjects.cs index ac7870e..f86f961 100644 --- a/AntiAircraftGun/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AntiAircraftGun/CollectionGenericObjects/MassiveGenericObjects.cs @@ -76,42 +76,33 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Insert(T obj, int position) { - if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); - if (_collection[position] != null) + if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); + if (_collection[position] == null) { - bool pushed = false; - for (int index = position + 1; index < Count; index++) - { - if (_collection[index] == null) - { - position = index; - pushed = true; - break; - } - } - - if (!pushed) - { - for (int index = position - 1; index >= 0; index--) - { - if (_collection[index] == null) - { - position = index; - pushed = true; - break; - } - } - } - - if (!pushed) - { - throw new CollectionOverflowException(Count); - } + _collection[position] = obj; + return position; } - - // вставка - _collection[position] = obj; - return position; + int index = position + 1; + while (index < _collection.Length) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + ++index; + } + index = position - 1; + while (index >= 0) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + --index; + } + throw new CollectionOverflowException(Count); } public T? Remove(int position) diff --git a/AntiAircraftGun/Exceptions/CollectionOverflowException.cs b/AntiAircraftGun/Exceptions/CollectionOverflowException.cs index c35f717..11a7ec3 100644 --- a/AntiAircraftGun/Exceptions/CollectionOverflowException.cs +++ b/AntiAircraftGun/Exceptions/CollectionOverflowException.cs @@ -9,6 +9,7 @@ namespace AntiAircraftGun.Exceptions; /// /// Класс, описывающий ошибку переполнения коллекции /// +[Serializable] public class CollectionOverflowException : ApplicationException { public CollectionOverflowException(int count) : base("В коллекции превышено допустимое колличество: " + count) { } diff --git a/AntiAircraftGun/Exceptions/ObjectNotFoundException.cs b/AntiAircraftGun/Exceptions/ObjectNotFoundException.cs index e34301d..65cb98d 100644 --- a/AntiAircraftGun/Exceptions/ObjectNotFoundException.cs +++ b/AntiAircraftGun/Exceptions/ObjectNotFoundException.cs @@ -9,6 +9,7 @@ namespace AntiAircraftGun.Exceptions; /// /// Класс, описывающий ошибку, что по указанной позиции нет элемента /// +[Serializable] public class ObjectNotFoundException : ApplicationException { public ObjectNotFoundException(int i) : base("Не найден объект по позиции " + i) { } diff --git a/AntiAircraftGun/Exceptions/PositionOutOfCollectionException.cs b/AntiAircraftGun/Exceptions/PositionOutOfCollectionException.cs index d9a60ff..7dc64bf 100644 --- a/AntiAircraftGun/Exceptions/PositionOutOfCollectionException.cs +++ b/AntiAircraftGun/Exceptions/PositionOutOfCollectionException.cs @@ -12,7 +12,7 @@ namespace AntiAircraftGun.Exceptions; [Serializable] public class PositionOutOfCollectionException { - public PositionOutOfCollectionException(int i) : base("Выход за границы коллекции.Позиция " + i) { } + public PositionOutOfCollectionException(int i) : base("Выход за границы коллекции. Позиция " + i) { } public PositionOutOfCollectionException() : base() { } public PositionOutOfCollectionException(string message) : base(message) { } public PositionOutOfCollectionException(string message, Exception exception) : base(message, exception) { } diff --git a/AntiAircraftGun/FormArmoredCarCollection.cs b/AntiAircraftGun/FormArmoredCarCollection.cs index 808bae9..ae8e76a 100644 --- a/AntiAircraftGun/FormArmoredCarCollection.cs +++ b/AntiAircraftGun/FormArmoredCarCollection.cs @@ -32,6 +32,7 @@ public partial class FormArmoredCarCollection : Form InitializeComponent(); _storageCollection = new(); _logger = logger; + _logger.LogInformation("Форма загрузилась"); } /// @@ -75,9 +76,11 @@ public partial class FormArmoredCarCollection : Form } } - catch (CollectionOverflowException) + catch (ObjectNotFoundException) { } + catch (CollectionOverflowException ex) { MessageBox.Show("Не удалось добавить объект"); + _logger.LogError("Ошибка: {Message}", ex.Message); } } @@ -93,10 +96,12 @@ public partial class FormArmoredCarCollection : Form { return; } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); try { @@ -104,19 +109,13 @@ public partial class FormArmoredCarCollection : Form { MessageBox.Show("Объект удален"); pictureBox.Image = _company.Show(); - } - else - { - MessageBox.Show("Не удалось удалить объект"); + _logger.LogInformation("Удален объект по позиции " + pos); } } - catch (PositionOutOfCollectionException) + catch (Exception ex) { - MessageBox.Show("Ошибка при удалении объекта"); - } - catch (Exception) - { - MessageBox.Show("Неизвестная ошибка при удалении объекта"); + MessageBox.Show("Не удалось удалить объект"); + _logger.LogError("Ошибка: {Message}", ex.Message); } } @@ -205,18 +204,25 @@ public partial class FormArmoredCarCollection : Form return; } - CollectionType collectionType = CollectionType.None; - if (radioButtonMassive.Checked) + try { - collectionType = CollectionType.Massive; + CollectionType collectionType = CollectionType.None; + if (radioButtonMassive.Checked) + { + collectionType = CollectionType.Massive; + } + else if (radioButtonList.Checked) + { + collectionType = CollectionType.List; + } + _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); + RerfreshListBoxItems(); + _logger.LogInformation("Коллекция добавлена " + textBoxCollectionName.Text); } - else if (radioButtonList.Checked) + catch (Exception ex) { - collectionType = CollectionType.List; + _logger.LogError("Ошибка: {Message}", ex.Message); } - - _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); - RerfreshListBoxItems(); } /// /// Удаление коллекции @@ -230,13 +236,20 @@ public partial class FormArmoredCarCollection : Form MessageBox.Show("Коллекция не выбрана"); return; } - - if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + try { - return; + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + RerfreshListBoxItems(); + _logger.LogInformation("Коллекция: " + listBoxCollection.SelectedItem.ToString() + " удалена"); + } + catch (Exception ex) + { + _logger.LogError("Ошибка: {Message}", ex.Message); } - _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); - RerfreshListBoxItems(); } /// /// Создание комании diff --git a/AntiAircraftGun/Program.cs b/AntiAircraftGun/Program.cs index c573349..ff85204 100644 --- a/AntiAircraftGun/Program.cs +++ b/AntiAircraftGun/Program.cs @@ -14,31 +14,35 @@ namespace AntiAircraftGun // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - var services = new ServiceCollection(); - ConfigureService(services); - using (ServiceProvider serviceProvider = services.BuildServiceProvider()) - { - Application.Run(serviceProvider.GetRequiredService()); - } + + ServiceCollection services = new(); + ConfigureServices(services); + using ServiceProvider serviceProvider = services.BuildServiceProvider(); + Application.Run(serviceProvider.GetRequiredService()); } /// /// DI /// /// - private static void ConfigureService(ServiceCollection services) + private static void ConfigureServices(ServiceCollection services) { - services - .AddSingleton() - .AddLogging(option => - { - option.SetMinimumLevel(LogLevel.Information); - var config = new ConfigurationBuilder() - .AddJsonFile("serilogConfig.json", optional: false, reloadOnChange: true) - .Build(); - option.AddSerilog(Log.Logger = new LoggerConfiguration() - .ReadFrom.Configuration(config) - .CreateLogger()); - }); + string[] path = Directory.GetCurrentDirectory().Split('\\'); + string pathNeed = ""; + for (int i = 0; i < path.Length - 3; i++) + { + pathNeed += path[i] + "\\"; + } + services.AddSingleton() + .AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddSerilog(new LoggerConfiguration() + .ReadFrom.Configuration(new ConfigurationBuilder() + .AddJsonFile($"{pathNeed}serilog.json") + .Build()) + .CreateLogger()); + }); } } + } } \ No newline at end of file diff --git a/AntiAircraftGun/serilog.config b/AntiAircraftGun/serilog.config deleted file mode 100644 index 54e4ba6..0000000 --- a/AntiAircraftGun/serilog.config +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/AntiAircraftGun/serilog.json b/AntiAircraftGun/serilog.json new file mode 100644 index 0000000..d1428e1 --- /dev/null +++ b/AntiAircraftGun/serilog.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { "path": "log.log" } + } + ], + "Properties": { + "Application": "Sample" + } + } +} diff --git a/AntiAircraftGun/serilogConfig.json b/AntiAircraftGun/serilogConfig.json deleted file mode 100644 index b607698..0000000 --- a/AntiAircraftGun/serilogConfig.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "AllowedHosts": "*", - "Serilog": { - "Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ], - "MinimumLevel": { - "Default": "Information", - "Override": { - "Microsoft": "Warning", - "System": "Warning" - } - }, - "Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ], - "WriteTo": [ - { "Name": "Console" }, - { - "Name": "File", - "Args": { - "path": "C:\\Users\\ipazu\\Desktop\\log.txt", - "rollingInterval": "Day", - "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff} | {Level:u} | {SourceContext} | {Message:1j}{NewLine}{Exception}" - } - } - ] - } -}