From 99d1787cca0d9dfe5fab2ae448651a93a29db67a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Fri, 11 Nov 2022 18:25:54 +0400 Subject: [PATCH 1/9] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=206=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/ExtentionShip.cs | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 WarmlyShip/WarmlyShip/ExtentionShip.cs diff --git a/WarmlyShip/WarmlyShip/ExtentionShip.cs b/WarmlyShip/WarmlyShip/ExtentionShip.cs new file mode 100644 index 0000000..84c237c --- /dev/null +++ b/WarmlyShip/WarmlyShip/ExtentionShip.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal static class ExtentionShip + { + + private static readonly char _separatorForObject = ':'; + + public static DrawingWarmlyShip CreateDrawningCar(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawingWarmlyShip(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawningMotorShip(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), Convert.ToBoolean(strs[5])); + } + return null; + } + + public static string GetDataForSave(this DrawingWarmlyShip warmlyShip) + { + var ship = warmlyShip.warmlyShip; + var str = $"{ship.Speed}{_separatorForObject}{ship.Weight}{_separatorForObject}{ship.BodyColor.Name}"; + if (ship is not EntityMotorShip motorShip) + { + return str; + } + return $"{str}{_separatorForObject}{motorShip.DopColor.Name}{_separatorForObject}{motorShip.Tubes}{_separatorForObject}{motorShip.Cistern}"; + } + } +} -- 2.25.1 From ce21b5e68f5f73f2d3ce9e66c4636919d77f4a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Fri, 11 Nov 2022 18:57:16 +0400 Subject: [PATCH 2/9] 1111 --- WarmlyShip/WarmlyShip/DrawningObjectShip.cs | 4 ++++ WarmlyShip/WarmlyShip/EntityWarmlyShip.cs | 6 ++++++ WarmlyShip/WarmlyShip/ExtentionShip.cs | 2 +- WarmlyShip/WarmlyShip/IDrawningObject.cs | 2 ++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs index e9c2b8a..f0da2a3 100644 --- a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs +++ b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs @@ -36,5 +36,9 @@ namespace WarmlyShip { _warmlyShip?.SetPosition(x, y, width, height); } + + public string GetInfo() => _warmlyShip?.GetDataForSave(); + + public static IDrawningObject Create(string data) => new DrawningObjectShip(data.CreateDrawningShip()); } } diff --git a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs index 79878fb..165ec36 100644 --- a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs @@ -21,5 +21,11 @@ namespace WarmlyShip Weight = weight <= 0 ? random.Next(1000, 2000) : weight; BodyColor = bodyColor; } + + public static EntityWarmlyShip Creator(string data) + { + string[] strs = data.Split(':'); + return new EntityWarmlyShip(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } } } diff --git a/WarmlyShip/WarmlyShip/ExtentionShip.cs b/WarmlyShip/WarmlyShip/ExtentionShip.cs index 84c237c..bbb188d 100644 --- a/WarmlyShip/WarmlyShip/ExtentionShip.cs +++ b/WarmlyShip/WarmlyShip/ExtentionShip.cs @@ -11,7 +11,7 @@ namespace WarmlyShip private static readonly char _separatorForObject = ':'; - public static DrawingWarmlyShip CreateDrawningCar(this string info) + public static DrawingWarmlyShip CreateDrawningShip(this string info) { string[] strs = info.Split(_separatorForObject); if (strs.Length == 3) diff --git a/WarmlyShip/WarmlyShip/IDrawningObject.cs b/WarmlyShip/WarmlyShip/IDrawningObject.cs index a732b2c..94af911 100644 --- a/WarmlyShip/WarmlyShip/IDrawningObject.cs +++ b/WarmlyShip/WarmlyShip/IDrawningObject.cs @@ -13,5 +13,7 @@ namespace WarmlyShip void MoveObject(Direction direction); void DrawningObject(Graphics g); (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + + string GetInfo(); } } -- 2.25.1 From 2906c0ea796523d3f5a75d8755ed36aee62542c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 13 Nov 2022 16:11:54 +0400 Subject: [PATCH 3/9] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/FormShipConfig.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WarmlyShip/WarmlyShip/FormShipConfig.cs b/WarmlyShip/WarmlyShip/FormShipConfig.cs index 8e7fa69..11452fc 100644 --- a/WarmlyShip/WarmlyShip/FormShipConfig.cs +++ b/WarmlyShip/WarmlyShip/FormShipConfig.cs @@ -15,7 +15,7 @@ namespace WarmlyShip DrawingWarmlyShip _warmlyShip = null; - private event ShipDelegate EventAddShip; + private Action EventAddShip; public FormShipConfig() { @@ -41,11 +41,11 @@ namespace WarmlyShip pictureBoxObject.Image = bmp; } - public void AddEvent(ShipDelegate ev) + public void AddEvent(Action ev) { if (EventAddShip == null) { - EventAddShip = new ShipDelegate(ev); + EventAddShip = ev; } else { -- 2.25.1 From 1fea5cae98c23afaa06a13e2f7707c94e16f0b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 13 Nov 2022 16:59:52 +0400 Subject: [PATCH 4/9] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/EntityWarmlyShip.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs index 165ec36..efa81b4 100644 --- a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs @@ -22,10 +22,5 @@ namespace WarmlyShip BodyColor = bodyColor; } - public static EntityWarmlyShip Creator(string data) - { - string[] strs = data.Split(':'); - return new EntityWarmlyShip(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2])); - } } } -- 2.25.1 From b9442a94dcd43cc5dbdca7c39210f8ef4d7013ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 13 Nov 2022 19:36:24 +0400 Subject: [PATCH 5/9] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/EntityWarmlyShip.cs | 1 - .../WarmlyShip/FormMapWithSetShip.Designer.cs | 75 ++++++++++++++++--- WarmlyShip/WarmlyShip/FormMapWithSetShip.cs | 30 ++++++++ WarmlyShip/WarmlyShip/FormMapWithSetShip.resx | 9 +++ .../WarmlyShip/MapWithSetShipGeneric.cs | 30 +++++--- WarmlyShip/WarmlyShip/MapsCollection.cs | 75 ++++++++++++++++++- 6 files changed, 194 insertions(+), 26 deletions(-) diff --git a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs index efa81b4..79878fb 100644 --- a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs @@ -21,6 +21,5 @@ namespace WarmlyShip Weight = weight <= 0 ? random.Next(1000, 2000) : weight; BodyColor = bodyColor; } - } } diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs index 21eb0cf..c9c8ff4 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs @@ -45,9 +45,16 @@ this.buttonRemoveShip = new System.Windows.Forms.Button(); this.buttonAddShip = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.menuStrip = new System.Windows.Forms.MenuStrip(); + this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.groupBox1.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip.SuspendLayout(); this.SuspendLayout(); // // groupBox1 @@ -63,9 +70,9 @@ this.groupBox1.Controls.Add(this.buttonRemoveShip); this.groupBox1.Controls.Add(this.buttonAddShip); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(815, 0); + this.groupBox1.Location = new System.Drawing.Point(815, 24); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 648); + this.groupBox1.Size = new System.Drawing.Size(200, 624); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; @@ -132,7 +139,6 @@ this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); this.comboBoxSelectorMap.TabIndex = 0; - //this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // maskedTextBoxPosition // @@ -147,7 +153,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::WarmlyShip.Properties.Resources.todown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(75, 589); + this.buttonDown.Location = new System.Drawing.Point(75, 565); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(50, 50); this.buttonDown.TabIndex = 10; @@ -159,7 +165,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::WarmlyShip.Properties.Resources.toleft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(19, 589); + this.buttonLeft.Location = new System.Drawing.Point(19, 565); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(50, 50); this.buttonLeft.TabIndex = 9; @@ -171,7 +177,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::WarmlyShip.Properties.Resources.totop; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(75, 533); + this.buttonUp.Location = new System.Drawing.Point(75, 509); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(50, 50); this.buttonUp.TabIndex = 8; @@ -183,7 +189,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::WarmlyShip.Properties.Resources.toright; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(131, 589); + this.buttonRight.Location = new System.Drawing.Point(131, 565); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(50, 50); this.buttonRight.TabIndex = 7; @@ -233,12 +239,52 @@ // pictureBox // this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(815, 648); + this.pictureBox.Size = new System.Drawing.Size(815, 624); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip + // + this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip.Location = new System.Drawing.Point(0, 0); + this.menuStrip.Name = "menuStrip"; + this.menuStrip.Size = new System.Drawing.Size(1015, 24); + this.menuStrip.TabIndex = 2; + // + // файлToolStripMenuItem + // + this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + this.файлToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Text = "Сохранение"; + 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.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.Filter = "txt file | *txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *txt"; + // // FormMapWithSetShip // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -246,6 +292,8 @@ this.ClientSize = new System.Drawing.Size(1015, 648); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); + this.Controls.Add(this.menuStrip); + this.MainMenuStrip = this.menuStrip; this.Name = "FormMapWithSetShip"; this.Text = "FormMapWithSetShip"; this.groupBox1.ResumeLayout(false); @@ -253,7 +301,10 @@ this.groupBoxMaps.ResumeLayout(false); this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip.ResumeLayout(false); + this.menuStrip.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -276,5 +327,11 @@ private ListBox listBoxMaps; private Button buttonAddMap; private TextBox textBoxNewMapName; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs index cbf17ab..c751166 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs @@ -183,5 +183,35 @@ namespace WarmlyShip } pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } + + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.resx b/WarmlyShip/WarmlyShip/FormMapWithSetShip.resx index f298a7b..934ed35 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.resx +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 265, 17 + \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs index 01d1c4c..209da81 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs @@ -65,6 +65,24 @@ namespace WarmlyShip return new(_pictureWidth, _pictureHeight); } + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var ship in _setShips.GetShips()) + { + data += $"{ship.GetInfo()}{separatorData}"; + } + return data; + } + + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setShips.Insert(DrawningObjectShip.Create(rec) as T); + } + } + private void Shaking() { int j = _setShips.Count - 1; @@ -107,17 +125,6 @@ namespace WarmlyShip private void DrawShips(Graphics g) { - /*for (int i = 0; i < _setShips.Count; ++i) - { - if (_setShips[i] != null ) - { - int temp = 0; - if (_setShips[i].GetCurrentPosition().Bottom - _setShips[i].GetCurrentPosition().Top < 75) temp = (int)(_setShips[i].GetCurrentPosition().Bottom - _setShips[i].GetCurrentPosition().Top); - _setShips[i].SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight); - _setShips[i].DrawningObject(g); - } - }*/ - int i = 0; foreach (var ship in _setShips.GetShips()) { @@ -131,6 +138,5 @@ namespace WarmlyShip ++i; } } - } } diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index badab5b..3b144de 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -9,7 +9,7 @@ namespace WarmlyShip { internal class MapsCollection { - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; public List Keys => _mapStorages.Keys.ToList(); @@ -17,9 +17,13 @@ namespace WarmlyShip private readonly int _pictureHeight; + private readonly char separatorDict = '|'; + + private readonly char separatorData = ';'; + public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -27,7 +31,7 @@ namespace WarmlyShip public void AddMap(string name, AbstractMap map) { if (_mapStorages.ContainsKey(name)) return; - _mapStorages.Add(name, new MapWithSetShipGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetShipGeneric(_pictureWidth, _pictureHeight, map)); } public void DelMap(string name) @@ -36,7 +40,7 @@ namespace WarmlyShip _mapStorages.Remove(name); } - public MapWithSetShipGeneric this[string ind] + public MapWithSetShipGeneric this[string ind] { get { @@ -45,5 +49,68 @@ namespace WarmlyShip } } + private static void WriteToFile(string text, FileStream stream) + { + byte[] info = new UTF8Encoding(true).GetBytes(text); + stream.Write(info, 0, info.Length); + } + + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (FileStream fs = new(filename, FileMode.Create)) + { + WriteToFile($"MapsCollection{Environment.NewLine}", fs); + foreach (var storage in _mapStorages) + { + + WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); + } + } + return true; + } + + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + if (!strs[0].Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + //очищаем записи + _mapStorages.Clear(); + for (int i = 1; i < strs.Length; ++i) + { + var elem = strs[i].Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetShipGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } } } -- 2.25.1 From 8f5f82ad8b019d8e7f4eeb3d0d8d4b54ec969fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 13 Nov 2022 20:05:11 +0400 Subject: [PATCH 6/9] =?UTF-8?q?=D0=91=D1=83=D0=BF=20=D0=B1=D0=B8=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/MapsCollection.cs | 69 ++++++++++++++----------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 3b144de..6fdf7ca 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.DirectoryServices.ActiveDirectory; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -61,13 +62,13 @@ namespace WarmlyShip { File.Delete(filename); } - using (FileStream fs = new(filename, FileMode.Create)) + using (StreamWriter sw = new(filename)) { - WriteToFile($"MapsCollection{Environment.NewLine}", fs); + sw.Write($"MapsCollection{Environment.NewLine}"); foreach (var storage in _mapStorages) { - WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); + sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); } } return true; @@ -79,37 +80,45 @@ namespace WarmlyShip { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string bufferStr; + bool firstIn = true; + while ((bufferStr = sr.ReadLine()) != null) { - bufferTextFromFile += temp.GetString(b); + if (firstIn) + { + if (!bufferStr.Contains("MapsCollection")) + { + return false; + } + else + { + _mapStorages.Clear(); + } + firstIn = false; + } + else + { + var elem = bufferStr.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "SecondMap": + map = new SecondMap(); + break; + case "LastMap": + map = new LastMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetShipGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } } } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (!strs[0].Contains("MapsCollection")) - { - //если нет такой записи, то это не те данные - return false; - } - //очищаем записи - _mapStorages.Clear(); - for (int i = 1; i < strs.Length; ++i) - { - var elem = strs[i].Split(separatorDict); - AbstractMap map = null; - switch (elem[1]) - { - case "SimpleMap": - map = new SimpleMap(); - break; - } - _mapStorages.Add(elem[0], new MapWithSetShipGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); - } return true; } } -- 2.25.1 From 4d79fe73ab2c40a320de5183a00c6bec10048912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 13 Nov 2022 20:13:21 +0400 Subject: [PATCH 7/9] kek --- WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs index c9c8ff4..157cb86 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs @@ -279,11 +279,11 @@ // // openFileDialog // - this.openFileDialog.Filter = "txt file | *txt"; + this.openFileDialog.Filter = "txt file | *.txt"; // // saveFileDialog // - this.saveFileDialog.Filter = "txt file | *txt"; + this.saveFileDialog.Filter = "txt file | *.txt"; // // FormMapWithSetShip // -- 2.25.1 From 1229514d8dbb7a70e0db638d21d07d1d4e10355a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 13 Nov 2022 20:28:32 +0400 Subject: [PATCH 8/9] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/FormMapWithSetShip.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs index c751166..ee87e04 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs @@ -201,11 +201,12 @@ namespace WarmlyShip private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { - if (saveFileDialog.ShowDialog() == DialogResult.OK) + if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + if (_mapsCollection.LoadData(openFileDialog.FileName)) { MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); } else { -- 2.25.1 From 2e7e0dcb5171590ba41f323dbf4481f887f09d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 16 Nov 2022 10:11:09 +0400 Subject: [PATCH 9/9] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/MapsCollection.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 6fdf7ca..503080b 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -50,12 +50,6 @@ namespace WarmlyShip } } - private static void WriteToFile(string text, FileStream stream) - { - byte[] info = new UTF8Encoding(true).GetBytes(text); - stream.Write(info, 0, info.Length); - } - public bool SaveData(string filename) { if (File.Exists(filename)) -- 2.25.1