2022-11-20 23:07:02 +04:00
|
|
|
import java.io.*;
|
2022-12-05 22:34:50 +04:00
|
|
|
import java.nio.file.NoSuchFileException;
|
2022-11-05 16:40:00 +04:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class MapsCollection {
|
2022-11-05 19:18:29 +04:00
|
|
|
private final HashMap<String, MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap>> _mapsStorage;
|
2022-11-05 16:40:00 +04:00
|
|
|
|
|
|
|
private final int _pictureWidth;
|
|
|
|
private final int _pictureHeight;
|
|
|
|
|
2022-11-20 23:07:02 +04:00
|
|
|
private final char separatorDict = '|';
|
|
|
|
private final char separatorData = ';';
|
|
|
|
|
2022-11-05 16:40:00 +04:00
|
|
|
public Set<String> getKeys() {
|
|
|
|
return _mapsStorage.keySet();
|
|
|
|
}
|
|
|
|
|
|
|
|
public MapsCollection(int pictureWidth, int pictureHeight) {
|
|
|
|
_mapsStorage = new HashMap<>();
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addMap(String name, AbstractMap map) {
|
|
|
|
if (!_mapsStorage.containsKey(name)) {
|
|
|
|
_mapsStorage.put(name, new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteMap(String name) {
|
|
|
|
_mapsStorage.remove(name);
|
|
|
|
}
|
|
|
|
|
2022-11-05 19:18:29 +04:00
|
|
|
public MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> getMap(String name) {
|
2022-11-05 16:40:00 +04:00
|
|
|
return _mapsStorage.getOrDefault(name, null);
|
|
|
|
}
|
2022-11-08 16:19:52 +04:00
|
|
|
|
|
|
|
public IDrawingObject getArtillery(String mapName, int index) {
|
|
|
|
var map = _mapsStorage.getOrDefault(mapName, null);
|
|
|
|
if (map != null) {
|
|
|
|
return map._setArtilleries.get(index);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-20 23:07:02 +04:00
|
|
|
|
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2022-12-05 22:34:50 +04:00
|
|
|
public void saveData(String filename) throws IOException {
|
2022-11-20 23:07:02 +04:00
|
|
|
File file = new File(filename);
|
|
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
file.createNewFile();
|
|
|
|
|
|
|
|
try (PrintWriter writer = new PrintWriter(file)) {
|
|
|
|
writer.println("MapsCollection");
|
|
|
|
|
|
|
|
for (var storage : _mapsStorage.entrySet()) {
|
|
|
|
writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 22:34:50 +04:00
|
|
|
public void loadData(String filename) throws IOException {
|
2022-11-20 23:07:02 +04:00
|
|
|
File file = new File(filename);
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
2022-12-05 22:34:50 +04:00
|
|
|
throw new FileNotFoundException("Файл не найден");
|
2022-11-20 23:07:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
|
|
|
String currentLine = reader.readLine();
|
|
|
|
|
|
|
|
if (currentLine == null || !currentLine.contains("MapsCollection")) {
|
2022-12-05 22:34:50 +04:00
|
|
|
throw new FileFormatException("Неверный формат файла");
|
2022-11-20 23:07:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
_mapsStorage.clear();
|
|
|
|
while ((currentLine = reader.readLine()) != null) {
|
2022-11-21 01:04:53 +04:00
|
|
|
var elements = currentLine.split(String.format("\\%c", separatorDict));
|
2022-11-20 23:07:02 +04:00
|
|
|
|
|
|
|
AbstractMap map = switch (elements[1]) {
|
|
|
|
case "SimpleMap" -> new SimpleMap();
|
|
|
|
case "ForestMap" -> new ForestMap();
|
2022-11-21 01:04:53 +04:00
|
|
|
default -> null;
|
2022-11-20 23:07:02 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
_mapsStorage.put(elements[0], new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map));
|
2022-11-21 01:04:53 +04:00
|
|
|
_mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "\n?"));
|
2022-11-20 23:07:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 01:48:43 +04:00
|
|
|
|
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2022-12-05 22:34:50 +04:00
|
|
|
public void saveMap(String mapName, String filename) throws Exception {
|
2022-11-21 01:48:43 +04:00
|
|
|
File file = new File(filename);
|
|
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
file.createNewFile();
|
|
|
|
|
|
|
|
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map = _mapsStorage.getOrDefault(mapName, null);
|
|
|
|
|
|
|
|
if (map == null) {
|
2022-12-05 22:34:50 +04:00
|
|
|
throw new IndexOutOfBoundsException();
|
2022-11-21 01:48:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
try (PrintWriter writer = new PrintWriter(file)) {
|
|
|
|
writer.println("Map");
|
|
|
|
writer.println(mapName);
|
|
|
|
writer.println(map.getMap().getClass().getSimpleName());
|
|
|
|
|
|
|
|
for (var artillery : map._setArtilleries.getArtilleries()) {
|
|
|
|
writer.println(artillery.getInfo());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 22:34:50 +04:00
|
|
|
public void loadMap(String filename) throws IOException {
|
2022-11-21 01:48:43 +04:00
|
|
|
File file = new File(filename);
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
2022-12-05 22:34:50 +04:00
|
|
|
throw new FileNotFoundException("Файл не найден");
|
2022-11-21 01:48:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
|
|
|
String currentLine = reader.readLine();
|
|
|
|
|
|
|
|
if (currentLine == null || !currentLine.contains("Map")) {
|
2022-12-05 22:34:50 +04:00
|
|
|
throw new FileFormatException("Неверный формат файла");
|
2022-11-21 01:48:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
String mapName = reader.readLine();
|
|
|
|
|
|
|
|
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map;
|
|
|
|
if (_mapsStorage.containsKey(mapName)) {
|
|
|
|
map = _mapsStorage.get(mapName);
|
|
|
|
if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) {
|
2022-12-05 22:34:50 +04:00
|
|
|
throw new FileFormatException("Неверный формат файла");
|
2022-11-21 01:48:43 +04:00
|
|
|
}
|
2022-11-22 15:59:06 +04:00
|
|
|
map._setArtilleries.clear();
|
2022-11-21 01:48:43 +04:00
|
|
|
} else {
|
|
|
|
map = switch (reader.readLine()) {
|
|
|
|
case "SimpleMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new SimpleMap());
|
|
|
|
case "ForestMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new ForestMap());
|
|
|
|
default -> null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((currentLine = reader.readLine()) != null) {
|
|
|
|
map._setArtilleries.insert(DrawingObjectArtillery.create(currentLine));
|
|
|
|
}
|
|
|
|
|
|
|
|
_mapsStorage.put(mapName, map);
|
|
|
|
}
|
|
|
|
}
|
2022-11-05 16:40:00 +04:00
|
|
|
}
|