144 lines
4.9 KiB
C#
144 lines
4.9 KiB
C#
using ProjectAirFighter.DrawningObjects;
|
|
using ProjectAirFighter.Generics;
|
|
using ProjectAirFighter.MovementStrategy;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
|
|
namespace ProjectAirFighter.Generics
|
|
{
|
|
internal class AirplanesGenericStorage
|
|
{
|
|
readonly Dictionary<string, AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>> _airplaneStorages;
|
|
public List<string> Keys => _airplaneStorages.Keys.ToList();
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
private readonly char _separatorRecords = ';';
|
|
private static readonly char _separatorForObject = ':';
|
|
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_airplaneStorages = new Dictionary<string, AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
public bool SaveData(string filename)
|
|
{
|
|
if (File.Exists(filename))
|
|
{
|
|
File.Delete(filename);
|
|
}
|
|
StringBuilder data = new();
|
|
foreach (KeyValuePair<string,
|
|
AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplaneStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawningAirplane? elem in record.Value.GetAirplanes)
|
|
{
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
}
|
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
}
|
|
|
|
if (data.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
string toWrite = $"AirplaneStorage{Environment.NewLine}{data}";
|
|
var strs = toWrite.Split(new char[] { '\n', '\r' },
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
using (StreamWriter sw = new(filename))
|
|
{
|
|
foreach (var str in strs)
|
|
{
|
|
sw.WriteLine(str);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool LoadData(string filename)
|
|
{
|
|
if (!File.Exists(filename))
|
|
{
|
|
return false;
|
|
}
|
|
using (StreamReader sr = new(filename))
|
|
{
|
|
|
|
|
|
string str = sr.ReadLine();
|
|
var strs = str.Split(new char[] { '\n', '\r' },
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
if (strs == null || strs.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (!strs[0].StartsWith("AirplaneStorage"))
|
|
{
|
|
return false;
|
|
}
|
|
_airplaneStorages.Clear();
|
|
do
|
|
{
|
|
string[] record = str.Split(_separatorForKeyValue,
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 2)
|
|
{
|
|
str = sr.ReadLine();
|
|
continue;
|
|
}
|
|
AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>
|
|
collection = new(_pictureWidth, _pictureHeight);
|
|
string[] set = record[1].Split(_separatorRecords,
|
|
StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
DrawningAirplane? airplane =
|
|
elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (airplane != null)
|
|
{
|
|
if (!(collection + airplane))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
_airplaneStorages.Add(record[0], collection);
|
|
|
|
str = sr.ReadLine();
|
|
} while (str != null);
|
|
}
|
|
return true;
|
|
}
|
|
public void AddSet(string name)
|
|
{
|
|
_airplaneStorages.Add(name, new AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
|
|
}
|
|
|
|
public void DelSet(string name)
|
|
{
|
|
if (!_airplaneStorages.ContainsKey(name))
|
|
return;
|
|
_airplaneStorages.Remove(name);
|
|
}
|
|
|
|
public AirplaneslGenericCollection<DrawningAirplane, DrawningObjectAirplane>? this[string ind]
|
|
{
|
|
get
|
|
{
|
|
if (_airplaneStorages.ContainsKey(ind))
|
|
return _airplaneStorages[ind];
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|