// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.handlers ;
import engine.gameManager.DbManager ;
import engine.mbEnums ;
import engine.objects.AbstractGameObject ;
import engine.objects.Building ;
import engine.objects.City ;
import engine.objects.Zone ;
import org.pmw.tinylog.Logger ;
import java.sql.* ;
import java.time.LocalDateTime ;
import java.util.ArrayList ;
public class dbCityHandler extends dbHandlerBase {
public dbCityHandler ( ) {
this . localClass = City . class ;
this . localObjectType = mbEnums . GameObjectType . valueOf ( this . localClass . getSimpleName ( ) ) ;
}
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 ) ;
zone . runAfterLoad ( ) ;
list . add ( zone ) ;
break ;
case "building" :
Building building = new Building ( rs ) ;
DbManager . addToCache ( building ) ;
building . runAfterLoad ( ) ;
list . add ( building ) ;
break ;
case "city" :
City city = new City ( rs ) ;
DbManager . addToCache ( city ) ;
city . runAfterLoad ( ) ;
list . add ( city ) ;
break ;
}
}
public ArrayList < AbstractGameObject > CREATE_CITY ( int ownerID , int parentZoneID , float xCoord , float yCoord , float zCoord , float rotation , float W , String name , LocalDateTime established ) {
LocalDateTime upgradeTime = LocalDateTime . now ( ) . plusHours ( 2 ) ;
ArrayList < AbstractGameObject > objectList = new ArrayList < > ( ) ;
try ( Connection connection = DbManager . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( "CALL `city_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)" ) ) {
preparedStatement . setLong ( 1 , ownerID ) ; //objectUUID of owning player
preparedStatement . setLong ( 2 , parentZoneID ) ; //objectUUID of parent (continent) zone
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 ) ) ;
boolean work = preparedStatement . execute ( ) ;
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 ) ;
}
return objectList ;
}
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 ;
}
public City GET_CITY ( final int cityId ) {
City city = ( City ) DbManager . getFromCache ( mbEnums . GameObjectType . City , cityId ) ;
if ( city ! = null )
return city ;
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`=?;" ) ) {
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 ;
}
}
}