package org.mmocore.gameserver.database.dao.impl.custom;
import org.mmocore.commons.database.dbutils.ParamReadStatementHandler;
import org.mmocore.commons.jdbchelper.ResultSetHandler;
import org.mmocore.gameserver.database.dao.AbstractGameServerDAO;
import org.mmocore.gameserver.object.Player;
import org.mmocore.gameserver.templates.custom.AcpTemplate;
import org.mmocore.gameserver.utils.JDBCHelper;
import java.sql.*;
/**
* Create by Mangol on 30.12.2015.
*/
public class AcpDAO extends AbstractGameServerDAO
{
private static final AcpDAO INSTANCE = new AcpDAO();
private static final String SQL_SELECT_ACP = "SELECT * FROM custom_acp WHERE player_obj_id=?";
private static final String SQL_SELECT_ACP_COUNT = "SELECT COUNT(*) FROM custom_acp WHERE player_obj_id=?";
private static final String SQL_INSERT_ACP = "INSERT INTO custom_acp (player_obj_id, auto_cp, cp_percent, cp_item_id, reuse_cp, auto_hp, hp_percent, hp_item_id, reuse_hp, auto_mp, mp_percent, mp_item_id, reuse_mp) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
private static final String SQL_UPDATE_ACP = "UPDATE custom_acp SET auto_cp=?, cp_percent=?, cp_item_id=?, reuse_cp=?, auto_hp=?, hp_percent=?, hp_item_id=?, reuse_hp=?, auto_mp=?, mp_percent=?, mp_item_id=?, reuse_mp=? WHERE player_obj_id=?";
public static AcpDAO getInstance()
{
return INSTANCE;
}
public AcpTemplate selectAcp(final Player player)
{
final AcpTemplate[] template = new AcpTemplate[1];
jdbcHelper.query(SQL_SELECT_ACP, new ResultSetHandler()
{
@Override
public void processRow(final ResultSet rs) throws SQLException
{
final boolean autoCp = rs.getBoolean("auto_cp");
final double cpPercent = rs.getDouble("cp_percent");
final int cpItemId = rs.getInt("cp_item_id");
final int reuseCp = rs.getInt("reuse_cp");
final boolean autoHp = rs.getBoolean("auto_hp");
final double hpPercent = rs.getDouble("hp_percent");
final int hpItemId = rs.getInt("hp_item_id");
final int reuseHp = rs.getInt("reuse_hp");
final boolean autoMp = rs.getBoolean("auto_mp");
final double mpPercent = rs.getDouble("mp_percent");
final int mpItemId = rs.getInt("mp_item_id");
final int reuseMp = rs.getInt("reuse_mp");
template[0] = new AcpTemplate(autoCp, autoHp, autoMp, cpPercent, hpPercent, mpPercent, cpItemId, hpItemId, mpItemId, reuseCp, reuseHp, reuseMp);
}
}, rs -> rs.setInt(1, player.getObjectId()));
return template[0];
}
public void insertUpdateAcp(final Player player, final AcpTemplate template)
{
final int[] count = new int[1];
JDBCHelper.select(SQL_SELECT_ACP_COUNT, new ParamReadStatementHandler()
{
@Override
public void setParams(final PreparedStatement statement) throws SQLException
{
statement.setInt(1, player.getObjectId());
}
@Override
public void handleRead(final ResultSet resultSet) throws SQLException
{
count[0] = resultSet.getInt(1);
}
});
JDBCHelper.insertUpdate(count[0] == 0 ? SQL_INSERT_ACP : SQL_UPDATE_ACP, statement -> {
if(count[0] == 0)
{
statement.setInt(1, player.getObjectId());
statement.setBoolean(2, template.isAutoCp());
statement.setDouble(3, template.getCpPercent());
statement.setInt(4, template.getCpItemId());
statement.setInt(5, template.getReuseCp());
statement.setBoolean(6, template.isAutoHp());
statement.setDouble(7, template.getHpPercent());
statement.setInt(8, template.getHpItemId());
statement.setInt(9, template.getReuseHp());
statement.setBoolean(10, template.isAutoMp());
statement.setDouble(11, template.getMpPercent());
statement.setInt(12, template.getMpItemId());
statement.setInt(13, template.getReuseMp());
}
if(count[0] > 0)
{
statement.setBoolean(1, template.isAutoCp());
statement.setDouble(2, template.getCpPercent());
statement.setInt(3, template.getCpItemId());
statement.setInt(4, template.getReuseCp());
statement.setBoolean(5, template.isAutoHp());
statement.setDouble(6, template.getHpPercent());
statement.setInt(7, template.getHpItemId());
statement.setInt(8, template.getReuseHp());
statement.setBoolean(9, template.isAutoMp());
statement.setDouble(10, template.getMpPercent());
statement.setInt(11, template.getMpItemId());
statement.setInt(12, template.getReuseMp());
statement.setInt(13, player.getObjectId());
}
});
}
}