Files
Server/src/engine/db/handlers/dbStaticWorldHandler.java
T
MagicBotandClaude Opus 5.5 08b50eecf0 Load static trainers from the zone JSON, comparing with the DB until migrated
World Boot Plan 3.2. With MB_STATIC_BUILDINGS=JSON, every TRAINER zone
mobile takes a static ID after the cities (npc:<zone>/m<i>,
npc:<zone>/<prop>/m<i>). While obj_npc still holds trainers of the same
zone and name, the boot only compares and writes
maps/<map>.trainer_migration.sql; afterwards the JSON trainers load
before the DB vendors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 08:54:14 -04:00

129 lines
4.7 KiB
Java

// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2026
// https://www.magicbane.com
package engine.db.handlers;
import engine.gameManager.DbManager;
import org.pmw.tinylog.Logger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeMap;
public class dbStaticWorldHandler extends dbHandlerBase {
public dbStaticWorldHandler() {
}
// Table and column come from StaticManifest's fixed list, never from input. -1 means "none" and is skipped.
public HashMap<Integer, Integer> GET_NEGATIVE_REFERENCES(String table, String column) {
HashMap<Integer, Integer> rowsByValue = new HashMap<>();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `" + column + "` AS `value`, COUNT(*) AS `rows` FROM `"
+ table + "` WHERE `" + column + "` < -1 GROUP BY `" + column + "`;")) {
ResultSet rs = preparedStatement.executeQuery();
while (rs.next())
rowsByValue.put(rs.getInt("value"), rs.getInt("rows"));
} catch (SQLException e) {
Logger.error(table + "." + column + ": " + e);
return null;
}
return rowsByValue;
}
// Every DB guild name by UID, for the NPC guild switch check (World Boot Plan 2.8); null on error.
public TreeMap<Integer, String> GET_GUILD_NAMES() {
TreeMap<Integer, String> namesByUID = new TreeMap<>();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `UID`, `name` FROM `obj_guild`;")) {
ResultSet rs = preparedStatement.executeQuery();
while (rs.next())
namesByUID.put(rs.getInt("UID"), rs.getString("name"));
} catch (SQLException e) {
Logger.error(e);
return null;
}
return namesByUID;
}
// obj_zone rows built by the import procedures; 0 once the 2.4 migration has run, -1 on error.
public int COUNT_STATIC_ZONE_ROWS() {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(*) AS `zones` FROM `obj_zone` WHERE `template` <> 0;")) {
ResultSet rs = preparedStatement.executeQuery();
if (rs.next())
return rs.getInt("zones");
} catch (SQLException e) {
Logger.error(e);
}
return -1;
}
// NPC cities still in obj_city; 0 once the 2.5 migration has run, -1 on error.
public int COUNT_NPC_CITY_ROWS() {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(*) AS `cities` FROM `obj_city` WHERE `isNpc` = 1;")) {
ResultSet rs = preparedStatement.executeQuery();
if (rs.next())
return rs.getInt("cities");
} catch (SQLException e) {
Logger.error(e);
}
return -1;
}
// "<parent>/<name>" of every obj_npc row, to tell whether the 3.2 trainer migration has run; null on error.
public HashSet<String> GET_NPC_ZONE_NAMES() {
HashSet<String> zoneNames = new HashSet<>();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `object`.`parent`, `obj_npc`.`npc_name` FROM `object` "
+ "INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID`;")) {
ResultSet rs = preparedStatement.executeQuery();
while (rs.next())
zoneNames.add(rs.getInt("parent") + "/" + rs.getString("npc_name"));
} catch (SQLException e) {
Logger.error(e);
return null;
}
return zoneNames;
}
}