8 Commits

Author SHA1 Message Date
5b23314bce Call of sortions implementation. 2022-12-06 16:31:24 +04:00
e3831212b6 Some fixes. 2022-12-06 16:30:05 +04:00
57fd0ad585 Compare by color logic. 2022-12-06 16:28:58 +04:00
a913fca4df Equals method in AbstractMap. 2022-12-06 16:28:04 +04:00
1dbafea6ea Comparer classes were implemented. 2022-12-06 13:46:45 +04:00
879c1e15ec Method to check equality of objects was implemented. 2022-12-06 13:28:41 +04:00
d622a4d68c Logging is implemented. 2022-11-20 17:48:39 +04:00
42ba1341e8 Throwing exceptions. 2022-11-20 02:40:26 +04:00
15 changed files with 495 additions and 48 deletions

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@@ -202,5 +202,45 @@ namespace AirFighter
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
public bool Equals(AbstractMap? other)
{
if (other == null)
{
return false;
}
var diffMap = other;
if (diffMap == null)
{
return false;
}
if (diffMap._height != _height)
{
return false;
}
if (diffMap._width != _width)
{
return false;
}
if (diffMap._size_x != _size_x)
{
return false;
}
if (diffMap._size_y != _size_y )
{
return false;
}
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (diffMap._map[i, j] != _map[j, i])
{
return false;
}
}
}
return true;
}
}
}

View File

@@ -8,6 +8,27 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>

View File

@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class AircraftCompareByColor : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xAircraft = x as DrawingObjectAircraft;
var yAircraft = y as DrawingObjectAircraft;
if (xAircraft == null && yAircraft == null)
{
return 0;
}
if (xAircraft == null && yAircraft != null)
{
return 1;
}
if (xAircraft != null && yAircraft == null)
{
return -1;
}
if (xAircraft.GetAircraft.Plane.BodyColor.R.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.R) != 0)
{
return xAircraft.GetAircraft.Plane.BodyColor.R.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.R);
}
if (xAircraft.GetAircraft.Plane.BodyColor.G.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.G) != 0)
{
return xAircraft.GetAircraft.Plane.BodyColor.G.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.G);
}
if (xAircraft.GetAircraft.Plane.BodyColor.B.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.B) != 0)
{
return xAircraft.GetAircraft.Plane.BodyColor.B.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.B);
}
if (xAircraft.GetAircraft.Plane is EntityMilitaryAircraft xMilitaryAircraft && yAircraft.GetAircraft.Plane is EntityMilitaryAircraft yMilitaryAircraft)
{
if (xMilitaryAircraft.ExtraColor.R.CompareTo(yMilitaryAircraft.ExtraColor.R) != 0)
{
return xMilitaryAircraft.ExtraColor.R.CompareTo(yMilitaryAircraft.ExtraColor.R);
}
if (xMilitaryAircraft.ExtraColor.G.CompareTo(yMilitaryAircraft.ExtraColor.G) != 0)
{
return xMilitaryAircraft.ExtraColor.G.CompareTo(yMilitaryAircraft.ExtraColor.G);
}
if (xMilitaryAircraft.ExtraColor.B.CompareTo(yMilitaryAircraft.ExtraColor.B) != 0)
{
return xMilitaryAircraft.ExtraColor.B.CompareTo(yMilitaryAircraft.ExtraColor.B);
}
}
var speedCompare = xAircraft.GetAircraft.Plane.Speed.CompareTo(yAircraft.GetAircraft.Plane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAircraft.GetAircraft.Plane.Weight.CompareTo(yAircraft.GetAircraft.Plane.Weight);
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class AircraftCompareByType : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xAircraft = x as DrawingObjectAircraft;
var yAircraft = y as DrawingObjectAircraft;
if (xAircraft == null && yAircraft == null)
{
return 0;
}
if (xAircraft == null && yAircraft != null)
{
return 1;
}
if (xAircraft != null && yAircraft == null)
{
return -1;
}
if (xAircraft.GetAircraft.GetType().Name != yAircraft.GetAircraft.GetType().Name)
{
if (xAircraft.GetAircraft.GetType().Name == "DrawingAircraft")
{
return -1;
}
return 1;
}
var speedCompare = xAircraft.GetAircraft.Plane.Speed.CompareTo(yAircraft.GetAircraft.Plane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAircraft.GetAircraft.Plane.Weight.CompareTo(yAircraft.GetAircraft.Plane.Weight);
}
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class AircraftNotFoundException : ApplicationException
{
public AircraftNotFoundException() : base() { }
public AircraftNotFoundException(string message) : base(message) { }
public AircraftNotFoundException(string message, Exception exception) : base(message, exception) { }
public AircraftNotFoundException(SerializationInfo info, StreamingContext contex) : base(info,contex) { }
}
}

