7 лабораторная работа
This commit is contained in:
parent
786492d756
commit
3cac227c53
@ -8,4 +8,14 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||||
|
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
|
||||||
|
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
@ -9,7 +9,7 @@ public abstract class AbstractCompany
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Размер места (ширина)
|
/// Размер места (ширина)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected readonly int _placeSizeWidth = 210;
|
protected readonly int _placeSizeWidth = 260;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Размер места (высота)
|
/// Размер места (высота)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace Battleship.CollectionGenericObjects;
|
using Battleship.Exceptions;
|
||||||
|
|
||||||
|
namespace Battleship.CollectionGenericObjects;
|
||||||
|
|
||||||
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||||
where T : class
|
where T : class
|
||||||
@ -40,7 +42,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
public T? Get(int position)
|
public T? Get(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= _collection.Count)
|
if (position < 0 || position >= _collection.Count)
|
||||||
return null;
|
throw new PositionOutOfCollectionException(position);
|
||||||
return _collection[position];
|
return _collection[position];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,13 +53,15 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
_collection.Add(obj);
|
_collection.Add(obj);
|
||||||
return _collection.Count - 1;
|
return _collection.Count - 1;
|
||||||
}
|
}
|
||||||
return -1;
|
throw new CollectionOverflowException(MaxCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Insert(T obj, int position)
|
public bool Insert(T obj, int position)
|
||||||
{
|
{
|
||||||
if (_collection.Count + 1 > _maxCount || position < 0 || position >= _collection.Count)
|
if (_collection.Count + 1 > MaxCount)
|
||||||
return false;
|
throw new CollectionOverflowException(MaxCount);
|
||||||
|
if (position < 0 || position >= MaxCount)
|
||||||
|
throw new PositionOutOfCollectionException(position);
|
||||||
_collection.Insert(position, obj);
|
_collection.Insert(position, obj);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -65,7 +69,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
public T Remove(int position)
|
public T Remove(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= _collection.Count)
|
if (position < 0 || position >= _collection.Count)
|
||||||
return null;
|
throw new PositionOutOfCollectionException(position);
|
||||||
T temp = _collection[position];
|
T temp = _collection[position];
|
||||||
_collection.RemoveAt(position);
|
_collection.RemoveAt(position);
|
||||||
return temp;
|
return temp;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace Battleship.CollectionGenericObjects;
|
using Battleship.Exceptions;
|
||||||
|
|
||||||
|
namespace Battleship.CollectionGenericObjects;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Параметризованный набор объектов
|
/// Параметризованный набор объектов
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -53,10 +55,12 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
public T? Get(int position)
|
public T? Get(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= _collection.Length)
|
if (position < 0 || position >= _collection.Length)
|
||||||
return null;
|
throw new PositionOutOfCollectionException(position);
|
||||||
|
if (_collection[position] == null)
|
||||||
|
throw new ObjectNotFoundException(position);
|
||||||
return _collection[position];
|
return _collection[position];
|
||||||
}
|
}
|
||||||
public int Insert(T obj) //??? Уточнить реализацию
|
public int Insert(T obj)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < _collection.Length; i++)
|
for (int i = 0; i < _collection.Length; i++)
|
||||||
{
|
{
|
||||||
@ -66,12 +70,12 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
throw new CollectionOverflowException(_collection.Length);
|
||||||
}
|
}
|
||||||
public bool Insert(T obj, int position)
|
public bool Insert(T obj, int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= _collection.Length) // проверка позиции
|
if (position < 0 || position >= _collection.Length) // проверка позиции
|
||||||
return false;
|
throw new PositionOutOfCollectionException(position);
|
||||||
if (_collection[position] == null) // Попытка вставить на указанную позицию
|
if (_collection[position] == null) // Попытка вставить на указанную позицию
|
||||||
{
|
{
|
||||||
_collection[position] = obj;
|
_collection[position] = obj;
|
||||||
@ -93,16 +97,17 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
throw new CollectionOverflowException(_collection.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Remove(int position)
|
public T Remove(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= _collection.Length || _collection[position]==null) // проверка позиции и наличия объекта
|
if (position < 0 || position >= _collection.Length) // проверка позиции
|
||||||
return null;
|
throw new PositionOutOfCollectionException(position);
|
||||||
|
if (_collection[position] == null)
|
||||||
|
throw new ObjectNotFoundException(position);
|
||||||
T temp = _collection[position];
|
T temp = _collection[position];
|
||||||
_collection[position] = null;
|
_collection[position] = null;
|
||||||
|
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,10 +31,10 @@ public class ShipDocks : AbstractCompany
|
|||||||
{
|
{
|
||||||
while (x + _placeSizeWidth < _pictureWidth)
|
while (x + _placeSizeWidth < _pictureWidth)
|
||||||
{
|
{
|
||||||
g.DrawLine(pen, x, y, x + _placeSizeWidth, y);
|
g.DrawLine(pen, x, y, x + _placeSizeWidth - 50, y);
|
||||||
g.DrawLine(pen, x, y, x, y + _placeSizeHeight);
|
g.DrawLine(pen, x, y, x, y + _placeSizeHeight);
|
||||||
g.DrawLine(pen, x, y + _placeSizeHeight, x+_placeSizeWidth, y + _placeSizeHeight);
|
g.DrawLine(pen, x, y + _placeSizeHeight, x + _placeSizeWidth - 50, y + _placeSizeHeight);
|
||||||
x += _placeSizeWidth + 50;
|
x += _placeSizeWidth;
|
||||||
}
|
}
|
||||||
y += _placeSizeHeight;
|
y += _placeSizeHeight;
|
||||||
x = 0;
|
x = 0;
|
||||||
@ -46,10 +46,13 @@ public class ShipDocks : AbstractCompany
|
|||||||
int count = 0;
|
int count = 0;
|
||||||
for (int iy = _placeSizeHeight * 11 + 5; iy >= 0; iy -= _placeSizeHeight)
|
for (int iy = _placeSizeHeight * 11 + 5; iy >= 0; iy -= _placeSizeHeight)
|
||||||
{
|
{
|
||||||
for(int ix = 5; ix + _placeSizeWidth+50 < _pictureWidth; ix += _placeSizeWidth + 50)
|
for(int ix = 5; ix + _placeSizeWidth <= _pictureWidth; ix += _placeSizeWidth)
|
||||||
{
|
{
|
||||||
_collection?.Get(count)?.SetPictureSize(_pictureWidth, _pictureHeight);
|
if (count < _collection.Count)
|
||||||
_collection?.Get(count)?.SetPosition(ix, iy);
|
{
|
||||||
|
_collection?.Get(count)?.SetPictureSize(_pictureWidth, _pictureHeight);
|
||||||
|
_collection?.Get(count)?.SetPosition(ix, iy);
|
||||||
|
}
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Battleship.Drawnings;
|
using Battleship.Drawnings;
|
||||||
|
using Battleship.Exceptions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Battleship.CollectionGenericObjects;
|
namespace Battleship.CollectionGenericObjects;
|
||||||
@ -97,7 +98,7 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
if (_storages.Count == 0)
|
if (_storages.Count == 0)
|
||||||
{
|
{
|
||||||
throw new Exception("В хранилище отсутствуют коллекции для сохранения");
|
throw new InvalidOperationException("В хранилище отсутствуют коллекции для сохранения");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (File.Exists(filename))
|
if (File.Exists(filename))
|
||||||
@ -143,7 +144,7 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
if (!File.Exists(filename))
|
if (!File.Exists(filename))
|
||||||
{
|
{
|
||||||
throw new Exception("Файл не существует");
|
throw new FileNotFoundException("Файл не существует");
|
||||||
}
|
}
|
||||||
|
|
||||||
using (StreamReader sr = new StreamReader(filename))
|
using (StreamReader sr = new StreamReader(filename))
|
||||||
@ -151,7 +152,7 @@ public class StorageCollection<T>
|
|||||||
string? str;
|
string? str;
|
||||||
str = sr.ReadLine();
|
str = sr.ReadLine();
|
||||||
if (str != _collectionKey.ToString())
|
if (str != _collectionKey.ToString())
|
||||||
throw new Exception("В файле неверные данные");
|
throw new FormatException("В файле неверные данные");
|
||||||
_storages.Clear();
|
_storages.Clear();
|
||||||
while ((str = sr.ReadLine()) != null)
|
while ((str = sr.ReadLine()) != null)
|
||||||
{
|
{
|
||||||
@ -164,7 +165,7 @@ public class StorageCollection<T>
|
|||||||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
{
|
||||||
throw new Exception("Не удалось определить тип коллекции:" + record[1]);
|
throw new InvalidOperationException("Не удалось определить тип коллекции:" + record[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||||
@ -174,8 +175,17 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
if (elem?.CreateDrawningShip() is T ship)
|
if (elem?.CreateDrawningShip() is T ship)
|
||||||
{
|
{
|
||||||
if (collection.Insert(ship) == -1)
|
try
|
||||||
throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]);
|
{
|
||||||
|
if (collection.Insert(ship) == -1)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
|
{
|
||||||
|
throw new CollectionOverflowException("Коллекция переполнена", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_storages.Add(record[0], collection);
|
_storages.Add(record[0], collection);
|
||||||
|
10
Battleship/Battleship/FormShipCollection.Designer.cs
generated
10
Battleship/Battleship/FormShipCollection.Designer.cs
generated
@ -66,9 +66,9 @@
|
|||||||
Tools.Controls.Add(panelStorage);
|
Tools.Controls.Add(panelStorage);
|
||||||
Tools.Controls.Add(comboBoxSelectorCompany);
|
Tools.Controls.Add(comboBoxSelectorCompany);
|
||||||
Tools.Dock = DockStyle.Right;
|
Tools.Dock = DockStyle.Right;
|
||||||
Tools.Location = new Point(1605, 40);
|
Tools.Location = new Point(1566, 40);
|
||||||
Tools.Name = "Tools";
|
Tools.Name = "Tools";
|
||||||
Tools.Size = new Size(488, 1224);
|
Tools.Size = new Size(488, 1209);
|
||||||
Tools.TabIndex = 0;
|
Tools.TabIndex = 0;
|
||||||
Tools.TabStop = false;
|
Tools.TabStop = false;
|
||||||
Tools.Text = "Инструменты";
|
Tools.Text = "Инструменты";
|
||||||
@ -249,7 +249,7 @@
|
|||||||
pictureBox.Dock = DockStyle.Fill;
|
pictureBox.Dock = DockStyle.Fill;
|
||||||
pictureBox.Location = new Point(0, 40);
|
pictureBox.Location = new Point(0, 40);
|
||||||
pictureBox.Name = "pictureBox";
|
pictureBox.Name = "pictureBox";
|
||||||
pictureBox.Size = new Size(1605, 1224);
|
pictureBox.Size = new Size(1566, 1209);
|
||||||
pictureBox.TabIndex = 1;
|
pictureBox.TabIndex = 1;
|
||||||
pictureBox.TabStop = false;
|
pictureBox.TabStop = false;
|
||||||
//
|
//
|
||||||
@ -259,7 +259,7 @@
|
|||||||
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
|
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
|
||||||
menuStrip.Location = new Point(0, 0);
|
menuStrip.Location = new Point(0, 0);
|
||||||
menuStrip.Name = "menuStrip";
|
menuStrip.Name = "menuStrip";
|
||||||
menuStrip.Size = new Size(2093, 40);
|
menuStrip.Size = new Size(2054, 40);
|
||||||
menuStrip.TabIndex = 2;
|
menuStrip.TabIndex = 2;
|
||||||
menuStrip.Text = "menuStrip";
|
menuStrip.Text = "menuStrip";
|
||||||
//
|
//
|
||||||
@ -298,7 +298,7 @@
|
|||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(13F, 32F);
|
AutoScaleDimensions = new SizeF(13F, 32F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(2093, 1264);
|
ClientSize = new Size(2054, 1249);
|
||||||
Controls.Add(pictureBox);
|
Controls.Add(pictureBox);
|
||||||
Controls.Add(Tools);
|
Controls.Add(Tools);
|
||||||
Controls.Add(menuStrip);
|
Controls.Add(menuStrip);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using Battleship.CollectionGenericObjects;
|
using Battleship.CollectionGenericObjects;
|
||||||
using Battleship.Drawnings;
|
using Battleship.Drawnings;
|
||||||
|
using Battleship.Exceptions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Battleship;
|
namespace Battleship;
|
||||||
|
|
||||||
@ -11,14 +12,19 @@ public partial class FormShipCollection : Form
|
|||||||
/// Компания
|
/// Компания
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private AbstractCompany? _company = null;
|
private AbstractCompany? _company = null;
|
||||||
|
/// <summary>
|
||||||
|
/// Логер
|
||||||
|
/// </summary>
|
||||||
|
private readonly ILogger _logger;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public FormShipCollection()
|
public FormShipCollection(ILogger<FormShipCollection> logger)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_storageCollection = new();
|
_storageCollection = new();
|
||||||
}
|
_logger = logger;
|
||||||
|
}
|
||||||
#region Работа с компанией
|
#region Работа с компанией
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Выбор компании
|
/// Выбор компании
|
||||||
@ -30,6 +36,33 @@ public partial class FormShipCollection : Form
|
|||||||
panelCompanyTools.Enabled = false;
|
panelCompanyTools.Enabled = false;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// Метод установки корабля в компанию
|
||||||
|
/// </summary>
|
||||||
|
private void SetShip(DrawningShip? ship)
|
||||||
|
{
|
||||||
|
if (_company == null)
|
||||||
|
return;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_company + ship != -1)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Объект добавлен");
|
||||||
|
pictureBox.Image = _company.Show();
|
||||||
|
_logger.LogInformation("Добавление корабля {ship} в коллекцию", ship);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("Не удалось добавить объект");
|
||||||
|
_logger.LogInformation("Не удалось добавить корабль {ship} в коллекцию", ship);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(CollectionOverflowException ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Ошибка переполнения коллекции");
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
/// Кнопка удаления корабля
|
/// Кнопка удаления корабля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
@ -45,17 +78,32 @@ public partial class FormShipCollection : Form
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int pos = Convert.ToInt32(maskedTextBox.Text);
|
||||||
|
if (_company - pos != null)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Объект удален");
|
||||||
|
pictureBox.Image = _company.Show();
|
||||||
|
_logger.LogInformation("Удаление корабля по индексу {pos}", pos);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("Не удалось удалить объект");
|
||||||
|
_logger.LogInformation("Не удалось удалить корабль из коллекции по индексу {pos}", pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(ObjectNotFoundException ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Ошибка: отсутствует объект");
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
|
}
|
||||||
|
catch (PositionOutOfCollectionException ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Ошибка: неправильная позиция");
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
int pos = Convert.ToInt32(maskedTextBox.Text);
|
|
||||||
if (_company - pos != null)
|
|
||||||
{
|
|
||||||
MessageBox.Show("Объект удален");
|
|
||||||
pictureBox.Image = _company.Show();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageBox.Show("Не удалось удалить объект");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Кнопка отправки на тест
|
/// Кнопка отправки на тест
|
||||||
@ -118,23 +166,7 @@ public partial class FormShipCollection : Form
|
|||||||
form.AddEvent(SetShip);
|
form.AddEvent(SetShip);
|
||||||
form.Show();
|
form.Show();
|
||||||
}
|
}
|
||||||
/// <summary>
|
|
||||||
/// Метод установки корабля в компанию
|
|
||||||
/// </summary>
|
|
||||||
private void SetShip(DrawningShip? ship)
|
|
||||||
{
|
|
||||||
if (_company == null)
|
|
||||||
return;
|
|
||||||
if (_company + ship != -1)
|
|
||||||
{
|
|
||||||
MessageBox.Show("Объект добавлен");
|
|
||||||
pictureBox.Image = _company.Show();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageBox.Show("Не удалось добавить объект");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
#endregion
|
||||||
#region Работа с коллекцией
|
#region Работа с коллекцией
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -147,6 +179,7 @@ public partial class FormShipCollection : Form
|
|||||||
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
|
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
_logger.LogInformation("Не удалось добавить коллекцию: не все данные заполнены");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,6 +193,7 @@ public partial class FormShipCollection : Form
|
|||||||
collectionType = CollectionType.List;
|
collectionType = CollectionType.List;
|
||||||
}
|
}
|
||||||
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||||||
|
_logger.LogInformation("Добавлена коллекция типа {type} с названием {name}", collectionType, textBoxCollectionName.Text);
|
||||||
RerfreshListBoxItems();
|
RerfreshListBoxItems();
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -177,6 +211,7 @@ public partial class FormShipCollection : Form
|
|||||||
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
|
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
|
||||||
return;
|
return;
|
||||||
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
||||||
|
_logger.LogInformation("Удаление коллекции с названием {name}", listBoxCollection.SelectedItem.ToString());
|
||||||
RerfreshListBoxItems();
|
RerfreshListBoxItems();
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -220,8 +255,9 @@ public partial class FormShipCollection : Form
|
|||||||
case "Доки":
|
case "Доки":
|
||||||
_company = new ShipDocks(pictureBox.Width, pictureBox.Height, collection);
|
_company = new ShipDocks(pictureBox.Width, pictureBox.Height, collection);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
panelCompanyTools.Enabled = true;
|
panelCompanyTools.Enabled = true;
|
||||||
RerfreshListBoxItems();
|
RerfreshListBoxItems();
|
||||||
}
|
}
|
||||||
@ -236,13 +272,16 @@ public partial class FormShipCollection : Form
|
|||||||
{
|
{
|
||||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_storageCollection.SaveData(saveFileDialog.FileName))
|
|
||||||
{
|
try {
|
||||||
|
_storageCollection.SaveData(saveFileDialog.FileName);
|
||||||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
|
||||||
}
|
}
|
||||||
else
|
catch(Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -255,14 +294,17 @@ public partial class FormShipCollection : Form
|
|||||||
{
|
{
|
||||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_storageCollection.LoadData(openFileDialog.FileName))
|
try
|
||||||
{
|
{
|
||||||
|
_storageCollection.LoadData(openFileDialog.FileName);
|
||||||
RerfreshListBoxItems();
|
RerfreshListBoxItems();
|
||||||
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
_logger.LogInformation("Загрузка из файла: {filename}", saveFileDialog.FileName);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Загрузка не выполнена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show("Загрузка не выполнена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace Battleship
|
namespace Battleship
|
||||||
{
|
{
|
||||||
internal static class Program
|
internal static class Program
|
||||||
@ -11,7 +16,30 @@ namespace Battleship
|
|||||||
// To customize application configuration such as set high DPI settings or default font,
|
// To customize application configuration such as set high DPI settings or default font,
|
||||||
// see https://aka.ms/applicationconfiguration.
|
// see https://aka.ms/applicationconfiguration.
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
Application.Run(new FormShipCollection());
|
var services = new ServiceCollection();
|
||||||
|
ConfigureServices(services);
|
||||||
|
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
|
||||||
|
{
|
||||||
|
Application.Run(serviceProvider.GetRequiredService<FormShipCollection>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static void ConfigureServices(ServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<FormShipCollection>()
|
||||||
|
.AddLogging(option =>
|
||||||
|
{
|
||||||
|
var configuration = new ConfigurationBuilder()
|
||||||
|
.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
|
.AddJsonFile(path: "C:\\Users\\grishazagidulin\\source\\repos\\Pi-12_Zagidulin_GA_Simple\\Battleship\\Battleship\\appSetting.json", optional: false, reloadOnChange: true)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var logger = new LoggerConfiguration()
|
||||||
|
.ReadFrom.Configuration(configuration)
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
|
option.SetMinimumLevel(LogLevel.Information);
|
||||||
|
option.AddSerilog(logger);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
20
Battleship/Battleship/appSetting.json
Normal file
20
Battleship/Battleship/appSetting.json
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Information",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/log_.log",
|
||||||
|
"rollingInterval": "Day",
|
||||||
|
"outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
|
||||||
|
"Properties": {
|
||||||
|
"Application": "Battleship"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user