package massim;

import massim.protocol.WorldData;
import massim.protocol.DynamicWorldData;
import massim.protocol.StaticWorldData;
import massim.util.IOUtil;

import java.nio.file.Paths;

public class ReplayWriter {

    private String replayPath;

    public ReplayWriter(String replayPath) {
        this.replayPath = replayPath;
    }

    public void updateState(String simId, String startTime, WorldData world) {
        // determine file name
        String file = "";
        if(world instanceof StaticWorldData) file = "static";
        else if(world instanceof DynamicWorldData) file = "step-" + String.valueOf(((DynamicWorldData) world).step);
        // save to file
        IOUtil.writeXMLToFile(
                world.toXML(world.getClass()),
                Paths.get(this.replayPath, startTime + "-" + simId, file + ".xml").toFile(),
                true);
    }
}

@@ -164,504 +164,985 @@
 ort 
-java.nio.file.Paths;%0A%0Apublic class ReplayWriter %7B%0A%0A    private String replayPath;%0A%0A    public ReplayWriter(String replayPath) %7B%0A        this.replayPath = replayPath;%0A    %7D%0A%0A    public void updateState(String simId, String startTime, WorldData world) %7B%0A        // determine file name%0A        String file = %22%22;%0A        if(world instanceof StaticWorldData) file
+org.json.JSONObject;%0A%0Aimport java.io.File;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0Aimport java.nio.file.Paths;%0A%0Apublic class ReplayWriter %7B%0A%0A    private static final int GROUP_SIZE = 5;%0A%0A    private String replayPath;%0A%0A    private int lastGroup = -1;%0A    private JSONObject cache = new JSONObject();%0A%0A    public ReplayWriter(String replayPath) %7B%0A        this.replayPath = replayPath;%0A    %7D%0A%0A    public void updateState(String simId, String startTime, WorldData world) %7B%0A        if (world instanceof DynamicWorldData) %7B%0A            DynamicWorldData dynamicWorld = (DynamicWorldData) world;%0A%0A            int step = dynamicWorld.step;%0A            int group
  = 
-%22
 st
-atic%22;%0A        else if(world instanceof DynamicWorldData) file = %22step-%22 + String.valueOf(((DynamicWorldData) world).step
+ep / GROUP_SIZE;%0A            String stepStr = String.valueOf(step);%0A%0A            if (lastGroup != group %7C%7C cache.has(stepStr)) cache = new JSONObject();%0A            cache.put(stepStr, new JSONObject(dynamicWorld));%0A            write(startTime, simId, String.valueOf(group * GROUP_SIZE), cache
 );%0A
+%0A
         
 // s
@@ -1141,54 +1141,117 @@
     
-// save to file%0A        IOUtil.writeXMLToFile(
+    lastGroup = group;%0A        %7D else %7B%0A            write(startTime, simId, %22static%22, new JSONObject(world));
 %0A   
@@ -1259,139 +1259,444 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-world.toXML(world.getClass()),%0A                Paths.get(this.replayPath, startTime + %22-%22 + simId, file + %22.xml%22).toFile(),
+private void write(String startTime, String simId, String name, JSONObject json) %7B%0A        String prefix = startTime + %22-%22 + simId;%0A        File file = Paths.get(this.replayPath, prefix, name + %22.json%22).toFile();%0A        File dir = file.getParentFile();%0A        if (!dir.exists()) dir.mkdirs();%0A%0A        try %7B%0A            FileWriter writer = new FileWriter(file);%0A            json.write(writer);%0A            writer.close();
 %0A   
@@ -1704,22 +1704,76 @@
     
-        true);
+%7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A   

write grouped json replays