refactor: use try-with-resources for Fredrick db operations

This commit is contained in:
P0nk
2021-04-05 00:13:32 +02:00
parent e13c313839
commit 4ec4600406

View File

@@ -29,15 +29,7 @@ import client.inventory.Item;
import client.inventory.ItemFactory; import client.inventory.ItemFactory;
import client.inventory.MapleInventory; import client.inventory.MapleInventory;
import client.inventory.MapleInventoryType; import client.inventory.MapleInventoryType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;
import client.inventory.manipulator.MapleInventoryManipulator; import client.inventory.manipulator.MapleInventoryManipulator;
import java.util.Collections;
import net.server.Server; import net.server.Server;
import net.server.world.World; import net.server.world.World;
import server.MapleItemInformationProvider; import server.MapleItemInformationProvider;
@@ -47,6 +39,11 @@ import tools.FilePrinter;
import tools.MaplePacketCreator; import tools.MaplePacketCreator;
import tools.Pair; import tools.Pair;
import java.sql.*;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/** /**
* *
* @author RonanLana - synchronization of Fredrick modules and operation results * @author RonanLana - synchronization of Fredrick modules and operation results
@@ -100,10 +97,8 @@ public class FredrickProcessor {
} }
public static void removeFredrickLog(int cid) { public static void removeFredrickLog(int cid) {
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
removeFredrickLog(con, cid); removeFredrickLog(con, cid);
con.close();
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); sqle.printStackTrace();
} }
@@ -112,22 +107,19 @@ public class FredrickProcessor {
private static void removeFredrickLog(Connection con, int cid) throws SQLException { private static void removeFredrickLog(Connection con, int cid) throws SQLException {
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `fredstorage` WHERE `cid` = ?")) { try (PreparedStatement ps = con.prepareStatement("DELETE FROM `fredstorage` WHERE `cid` = ?")) {
ps.setInt(1, cid); ps.setInt(1, cid);
ps.execute(); ps.executeUpdate();
} }
} }
public static void insertFredrickLog(int cid) { public static void insertFredrickLog(int cid) {
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
removeFredrickLog(con, cid); removeFredrickLog(con, cid);
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `fredstorage` (`cid`, `daynotes`, `timestamp`) VALUES (?, 0, ?)")) { try (PreparedStatement ps = con.prepareStatement("INSERT INTO `fredstorage` (`cid`, `daynotes`, `timestamp`) VALUES (?, 0, ?)")) {
ps.setInt(1, cid); ps.setInt(1, cid);
ps.setTimestamp(2, new Timestamp(System.currentTimeMillis())); ps.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
ps.execute(); ps.executeUpdate();
} }
con.close();
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); sqle.printStackTrace();
} }
@@ -145,64 +137,59 @@ public class FredrickProcessor {
expiredCnames.add(name); expiredCnames.add(name);
} }
} }
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("DELETE FROM `notes` WHERE `from` LIKE ? AND `to` LIKE ?")) {
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `notes` WHERE `from` LIKE ? AND `to` LIKE ?")) { ps.setString(1, "FREDRICK");
ps.setString(1, "FREDRICK");
for (String cname : expiredCnames) {
for (String cname : expiredCnames) { ps.setString(2, cname);
ps.setString(2, cname); ps.executeBatch();
ps.executeBatch();
}
} }
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void runFredrickSchedule() { public static void runFredrickSchedule() {
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
List<Pair<Integer, Integer>> expiredCids = new LinkedList<>(); List<Pair<Integer, Integer>> expiredCids = new LinkedList<>();
List<Pair<Pair<Integer, String>, Integer>> notifCids = new LinkedList<>(); List<Pair<Pair<Integer, String>, Integer>> notifCids = new LinkedList<>();
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM fredstorage f LEFT JOIN (SELECT id, name, world, lastLogoutTime FROM characters) AS c ON c.id = f.cid")) { try (PreparedStatement ps = con.prepareStatement("SELECT * FROM fredstorage f LEFT JOIN (SELECT id, name, world, lastLogoutTime FROM characters) AS c ON c.id = f.cid");
try (ResultSet rs = ps.executeQuery()) { ResultSet rs = ps.executeQuery()) {
long curTime = System.currentTimeMillis(); long curTime = System.currentTimeMillis();
while (rs.next()) { while (rs.next()) {
int cid = rs.getInt("cid"); int cid = rs.getInt("cid");
int world = rs.getInt("world"); int world = rs.getInt("world");
Timestamp ts = rs.getTimestamp("timestamp"); Timestamp ts = rs.getTimestamp("timestamp");
int daynotes = Math.min(dailyReminders.length - 1, rs.getInt("daynotes")); int daynotes = Math.min(dailyReminders.length - 1, rs.getInt("daynotes"));
int elapsedDays = timestampElapsedDays(ts, curTime); int elapsedDays = timestampElapsedDays(ts, curTime);
if (elapsedDays > 100) { if (elapsedDays > 100) {
expiredCids.add(new Pair<>(cid, world)); expiredCids.add(new Pair<>(cid, world));
} else { } else {
int notifDay = dailyReminders[daynotes]; int notifDay = dailyReminders[daynotes];
if (elapsedDays >= notifDay) { if (elapsedDays >= notifDay) {
do { do {
daynotes++; daynotes++;
notifDay = dailyReminders[daynotes]; notifDay = dailyReminders[daynotes];
} while (elapsedDays >= notifDay); } while (elapsedDays >= notifDay);
Timestamp logoutTs = rs.getTimestamp("lastLogoutTime"); Timestamp logoutTs = rs.getTimestamp("lastLogoutTime");
int inactivityDays = timestampElapsedDays(logoutTs, curTime); int inactivityDays = timestampElapsedDays(logoutTs, curTime);
if (inactivityDays < 7 || daynotes >= dailyReminders.length - 1) { // don't spam inactive players if (inactivityDays < 7 || daynotes >= dailyReminders.length - 1) { // don't spam inactive players
String name = rs.getString("name"); String name = rs.getString("name");
notifCids.add(new Pair<>(new Pair<>(cid, name), daynotes)); notifCids.add(new Pair<>(new Pair<>(cid, name), daynotes));
}
} }
} }
} }
} }
} }
if (!expiredCids.isEmpty()) { if (!expiredCids.isEmpty()) {
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `inventoryitems` WHERE `type` = ? AND `characterid` = ?")) { try (PreparedStatement ps = con.prepareStatement("DELETE FROM `inventoryitems` WHERE `type` = ? AND `characterid` = ?")) {
ps.setInt(1, ItemFactory.MERCHANT.getValue()); ps.setInt(1, ItemFactory.MERCHANT.getValue());
@@ -211,15 +198,15 @@ public class FredrickProcessor {
ps.setInt(2, cid.getLeft()); ps.setInt(2, cid.getLeft());
ps.addBatch(); ps.addBatch();
} }
ps.executeBatch(); ps.executeBatch();
} }
try (PreparedStatement ps = con.prepareStatement("UPDATE `characters` SET `MerchantMesos` = 0 WHERE `id` = ?")) { try (PreparedStatement ps = con.prepareStatement("UPDATE `characters` SET `MerchantMesos` = 0 WHERE `id` = ?")) {
for (Pair<Integer, Integer> cid : expiredCids) { for (Pair<Integer, Integer> cid : expiredCids) {
ps.setInt(1, cid.getLeft()); ps.setInt(1, cid.getLeft());
ps.addBatch(); ps.addBatch();
World wserv = Server.getInstance().getWorld(cid.getRight()); World wserv = Server.getInstance().getWorld(cid.getRight());
if (wserv != null) { if (wserv != null) {
MapleCharacter chr = wserv.getPlayerStorage().getCharacterById(cid.getLeft()); MapleCharacter chr = wserv.getPlayerStorage().getCharacterById(cid.getLeft());
@@ -228,52 +215,48 @@ public class FredrickProcessor {
} }
} }
} }
ps.executeBatch(); ps.executeBatch();
} }
removeFredrickReminders(expiredCids); removeFredrickReminders(expiredCids);
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `fredstorage` WHERE `cid` = ?")) { try (PreparedStatement ps = con.prepareStatement("DELETE FROM `fredstorage` WHERE `cid` = ?")) {
for (Pair<Integer, Integer> cid : expiredCids) { for (Pair<Integer, Integer> cid : expiredCids) {
ps.setInt(1, cid.getLeft()); ps.setInt(1, cid.getLeft());
ps.addBatch(); ps.addBatch();
} }
ps.executeBatch(); ps.executeBatch();
} }
} }
if (!notifCids.isEmpty()) { if (!notifCids.isEmpty()) {
try (PreparedStatement ps = con.prepareStatement("UPDATE `fredstorage` SET `daynotes` = ? WHERE `cid` = ?")) { try (PreparedStatement ps = con.prepareStatement("UPDATE `fredstorage` SET `daynotes` = ? WHERE `cid` = ?")) {
for (Pair<Pair<Integer, String>, Integer> cid : notifCids) { for (Pair<Pair<Integer, String>, Integer> cid : notifCids) {
ps.setInt(1, cid.getRight()); ps.setInt(1, cid.getRight());
ps.setInt(2, cid.getLeft().getLeft()); ps.setInt(2, cid.getLeft().getLeft());
ps.addBatch(); ps.addBatch();
String msg = fredrickReminderMessage(cid.getRight() - 1); String msg = fredrickReminderMessage(cid.getRight() - 1);
MapleCharacter.sendNote(cid.getLeft().getRight(), "FREDRICK", msg, (byte) 0); MapleCharacter.sendNote(cid.getLeft().getRight(), "FREDRICK", msg, (byte) 0);
} }
ps.executeBatch(); ps.executeBatch();
} }
} }
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static boolean deleteFredrickItems(int cid) { private static boolean deleteFredrickItems(int cid) {
try { try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("DELETE FROM `inventoryitems` WHERE `type` = ? AND `characterid` = ?")) {
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `inventoryitems` WHERE `type` = ? AND `characterid` = ?")) { ps.setInt(1, ItemFactory.MERCHANT.getValue());
ps.setInt(1, ItemFactory.MERCHANT.getValue()); ps.setInt(2, cid);
ps.setInt(2, cid); ps.executeUpdate();
ps.execute();
}
con.close();
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();