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

148 lines
5.5 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;
2024-03-18 09:38:33 -04:00
import engine.objects.Item;
2024-03-24 07:55:59 -04:00
import engine.objects.MobLoot;
2022-04-30 09:41:17 -04:00
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
2024-03-28 04:38:52 -04:00
int templateID = rs.getInt("templateID");
2022-04-30 09:41:17 -04:00
2024-03-24 07:55:59 -04:00
Item item = new Item(templateID);
item.objectUUID = MobLoot.lastNegativeID.decrementAndGet();
contract.getSellInventory().add(item);
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
}