Files
lakebane/src/engine/db/handlers/dbZoneHandler.java
T

131 lines
4.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;
2023-05-23 10:27:03 -04:00
import engine.gameManager.ZoneManager;
import engine.math.Vector2f;
2022-04-30 09:41:17 -04:00
import engine.objects.Zone;
2023-05-23 09:57:17 -04:00
import org.pmw.tinylog.Logger;
2022-04-30 09:41:17 -04:00
2023-05-23 09:57:17 -04:00
import java.sql.Connection;
import java.sql.PreparedStatement;
2022-04-30 09:41:17 -04:00
import java.sql.ResultSet;
2023-05-23 09:57:17 -04:00
import java.sql.SQLException;
2022-04-30 09:41:17 -04:00
import java.util.ArrayList;
public class dbZoneHandler extends dbHandlerBase {
2023-07-15 09:23:48 -04:00
public dbZoneHandler() {
this.localClass = Zone.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 ArrayList<Zone> GET_ALL_NODES(Zone zone) {
ArrayList<Zone> wsmList = new ArrayList<>();
wsmList.addAll(zone.getNodes());
if (zone.absX == 0.0f) {
zone.absX = zone.getXCoord();
}
if (zone.absY == 0.0f) {
zone.absY = zone.getYCoord();
}
if (zone.absZ == 0.0f) {
zone.absZ = zone.getZCoord();
}
for (Zone child : zone.getNodes()) {
child.absX = child.getXCoord() + zone.absX;
child.absY = child.getYCoord() + zone.absY;
child.absZ = child.getZCoord() + zone.absZ;
wsmList.addAll(this.GET_ALL_NODES(child));
}
return wsmList;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public Zone GET_BY_UID(long ID) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Zone zone = (Zone) DbManager.getFromCache(Enum.GameObjectType.Zone, (int) ID);
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
if (zone != null)
return zone;
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_zone`.*, `object`.`parent` FROM `object` INNER JOIN `obj_zone` ON `obj_zone`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;")) {
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setLong(1, ID);
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
ResultSet rs = preparedStatement.executeQuery();
zone = (Zone) getObjectFromRs(rs);
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
} catch (SQLException e) {
Logger.error(e);
}
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
return zone;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public ArrayList<Zone> GET_MAP_NODES(final int objectUUID) {
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
ArrayList<Zone> zoneList = new ArrayList<>();
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_zone`.*, `object`.`parent` FROM `object` INNER JOIN `obj_zone` ON `obj_zone`.`UID` = `object`.`UID` WHERE `object`.`parent` = ?;")) {
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setLong(1, objectUUID);
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
ResultSet rs = preparedStatement.executeQuery();
zoneList = getObjectsFromRs(rs, 2000);
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
} catch (SQLException e) {
Logger.error(e);
}
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
return zoneList;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void LOAD_ZONE_EXTENTS() {
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_zone_size`;")) {
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
ResultSet rs = preparedStatement.executeQuery();
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
while (rs.next()) {
Vector2f zoneSize = new Vector2f();
int loadNum = rs.getInt("loadNum");
zoneSize.x = rs.getFloat("xRadius");
zoneSize.y = rs.getFloat("zRadius");
ZoneManager._zone_size_data.put(loadNum, zoneSize);
}
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 boolean DELETE_ZONE(final Zone zone) {
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("DELETE FROM `object` WHERE `UID` = ? AND `type` = 'zone'")) {
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setInt(1, zone.getObjectUUID());
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
} catch (SQLException e) {
Logger.error(e);
}
2023-05-23 09:57:17 -04:00
2023-07-15 09:23:48 -04:00
return false;
}
2022-04-30 09:41:17 -04:00
}