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.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 ;
this . localObjectType = Enum . GameObjectType . valueOf ( this . localClass . getSimpleName ());
}
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 );
list . add ( zone );
break ;
case "building" :
Building building = new Building ( rs );
DbManager . addToCache ( building );
list . add ( building );
break ;
case "city" :
City city = new City ( rs );
DbManager . addToCache ( city );
list . add ( city );
break ;
}
}
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
public ArrayList < AbstractGameObject > CREATE_CITY ( int ownerID , int parentZoneID , int realmID , 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 ();
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
preparedStatement . setLong ( 3 , realmID ); //objectUUID of realm city belongs in
preparedStatement . setFloat ( 4 , xCoord ); //xOffset from parentZone center
preparedStatement . setFloat ( 5 , yCoord ); //yOffset from parentZone center
preparedStatement . setFloat ( 6 , zCoord ); //zOffset from parentZone center
preparedStatement . setString ( 7 , name ); //city name
preparedStatement . setTimestamp ( 8 , Timestamp . valueOf ( established ));
preparedStatement . setFloat ( 9 , rotation );
preparedStatement . setFloat ( 10 , W );
preparedStatement . setTimestamp ( 11 , Timestamp . valueOf ( upgradeTime ));
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
boolean work = execute ();
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
if ( work ) {
ResultSet rs = preparedStatement . getResultSet ();
while ( rs . next ()) {
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-05-21 17:00:17 -04:00
public ArrayList < City > GET_CITIES_BY_ZONE ( final int objectUUID ) {
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
ArrayList < City > cityList = new ArrayList <> ();
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`.`parent`=?;" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
preparedStatement . setLong ( 1 , objectUUID );
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
ResultSet rs = preparedStatement . executeQuery ();
cityList = getObjectsFromRs ( rs , 100 );
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
return cityList ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
public City GET_CITY ( final int cityId ) {
2022-04-30 09:41:17 -04:00
2023-05-21 17:00:17 -04:00
City city = ( City ) DbManager . getFromCache ( Enum . 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
}