Search in sources :

Example 1 with StoredStat

use of au.com.mineauz.minigames.stats.StoredStat in project Minigames by AddstarMC.

the class ScoreboardCommand method onCommand.

@Override
public boolean onCommand(final CommandSender sender, Minigame ignore, String label, String[] args) {
    if (args == null || args.length < 3) {
        return false;
    }
    // Decode arguments
    final Minigame minigame = plugin.mdata.getMinigame(args[0]);
    if (minigame == null) {
        sender.sendMessage(ChatColor.RED + "No Minigame found by the name " + args[0]);
        return true;
    }
    final MinigameStat stat = MinigameStats.getStat(args[1]);
    if (stat == null) {
        sender.sendMessage(ChatColor.RED + "No statistic found by the name " + args[1]);
        return true;
    }
    final StatSettings settings = minigame.getSettings(stat);
    StatValueField field = null;
    for (StatValueField f : settings.getFormat().getFields()) {
        if (f.name().equalsIgnoreCase(args[2])) {
            field = f;
            break;
        }
    }
    if (field == null) {
        sender.sendMessage(ChatColor.RED + "No field found by the name " + args[2] + " for the statistic " + stat.getDisplayName());
        return true;
    }
    // Prepare defaults for optionals
    ScoreboardOrder order;
    switch(field) {
        case Last:
        case Total:
        case Max:
            order = ScoreboardOrder.DESCENDING;
            break;
        case Min:
            order = ScoreboardOrder.ASCENDING;
            break;
        default:
            throw new AssertionError();
    }
    int start = 0;
    int length = 8;
    // Now the optionals
    for (int i = 3; i < args.length - 1; i += 2) {
        if (args[i].equalsIgnoreCase("-o")) {
            // Order
            if (args[i + 1].equalsIgnoreCase("asc") || args[i + 1].equalsIgnoreCase("ascending")) {
                order = ScoreboardOrder.ASCENDING;
            } else if (args[i + 1].equalsIgnoreCase("desc") || args[i + 1].equalsIgnoreCase("descending")) {
                order = ScoreboardOrder.DESCENDING;
            } else {
                sender.sendMessage(ChatColor.RED + "Unknown order " + args[i + 1] + ". Expected asc, ascending, desc, or descending.");
                return true;
            }
        } else if (args[i].equalsIgnoreCase("-l")) {
            // Length
            if (args[i + 1].matches("[1-9][0-9]*")) {
                length = Integer.parseInt(args[i + 1]);
            } else {
                sender.sendMessage(ChatColor.RED + "Unknown length " + args[i + 1] + ". Expected positive non-zero number");
                return true;
            }
        } else if (args[i].equalsIgnoreCase("-s")) {
            // Start
            if (args[i + 1].matches("[1-9][0-9]*")) {
                start = Integer.parseInt(args[i + 1]) - 1;
            } else {
                sender.sendMessage(ChatColor.RED + "Unknown start " + args[i + 1] + ". Expected positive non-zero number");
                return true;
            }
        } else {
            sender.sendMessage(ChatColor.RED + "Unknown option " + args[i] + ". Expected -o, -l, or -s");
            return true;
        }
    }
    final ScoreboardOrder fOrder = order;
    final StatValueField fField = field;
    sender.sendMessage(ChatColor.GRAY + "Loading scoreboard...");
    // Now load the values
    ListenableFuture<List<StoredStat>> future = plugin.getBackend().loadStats(minigame, stat, field, order, start, length);
    Futures.addCallback(future, new FutureCallback<List<StoredStat>>() {

        @Override
        public void onSuccess(List<StoredStat> result) {
            sender.sendMessage(ChatColor.GREEN + minigame.getName(true) + " Scoreboard: " + settings.getDisplayName() + " - " + fField.getTitle() + " " + fOrder.toString().toLowerCase());
            for (StoredStat playerStat : result) {
                sender.sendMessage(ChatColor.AQUA + playerStat.getPlayerDisplayName() + ": " + ChatColor.WHITE + stat.displayValue(playerStat.getValue(), settings));
            }
        }

        @Override
        public void onFailure(Throwable t) {
            sender.sendMessage(ChatColor.RED + "An internal error occured while loading the statistics");
            t.printStackTrace();
        }
    });
    return true;
}
Also used : StoredStat(au.com.mineauz.minigames.stats.StoredStat) StatValueField(au.com.mineauz.minigames.stats.StatValueField) MinigameStat(au.com.mineauz.minigames.stats.MinigameStat) ScoreboardOrder(au.com.mineauz.minigames.minigame.ScoreboardOrder) ArrayList(java.util.ArrayList) List(java.util.List) StatSettings(au.com.mineauz.minigames.stats.StatSettings) Minigame(au.com.mineauz.minigames.minigame.Minigame)

