All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bigcustard.glide.code.command.ExitCommand Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.bigcustard.glide.code.command;

import com.bigcustard.glide.code.Game;
import com.bigcustard.glide.code.GameRenameException;
import com.bigcustard.glide.code.GameStore;
import com.bigcustard.scene2dplus.command.AbstractCommand;
import com.bigcustard.util.FutureSupplier;
import com.bigcustard.util.FutureSuppliers;

import java.util.function.BiConsumer;

public class ExitCommand extends AbstractCommand {
    private final Game game;
    private final GameStore gameStore;
    private final FutureSupplier saveChoiceSupplier;
    private final FutureSupplier gameNameSupplier;
    private final BiConsumer errorReporter;
    private final Runnable exitProcess;

    public ExitCommand(Game game,
                       GameStore gameStore,
                       FutureSupplier saveChoiceSupplier,
                       FutureSupplier gameNameSupplier,
                       BiConsumer errorReporter,
                       Runnable exitProcess) {
        this.game = game;
        this.gameStore = gameStore;
        this.saveChoiceSupplier = saveChoiceSupplier;
        this.gameNameSupplier = gameNameSupplier;
        this.errorReporter = errorReporter;
        this.exitProcess = exitProcess;
    }

    @Override
    public void execute() {
        if (game.isNamed()) {
            exitProcess.run();
        } else if (game.isModified()){
            nameOrDeleteGame();
        } else {
            gameStore.delete(game.token());
            exitProcess.run();
        }
    }

    private void nameOrDeleteGame() {
        FutureSuppliers.onGet(saveChoiceSupplier, (save) -> {
            if (save) {
                saveGame();
            } else {
                gameStore.delete(game.token());
                exitProcess.run();
            }
        });
    }

    private void saveGame() {
        FutureSuppliers.onGet(gameNameSupplier, (newName) -> {
            try {
                gameStore.rename(game.token(), newName);
                exitProcess.run();
            } catch (GameRenameException e) {
                errorReporter.accept(e, this::saveGame);
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy