178 lines
5.3 KiB
Java
178 lines
5.3 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.ListIterator;
|
|
|
|
public class MapsCollection
|
|
{
|
|
public LinkedHashMap<String, MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>> _mapStorages;
|
|
private int _pictureWidth;
|
|
private int _pictureHeight;
|
|
|
|
private char separatorDict = '|';
|
|
private char separatorData = ';';
|
|
|
|
public List<String> getKeys() { return _mapStorages.keySet().stream().toList(); }
|
|
|
|
public MapsCollection(int pictureWidth, int pictureHeight)
|
|
{
|
|
_mapStorages = new LinkedHashMap<>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
|
|
public boolean SaveData(String filename)
|
|
{
|
|
File file = new File(filename);
|
|
if (file.exists()) file.delete();
|
|
|
|
try {
|
|
file.createNewFile();
|
|
|
|
FileWriter writer = new FileWriter(file);
|
|
writer.append("MapsCollection\n");
|
|
|
|
for(String key : _mapStorages.keySet()) {
|
|
String record = key + separatorDict + _mapStorages.get(key).GetData(separatorDict, separatorData) + "\n";
|
|
writer.append(record);
|
|
}
|
|
|
|
writer.flush();
|
|
writer.close();
|
|
} catch(Exception err) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public boolean LoadData(String filename)
|
|
{
|
|
File file = new File(filename);
|
|
if (!file.exists()) return false;
|
|
|
|
try {
|
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
String current = reader.readLine();
|
|
|
|
if (!current.equals("MapsCollection")) return false;
|
|
_mapStorages.clear();
|
|
|
|
while ((current = reader.readLine()) != null)
|
|
{
|
|
String elem[] = current.split("\\" + separatorDict);
|
|
AbstractMap map = null;
|
|
switch (elem[1])
|
|
{
|
|
case "SimpleMap":
|
|
map = new SimpleMap();
|
|
break;
|
|
case "MyMap":
|
|
map = new MyMap();
|
|
break;
|
|
}
|
|
_mapStorages.put(elem[0], new MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
|
_mapStorages.get(elem[0]).LoadData(elem[2].split(separatorData + ""));
|
|
}
|
|
|
|
reader.close();
|
|
} catch(Exception err) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
public boolean SaveMap(String filename, String key) {
|
|
File file = new File(filename);
|
|
if(file.exists()) file.delete();
|
|
|
|
MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap> item = _mapStorages.getOrDefault(key, null);
|
|
if(item == null) return false;
|
|
|
|
try {
|
|
file.createNewFile();
|
|
FileWriter writer = new FileWriter(file);
|
|
|
|
writer.append("Map\n");
|
|
writer.append(key + "\n");
|
|
writer.append(item.getMap().getClass().getName() + "\n");
|
|
ListIterator<DrawingObjectAircraft> iter = item.getAircraftsIter();
|
|
while(iter.hasNext()) {
|
|
writer.append(AircraftFactory.getDataForSave(iter.next().getAircraft()) + "\n");
|
|
}
|
|
|
|
writer.flush();
|
|
writer.close();
|
|
} catch(Exception err) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public boolean LoadMap(String filename) {
|
|
File file = new File(filename);
|
|
if (!file.exists()) return false;
|
|
|
|
try {
|
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
String format = reader.readLine();
|
|
if (!format.equals("Map")) return false;
|
|
|
|
String key = reader.readLine();
|
|
String mapStr = reader.readLine();
|
|
|
|
AbstractMap map = null;
|
|
|
|
switch (mapStr)
|
|
{
|
|
case "SimpleMap":
|
|
map = new SimpleMap();
|
|
break;
|
|
case "MyMap":
|
|
map = new MyMap();
|
|
break;
|
|
}
|
|
|
|
if(_mapStorages.containsKey(key)) _mapStorages.get(key).ClearMap();
|
|
else _mapStorages.put(key, new MapWithSetAircraftsGeneric<>(_pictureWidth, _pictureHeight, map));
|
|
|
|
String current = null;
|
|
while ((current = reader.readLine()) != null)
|
|
{
|
|
_mapStorages.get(key).addAircraft(DrawingObjectAircraft.Create(current));
|
|
}
|
|
reader.close();
|
|
} catch(Exception err) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public void AddMap(String name, AbstractMap map)
|
|
{
|
|
// TODO Прописать логику для добавления
|
|
boolean check = _mapStorages.containsKey(name);
|
|
if (check) return;
|
|
|
|
_mapStorages.put(name, new MapWithSetAircraftsGeneric(_pictureWidth, _pictureHeight, map));
|
|
}
|
|
|
|
public void DelMap(String name)
|
|
{
|
|
// TODO Прописать логику для удаления
|
|
_mapStorages.remove(name);
|
|
}
|
|
|
|
public MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap> getMap(String name)
|
|
{
|
|
return _mapStorages.getOrDefault(name, null);
|
|
}
|
|
}
|