2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.handlers ;
import engine.Enum.GameObjectType ;
import engine.Enum.ProtectionState ;
import engine.Enum.TransactionType ;
import engine.gameManager.DbManager ;
import engine.math.Vector3fImmutable ;
import engine.objects.* ;
import engine.server.MBServerStatics ;
import org.joda.time.DateTime ;
import org.pmw.tinylog.Logger ;
import java.net.UnknownHostException ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.concurrent.ConcurrentHashMap ;
public class dbWarehouseHandler extends dbHandlerBase {
private static final ConcurrentHashMap < Integer , String > columns = new ConcurrentHashMap <> ( MBServerStatics . CHM_INIT_CAP , MBServerStatics . CHM_LOAD , MBServerStatics . CHM_THREAD_LOW );
public dbWarehouseHandler () {
this . localClass = Warehouse . class ;
this . localObjectType = engine . Enum . GameObjectType . valueOf ( this . localClass . getSimpleName ());
}
public ArrayList < AbstractGameObject > CREATE_WAREHOUSE ( int parentZoneID , int OwnerUUID , String name , int meshUUID ,
Vector3fImmutable location , float meshScale , int currentHP ,
ProtectionState protectionState , int currentGold , int rank ,
DateTime upgradeDate , int blueprintUUID , float w , float rotY ) {
prepareCallable ( "CALL `WAREHOUSE_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?);" );
setInt ( 1 , parentZoneID );
setInt ( 2 , OwnerUUID );
setString ( 3 , name );
setInt ( 4 , meshUUID );
setFloat ( 5 , location . x );
setFloat ( 6 , location . y );
setFloat ( 7 , location . z );
setFloat ( 8 , meshScale );
setInt ( 9 , currentHP );
setString ( 10 , protectionState . name ());
setInt ( 11 , currentGold );
setInt ( 12 , rank );
if ( upgradeDate != null ) {
setTimeStamp ( 13 , upgradeDate . getMillis ());
} else {
setNULL ( 13 , java . sql . Types . DATE );
}
setInt ( 14 , blueprintUUID );
setFloat ( 15 , w );
setFloat ( 16 , rotY );
ArrayList < AbstractGameObject > list = new ArrayList <> ();
//System.out.println(this.cs.get().toString());
try {
boolean work = execute ();
if ( work ) {
2023-05-20 10:51:25 -04:00
ResultSet rs = this . callableStatement . get (). getResultSet ();
2022-04-30 09:41:17 -04:00
while ( rs . next ()) {
addObject ( list , rs );
}
rs . close ();
} else {
2023-05-20 10:51:25 -04:00
Logger . info ( "Warehouse Creation Failed: " + this . callableStatement . get (). toString ());
2022-04-30 09:41:17 -04:00
return list ; //city creation failure
}
2023-05-20 10:51:25 -04:00
while ( this . callableStatement . get (). getMoreResults ()) {
ResultSet rs = this . callableStatement . get (). getResultSet ();
2022-04-30 09:41:17 -04:00
while ( rs . next ()) {
addObject ( list , rs );
}
rs . close ();
}
} catch ( SQLException e ) {
2023-05-20 10:51:25 -04:00
Logger . info ( "Warehouse Creation Failed, SQLException: " + this . callableStatement . get (). toString () + e . toString ());
2022-04-30 09:41:17 -04:00
return list ; //city creation failure
} catch ( UnknownHostException e ) {
2023-05-20 10:51:25 -04:00
Logger . info ( "Warehouse Creation Failed, UnknownHostException: " + this . callableStatement . get (). toString ());
2022-04-30 09:41:17 -04:00
return list ; //city creation failure
} finally {
closeCallable ();
}
return list ;
}
public boolean updateLocks ( final Warehouse wh , long locks ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_locks`=? WHERE `UID` = ?" );
setLong ( 1 , locks );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateGold ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_gold`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateStone ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_stone`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateTruesteel ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_truesteel`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateIron ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_iron`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateAdamant ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_adamant`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateLumber ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_lumber`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateOak ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_oak`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateBronzewood ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_bronzewood`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateMandrake ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_mandrake`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateCoal ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_coal`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateAgate ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_agate`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateDiamond ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_diamond`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateOnyx ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_onyx`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateAzoth ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_azoth`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateOrichalk ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_orichalk`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateAntimony ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_antimony`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateSulfur ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_sulfur`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateQuicksilver ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_quicksilver`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateGalvor ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_galvor`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateWormwood ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_wormwood`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateObsidian ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_obsidian`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateBloodstone ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_bloodstone`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean updateMithril ( final Warehouse wh , int amount ) {
prepareCallable ( "UPDATE `obj_warehouse` SET `warehouse_mithril`=? WHERE `UID` = ?" );
setInt ( 1 , amount );
setInt ( 2 , wh . getUID ());
return ( executeUpdate () != 0 );
}
public boolean CREATE_TRANSACTION ( int warehouseBuildingID , GameObjectType targetType , int targetUUID , TransactionType transactionType , Resource resource , int amount , DateTime date ){
Transaction transactions = null ;
prepareCallable ( "INSERT INTO `dyn_warehouse_transactions` (`warehouseUID`, `targetType`,`targetUID`, `type`,`resource`,`amount`,`date` ) VALUES (?,?,?,?,?,?,?)" );
setLong ( 1 , warehouseBuildingID );
setString ( 2 , targetType . name ());
setLong ( 3 , targetUUID );
setString ( 4 , transactionType . name ());
setString ( 5 , resource . name ());
setInt ( 6 , amount );
setTimeStamp ( 7 , date . getMillis ());
return ( executeUpdate () != 0 );
}
public static void addObject ( ArrayList < AbstractGameObject > list , ResultSet rs ) throws SQLException , UnknownHostException {
String type = rs . getString ( "type" );
switch ( type ) {
case "building" :
Building building = new Building ( rs );
DbManager . addToCache ( building );
list . add ( building );
break ;
case "warehouse" :
Warehouse warehouse = new Warehouse ( rs );
DbManager . addToCache ( warehouse );
list . add ( warehouse );
break ;
}
}
public ArrayList < Transaction > GET_TRANSACTIONS_FOR_WAREHOUSE ( final int warehouseUUID ) {
ArrayList < Transaction > transactionsList = new ArrayList <> ();
prepareCallable ( "SELECT * FROM dyn_warehouse_transactions WHERE `warehouseUID` = ?;" );
setInt ( 1 , warehouseUUID );
try {
ResultSet rs = executeQuery ();
//shrines cached in rs for easy cache on creation.
while ( rs . next ()) {
Transaction transactions = new Transaction ( rs );
transactionsList . add ( transactions );
}
} catch ( SQLException e ) {
Logger . error ( e . getErrorCode () + ' ' + e . getMessage (), e );
} finally {
closeCallable ();
}
return transactionsList ;
}
public void LOAD_ALL_WAREHOUSES () {
Warehouse thisWarehouse ;
prepareCallable ( "SELECT `obj_warehouse`.*, `object`.`parent`, `object`.`type` FROM `object` LEFT JOIN `obj_warehouse` ON `object`.`UID` = `obj_warehouse`.`UID` WHERE `object`.`type` = 'warehouse';" );
try {
ResultSet rs = executeQuery ();
while ( rs . next ()) {
thisWarehouse = new Warehouse ( rs );
thisWarehouse . runAfterLoad ();
thisWarehouse . loadAllTransactions ();
}
} catch ( SQLException e ) {
Logger . error ( e . getErrorCode () + ' ' + e . getMessage (), e );
} finally {
closeCallable ();
}
}
}