Minor fixes in ArtillerySerde. Added MapsCollection saving to and loading from file
This commit is contained in:
parent
ebc0f07204
commit
96d181608f
@ -1,10 +1,10 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
public class ArtillerySerde { // Artillery Serialization/Deserialization
|
public class ArtillerySerde { // Artillery Serialization/Deserialization
|
||||||
private static final String _separatorForObject = ":";
|
private static final char _separatorForObject = ':';
|
||||||
|
|
||||||
public static DrawingArtillery deserialize(String info) {
|
public static DrawingArtillery deserialize(String info) {
|
||||||
String[] strings = info.split(_separatorForObject);
|
String[] strings = info.split(Character.toString(_separatorForObject));
|
||||||
|
|
||||||
int speed = Integer.parseInt(strings[0]);
|
int speed = Integer.parseInt(strings[0]);
|
||||||
float weight = Float.parseFloat(strings[1]);
|
float weight = Float.parseFloat(strings[1]);
|
||||||
@ -38,7 +38,7 @@ public class ArtillerySerde { // Artillery Serialization/Deserialization
|
|||||||
EntityArtillery artillery = drawingArtillery.getArtillery();
|
EntityArtillery artillery = drawingArtillery.getArtillery();
|
||||||
|
|
||||||
String result = String.format(
|
String result = String.format(
|
||||||
"%d%s%f%s%d%s%s%s%d",
|
"%d%c%f%c%d%c%s%c%d",
|
||||||
artillery.getSpeed(),
|
artillery.getSpeed(),
|
||||||
_separatorForObject,
|
_separatorForObject,
|
||||||
artillery.getWeight(),
|
artillery.getWeight(),
|
||||||
@ -55,7 +55,7 @@ public class ArtillerySerde { // Artillery Serialization/Deserialization
|
|||||||
}
|
}
|
||||||
|
|
||||||
return String.format(
|
return String.format(
|
||||||
"%s%s%d%s%b%s%b",
|
"%s%c%d%c%b%c%b",
|
||||||
result,
|
result,
|
||||||
_separatorForObject,
|
_separatorForObject,
|
||||||
advanced.getDopColor().getRGB(),
|
advanced.getDopColor().getRGB(),
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import java.io.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -7,6 +8,9 @@ public class MapsCollection {
|
|||||||
private final int _pictureWidth;
|
private final int _pictureWidth;
|
||||||
private final int _pictureHeight;
|
private final int _pictureHeight;
|
||||||
|
|
||||||
|
private final char separatorDict = '|';
|
||||||
|
private final char separatorData = ';';
|
||||||
|
|
||||||
public Set<String> getKeys() {
|
public Set<String> getKeys() {
|
||||||
return _mapsStorage.keySet();
|
return _mapsStorage.keySet();
|
||||||
}
|
}
|
||||||
@ -38,4 +42,56 @@ public class MapsCollection {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
|
public boolean saveData(String filename) throws IOException {
|
||||||
|
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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean loadData(String filename) throws IOException {
|
||||||
|
File file = new File(filename);
|
||||||
|
|
||||||
|
if (!file.exists()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||||
|
String currentLine = reader.readLine();
|
||||||
|
|
||||||
|
if (currentLine == null || !currentLine.contains("MapsCollection")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_mapsStorage.clear();
|
||||||
|
while ((currentLine = reader.readLine()) != null) {
|
||||||
|
var elements = currentLine.split(Character.toString(separatorDict));
|
||||||
|
|
||||||
|
AbstractMap map = switch (elements[1]) {
|
||||||
|
case "SimpleMap" -> new SimpleMap();
|
||||||
|
case "ForestMap" -> new ForestMap();
|
||||||
|
};
|
||||||
|
|
||||||
|
_mapsStorage.put(elements[0], new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map));
|
||||||
|
_mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "*(\n?)"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user