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);
    }
}

package massim;

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

import org.json.JSONObject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;

public class ReplayWriter {

    private static final int GROUP_SIZE = 5;

    private String replayPath;

    private int lastGroup = -1;
    private JSONObject cache = new JSONObject();

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

    public void updateState(String simId, String startTime, WorldData world) {
        if (world instanceof DynamicWorldData) {
            DynamicWorldData dynamicWorld = (DynamicWorldData) world;

            int step = dynamicWorld.step;
            int group = step / GROUP_SIZE;
            String stepStr = String.valueOf(step);

            if (lastGroup != group || cache.has(stepStr)) cache = new JSONObject();
            cache.put(stepStr, new JSONObject(dynamicWorld));
            write(startTime, simId, String.valueOf(group * GROUP_SIZE), cache);

            lastGroup = group;
        } else {
            write(startTime, simId, "static", new JSONObject(world));
        }
    }

    private void write(String startTime, String simId, String name, JSONObject json) {
        String prefix = startTime + "-" + simId;
        File file = Paths.get(this.replayPath, prefix, name + ".json").toFile();
        File dir = file.getParentFile();
        if (!dir.exists()) dir.mkdirs();

        try {
            FileWriter writer = new FileWriter(file);
            json.write(writer);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

write grouped json replays