package skadistats.clarity.examples.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import skadistats.clarity.processor.reader.OnTickStart;
import skadistats.clarity.processor.runner.Context;
import skadistats.clarity.processor.runner.SimpleRunner;
import skadistats.clarity.source.MappedFileSource;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

public class Main {

    private final Logger log = LoggerFactory.getLogger(Main.class.getPackage().getClass());

    @OnTickStart
    public void onMessage(Context ctx, boolean synthetic) throws InterruptedException, FileNotFoundException, UnsupportedEncodingException {
    }

    public void run(String[] args) throws Exception {
        File folder = new File("D:\\replays");
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) {
            if (file.isFile() && file.getName().endsWith(".dem")) {
                System.out.println(file.getPath());
                new SimpleRunner(new MappedFileSource(file)).runWith(this);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        new Main().run(args);
    }

}

@@ -123,36 +123,246 @@
 ity.
-processor.reader.OnTickStart
+model.Entity;%0Aimport skadistats.clarity.model.FieldPath;%0Aimport skadistats.clarity.processor.entities.OnEntityCreated;%0Aimport skadistats.clarity.processor.entities.OnEntityUpdated;%0Aimport skadistats.clarity.processor.entities.UsesEntities
 ;%0Aim
@@ -524,112 +524,21 @@
 e;%0A%0A
-import java.io.File;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.UnsupportedEncodingException;%0A
+@UsesEntities
 %0Apub
@@ -656,487 +656,1397 @@
     
-@OnTickStart%0A    public void onMessage(Context ctx, boolean synthetic) throws InterruptedException, FileNotFoundException, UnsupportedEncodingException %7B%0A    %7D%0A%0A    public void run(String%5B%5D args) throws Exception %7B%0A        File folder = new File(%22D:%5C%5Creplays%22);%0A        File%5B%5D listOfFiles = folder.listFiles();%0A        for (File file : listOfFiles) %7B%0A            if (file.isFile() && file.getName().endsWith(%22.dem%22)) %7B%0A                System.out.println(file.getPath());%0A        
+private FieldPath mana;%0A    private FieldPath maxMana;%0A%0A    private boolean isHero(Entity e) %7B%0A        return e.getDtClass().getDtName().startsWith(%22CDOTA_Unit_Hero%22);%0A    %7D%0A%0A    private void ensureFieldPaths(Entity e) %7B%0A        if (mana == null) %7B%0A            mana = e.getDtClass().getFieldPathForName(%22m_flMana%22);%0A            maxMana = e.getDtClass().getFieldPathForName(%22m_flMaxMana%22);%0A        %7D%0A    %7D%0A%0A    @OnEntityCreated%0A    public void onCreated(Context ctx, Entity e) %7B%0A        if (!isHero(e)) %7B%0A            return;%0A        %7D%0A        ensureFieldPaths(e);%0A        System.out.format(%22%25s (%25s/%25s)%5Cn%22, e.getDtClass().getDtName(), e.getPropertyForFieldPath(mana), e.getPropertyForFieldPath(maxMana));%0A    %7D%0A%0A    @OnEntityUpdated%0A    public void onUpdated(Context ctx, Entity e, FieldPath%5B%5D updatedPaths, int updateCount) %7B%0A        if (!isHero(e)) %7B%0A            return;%0A        %7D%0A        ensureFieldPaths(e);%0A        boolean update = false;%0A        for (int i = 0; i %3C updateCount; i++) %7B%0A            if (updatedPaths%5Bi%5D.equals(mana) %7C%7C updatedPaths%5Bi%5D.equals(maxMana)) %7B%0A                update = true;%0A                break;%0A            %7D%0A        %7D%0A        if (update) %7B%0A            System.out.format(%22%25s (%25s/%25s)%5Cn%22, e.getDtClass().getDtName(), e.getPropertyForFieldPath(mana), e.getPropertyForFieldPath(maxMana));%0A        %7D%0A    %7D%0A%0A%0A    public void run(String%5B%5D args) throws Exception %7B%0A
     
@@ -2091,12 +2091,15 @@
 rce(
-file
+args%5B0%5D
 )).r
@@ -2116,32 +2116,8 @@
 s);%0A
-            %7D%0A        %7D%0A
     

test for correct decoding of m_flMana / m_flMaxMana