Search in sources :

Example 6 with ConnectionHandler

use of au.com.mineauz.minigames.backend.ConnectionHandler in project Minigames by AddstarMC.

the class SQLiteStatLoader method loadStatValues.

public List<StoredStat> loadStatValues(Minigame minigame, MinigameStat stat, StatValueField field, ScoreboardOrder order, int offset, int length) {
    MinigameUtils.debugMessage("SQLite beginning stat load for " + minigame.getName(false) + ", " + stat + ", " + field);
    ConnectionHandler handler = null;
    try {
        handler = backend.getPool().getConnection();
        // First get the id
        int minigameId = backend.getMinigameId(handler, minigame);
        return loadStats(handler, minigameId, stat, field, order, offset, length);
    } catch (SQLException e) {
        return Collections.emptyList();
    } finally {
        if (handler != null) {
            handler.release();
        }
        MinigameUtils.debugMessage("SQLite completed stat load for " + minigame.getName(false));
    }
}
Also used : ConnectionHandler(au.com.mineauz.minigames.backend.ConnectionHandler) SQLException(java.sql.SQLException)

Example 7 with ConnectionHandler

use of au.com.mineauz.minigames.backend.ConnectionHandler in project Minigames by AddstarMC.

the class SQLiteStatLoader method loadSingleValue.

public long loadSingleValue(Minigame minigame, MinigameStat stat, StatValueField field, UUID playerId) {
    ConnectionHandler handler = null;
    try {
        handler = backend.getPool().getConnection();
        // First get the id
        int minigameId = backend.getMinigameId(handler, minigame);
        String statName = stat.getName() + field.getSuffix();
        ResultSet rs = handler.executeQuery(getSingle, minigameId, playerId.toString(), statName);
        try {
            if (rs.next()) {
                return rs.getLong("value");
            } else {
                return 0;
            }
        } finally {
            rs.close();
        }
    } catch (SQLException e) {
        logger.log(Level.SEVERE, "Failed to load stat for " + minigame.getName(false) + " " + playerId, e);
        return 0;
    } finally {
        if (handler != null) {
            handler.release();
        }
    }
}
Also used : ConnectionHandler(au.com.mineauz.minigames.backend.ConnectionHandler) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 8 with ConnectionHandler

use of au.com.mineauz.minigames.backend.ConnectionHandler in project Minigames by AddstarMC.

the class SQLiteStatSaver method saveData.

public void saveData(StoredGameStats data) {
    MinigameUtils.debugMessage("SQLite beginning save of " + data);
    ConnectionHandler handler = null;
    try {
        handler = backend.getPool().getConnection();
        try {
            handler.beginTransaction();
            // Get the minigame id and update both the player and game
            int minigameId = backend.getMinigameId(handler, data.getMinigame());
            backend.updatePlayer(handler, data.getPlayer());
            saveStats(handler, data, data.getPlayer().getUUID(), minigameId);
            // Commit the changes
            handler.endTransaction();
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "Failed to save stats for " + data.getPlayer().getName(), e);
            handler.endTransactionFail();
        } finally {
            MinigameUtils.debugMessage("SQLite completed save of " + data);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (handler != null) {
            handler.release();
        }
    }
}
Also used : ConnectionHandler(au.com.mineauz.minigames.backend.ConnectionHandler) SQLException(java.sql.SQLException)

Example 9 with ConnectionHandler

use of au.com.mineauz.minigames.backend.ConnectionHandler in project Minigames by AddstarMC.

the class MySQLStatSaver method saveData.

public void saveData(StoredGameStats data) {
    MinigameUtils.debugMessage("MySQL beginning save of " + data);
    ConnectionHandler handler = null;
    try {
        handler = backend.getPool().getConnection();
        try {
            handler.beginTransaction();
            // Get the minigame id and update both the player and game
            int minigameId = backend.getMinigameId(handler, data.getMinigame());
            backend.updatePlayer(handler, data.getPlayer());
            saveStats(handler, data, data.getPlayer().getUUID(), minigameId);
            // Commit the changes
            handler.endTransaction();
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "Failed to save stats for " + data.getPlayer().getName(), e);
            handler.endTransactionFail();
        } finally {
            MinigameUtils.debugMessage("MySQL completed save of " + data);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (handler != null) {
            handler.release();
        }
    }
}
Also used : ConnectionHandler(au.com.mineauz.minigames.backend.ConnectionHandler) SQLException(java.sql.SQLException)

Example 10 with ConnectionHandler

use of au.com.mineauz.minigames.backend.ConnectionHandler in project Minigames by AddstarMC.

the class SQLiteBackend method loadStatSettings.

@Override
public Map<MinigameStat, StatSettings> loadStatSettings(Minigame minigame) {
    ConnectionHandler handler = null;
    try {
        handler = pool.getConnection();
        int minigameId = getMinigameId(handler, minigame);
        ResultSet rs = handler.executeQuery(loadStatSettings, minigameId);
        Map<MinigameStat, StatSettings> settings = Maps.newHashMap();
        try {
            while (rs.next()) {
                String statName = rs.getString("stat");
                String rawFormat = rs.getString("format");
                String displayName = rs.getString("display_name");
                MinigameStat stat = MinigameStats.getStat(statName);
                if (stat == null) {
                    // Just ignore it
                    continue;
                }
                // Decode format
                StatFormat format = null;
                for (StatFormat f : StatFormat.values()) {
                    if (f.name().equalsIgnoreCase(rawFormat)) {
                        format = f;
                        break;
                    }
                }
                if (format == null) {
                    format = stat.getFormat();
                }
                StatSettings setting = new StatSettings(stat, format, displayName);
                settings.put(stat, setting);
            }
            return settings;
        } finally {
            rs.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
        return Collections.emptyMap();
    } finally {
        if (handler != null) {
            handler.release();
        }
    }
}
Also used : StatFormat(au.com.mineauz.minigames.stats.StatFormat) ConnectionHandler(au.com.mineauz.minigames.backend.ConnectionHandler) MinigameStat(au.com.mineauz.minigames.stats.MinigameStat) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) StatSettings(au.com.mineauz.minigames.stats.StatSettings)

Aggregations

ConnectionHandler (au.com.mineauz.minigames.backend.ConnectionHandler)12 SQLException (java.sql.SQLException)12 StatSettings (au.com.mineauz.minigames.stats.StatSettings)4 ResultSet (java.sql.ResultSet)4 ConnectionPool (au.com.mineauz.minigames.backend.ConnectionPool)2 MinigameStat (au.com.mineauz.minigames.stats.MinigameStat)2 StatFormat (au.com.mineauz.minigames.stats.StatFormat)2 File (java.io.File)1