Example 2 with StoredStat

use of au.com.mineauz.minigames.stats.StoredStat in project Minigames by AddstarMC.

the class SQLiteStatLoader method loadStat.

private StoredStat loadStat(ResultSet rs) throws SQLException {
    UUID playerId = UUID.fromString(rs.getString("player_id"));
    String name = rs.getString("name");
    String displayName = rs.getString("displayname");
    long value = rs.getLong("value");
    return new StoredStat(playerId, name, displayName, value);
}
Also used : StoredStat(au.com.mineauz.minigames.stats.StoredStat) UUID(java.util.UUID)

Example 3 with StoredStat

use of au.com.mineauz.minigames.stats.StoredStat in project Minigames by AddstarMC.

the class MySQLStatLoader method loadStats.

// Loads from the stats table
private List<StoredStat> loadStats(ConnectionHandler handler, int minigameId, MinigameStat stat, StatValueField field, ScoreboardOrder order, int offset, int length) throws SQLException {
    String statName = stat.getName() + field.getSuffix();
    try {
        StatementKey statement;
        switch(order) {
            case ASCENDING:
                statement = getSingleAsc;
                break;
            case DESCENDING:
                statement = getSingleDesc;
                break;
            default:
                throw new AssertionError();
        }
        ResultSet rs = handler.executeQuery(statement, minigameId, statName, offset, length);
        List<StoredStat> stats = Lists.newArrayList();
        try {
            while (rs.next()) {
                stats.add(loadStat(rs));
            }
            return stats;
        } finally {
            rs.close();
        }
    } finally {
        handler.release();
    }
}
Also used : ResultSet(java.sql.ResultSet) StatementKey(au.com.mineauz.minigames.backend.StatementKey) StoredStat(au.com.mineauz.minigames.stats.StoredStat)

Example 4 with StoredStat

use of au.com.mineauz.minigames.stats.StoredStat in project Minigames by AddstarMC.

the class MySQLStatLoader method loadStat.

private StoredStat loadStat(ResultSet rs) throws SQLException {
    UUID playerId = UUID.fromString(rs.getString("player_id"));
    String name = rs.getString("name");
    String displayName = rs.getString("displayname");
    long value = rs.getLong("value");
    return new StoredStat(playerId, name, displayName, value);
}
Also used : StoredStat(au.com.mineauz.minigames.stats.StoredStat) UUID(java.util.UUID)

Example 5 with StoredStat

use of au.com.mineauz.minigames.stats.StoredStat in project Minigames by AddstarMC.

the class SQLiteStatLoader method loadStats.

// Loads from the stats table
private List<StoredStat> loadStats(ConnectionHandler handler, int minigameId, MinigameStat stat, StatValueField field, ScoreboardOrder order, int offset, int length) throws SQLException {
    String statName = stat.getName() + field.getSuffix();
    StatementKey statement;
    switch(order) {
        case ASCENDING:
            statement = getSingleAsc;
            break;
        case DESCENDING:
            statement = getSingleDesc;
            break;
        default:
            throw new AssertionError();
    }
    ResultSet rs = handler.executeQuery(statement, minigameId, statName, offset, length);
    List<StoredStat> stats = Lists.newArrayList();
    try {
        while (rs.next()) {
            stats.add(loadStat(rs));
        }
        return stats;
    } finally {
        rs.close();
    }
}
Also used : ResultSet(java.sql.ResultSet) StatementKey(au.com.mineauz.minigames.backend.StatementKey) StoredStat(au.com.mineauz.minigames.stats.StoredStat)

Aggregations

StoredStat (au.com.mineauz.minigames.stats.StoredStat)5 StatementKey (au.com.mineauz.minigames.backend.StatementKey)2 ResultSet (java.sql.ResultSet)2 UUID (java.util.UUID)2 Minigame (au.com.mineauz.minigames.minigame.Minigame)1 ScoreboardOrder (au.com.mineauz.minigames.minigame.ScoreboardOrder)1 MinigameStat (au.com.mineauz.minigames.stats.MinigameStat)1 StatSettings (au.com.mineauz.minigames.stats.StatSettings)1 StatValueField (au.com.mineauz.minigames.stats.StatValueField)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1