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

This commit is contained in:
P0nk
2021-04-04 15:19:32 +02:00
parent b350fedc66
commit 5899cce9d8

View File

@@ -21,13 +21,6 @@
*/ */
package net.server.guild; package net.server.guild;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;
import client.MapleCharacter; import client.MapleCharacter;
import client.MapleClient; import client.MapleClient;
import net.server.Server; import net.server.Server;
@@ -39,19 +32,25 @@ import net.server.world.MaplePartyCharacter;
import tools.DatabaseConnection; import tools.DatabaseConnection;
import tools.MaplePacketCreator; import tools.MaplePacketCreator;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
/** /**
*
* @author XoticStory * @author XoticStory
* @author Ronan * @author Ronan
*/ */
public class MapleAlliance { public class MapleAlliance {
final private List<Integer> guilds = new LinkedList<>(); final private List<Integer> guilds = new LinkedList<>();
private int allianceId = -1; private int allianceId = -1;
private int capacity; private int capacity;
private String name; private String name;
private String notice = ""; private String notice = "";
private String rankTitles[] = new String[5]; private String[] rankTitles = new String[5];
public MapleAlliance(String name, int id) { public MapleAlliance(String name, int id) {
this.name = name; this.name = name;
@@ -61,36 +60,33 @@ public class MapleAlliance {
rankTitles[i] = ranks[i]; rankTitles[i] = ranks[i];
} }
} }
public static boolean canBeUsedAllianceName(String name) { public static boolean canBeUsedAllianceName(String name) {
if (name.contains(" ") || name.length() > 12) { if (name.contains(" ") || name.length() > 12) {
return false; return false;
} }
try {
ResultSet rs; try (Connection con = DatabaseConnection.getConnection();
Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT name FROM alliance WHERE name = ?")) {
try (PreparedStatement ps = con.prepareStatement("SELECT name FROM alliance WHERE name = ?")) { ps.setString(1, name);
ps.setString(1, name);
rs = ps.executeQuery(); try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) { if (rs.next()) {
ps.close();
rs.close();
return false; return false;
} }
} }
rs.close();
con.close();
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
} }
private static List<MapleCharacter> getPartyGuildMasters(MapleParty party) { private static List<MapleCharacter> getPartyGuildMasters(MapleParty party) {
List<MapleCharacter> mcl = new LinkedList<>(); List<MapleCharacter> mcl = new LinkedList<>();
for(MaplePartyCharacter mpc: party.getMembers()) { for (MaplePartyCharacter mpc : party.getMembers()) {
MapleCharacter chr = mpc.getPlayer(); MapleCharacter chr = mpc.getPlayer();
if (chr != null) { if (chr != null) {
MapleCharacter lchr = party.getLeader().getPlayer(); MapleCharacter lchr = party.getLeader().getPlayer();
@@ -100,9 +96,9 @@ public class MapleAlliance {
} }
} }
if(!mcl.isEmpty() && !mcl.get(0).isPartyLeader()) { if (!mcl.isEmpty() && !mcl.get(0).isPartyLeader()) {
for(int i = 1; i < mcl.size(); i++) { for (int i = 1; i < mcl.size(); i++) {
if(mcl.get(i).isPartyLeader()) { if (mcl.get(i).isPartyLeader()) {
MapleCharacter temp = mcl.get(0); MapleCharacter temp = mcl.get(0);
mcl.set(0, mcl.get(i)); mcl.set(0, mcl.get(i));
mcl.set(i, temp); mcl.set(i, temp);
@@ -115,19 +111,24 @@ public class MapleAlliance {
public static MapleAlliance createAlliance(MapleParty party, String name) { public static MapleAlliance createAlliance(MapleParty party, String name) {
List<MapleCharacter> guildMasters = getPartyGuildMasters(party); List<MapleCharacter> guildMasters = getPartyGuildMasters(party);
if(guildMasters.size() != 2) return null; if (guildMasters.size() != 2) {
return null;
}
List<Integer> guilds = new LinkedList<>(); List<Integer> guilds = new LinkedList<>();
for(MapleCharacter mc: guildMasters) guilds.add(mc.getGuildId()); for (MapleCharacter mc : guildMasters) {
guilds.add(mc.getGuildId());
}
MapleAlliance alliance = MapleAlliance.createAllianceOnDb(guilds, name); MapleAlliance alliance = MapleAlliance.createAllianceOnDb(guilds, name);
if(alliance != null) { if (alliance != null) {
alliance.setCapacity(guilds.size()); alliance.setCapacity(guilds.size());
for(Integer g: guilds) for (Integer g : guilds) {
alliance.addGuild(g); alliance.addGuild(g);
}
int id = alliance.getId(); int id = alliance.getId();
try { try {
for(int i = 0; i < guildMasters.size(); i++) { for (int i = 0; i < guildMasters.size(); i++) {
Server.getInstance().setGuildAllianceId(guilds.get(i), id); Server.getInstance().setGuildAllianceId(guilds.get(i), id);
Server.getInstance().resetAllianceGuildPlayersRank(guilds.get(i)); Server.getInstance().resetAllianceGuildPlayersRank(guilds.get(i));
@@ -138,7 +139,7 @@ public class MapleAlliance {
} }
Server.getInstance().addAlliance(id, alliance); Server.getInstance().addAlliance(id, alliance);
int worldid = guildMasters.get(0).getWorld(); int worldid = guildMasters.get(0).getWorld();
Server.getInstance().allianceMessage(id, MaplePacketCreator.updateAllianceInfo(alliance, worldid), -1, -1); Server.getInstance().allianceMessage(id, MaplePacketCreator.updateAllianceInfo(alliance, worldid), -1, -1);
Server.getInstance().allianceMessage(id, MaplePacketCreator.getGuildAlliances(alliance, worldid), -1, -1); // thanks Vcoc for noticing guilds from other alliances being visually stacked here due to this not being updated Server.getInstance().allianceMessage(id, MaplePacketCreator.getGuildAlliances(alliance, worldid), -1, -1); // thanks Vcoc for noticing guilds from other alliances being visually stacked here due to this not being updated
@@ -150,40 +151,34 @@ public class MapleAlliance {
return alliance; return alliance;
} }
public static MapleAlliance createAllianceOnDb(List<Integer> guilds, String name) { public static MapleAlliance createAllianceOnDb(List<Integer> guilds, String name) {
// will create an alliance, where the first guild listed is the leader and the alliance name MUST BE already checked for unicity. // will create an alliance, where the first guild listed is the leader and the alliance name MUST BE already checked for unicity.
int id = -1; int id = -1;
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection(); try (PreparedStatement ps = con.prepareStatement("INSERT INTO `alliance` (`name`) VALUES (?)", PreparedStatement.RETURN_GENERATED_KEYS)) {
PreparedStatement ps = con.prepareStatement("INSERT INTO `alliance` (`name`) VALUES (?)", PreparedStatement.RETURN_GENERATED_KEYS); ps.setString(1, name);
ps.setString(1, name);
ps.executeUpdate();
try (ResultSet rs = ps.getGeneratedKeys()) {
rs.next();
id = rs.getInt(1);
}
for(int i = 0; i < guilds.size(); i++) {
int guild = guilds.get(i);
ps = con.prepareStatement("INSERT INTO `allianceguilds` (`allianceid`, `guildid`) VALUES (?, ?)");
ps.setInt(1, id);
ps.setInt(2, guild);
ps.executeUpdate(); ps.executeUpdate();
ps.close(); try (ResultSet rs = ps.getGeneratedKeys()) {
rs.next();
id = rs.getInt(1);
}
}
for (int guild : guilds) {
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `allianceguilds` (`allianceid`, `guildid`) VALUES (?, ?)")) {
ps.setInt(1, id);
ps.setInt(2, guild);
ps.executeUpdate();
}
} }
ps.close();
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
return (new MapleAlliance(name, id)); return new MapleAlliance(name, id);
} }
public static MapleAlliance loadAlliance(int id) { public static MapleAlliance loadAlliance(int id) {
@@ -191,183 +186,144 @@ public class MapleAlliance {
return null; return null;
} }
MapleAlliance alliance = new MapleAlliance(null, -1); MapleAlliance alliance = new MapleAlliance(null, -1);
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM alliance WHERE id = ?"); try (PreparedStatement ps = con.prepareStatement("SELECT * FROM alliance WHERE id = ?")) {
ps.setInt(1, id); ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (!rs.next()) { try (ResultSet rs = ps.executeQuery()) {
rs.close(); if (!rs.next()) {
ps.close(); return null;
con.close(); }
return null;
alliance.allianceId = id;
alliance.capacity = rs.getInt("capacity");
alliance.name = rs.getString("name");
alliance.notice = rs.getString("notice");
String[] ranks = new String[5];
ranks[0] = rs.getString("rank1");
ranks[1] = rs.getString("rank2");
ranks[2] = rs.getString("rank3");
ranks[3] = rs.getString("rank4");
ranks[4] = rs.getString("rank5");
alliance.rankTitles = ranks;
}
} }
alliance.allianceId = id;
alliance.capacity = rs.getInt("capacity"); try (PreparedStatement ps = con.prepareStatement("SELECT guildid FROM allianceguilds WHERE allianceid = ?")) {
alliance.name = rs.getString("name"); ps.setInt(1, id);
alliance.notice = rs.getString("notice"); try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String ranks[] = new String[5]; alliance.addGuild(rs.getInt("guildid"));
ranks[0] = rs.getString("rank1"); }
ranks[1] = rs.getString("rank2"); }
ranks[2] = rs.getString("rank3");
ranks[3] = rs.getString("rank4");
ranks[4] = rs.getString("rank5");
alliance.rankTitles = ranks;
ps.close();
rs.close();
ps = con.prepareStatement("SELECT guildid FROM allianceguilds WHERE allianceid = ?");
ps.setInt(1, id);
rs = ps.executeQuery();
while(rs.next()) {
alliance.addGuild(rs.getInt("guildid"));
} }
ps.close();
rs.close();
con.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return alliance; return alliance;
} }
public void saveToDB() { public void saveToDB() {
try { try (Connection con = DatabaseConnection.getConnection()) {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE `alliance` SET capacity = ?, notice = ?, rank1 = ?, rank2 = ?, rank3 = ?, rank4 = ?, rank5 = ? WHERE id = ?"); try (PreparedStatement ps = con.prepareStatement("UPDATE `alliance` SET capacity = ?, notice = ?, rank1 = ?, rank2 = ?, rank3 = ?, rank4 = ?, rank5 = ? WHERE id = ?")) {
ps.setInt(1, this.capacity); ps.setInt(1, this.capacity);
ps.setString(2, this.notice); ps.setString(2, this.notice);
ps.setString(3, this.rankTitles[0]); ps.setString(3, this.rankTitles[0]);
ps.setString(4, this.rankTitles[1]); ps.setString(4, this.rankTitles[1]);
ps.setString(5, this.rankTitles[2]); ps.setString(5, this.rankTitles[2]);
ps.setString(6, this.rankTitles[3]); ps.setString(6, this.rankTitles[3]);
ps.setString(7, this.rankTitles[4]); ps.setString(7, this.rankTitles[4]);
ps.setInt(8, this.allianceId); ps.setInt(8, this.allianceId);
ps.executeUpdate();
ps.close();
ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE allianceid = ?");
ps.setInt(1, this.allianceId);
ps.executeUpdate();
ps.close();
for(int i = 0; i < guilds.size(); i++) {
int guild = guilds.get(i);
ps = con.prepareStatement("INSERT INTO `allianceguilds` (`allianceid`, `guildid`) VALUES (?, ?)");
ps.setInt(1, this.allianceId);
ps.setInt(2, guild);
ps.executeUpdate(); ps.executeUpdate();
ps.close();
} }
con.close(); try (PreparedStatement ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE allianceid = ?")) {
ps.setInt(1, this.allianceId);
ps.executeUpdate();
}
for (int guild : guilds) {
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `allianceguilds` (`allianceid`, `guildid`) VALUES (?, ?)")) {
ps.setInt(1, this.allianceId);
ps.setInt(2, guild);
ps.executeUpdate();
}
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void disbandAlliance(int allianceId) { public static void disbandAlliance(int allianceId) {
PreparedStatement ps = null; try (Connection con = DatabaseConnection.getConnection()) {
Connection con = null;
try { try (PreparedStatement ps = con.prepareStatement("DELETE FROM `alliance` WHERE id = ?")) {
con = DatabaseConnection.getConnection(); ps.setInt(1, allianceId);
ps.executeUpdate();
ps = con.prepareStatement("DELETE FROM `alliance` WHERE id = ?"); }
ps.setInt(1, allianceId);
ps.executeUpdate(); try (PreparedStatement ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE allianceid = ?")) {
ps.close(); ps.setInt(1, allianceId);
ps.executeUpdate();
ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE allianceid = ?"); }
ps.setInt(1, allianceId);
ps.executeUpdate();
ps.close();
con.close();
Server.getInstance().allianceMessage(allianceId, MaplePacketCreator.disbandAlliance(allianceId), -1, -1); Server.getInstance().allianceMessage(allianceId, MaplePacketCreator.disbandAlliance(allianceId), -1, -1);
Server.getInstance().disbandAlliance(allianceId); Server.getInstance().disbandAlliance(allianceId);
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); sqle.printStackTrace();
} finally {
try {
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} }
} }
private static void removeGuildFromAllianceOnDb(int guildId) { private static void removeGuildFromAllianceOnDb(int guildId) {
PreparedStatement ps = null; try (Connection con = DatabaseConnection.getConnection();
Connection con = null; PreparedStatement ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE guildid = ?")) {
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE guildid = ?");
ps.setInt(1, guildId); ps.setInt(1, guildId);
ps.executeUpdate(); ps.executeUpdate();
ps.close();
con.close();
} catch (SQLException sqle) { } catch (SQLException sqle) {
sqle.printStackTrace(); sqle.printStackTrace();
} finally {
try {
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} }
} }
public static boolean removeGuildFromAlliance(int allianceId, int guildId, int worldId) { public static boolean removeGuildFromAlliance(int allianceId, int guildId, int worldId) {
Server srv = Server.getInstance(); Server srv = Server.getInstance();
MapleAlliance alliance = srv.getAlliance(allianceId); MapleAlliance alliance = srv.getAlliance(allianceId);
if (alliance.getLeader().getGuildId() == guildId) { if (alliance.getLeader().getGuildId() == guildId) {
return false; return false;
} }
srv.allianceMessage(alliance.getId(), MaplePacketCreator.removeGuildFromAlliance(alliance, guildId, worldId), -1, -1); srv.allianceMessage(alliance.getId(), MaplePacketCreator.removeGuildFromAlliance(alliance, guildId, worldId), -1, -1);
srv.removeGuildFromAlliance(alliance.getId(), guildId); srv.removeGuildFromAlliance(alliance.getId(), guildId);
removeGuildFromAllianceOnDb(guildId); removeGuildFromAllianceOnDb(guildId);
srv.allianceMessage(alliance.getId(), MaplePacketCreator.getGuildAlliances(alliance, worldId), -1, -1); srv.allianceMessage(alliance.getId(), MaplePacketCreator.getGuildAlliances(alliance, worldId), -1, -1);
srv.allianceMessage(alliance.getId(), MaplePacketCreator.allianceNotice(alliance.getId(), alliance.getNotice()), -1, -1); srv.allianceMessage(alliance.getId(), MaplePacketCreator.allianceNotice(alliance.getId(), alliance.getNotice()), -1, -1);
srv.guildMessage(guildId, MaplePacketCreator.disbandAlliance(alliance.getId())); srv.guildMessage(guildId, MaplePacketCreator.disbandAlliance(alliance.getId()));
alliance.dropMessage("[" + srv.getGuild(guildId, worldId).getName() + "] guild has left the union."); alliance.dropMessage("[" + srv.getGuild(guildId, worldId).getName() + "] guild has left the union.");
return true; return true;
} }
public void updateAlliancePackets(MapleCharacter chr) { public void updateAlliancePackets(MapleCharacter chr) {
if (allianceId > 0) { if (allianceId > 0) {
this.broadcastMessage(MaplePacketCreator.updateAllianceInfo(this, chr.getWorld())); this.broadcastMessage(MaplePacketCreator.updateAllianceInfo(this, chr.getWorld()));
this.broadcastMessage(MaplePacketCreator.allianceNotice(this.getId(), this.getNotice())); this.broadcastMessage(MaplePacketCreator.allianceNotice(this.getId(), this.getNotice()));
} }
} }
public boolean removeGuild(int gid) { public boolean removeGuild(int gid) {
synchronized (guilds) { synchronized (guilds) {
int index = getGuildIndex(gid); int index = getGuildIndex(gid);
if(index == -1) return false; if (index == -1) {
return false;
}
guilds.remove(index); guilds.remove(index);
return true; return true;
} }
@@ -375,8 +331,10 @@ public class MapleAlliance {
public boolean addGuild(int gid) { public boolean addGuild(int gid) {
synchronized (guilds) { synchronized (guilds) {
if(guilds.size() == capacity || getGuildIndex(gid) > -1) return false; if (guilds.size() == capacity || getGuildIndex(gid) > -1) {
return false;
}
guilds.add(gid); guilds.add(gid);
return true; return true;
} }
@@ -392,7 +350,7 @@ public class MapleAlliance {
return -1; return -1;
} }
} }
public void setRankTitle(String[] ranks) { public void setRankTitle(String[] ranks) {
rankTitles = ranks; rankTitles = ranks;
} }
@@ -400,9 +358,9 @@ public class MapleAlliance {
public String getRankTitle(int rank) { public String getRankTitle(int rank) {
return rankTitles[rank - 1]; return rankTitles[rank - 1];
} }
public List<Integer> getGuilds() { public List<Integer> getGuilds() {
synchronized(guilds) { synchronized (guilds) {
List<Integer> guilds_ = new LinkedList<>(); List<Integer> guilds_ = new LinkedList<>();
for (int guild : guilds) { for (int guild : guilds) {
if (guild != -1) { if (guild != -1) {
@@ -412,7 +370,7 @@ public class MapleAlliance {
return guilds_; return guilds_;
} }
} }
public String getAllianceNotice() { public String getAllianceNotice() {
return notice; return notice;
} }
@@ -420,7 +378,7 @@ public class MapleAlliance {
public String getNotice() { public String getNotice() {
return notice; return notice;
} }
public void setNotice(String notice) { public void setNotice(String notice) {
this.notice = notice; this.notice = notice;
} }
@@ -432,11 +390,11 @@ public class MapleAlliance {
public void setCapacity(int newCapacity) { public void setCapacity(int newCapacity) {
this.capacity = newCapacity; this.capacity = newCapacity;
} }
public int getCapacity() { public int getCapacity() {
return this.capacity; return this.capacity;
} }
public int getId() { public int getId() {
return allianceId; return allianceId;
} }
@@ -444,40 +402,42 @@ public class MapleAlliance {
public String getName() { public String getName() {
return name; return name;
} }
public MapleGuildCharacter getLeader() { public MapleGuildCharacter getLeader() {
synchronized(guilds) { synchronized (guilds) {
for(Integer gId: guilds) { for (Integer gId : guilds) {
MapleGuild guild = Server.getInstance().getGuild(gId); MapleGuild guild = Server.getInstance().getGuild(gId);
MapleGuildCharacter mgc = guild.getMGC(guild.getLeaderId()); MapleGuildCharacter mgc = guild.getMGC(guild.getLeaderId());
if(mgc.getAllianceRank() == 1) return mgc; if (mgc.getAllianceRank() == 1) {
return mgc;
}
} }
return null; return null;
} }
} }
public void dropMessage(String message) { public void dropMessage(String message) {
dropMessage(5, message); dropMessage(5, message);
} }
public void dropMessage(int type, String message) { public void dropMessage(int type, String message) {
synchronized(guilds) { synchronized (guilds) {
for(Integer gId: guilds) { for (Integer gId : guilds) {
MapleGuild guild = Server.getInstance().getGuild(gId); MapleGuild guild = Server.getInstance().getGuild(gId);
guild.dropMessage(type, message); guild.dropMessage(type, message);
} }
} }
} }
public void broadcastMessage(byte[] packet) { public void broadcastMessage(byte[] packet) {
Server.getInstance().allianceMessage(allianceId, packet, -1, -1); Server.getInstance().allianceMessage(allianceId, packet, -1, -1);
} }
public static void sendInvitation(MapleClient c, String targetGuildName, int allianceId) { public static void sendInvitation(MapleClient c, String targetGuildName, int allianceId) {
MapleGuild mg = Server.getInstance().getGuildByName(targetGuildName); MapleGuild mg = Server.getInstance().getGuildByName(targetGuildName);
if(mg == null) { if (mg == null) {
c.getPlayer().dropMessage(5, "The entered guild does not exist."); c.getPlayer().dropMessage(5, "The entered guild does not exist.");
} else { } else {
if (mg.getAllianceId() > 0) { if (mg.getAllianceId() > 0) {
@@ -496,28 +456,28 @@ public class MapleAlliance {
} }
} }
} }
public static boolean answerInvitation(int targetId, String targetGuildName, int allianceId, boolean answer) { public static boolean answerInvitation(int targetId, String targetGuildName, int allianceId, boolean answer) {
MapleInviteResult res = MapleInviteCoordinator.answerInvite(InviteType.ALLIANCE, targetId, allianceId, answer); MapleInviteResult res = MapleInviteCoordinator.answerInvite(InviteType.ALLIANCE, targetId, allianceId, answer);
String msg; String msg;
MapleCharacter sender = res.from; MapleCharacter sender = res.from;
switch (res.result) { switch (res.result) {
case ACCEPTED: case ACCEPTED:
return true; return true;
case DENIED: case DENIED:
msg = "[" + targetGuildName + "] guild has denied your guild alliance invitation."; msg = "[" + targetGuildName + "] guild has denied your guild alliance invitation.";
break; break;
default: default:
msg = "The guild alliance request has not been accepted, since the invitation expired."; msg = "The guild alliance request has not been accepted, since the invitation expired.";
} }
if (sender != null) { if (sender != null) {
sender.dropMessage(5, msg); sender.dropMessage(5, msg);
} }
return false; return false;
} }
} }