2026-07-26 19:08:44 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2026
// https://www.magicbane.com
2022-04-30 09:41:17 -04:00
package engine.db.handlers ;
import engine.gameManager.DbManager ;
2024-04-05 07:59:44 -04:00
import engine.mbEnums ;
2022-04-30 09:41:17 -04:00
import engine.objects.AbstractGameObject ;
import engine.objects.Building ;
import engine.objects.City ;
import engine.objects.Zone ;
import org.pmw.tinylog.Logger ;
2023-05-21 17:00:17 -04:00
import java.sql.* ;
2022-04-30 09:41:17 -04:00
import java.time.LocalDateTime ;
import java.util.ArrayList ;
public class dbCityHandler extends dbHandlerBase {
2023-05-21 17:00:17 -04:00
public dbCityHandler () {
this . localClass = City . class ;
2024-04-05 07:59:44 -04:00
this . localObjectType = mbEnums . GameObjectType . valueOf ( this . localClass . getSimpleName ());
2023-05-21 17:00:17 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
public static void addObject ( ArrayList < AbstractGameObject > list , ResultSet rs ) throws SQLException {
String type = rs . getString ( "type" );
switch ( type ) {
case "zone" :
Zone zone = new Zone ( rs );
DbManager . addToCache ( zone );
2023-12-24 09:44:01 -05:00
zone . runAfterLoad ();
2023-05-21 17:00:17 -04:00
list . add ( zone );
break ;
case "building" :
Building building = new Building ( rs );
DbManager . addToCache ( building );
2023-12-24 09:44:01 -05:00
building . runAfterLoad ();
2023-05-21 17:00:17 -04:00
list . add ( building );
break ;
case "city" :
City city = new City ( rs );
DbManager . addToCache ( city );
2023-12-24 09:44:01 -05:00
city . runAfterLoad ();
2023-05-21 17:00:17 -04:00
list . add ( city );
break ;
}
}
2022-04-30 09:41:17 -04:00
2024-05-06 13:33:28 -04:00
public ArrayList < AbstractGameObject > CREATE_CITY ( int ownerID , int parentZoneID , float xCoord , float yCoord , float zCoord , float rotation , float W , String name , LocalDateTime established ) {
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
LocalDateTime upgradeTime = LocalDateTime . now (). plusHours ( 2 );
ArrayList < AbstractGameObject > objectList = new ArrayList <> ();
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
try ( Connection connection = DbManager . getConnection ();
2024-05-06 13:56:18 -04:00
PreparedStatement preparedStatement = connection . prepareStatement ( "CALL `city_CREATE`(?, ?, ?, ?, ?, ?, ?, ?,?,?)" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
preparedStatement . setLong ( 1 , ownerID ); //objectUUID of owning player
preparedStatement . setLong ( 2 , parentZoneID ); //objectUUID of parent (continent) zone
2024-05-06 13:33:28 -04:00
preparedStatement . setFloat ( 3 , xCoord ); //xOffset from parentZone center
preparedStatement . setFloat ( 4 , yCoord ); //yOffset from parentZone center
preparedStatement . setFloat ( 5 , zCoord ); //zOffset from parentZone center
preparedStatement . setString ( 6 , name ); //city name
preparedStatement . setTimestamp ( 7 , Timestamp . valueOf ( established ));
preparedStatement . setFloat ( 8 , rotation );
preparedStatement . setFloat ( 9 , W );
preparedStatement . setTimestamp ( 10 , Timestamp . valueOf ( upgradeTime ));
2022-04-30 09:41:17 -04:00
2023-05-23 10:50:00 -04:00
boolean work = preparedStatement . execute ();
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
if ( work ) {
2023-05-23 10:50:00 -04:00
2023-05-21 17:00:17 -04:00
ResultSet rs = preparedStatement . getResultSet ();
2023-05-23 10:50:00 -04:00
while ( rs . next ())
2023-05-21 17:00:17 -04:00
addObject ( objectList , rs );
rs . close ();
} else {
Logger . info ( "City Placement Failed: " + preparedStatement );
return objectList ; //city creation failure
}
while ( preparedStatement . getMoreResults ()) {
ResultSet rs = preparedStatement . getResultSet ();
while ( rs . next ()) {
addObject ( objectList , rs );
}
rs . close ();
}
} catch ( SQLException e ) {
Logger . error ( e );
}
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
return objectList ;
}
2022-04-30 09:41:17 -04:00
2023-10-17 08:45:33 -04:00
public ArrayList < City > GET_ALL_CITIES () {
ArrayList < City > cityList = new ArrayList <> ();
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT `obj_city`.*, `object`.`parent` FROM `obj_city` INNER JOIN `object` ON `object`.`UID` = `obj_city`.`UID` ORDER BY `object`.`UID` ASC;" )) {
ResultSet rs = preparedStatement . executeQuery ();
cityList = getObjectsFromRs ( rs , 100 );
} catch ( SQLException e ) {
Logger . error ( e );
}
return cityList ;
}
2023-05-21 17:00:17 -04:00
public City GET_CITY ( final int cityId ) {
2022-04-30 09:41:17 -04:00
2024-04-05 07:59:44 -04:00
City city = ( City ) DbManager . getFromCache ( mbEnums . GameObjectType . City , cityId );
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
if ( city != null )
return city ;
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT `obj_city`.*, `object`.`parent` FROM `obj_city` INNER JOIN `object` ON `object`.`UID` = `obj_city`.`UID` WHERE `object`.`UID`=?;" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
preparedStatement . setLong ( 1 , cityId );
ResultSet rs = preparedStatement . executeQuery ();
city = ( City ) getObjectFromRs ( rs );
} catch ( SQLException e ) {
Logger . error ( e );
}
return city ;
}
public boolean updateforceRename ( City city , boolean value ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_city` SET `forceRename`=?"
+ " WHERE `UID` = ?" )) {
preparedStatement . setByte ( 1 , ( value == true ) ? ( byte ) 1 : ( byte ) 0 );
preparedStatement . setInt ( 2 , city . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean updateOpenCity ( City city , boolean value ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_city` SET `open`=?"
+ " WHERE `UID` = ?" )) {
preparedStatement . setByte ( 1 , ( value == true ) ? ( byte ) 1 : ( byte ) 0 );
preparedStatement . setInt ( 2 , city . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean updateTOL ( City city , int tolID ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_city` SET `treeOfLifeUUID`=?"
+ " WHERE `UID` = ?" )) {
preparedStatement . setInt ( 1 , tolID );
preparedStatement . setInt ( 2 , city . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean renameCity ( City city , String name ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_city` SET `name`=?"
+ " WHERE `UID` = ?" )) {
preparedStatement . setString ( 1 , name );
preparedStatement . setInt ( 2 , city . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean updateSiegesWithstood ( City city , int value ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_city` SET `name`=?"
+ " WHERE `UID` = ?" )) {
preparedStatement . setInt ( 1 , value );
preparedStatement . setInt ( 2 , city . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean updateRealmTaxDate ( City city , LocalDateTime localDateTime ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_city` SET `realmTaxDate` =?"
+ " WHERE `UID` = ?" )) {
preparedStatement . setTimestamp ( 1 , Timestamp . valueOf ( localDateTime ));
preparedStatement . setInt ( 2 , city . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
2022-04-30 09:41:17 -04:00
}