Search in sources :

Example 1 with FileUtil

use of me.semx11.autotip.util.FileUtil in project Hyperium by HyperiumClient.

the class Autotip method setup.

private void setup() {
    try {
        fileUtil = new FileUtil(this);
        gson = new GsonBuilder().registerTypeAdapter(Config.class, new ConfigCreator(this)).registerTypeAdapter(StatsDaily.class, new StatsDailyCreator(this)).setExclusionStrategies(new AnnotationExclusionStrategy()).setPrettyPrinting().create();
        config = new Config(this);
        reloadGlobalSettings();
        reloadLocale();
        sessionManager = new SessionManager(this);
        statsManager = new StatsManager(this);
        migrationManager = new MigrationManager(this);
        fileUtil.createDirectories();
        config.load();
        taskManager.getExecutor().execute(() -> migrationManager.migrateLegacyFiles());
        registerEvents(new EventClientConnection(this), new EventChatReceived(this));
        registerCommands(new CommandAutotip(this), new CommandLimbo(this));
        Runtime.getRuntime().addShutdownHook(new Thread(sessionManager::logout));
        initialized = true;
    } catch (IOException e) {
        messageUtil.send("Autotip is disabled because it couldn't create the required files.");
        ErrorReport.reportException(e);
    } catch (IllegalStateException e) {
        messageUtil.send("Autotip is disabled because it couldn't connect to the API.");
        ErrorReport.reportException(e);
    }
}
Also used : ConfigCreator(me.semx11.autotip.gson.creator.ConfigCreator) CommandAutotip(me.semx11.autotip.command.impl.CommandAutotip) GsonBuilder(com.google.gson.GsonBuilder) EventClientConnection(me.semx11.autotip.event.impl.EventClientConnection) Config(me.semx11.autotip.config.Config) SessionManager(me.semx11.autotip.core.SessionManager) CommandLimbo(me.semx11.autotip.command.impl.CommandLimbo) StatsDailyCreator(me.semx11.autotip.gson.creator.StatsDailyCreator) IOException(java.io.IOException) MigrationManager(me.semx11.autotip.core.MigrationManager) AnnotationExclusionStrategy(me.semx11.autotip.gson.exclusion.AnnotationExclusionStrategy) StatsManager(me.semx11.autotip.core.StatsManager) EventChatReceived(me.semx11.autotip.event.impl.EventChatReceived) FileUtil(me.semx11.autotip.util.FileUtil)

Example 2 with FileUtil

use of me.semx11.autotip.util.FileUtil in project Hyperium by HyperiumClient.

the class Config method migrate.

public Config migrate() {
    FileUtil fileUtil = autotip.getFileUtil();
    // Check if legacy config file exists
    File legacyFile = fileUtil.getFile("options.at");
    if (!legacyFile.exists())
        return this;
    try {
        List<String> lines = Files.readAllLines(fileUtil.getPath("options.at"));
        if (lines.size() < 2)
            return this;
        enabled = Boolean.parseBoolean(lines.get(0));
        try {
            messageOption = MessageOption.valueOf(lines.get(1));
        } catch (IllegalArgumentException | NullPointerException e) {
            messageOption = MessageOption.SHOWN;
        }
        // Deletes old file to complete migration
        fileUtil.delete(legacyFile);
        return save();
    } catch (IOException e) {
        Autotip.LOGGER.error("Could not read legacy options.at file!");
        return save();
    }
}
Also used : IOException(java.io.IOException) FileUtil(me.semx11.autotip.util.FileUtil) File(java.io.File)

Example 3 with FileUtil

use of me.semx11.autotip.util.FileUtil in project Hyperium by HyperiumClient.

the class StatsDaily method migrate.

