Search in sources :

Example 1 with StatsJSON

use of me.shadorc.shadbot.data.stats.annotation.StatsJSON in project Shadbot by Shadorc.

the class StatsManager method save.

@DataSave(filePath = FILE_NAME, initialDelay = 10, period = 10, unit = TimeUnit.MINUTES)
public static void save() throws JSONException, IOException {
    JSONObject mainObj = new JSONObject();
    Reflections reflections = new Reflections(StatsManager.class.getPackage().getName(), new MethodAnnotationsScanner());
    for (Method jsonMethod : reflections.getMethodsAnnotatedWith(StatsJSON.class)) {
        StatsJSON annotation = jsonMethod.getAnnotation(StatsJSON.class);
        try {
            mainObj.put(annotation.name(), jsonMethod.invoke(null));
        } catch (Exception err) {
            LogUtils.error(err, String.format("An error occurred while saving statistics %s.", jsonMethod.getDeclaringClass().getSimpleName()));
        }
    }
    try (FileWriter writer = new FileWriter(FILE)) {
        writer.write(mainObj.toString(Config.JSON_INDENT_FACTOR));
    }
}
Also used : JSONObject(org.json.JSONObject) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) FileWriter(java.io.FileWriter) StatsJSON(me.shadorc.shadbot.data.stats.annotation.StatsJSON) Method(java.lang.reflect.Method) IOException(java.io.IOException) JSONException(org.json.JSONException) Reflections(org.reflections.Reflections) DataSave(me.shadorc.shadbot.data.annotation.DataSave)

Example 2 with StatsJSON

use of me.shadorc.shadbot.data.stats.annotation.StatsJSON in project Shadbot by Shadorc.

the class CommandStatsManager method toJSON.

@StatsJSON(name = NAME)
public static JSONObject toJSON() {
    JSONObject mainObj = new JSONObject();
    for (CommandEnum cmdEnum : COMMANDS_STATS_MAP.keySet()) {
        JSONObject statsObj = new JSONObject();
        for (String key : COMMANDS_STATS_MAP.get(cmdEnum).keySet()) {
            statsObj.put(key, COMMANDS_STATS_MAP.get(cmdEnum).get(key).get());
        }
        mainObj.put(cmdEnum.toString(), statsObj);
    }
    return mainObj;
}
Also used : JSONObject(org.json.JSONObject) StatsJSON(me.shadorc.shadbot.data.stats.annotation.StatsJSON)

Example 3 with StatsJSON

use of me.shadorc.shadbot.data.stats.annotation.StatsJSON in project Shadbot by Shadorc.

the class MoneyStatsManager method toJSON.

@StatsJSON(name = NAME)
public static JSONObject toJSON() {
    JSONObject mainObj = new JSONObject();
    for (MoneyEnum moneyEnum : MONEY_STATS_MAP.keySet()) {
        JSONObject statsObj = new JSONObject();
        for (String key : MONEY_STATS_MAP.get(moneyEnum).keySet()) {
            statsObj.put(key, MONEY_STATS_MAP.get(moneyEnum).get(key).get());
        }
        mainObj.put(moneyEnum.toString(), statsObj);
    }
    return mainObj;
}
Also used : JSONObject(org.json.JSONObject) StatsJSON(me.shadorc.shadbot.data.stats.annotation.StatsJSON)

Aggregations

StatsJSON (me.shadorc.shadbot.data.stats.annotation.StatsJSON)3 JSONObject (org.json.JSONObject)3 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 DataSave (me.shadorc.shadbot.data.annotation.DataSave)1 JSONException (org.json.JSONException)1 Reflections (org.reflections.Reflections)1 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)1