Files
BattleBane/src/engine/db/handlers/dbContractHandler.java
T

169 lines
6.2 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.handlers;
import engine.Enum;
import engine.gameManager.DbManager;
import engine.objects.Contract;
import engine.objects.ItemBase;
import engine.objects.MobEquipment;
import org.pmw.tinylog.Logger;
2023-05-21 17:20:09 -04:00
import java.sql.Connection;
import java.sql.PreparedStatement;
2022-04-30 09:41:17 -04:00
import java.sql.ResultSet;
import java.sql.SQLException;
public class dbContractHandler extends dbHandlerBase {
2023-07-15 09:23:48 -04:00
public dbContractHandler() {
this.localClass = Contract.class;
this.localObjectType = Enum.GameObjectType.valueOf(this.localClass.getSimpleName());
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public Contract GET_CONTRACT(final int objectUUID) {
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
Contract contract = (Contract) DbManager.getFromCache(Enum.GameObjectType.Contract, objectUUID);
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
if (contract != null)
return contract;
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
if (objectUUID == 0)
return null;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_contract` WHERE `ID` = ?")) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setInt(1, objectUUID);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
ResultSet rs = preparedStatement.executeQuery();
contract = (Contract) getObjectFromRs(rs);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
} catch (SQLException e) {
Logger.error(e);
}
return contract;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void LOAD_CONTRACT_INVENTORY(final Contract contract) {
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_inventoryset` WHERE `inventorySet` = ?;")) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setInt(1, contract.inventorySet);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
ResultSet rs = preparedStatement.executeQuery();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
while (rs.next()) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//handle item base
int itemBaseID = rs.getInt("itembaseID");
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
ItemBase ib = ItemBase.getItemBase(itemBaseID);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (ib != null) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
MobEquipment me = new MobEquipment(ib, 0, 0);
contract.getSellInventory().add(me);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//handle magic effects
String prefix = rs.getString("prefix");
int pRank = rs.getInt("pRank");
String suffix = rs.getString("suffix");
int sRank = rs.getInt("sRank");
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (prefix != null) {
me.setPrefix(prefix, pRank);
me.setIsID(true);
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (suffix != null) {
me.setSuffix(suffix, sRank);
me.setIsID(true);
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
}
}
} catch (SQLException e) {
Logger.error(e);
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void LOAD_SELL_LIST_FOR_CONTRACT(final Contract contract) {
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_contract_selltype` WHERE `contractID` = ?;")) {
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setInt(1, contract.getObjectUUID());
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
ResultSet rs = preparedStatement.executeQuery();
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
while (rs.next()) {
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
int type = rs.getInt("type");
int value = rs.getInt("value");
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
switch (type) {
case 1:
contract.getBuyItemType().add(value);
break;
case 2:
contract.getBuySkillToken().add(value);
break;
case 3:
contract.getBuyUnknownToken().add(value);
break;
}
}
} catch (SQLException e) {
Logger.error(e);
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public boolean updateAllowedBuildings(final Contract con, final long slotbitvalue) {
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `static_npc_contract` SET `allowedBuildingTypeID`=? WHERE `contractID`=?")) {
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setLong(1, slotbitvalue);
preparedStatement.setInt(2, con.getContractID());
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
} catch (SQLException e) {
Logger.error(e);
}
return false;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public boolean updateDatabase(final Contract con) {
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `static_npc_contract` SET `contractID`=?, `name`=?, "
+ "`mobbaseID`=?, `classID`=?, vendorDialog=?, iconID=?, allowedBuildingTypeID=? WHERE `ID`=?")) {
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setInt(1, con.getContractID());
preparedStatement.setString(2, con.getName());
preparedStatement.setInt(3, con.getMobbaseID());
preparedStatement.setInt(4, con.getClassID());
preparedStatement.setInt(5, (con.getVendorDialog() != null) ? con.getVendorDialog().getObjectUUID() : 0);
preparedStatement.setInt(6, con.getIconID());
preparedStatement.setInt(8, con.getObjectUUID());
preparedStatement.setLong(7, con.getAllowedBuildings().toLong());
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-21 17:20:09 -04:00
2023-07-15 09:23:48 -04:00
} catch (SQLException e) {
Logger.error(e);
}
return false;
}
2022-04-30 09:41:17 -04:00
}