View File

@@ -17,6 +17,8 @@ namespace AirFighter
public float Step => _aircraft.Plane?.Step ?? 0;
public DrawingAircraft GetAircraft => _aircraft;
public (float Left, float Top, float Right, float Bottom) GetCurrentPosition()
{
return _aircraft?.GetCurrentPosition() ?? default;
@@ -47,7 +49,55 @@ namespace AirFighter
public string ReceiveInfo() => _aircraft?.ReceiveDataForSaving();
public static IDrawingObject Create(string data) => new DrawingObjectAircraft(data.CreateDrawingAircraft());
}
public bool Equals(IDrawingObject? other)
{
if (other == null)
{
return false;
}
var diffAircraft = other as DrawingObjectAircraft;
if (diffAircraft == null)
{
return false;
}
var aircraft = _aircraft.Plane;
var diffAircraftCast = diffAircraft._aircraft.Plane;
if (aircraft.Speed != diffAircraftCast.Speed)
{
return false;
}
if (aircraft.Weight != diffAircraftCast.Weight)
{
return false;
}
if (aircraft.BodyColor != diffAircraftCast.BodyColor)
{
return false;
}
if (aircraft is EntityMilitaryAircraft || diffAircraftCast is EntityMilitaryAircraft)
{
if (aircraft is EntityMilitaryAircraft && diffAircraftCast is EntityMilitaryAircraft)
{
if ((aircraft as EntityMilitaryAircraft).ExtraColor != (diffAircraftCast as EntityMilitaryAircraft).ExtraColor)
{
return false;
}
if ((aircraft as EntityMilitaryAircraft).Rockets != (diffAircraftCast as EntityMilitaryAircraft).Rockets)
{
return false;
}
if ((aircraft as EntityMilitaryAircraft).ExtraWings != (diffAircraftCast as EntityMilitaryAircraft).ExtraWings)
{
return false;
}
return true;
}
return false;
}
return true;
}
}
}

View File

@@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.PanelGroupBox = new System.Windows.Forms.GroupBox();
this.SortByColorButton = new System.Windows.Forms.Button();
this.SortByTypeButton = new System.Windows.Forms.Button();
this.MapsGroupBox = new System.Windows.Forms.GroupBox();
this.DeleteMapButton = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
@@ -59,6 +61,8 @@
//
// PanelGroupBox
//
this.PanelGroupBox.Controls.Add(this.SortByColorButton);
this.PanelGroupBox.Controls.Add(this.SortByTypeButton);
this.PanelGroupBox.Controls.Add(this.MapsGroupBox);
this.PanelGroupBox.Controls.Add(this.buttonDown);
this.PanelGroupBox.Controls.Add(this.buttonRight);
@@ -72,11 +76,31 @@
this.PanelGroupBox.Dock = System.Windows.Forms.DockStyle.Right;
this.PanelGroupBox.Location = new System.Drawing.Point(672, 24);
this.PanelGroupBox.Name = "PanelGroupBox";
this.PanelGroupBox.Size = new System.Drawing.Size(218, 599);
this.PanelGroupBox.Size = new System.Drawing.Size(218, 702);
this.PanelGroupBox.TabIndex = 0;
this.PanelGroupBox.TabStop = false;
this.PanelGroupBox.Text = "Function panel";
//
// SortByColorButton
//
this.SortByColorButton.Location = new System.Drawing.Point(12, 339);
this.SortByColorButton.Name = "SortByColorButton";
this.SortByColorButton.Size = new System.Drawing.Size(176, 36);
this.SortByColorButton.TabIndex = 17;
this.SortByColorButton.Text = "Sort by color";
this.SortByColorButton.UseVisualStyleBackColor = true;
this.SortByColorButton.Click += new System.EventHandler(this.SortByColorButton_Click);
//
// SortByTypeButton
//
this.SortByTypeButton.Location = new System.Drawing.Point(12, 297);
this.SortByTypeButton.Name = "SortByTypeButton";
this.SortByTypeButton.Size = new System.Drawing.Size(176, 36);
this.SortByTypeButton.TabIndex = 16;
this.SortByTypeButton.Text = "Sort by type";
this.SortByTypeButton.UseVisualStyleBackColor = true;
this.SortByTypeButton.Click += new System.EventHandler(this.SortByTypeButton_Click);
//
// MapsGroupBox
//
this.MapsGroupBox.Controls.Add(this.DeleteMapButton);
@@ -145,7 +169,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AirFighter.Properties.Resources.ArrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(99, 557);
this.buttonDown.Location = new System.Drawing.Point(99, 660);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 11;
@@ -157,7 +181,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AirFighter.Properties.Resources.ArrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(135, 557);
this.buttonRight.Location = new System.Drawing.Point(135, 660);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 10;
@@ -166,7 +190,7 @@
//
// ShowMapButton
//
this.ShowMapButton.Location = new System.Drawing.Point(6, 483);
this.ShowMapButton.Location = new System.Drawing.Point(12, 578);
this.ShowMapButton.Name = "ShowMapButton";
this.ShowMapButton.Size = new System.Drawing.Size(182, 36);
this.ShowMapButton.TabIndex = 14;
@@ -179,7 +203,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AirFighter.Properties.Resources.ArrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(63, 557);
this.buttonLeft.Location = new System.Drawing.Point(63, 660);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 9;
@@ -188,7 +212,7 @@
//
// ShowHangarButton
//
this.ShowHangarButton.Location = new System.Drawing.Point(6, 447);
this.ShowHangarButton.Location = new System.Drawing.Point(12, 542);
this.ShowHangarButton.Name = "ShowHangarButton";
this.ShowHangarButton.Size = new System.Drawing.Size(182, 30);
this.ShowHangarButton.TabIndex = 13;
@@ -201,7 +225,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AirFighter.Properties.Resources.ArrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(99, 521);
this.buttonUp.Location = new System.Drawing.Point(99, 624);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 8;
@@ -210,7 +234,7 @@
//
// DeleteAircraftButton
//
this.DeleteAircraftButton.Location = new System.Drawing.Point(6, 405);
this.DeleteAircraftButton.Location = new System.Drawing.Point(12, 500);
this.DeleteAircraftButton.Name = "DeleteAircraftButton";
this.DeleteAircraftButton.Size = new System.Drawing.Size(182, 36);
this.DeleteAircraftButton.TabIndex = 12;
@@ -220,7 +244,7 @@
//
// maskedTextBoxPostion
//
this.maskedTextBoxPostion.Location = new System.Drawing.Point(6, 358);
this.maskedTextBoxPostion.Location = new System.Drawing.Point(12, 453);
this.maskedTextBoxPostion.Mask = "00";
this.maskedTextBoxPostion.Name = "maskedTextBoxPostion";
this.maskedTextBoxPostion.Size = new System.Drawing.Size(182, 23);
@@ -228,7 +252,7 @@
//
// AddAircraftButton
//
this.AddAircraftButton.Location = new System.Drawing.Point(6, 317);
this.AddAircraftButton.Location = new System.Drawing.Point(12, 412);
this.AddAircraftButton.Name = "AddAircraftButton";
this.AddAircraftButton.Size = new System.Drawing.Size(182, 35);
this.AddAircraftButton.TabIndex = 10;
@@ -241,7 +265,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 24);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(672, 599);
this.pictureBox.Size = new System.Drawing.Size(672, 702);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@@ -266,14 +290,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(100, 22);
this.SaveToolStripMenuItem.Text = "Save";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(100, 22);
this.LoadToolStripMenuItem.Text = "Load";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@@ -290,7 +314,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(890, 623);
this.ClientSize = new System.Drawing.Size(890, 726);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.PanelGroupBox);
this.Controls.Add(this.menuStrip);
@@ -334,5 +358,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button SortByColorButton;
private Button SortByTypeButton;
}
}