public void migrate() {
    // Check if legacy stats file exists
    FileUtil fileUtil = autotip.getFileUtil();
    File file = fileUtil.getLegacyStatsFile(date);
    if (!file.exists())
        return;
    LegacyState state = autotip.getMigrationManager().getLegacyState(date);
    try {
        // Reads the contents of the file. If the file has less than 2 lines, ignore file.
        List<String> lines = Files.readAllLines(file.toPath());
        if (lines.size() < 2) {
            fileUtil.delete(file);
            return;
        }
        // Parses the first line of the file to tips sent and received (e.g. "124:119").
        Matcher tipMatcher = TIPS_PATTERN.matcher(lines.get(0));
        if (tipMatcher.matches()) {
            tipsSent = Integer.parseInt(tipMatcher.group("sent"));
            if (tipMatcher.group("received") != null)
                tipsReceived = Integer.parseInt(tipMatcher.group("received"));
        }
        // This is to fix the wrong tips count in the period between the XP change, and the Autotip fix.
        if (state == LegacyState.BACKTRACK)
            tipsReceived /= 2;
        // Every tip you send is worth 50 XP.
        xpSent = tipsSent * 50;
        // This is to account for tips received before the XP change, as they gave you 30 XP, not 60 XP.
        xpReceived = (state == LegacyState.BEFORE ? 30 : 60) * tipsReceived;
        // Parses each line with game-data (e.g. "Arcade:2900:2400") to a Map.
        gameStatistics = lines.stream().skip(2).filter(s -> GAME_PATTERN.matcher(s).matches()).collect(Collectors.toMap(s -> s.split(":")[0], s -> {
            String[] split = s.split(":");
            int sent = Integer.parseInt(split[1]);
            int received = split.length > 2 ? Integer.parseInt(split[2]) : 0;
            return new Coins(sent, received);
        }));
        // Remove grouped stats
        settings.getGameGroups().stream().filter(group -> gameStatistics.containsKey(group.getName())).forEach(group -> {
            Coins coins = gameStatistics.get(group.getName());
            group.getGames().forEach(game -> addCoins(game, coins));
        });
        // Convert aliases
        settings.getGameAliases().forEach(alias -> alias.getAliases().stream().filter(aliasAlias -> gameStatistics.containsKey(aliasAlias)).map(aliasAlias -> gameStatistics.get(aliasAlias)).forEach(coins -> alias.getGames().forEach(aliasGame -> addCoins(aliasGame, coins))));
        // Deletes old file to complete migration.
        fileUtil.delete(file);
        Autotip.LOGGER.info("Migrated legacy stats file " + file.getName());
        save();
    } catch (IOException e) {
        Autotip.LOGGER.error("Could not read file " + file.getName(), e);
        save();
    }
}
Also used : Autotip(me.semx11.autotip.Autotip) List(java.util.List) LegacyState(me.semx11.autotip.core.MigrationManager.LegacyState) Matcher(java.util.regex.Matcher) FileUtil(me.semx11.autotip.util.FileUtil) Files(java.nio.file.Files) LocalDate(java.time.LocalDate) IOException(java.io.IOException) Pattern(java.util.regex.Pattern) Collectors(java.util.stream.Collectors) File(java.io.File) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) LegacyState(me.semx11.autotip.core.MigrationManager.LegacyState) FileUtil(me.semx11.autotip.util.FileUtil) File(java.io.File)

Aggregations

IOException (java.io.IOException)3 FileUtil (me.semx11.autotip.util.FileUtil)3 File (java.io.File)2 GsonBuilder (com.google.gson.GsonBuilder)1 Files (java.nio.file.Files)1 LocalDate (java.time.LocalDate)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Autotip (me.semx11.autotip.Autotip)1 CommandAutotip (me.semx11.autotip.command.impl.CommandAutotip)1 CommandLimbo (me.semx11.autotip.command.impl.CommandLimbo)1 Config (me.semx11.autotip.config.Config)1 MigrationManager (me.semx11.autotip.core.MigrationManager)1 LegacyState (me.semx11.autotip.core.MigrationManager.LegacyState)1 SessionManager (me.semx11.autotip.core.SessionManager)1 StatsManager (me.semx11.autotip.core.StatsManager)1 EventChatReceived (me.semx11.autotip.event.impl.EventChatReceived)1 EventClientConnection (me.semx11.autotip.event.impl.EventClientConnection)1