View File

@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@@ -21,9 +22,12 @@ namespace AirFighter
private readonly MapsCollection _mapsCollection;
public FormMapWithSetAircrafts()
private readonly ILogger _logger;
public FormMapWithSetAircrafts(ILogger<FormMapWithSetAircrafts> logger)
{
InitializeComponent();
_logger = logger;
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxMapSelection.Items.Clear();
foreach (var elem in _mapsDict)
@@ -76,16 +80,32 @@ namespace AirFighter
return;
}
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectAircraft(aircraft) != -1)
try
{
MessageBox.Show("Object is added");
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectAircraft(aircraft) != -1)
{
MessageBox.Show("Object is added");
_logger.LogInformation($"{aircraft.Plane.GetType()} was added.");
}
else
{
MessageBox.Show("Unable to add object");
_logger.LogInformation($"Unable to add object.");
}
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
catch (VaultOverflowException ex)
{
MessageBox.Show("Unable to add object");
_logger.LogWarning($"Vault overflow exception: {ex.Message}");
MessageBox.Show($"Vault overflow exception: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
_logger.LogWarning($"Unknown error: {ex.Message}");
MessageBox.Show($"Unknown error: {ex.Message}");
}
}
private void DeleteAircraftButton_Click(object sender, EventArgs e)
@@ -104,15 +124,31 @@ namespace AirFighter
return;
}
int pos = Convert.ToInt32(maskedTextBoxPostion.Text);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
try
{
MessageBox.Show("Object is deleted");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{
MessageBox.Show("Object is deleted");
_logger.LogInformation($"Aircraft was removed from {pos} position.");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Unable to delete object");
_logger.LogInformation($"Unable to remove aircraft from {pos} position.");
}
}
else
catch(AircraftNotFoundException ex)
{
MessageBox.Show("Unable to delete object");
MessageBox.Show($"Deletion error: {ex.Message}");
_logger.LogWarning($"Aircraft is not found exception: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Unknown error: {ex.Message}");
_logger.LogWarning($"Unknow error: {ex.Message}.");
}
}
private void ShowHangarButton_Click(object sender, EventArgs e)
@@ -164,20 +200,24 @@ namespace AirFighter
if (comboBoxMapSelection.SelectedIndex == -1 || string.IsNullOrEmpty(newMapTextBox.Text))
{
MessageBox.Show("Not all data is filled", " Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning("Failed to add map since {0}", comboBoxMapSelection.SelectedIndex == -1? "example of map wasn't choosen." : "name of map is missing");
return;
}
if (!_mapsDict.ContainsKey(comboBoxMapSelection.Text))
{
MessageBox.Show("There is no such map","Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"There is no map with {comboBoxMapSelection.Text} name.");
return;
}
_mapsCollection.AddMap(newMapTextBox.Text, _mapsDict[comboBoxMapSelection.Text]);
RefreshMaps();
_logger.LogInformation("Map was added.");
}
private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
_logger.LogInformation($"Map was changed to {listBoxMaps.SelectedItem}.");
}
private void DeleteMapButton_Click(object sender, EventArgs e)
@@ -191,6 +231,7 @@ namespace AirFighter
{
_mapsCollection.DeleteMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
RefreshMaps();
_logger.LogInformation($"Map was removed {listBoxMaps.SelectedItem}.");
}
}
@@ -198,13 +239,16 @@ namespace AirFighter
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.SaveData(saveFileDialog.FileName))
try
{
_mapsCollection.SaveData(saveFileDialog.FileName);
MessageBox.Show("Data is saved successfully!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation($"{saveFileDialog.FileName} is saved.");
}
else
catch(Exception ex)
{
MessageBox.Show("Something went wrong!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"Something went wrong: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Unable to save: {saveFileDialog.FileName} | {ex.Message}.");
}
}
}
@@ -213,16 +257,43 @@ namespace AirFighter
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(openFileDialog.FileName))
try
{
_mapsCollection.LoadData(openFileDialog.FileName);
RefreshMaps();
MessageBox.Show("Data is loaded successfully!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation($"{openFileDialog.FileName} is loaded.");
}
else
catch (Exception ex)
{
MessageBox.Show("Something went wrong!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"Something went wrong: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Unable to load: {openFileDialog.FileName} | {ex.Message}.");
}
}
}
private void SortByTypeButton_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ??
string.Empty].Sort(new AircraftCompareByType());
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void SortByColorButton_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ??
string.Empty].Sort(new AircraftCompareByColor());
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal interface IDrawingObject
internal interface IDrawingObject : IEquatable<IDrawingObject>
{
public float Step { get; }
void SetObject(int x, int y, int width, int height);

View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal class MapWithSetAircraftsGeneric<T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
private readonly int _pictureWidth;
@@ -91,6 +91,11 @@ namespace AirFighter
_setAircrafts.Insert(DrawingObjectAircraft.Create(record) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setAircrafts.SortSet(comparer);
}
private void Shaking()
{
int j = _setAircrafts.Count - 1;

View File

@@ -55,7 +55,7 @@ namespace AirFighter
}
public bool SaveData(string filename)
public void SaveData(string filename)
{
if (File.Exists(filename))
{
@@ -68,16 +68,14 @@ namespace AirFighter
{
sw.WriteLine($"{vault.Key}{separatorDict}{vault.Value.GetData(separatorDict, separatorData)}");
}
}
return true;
}
}
public bool LoadData(string filename)
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
throw new FileNotFoundException("File was not found.");
}
string str;
@@ -91,7 +89,7 @@ namespace AirFighter
{
if (!str.Contains("MapsCollection"))
{
return false;
throw new FileFormatException("Wrong data format in file.");
}
count++;
}
@@ -116,7 +114,7 @@ namespace AirFighter
}
return true;
}
}

View File

@@ -1,3 +1,11 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.Logging;
using Serilog;
using System.ServiceProcess;
namespace AirFighter
{
internal static class Program
@@ -11,7 +19,32 @@ namespace AirFighter
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormMapWithSetAircrafts());
var services = new ServiceCollection();
ConfigureServices(services);
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
{
Application.Run(serviceProvider.GetRequiredService<FormMapWithSetAircrafts>());
}
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddSingleton<FormMapWithSetAircrafts>().AddLogging(option =>
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(path: "appsettings.json", false, true)
.Build();
var serilogLogger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();
option.SetMinimumLevel(LogLevel.Information);
option.AddSerilog(serilogLogger);
});
}
}
}

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal class SetAircraftsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
@@ -29,8 +29,17 @@ namespace AirFighter
public int Insert(T aircraft, int position)
{
foreach (var instAircraft in _places)
{
if (aircraft.Equals(instAircraft))
{
return -1;
}
}
if (position > Count || position < 0 || Count == _maxCount)
{
throw new VaultOverflowException("Overflow of storage.");
return -1;
}
@@ -54,7 +63,7 @@ namespace AirFighter
return null;
}
throw new AircraftNotFoundException($"There is no aircraft in {position} position.");
return null;
}
@@ -96,5 +105,14 @@ namespace AirFighter
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class VaultOverflowException : ApplicationException
{
public VaultOverflowException() : base() { }
public VaultOverflowException(string message) : base(message) { }
public VaultOverflowException(string message, Exception exception) : base(message, exception) { }
protected VaultOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}
}

View File

@@ -0,0 +1,17 @@
{
"Serilog": {
"Using": ["Serilog.Sinks.File" ],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/logs.log",
"outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
}
}