Switch to Maven file structure

This commit is contained in:
P0nk
2021-03-30 21:07:35 +02:00
parent 4acc5675d6
commit 813643036b
817 changed files with 16 additions and 0 deletions

View File

@@ -0,0 +1,523 @@
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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.MapleClient;
import net.server.Server;
import net.server.coordinator.world.MapleInviteCoordinator;
import net.server.coordinator.world.MapleInviteCoordinator.InviteType;
import net.server.coordinator.world.MapleInviteCoordinator.MapleInviteResult;
import net.server.world.MapleParty;
import net.server.world.MaplePartyCharacter;
import tools.DatabaseConnection;
import tools.MaplePacketCreator;
/**
*
* @author XoticStory
* @author Ronan
*/
public class MapleAlliance {
final private List<Integer> guilds = new LinkedList<>();
private int allianceId = -1;
private int capacity;
private String name;
private String notice = "";
private String rankTitles[] = new String[5];
public MapleAlliance(String name, int id) {
this.name = name;
allianceId = id;
String[] ranks = {"Master", "Jr. Master", "Member", "Member", "Member"};
for (int i = 0; i < 5; i++) {
rankTitles[i] = ranks[i];
}
}
public static boolean canBeUsedAllianceName(String name) {
if (name.contains(" ") || name.length() > 12) {
return false;
}
try {
ResultSet rs;
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("SELECT name FROM alliance WHERE name = ?")) {
ps.setString(1, name);
rs = ps.executeQuery();
if (rs.next()) {
ps.close();
rs.close();
return false;
}
}
rs.close();
con.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
private static List<MapleCharacter> getPartyGuildMasters(MapleParty party) {
List<MapleCharacter> mcl = new LinkedList<>();
for(MaplePartyCharacter mpc: party.getMembers()) {
MapleCharacter chr = mpc.getPlayer();
if (chr != null) {
MapleCharacter lchr = party.getLeader().getPlayer();
if (chr.getGuildRank() == 1 && lchr != null && chr.getMapId() == lchr.getMapId()) {
mcl.add(chr);
}
}
}
if(!mcl.isEmpty() && !mcl.get(0).isPartyLeader()) {
for(int i = 1; i < mcl.size(); i++) {
if(mcl.get(i).isPartyLeader()) {
MapleCharacter temp = mcl.get(0);
mcl.set(0, mcl.get(i));
mcl.set(i, temp);
}
}
}
return mcl;
}
public static MapleAlliance createAlliance(MapleParty party, String name) {
List<MapleCharacter> guildMasters = getPartyGuildMasters(party);
if(guildMasters.size() != 2) return null;
List<Integer> guilds = new LinkedList<>();
for(MapleCharacter mc: guildMasters) guilds.add(mc.getGuildId());
MapleAlliance alliance = MapleAlliance.createAllianceOnDb(guilds, name);
if(alliance != null) {
alliance.setCapacity(guilds.size());
for(Integer g: guilds)
alliance.addGuild(g);
int id = alliance.getId();
try {
for(int i = 0; i < guildMasters.size(); i++) {
Server.getInstance().setGuildAllianceId(guilds.get(i), id);
Server.getInstance().resetAllianceGuildPlayersRank(guilds.get(i));
MapleCharacter chr = guildMasters.get(i);
chr.getMGC().setAllianceRank((i == 0) ? 1 : 2);
Server.getInstance().getGuild(chr.getGuildId()).getMGC(chr.getId()).setAllianceRank((i == 0) ? 1 : 2);
chr.saveGuildStatus();
}
Server.getInstance().addAlliance(id, alliance);
int worldid = guildMasters.get(0).getWorld();
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
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return alliance;
}
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.
int id = -1;
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO `alliance` (`name`) VALUES (?)", PreparedStatement.RETURN_GENERATED_KEYS);
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.close();
}
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return (new MapleAlliance(name, id));
}
public static MapleAlliance loadAlliance(int id) {
if (id <= 0) {
return null;
}
MapleAlliance alliance = new MapleAlliance(null, -1);
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM alliance WHERE id = ?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
rs.close();
ps.close();
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;
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) {
e.printStackTrace();
}
return alliance;
}
public void saveToDB() {
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE `alliance` SET capacity = ?, notice = ?, rank1 = ?, rank2 = ?, rank3 = ?, rank4 = ?, rank5 = ? WHERE id = ?");
ps.setInt(1, this.capacity);
ps.setString(2, this.notice);
ps.setString(3, this.rankTitles[0]);
ps.setString(4, this.rankTitles[1]);
ps.setString(5, this.rankTitles[2]);
ps.setString(6, this.rankTitles[3]);
ps.setString(7, this.rankTitles[4]);
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.close();
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void disbandAlliance(int allianceId) {
PreparedStatement ps = null;
Connection con = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("DELETE FROM `alliance` WHERE id = ?");
ps.setInt(1, allianceId);
ps.executeUpdate();
ps.close();
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().disbandAlliance(allianceId);
} catch (SQLException sqle) {
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) {
PreparedStatement ps = null;
Connection con = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("DELETE FROM `allianceguilds` WHERE guildid = ?");
ps.setInt(1, guildId);
ps.executeUpdate();
ps.close();
con.close();
} catch (SQLException sqle) {
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) {
Server srv = Server.getInstance();
MapleAlliance alliance = srv.getAlliance(allianceId);
if (alliance.getLeader().getGuildId() == guildId) {
return false;
}
srv.allianceMessage(alliance.getId(), MaplePacketCreator.removeGuildFromAlliance(alliance, guildId, worldId), -1, -1);
srv.removeGuildFromAlliance(alliance.getId(), guildId);
removeGuildFromAllianceOnDb(guildId);
srv.allianceMessage(alliance.getId(), MaplePacketCreator.getGuildAlliances(alliance, worldId), -1, -1);
srv.allianceMessage(alliance.getId(), MaplePacketCreator.allianceNotice(alliance.getId(), alliance.getNotice()), -1, -1);
srv.guildMessage(guildId, MaplePacketCreator.disbandAlliance(alliance.getId()));
alliance.dropMessage("[" + srv.getGuild(guildId, worldId).getName() + "] guild has left the union.");
return true;
}
public void updateAlliancePackets(MapleCharacter chr) {
if (allianceId > 0) {
this.broadcastMessage(MaplePacketCreator.updateAllianceInfo(this, chr.getWorld()));
this.broadcastMessage(MaplePacketCreator.allianceNotice(this.getId(), this.getNotice()));
}
}
public boolean removeGuild(int gid) {
synchronized (guilds) {
int index = getGuildIndex(gid);
if(index == -1) return false;
guilds.remove(index);
return true;
}
}
public boolean addGuild(int gid) {
synchronized (guilds) {
if(guilds.size() == capacity || getGuildIndex(gid) > -1) return false;
guilds.add(gid);
return true;
}
}
private int getGuildIndex(int gid) {
synchronized (guilds) {
for (int i = 0; i < guilds.size(); i++) {
if (guilds.get(i) == gid) {
return i;
}
}
return -1;
}
}
public void setRankTitle(String[] ranks) {
rankTitles = ranks;
}
public String getRankTitle(int rank) {
return rankTitles[rank - 1];
}
public List<Integer> getGuilds() {
synchronized(guilds) {
List<Integer> guilds_ = new LinkedList<>();
for (int guild : guilds) {
if (guild != -1) {
guilds_.add(guild);
}
}
return guilds_;
}
}
public String getAllianceNotice() {
return notice;
}
public String getNotice() {
return notice;
}
public void setNotice(String notice) {
this.notice = notice;
}
public void increaseCapacity(int inc) {
this.capacity += inc;
}
public void setCapacity(int newCapacity) {
this.capacity = newCapacity;
}
public int getCapacity() {
return this.capacity;
}
public int getId() {
return allianceId;
}
public String getName() {
return name;
}
public MapleGuildCharacter getLeader() {
synchronized(guilds) {
for(Integer gId: guilds) {
MapleGuild guild = Server.getInstance().getGuild(gId);
MapleGuildCharacter mgc = guild.getMGC(guild.getLeaderId());
if(mgc.getAllianceRank() == 1) return mgc;
}
return null;
}
}
public void dropMessage(String message) {
dropMessage(5, message);
}
public void dropMessage(int type, String message) {
synchronized(guilds) {
for(Integer gId: guilds) {
MapleGuild guild = Server.getInstance().getGuild(gId);
guild.dropMessage(type, message);
}
}
}
public void broadcastMessage(byte[] packet) {
Server.getInstance().allianceMessage(allianceId, packet, -1, -1);
}
public static void sendInvitation(MapleClient c, String targetGuildName, int allianceId) {
MapleGuild mg = Server.getInstance().getGuildByName(targetGuildName);
if(mg == null) {
c.getPlayer().dropMessage(5, "The entered guild does not exist.");
} else {
if (mg.getAllianceId() > 0) {
c.getPlayer().dropMessage(5, "The entered guild is already registered on a guild alliance.");
} else {
MapleCharacter victim = mg.getMGC(mg.getLeaderId()).getCharacter();
if (victim == null) {
c.getPlayer().dropMessage(5, "The master of the guild that you offered an invitation is currently not online.");
} else {
if (MapleInviteCoordinator.createInvite(InviteType.ALLIANCE, c.getPlayer(), allianceId, victim.getId())) {
victim.getClient().announce(MaplePacketCreator.allianceInvite(allianceId, c.getPlayer()));
} else {
c.getPlayer().dropMessage(5, "The master of the guild that you offered an invitation is currently managing another invite.");
}
}
}
}
}
public static boolean answerInvitation(int targetId, String targetGuildName, int allianceId, boolean answer) {
MapleInviteResult res = MapleInviteCoordinator.answerInvite(InviteType.ALLIANCE, targetId, allianceId, answer);
String msg;
MapleCharacter sender = res.from;
switch (res.result) {
case ACCEPTED:
return true;
case DENIED:
msg = "[" + targetGuildName + "] guild has denied your guild alliance invitation.";
break;
default:
msg = "The guild alliance request has not been accepted, since the invitation expired.";
}
if (sender != null) {
sender.dropMessage(5, msg);
}
return false;
}
}

View File

@@ -0,0 +1,849 @@
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.server.guild;
import client.MapleCharacter;
import client.MapleClient;
import config.YamlConfig;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import net.server.PlayerStorage;
import net.server.Server;
import net.server.channel.Channel;
import tools.DatabaseConnection;
import tools.MaplePacketCreator;
import net.server.audit.locks.MonitoredLockType;
import net.server.coordinator.world.MapleInviteCoordinator;
import net.server.coordinator.world.MapleInviteCoordinator.InviteType;
import net.server.coordinator.world.MapleInviteCoordinator.MapleInviteResult;
import net.server.coordinator.matchchecker.MapleMatchCheckerCoordinator;
public class MapleGuild {
private enum BCOp {
NONE, DISBAND, EMBLEMCHANGE
}
private final List<MapleGuildCharacter> members;
private final Lock membersLock = MonitoredReentrantLockFactory.createLock(MonitoredLockType.GUILD, true);
private String rankTitles[] = new String[5]; // 1 = master, 2 = jr, 5 = lowest member
private String name, notice;
private int id, gp, logo, logoColor, leader, capacity, logoBG, logoBGColor, signature, allianceId;
private int world;
private Map<Integer, List<Integer>> notifications = new LinkedHashMap<>();
private boolean bDirty = true;
public MapleGuild(int guildid, int world) {
this.world = world;
members = new ArrayList<>();
Connection con = null;
try {
con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM guilds WHERE guildid = " + guildid);
ResultSet rs = ps.executeQuery();
if (!rs.first()) {
id = -1;
ps.close();
rs.close();
return;
}
id = guildid;
name = rs.getString("name");
gp = rs.getInt("GP");
logo = rs.getInt("logo");
logoColor = rs.getInt("logoColor");
logoBG = rs.getInt("logoBG");
logoBGColor = rs.getInt("logoBGColor");
capacity = rs.getInt("capacity");
for (int i = 1; i <= 5; i++) {
rankTitles[i - 1] = rs.getString("rank" + i + "title");
}
leader = rs.getInt("leader");
notice = rs.getString("notice");
signature = rs.getInt("signature");
allianceId = rs.getInt("allianceId");
ps.close();
rs.close();
ps = con.prepareStatement("SELECT id, name, level, job, guildrank, allianceRank FROM characters WHERE guildid = ? ORDER BY guildrank ASC, name ASC");
ps.setInt(1, guildid);
rs = ps.executeQuery();
if (!rs.first()) {
rs.close();
ps.close();
return;
}
do {
members.add(new MapleGuildCharacter(null, rs.getInt("id"), rs.getInt("level"), rs.getString("name"), (byte) -1, world, rs.getInt("job"), rs.getInt("guildrank"), guildid, false, rs.getInt("allianceRank")));
} while (rs.next());
ps.close();
rs.close();
con.close();
} catch (SQLException se) {
se.printStackTrace();
System.out.println("Unable to read guild information from sql: " + se);
}
}
private void buildNotifications() {
if (!bDirty) {
return;
}
Set<Integer> chs = Server.getInstance().getOpenChannels(world);
synchronized (notifications) {
if (notifications.keySet().size() != chs.size()) {
notifications.clear();
for (Integer ch : chs) {
notifications.put(ch, new LinkedList<Integer>());
}
} else {
for (List<Integer> l : notifications.values()) {
l.clear();
}
}
}
membersLock.lock();
try {
for (MapleGuildCharacter mgc : members) {
if (!mgc.isOnline()) {
continue;
}
List<Integer> chl;
synchronized (notifications) {
chl = notifications.get(mgc.getChannel());
}
if (chl != null) chl.add(mgc.getId());
//Unable to connect to Channel... error was here
}
} finally {
membersLock.unlock();
}
bDirty = false;
}
public void writeToDB(boolean bDisband) {
try {
Connection con = DatabaseConnection.getConnection();
if (!bDisband) {
StringBuilder builder = new StringBuilder();
builder.append("UPDATE guilds SET GP = ?, logo = ?, logoColor = ?, logoBG = ?, logoBGColor = ?, ");
for (int i = 0; i < 5; i++) {
builder.append("rank").append(i + 1).append("title = ?, ");
}
builder.append("capacity = ?, notice = ? WHERE guildid = ?");
try (PreparedStatement ps = con.prepareStatement(builder.toString())) {
ps.setInt(1, gp);
ps.setInt(2, logo);
ps.setInt(3, logoColor);
ps.setInt(4, logoBG);
ps.setInt(5, logoBGColor);
for (int i = 6; i < 11; i++) {
ps.setString(i, rankTitles[i - 6]);
}
ps.setInt(11, capacity);
ps.setString(12, notice);
ps.setInt(13, this.id);
ps.execute();
}
} else {
PreparedStatement ps = con.prepareStatement("UPDATE characters SET guildid = 0, guildrank = 5 WHERE guildid = ?");
ps.setInt(1, this.id);
ps.execute();
ps.close();
ps = con.prepareStatement("DELETE FROM guilds WHERE guildid = ?");
ps.setInt(1, this.id);
ps.execute();
ps.close();
membersLock.lock();
try {
this.broadcast(MaplePacketCreator.guildDisband(this.id));
} finally {
membersLock.unlock();
}
}
con.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
public int getId() {
return id;
}
public int getLeaderId() {
return leader;
}
public int setLeaderId(int charId) {
return leader = charId;
}
public int getGP() {
return gp;
}
public int getLogo() {
return logo;
}
public void setLogo(int l) {
logo = l;
}
public int getLogoColor() {
return logoColor;
}
public void setLogoColor(int c) {
logoColor = c;
}
public int getLogoBG() {
return logoBG;
}
public void setLogoBG(int bg) {
logoBG = bg;
}
public int getLogoBGColor() {
return logoBGColor;
}
public void setLogoBGColor(int c) {
logoBGColor = c;
}
public String getNotice() {
if (notice == null) {
return "";
}
return notice;
}
public String getName() {
return name;
}
public List<MapleGuildCharacter> getMembers() {
membersLock.lock();
try {
return new ArrayList<>(members);
} finally {
membersLock.unlock();
}
}
public int getCapacity() {
return capacity;
}
public int getSignature() {
return signature;
}
public void broadcastNameChanged() {
PlayerStorage ps = Server.getInstance().getWorld(world).getPlayerStorage();
for (MapleGuildCharacter mgc : getMembers()) {
MapleCharacter chr = ps.getCharacterById(mgc.getId());
if (chr == null || !chr.isLoggedinWorld()) continue;
byte[] packet = MaplePacketCreator.guildNameChanged(chr.getId(), this.getName());
chr.getMap().broadcastMessage(chr, packet);
}
}
public void broadcastEmblemChanged() {
PlayerStorage ps = Server.getInstance().getWorld(world).getPlayerStorage();
for (MapleGuildCharacter mgc : getMembers()) {
MapleCharacter chr = ps.getCharacterById(mgc.getId());
if (chr == null || !chr.isLoggedinWorld()) continue;
byte[] packet = MaplePacketCreator.guildMarkChanged(chr.getId(), this);
chr.getMap().broadcastMessage(chr, packet);
}
}
public void broadcastInfoChanged() {
PlayerStorage ps = Server.getInstance().getWorld(world).getPlayerStorage();
for (MapleGuildCharacter mgc : getMembers()) {
MapleCharacter chr = ps.getCharacterById(mgc.getId());
if (chr == null || !chr.isLoggedinWorld()) continue;
byte[] packet = MaplePacketCreator.showGuildInfo(chr);
chr.announce(packet);
}
}
public void broadcast(final byte[] packet) {
broadcast(packet, -1, BCOp.NONE);
}
public void broadcast(final byte[] packet, int exception) {
broadcast(packet, exception, BCOp.NONE);
}
public void broadcast(final byte[] packet, int exceptionId, BCOp bcop) {
membersLock.lock(); // membersLock awareness thanks to ProjectNano dev team
try {
synchronized (notifications) {
if (bDirty) {
buildNotifications();
}
try {
for (Integer b : Server.getInstance().getOpenChannels(world)) {
if (notifications.get(b).size() > 0) {
if (bcop == BCOp.DISBAND) {
Server.getInstance().getWorld(world).setGuildAndRank(notifications.get(b), 0, 5, exceptionId);
} else if (bcop == BCOp.EMBLEMCHANGE) {
Server.getInstance().getWorld(world).changeEmblem(this.id, notifications.get(b), new MapleGuildSummary(this));
} else {
Server.getInstance().getWorld(world).sendPacket(notifications.get(b), packet, exceptionId);
}
}
}
} catch (Exception re) {
re.printStackTrace();
System.out.println("Failed to contact channel(s) for broadcast.");//fu?
}
}
} finally {
membersLock.unlock();
}
}
public void guildMessage(final byte[] serverNotice) {
membersLock.lock();
try {
for (MapleGuildCharacter mgc : members) {
for (Channel cs : Server.getInstance().getChannelsFromWorld(world)) {
if (cs.getPlayerStorage().getCharacterById(mgc.getId()) != null) {
cs.getPlayerStorage().getCharacterById(mgc.getId()).getClient().announce(serverNotice);
break;
}
}
}
} finally {
membersLock.unlock();
}
}
public void dropMessage(String message) {
dropMessage(5, message);
}
public void dropMessage(int type, String message) {
membersLock.lock();
try {
for (MapleGuildCharacter mgc : members) {
if(mgc.getCharacter() != null) {
mgc.getCharacter().dropMessage(type, message);
}
}
} finally {
membersLock.unlock();
}
}
public void broadcastMessage(byte[] packet) {
Server.getInstance().guildMessage(id, packet);
}
public final void setOnline(int cid, boolean online, int channel) {
membersLock.lock();
try {
boolean bBroadcast = true;
for (MapleGuildCharacter mgc : members) {
if (mgc.getId() == cid) {
if (mgc.isOnline() && online) {
bBroadcast = false;
}
mgc.setOnline(online);
mgc.setChannel(channel);
break;
}
}
if (bBroadcast) {
this.broadcast(MaplePacketCreator.guildMemberOnline(id, cid, online), cid);
}
bDirty = true;
} finally {
membersLock.unlock();
}
}
public void guildChat(String name, int cid, String message) {
membersLock.lock();
try {
this.broadcast(MaplePacketCreator.multiChat(name, message, 2), cid);
} finally {
membersLock.unlock();
}
}
public String getRankTitle(int rank) {
return rankTitles[rank - 1];
}
public static int createGuild(int leaderId, String name) {
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT guildid FROM guilds WHERE name = ?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
if (rs.first()) {
ps.close();
rs.close();
return 0;
}
ps.close();
rs.close();
ps = con.prepareStatement("INSERT INTO guilds (`leader`, `name`, `signature`) VALUES (?, ?, ?)");
ps.setInt(1, leaderId);
ps.setString(2, name);
ps.setInt(3, (int) System.currentTimeMillis());
ps.execute();
ps.close();
ps = con.prepareStatement("SELECT guildid FROM guilds WHERE leader = ?");
ps.setInt(1, leaderId);
rs = ps.executeQuery();
rs.first();
int guildId = rs.getInt("guildid");
rs.close();
ps.close();
ps = con.prepareStatement("UPDATE characters SET guildid = ? WHERE id = ?");
ps.setInt(1, guildId);
ps.setInt(2, leaderId);
ps.executeUpdate();
ps.close();
con.close();
return guildId;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public int addGuildMember(MapleGuildCharacter mgc, MapleCharacter chr) {
membersLock.lock();
try {
if (members.size() >= capacity) {
return 0;
}
for (int i = members.size() - 1; i >= 0; i--) {
if (members.get(i).getGuildRank() < 5 || members.get(i).getName().compareTo(mgc.getName()) < 0) {
mgc.setCharacter(chr);
members.add(i + 1, mgc);
bDirty = true;
break;
}
}
this.broadcast(MaplePacketCreator.newGuildMember(mgc));
return 1;
} finally {
membersLock.unlock();
}
}
public void leaveGuild(MapleGuildCharacter mgc) {
membersLock.lock();
try {
this.broadcast(MaplePacketCreator.memberLeft(mgc, false));
members.remove(mgc);
bDirty = true;
} finally {
membersLock.unlock();
}
}
public void expelMember(MapleGuildCharacter initiator, String name, int cid) {
membersLock.lock();
try {
java.util.Iterator<MapleGuildCharacter> itr = members.iterator();
MapleGuildCharacter mgc;
while (itr.hasNext()) {
mgc = itr.next();
if (mgc.getId() == cid && initiator.getGuildRank() < mgc.getGuildRank()) {
this.broadcast(MaplePacketCreator.memberLeft(mgc, true));
itr.remove();
bDirty = true;
try {
if (mgc.isOnline()) {
Server.getInstance().getWorld(mgc.getWorld()).setGuildAndRank(cid, 0, 5);
} else {
try {
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("INSERT INTO notes (`to`, `from`, `message`, `timestamp`) VALUES (?, ?, ?, ?)")) {
ps.setString(1, mgc.getName());
ps.setString(2, initiator.getName());
ps.setString(3, "You have been expelled from the guild.");
ps.setLong(4, System.currentTimeMillis());
ps.executeUpdate();
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("expelMember - MapleGuild " + e);
}
Server.getInstance().getWorld(mgc.getWorld()).setOfflineGuildStatus((short) 0, (byte) 5, cid);
}
} catch (Exception re) {
re.printStackTrace();
return;
}
return;
}
}
System.out.println("Unable to find member with name " + name + " and id " + cid);
} finally {
membersLock.unlock();
}
}
public void changeRank(int cid, int newRank) {
membersLock.lock();
try {
for (MapleGuildCharacter mgc : members) {
if (cid == mgc.getId()) {
changeRank(mgc, newRank);
return;
}
}
} finally {
membersLock.unlock();
}
}
public void changeRank(MapleGuildCharacter mgc, int newRank) {
try {
if (mgc.isOnline()) {
Server.getInstance().getWorld(mgc.getWorld()).setGuildAndRank(mgc.getId(), this.id, newRank);
mgc.setGuildRank(newRank);
} else {
Server.getInstance().getWorld(mgc.getWorld()).setOfflineGuildStatus((short) this.id, (byte) newRank, mgc.getId());
mgc.setOfflineGuildRank(newRank);
}
} catch (Exception re) {
re.printStackTrace();
return;
}
membersLock.lock();
try {
this.broadcast(MaplePacketCreator.changeRank(mgc));
} finally {
membersLock.unlock();
}
}
public void setGuildNotice(String notice) {
this.notice = notice;
this.writeToDB(false);
membersLock.lock();
try {
this.broadcast(MaplePacketCreator.guildNotice(this.id, notice));
} finally {
membersLock.unlock();
}
}
public void memberLevelJobUpdate(MapleGuildCharacter mgc) {
membersLock.lock();
try {
for (MapleGuildCharacter member : members) {
if (mgc.equals(member)) {
member.setJobId(mgc.getJobId());
member.setLevel(mgc.getLevel());
this.broadcast(MaplePacketCreator.guildMemberLevelJobUpdate(mgc));
break;
}
}
} finally {
membersLock.unlock();
}
}
@Override
public boolean equals(Object other) {
if (!(other instanceof MapleGuildCharacter)) {
return false;
}
MapleGuildCharacter o = (MapleGuildCharacter) other;
return (o.getId() == id && o.getName().equals(name));
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 89 * hash + this.id;
return hash;
}
public void changeRankTitle(String[] ranks) {
System.arraycopy(ranks, 0, rankTitles, 0, 5);
membersLock.lock();
try {
this.broadcast(MaplePacketCreator.rankTitleChange(this.id, ranks));
} finally {
membersLock.unlock();
}
this.writeToDB(false);
}
public void disbandGuild() {
if(allianceId > 0) {
if (!MapleAlliance.removeGuildFromAlliance(allianceId, id, world)) {
MapleAlliance.disbandAlliance(allianceId);
}
}
membersLock.lock();
try {
this.writeToDB(true);
this.broadcast(null, -1, BCOp.DISBAND);
} finally {
membersLock.unlock();
}
}
public void setGuildEmblem(short bg, byte bgcolor, short logo, byte logocolor) {
this.logoBG = bg;
this.logoBGColor = bgcolor;
this.logo = logo;
this.logoColor = logocolor;
this.writeToDB(false);
membersLock.lock();
try {
this.broadcast(null, -1, BCOp.EMBLEMCHANGE);
} finally {
membersLock.unlock();
}
}
public MapleGuildCharacter getMGC(int cid) {
membersLock.lock();
try {
for (MapleGuildCharacter mgc : members) {
if (mgc.getId() == cid) {
return mgc;
}
}
return null;
} finally {
membersLock.unlock();
}
}
public boolean increaseCapacity() {
if (capacity > 99) {
return false;
}
capacity += 5;
this.writeToDB(false);
membersLock.lock();
try {
this.broadcast(MaplePacketCreator.guildCapacityChange(this.id, this.capacity));
} finally {
membersLock.unlock();
}
return true;
}
public void gainGP(int amount) {
this.gp += amount;
this.writeToDB(false);
this.guildMessage(MaplePacketCreator.updateGP(this.id, this.gp));
this.guildMessage(MaplePacketCreator.getGPMessage(amount));
}
public void removeGP(int amount){
this.gp -= amount;
this.writeToDB(false);
this.guildMessage(MaplePacketCreator.updateGP(this.id, this.gp));
}
public static MapleGuildResponse sendInvitation(MapleClient c, String targetName) {
MapleCharacter mc = c.getChannelServer().getPlayerStorage().getCharacterByName(targetName);
if (mc == null) {
return MapleGuildResponse.NOT_IN_CHANNEL;
}
if (mc.getGuildId() > 0) {
return MapleGuildResponse.ALREADY_IN_GUILD;
}
MapleCharacter sender = c.getPlayer();
if (MapleInviteCoordinator.createInvite(InviteType.GUILD, sender, sender.getGuildId(), mc.getId())) {
mc.getClient().announce(MaplePacketCreator.guildInvite(sender.getGuildId(), sender.getName()));
return null;
} else {
return MapleGuildResponse.MANAGING_INVITE;
}
}
public static boolean answerInvitation(int targetId, String targetName, int guildId, boolean answer) {
MapleInviteResult res = MapleInviteCoordinator.answerInvite(InviteType.GUILD, targetId, guildId, answer);
MapleGuildResponse mgr;
MapleCharacter sender = res.from;
switch (res.result) {
case ACCEPTED:
return true;
case DENIED:
mgr = MapleGuildResponse.DENIED_INVITE;
break;
default:
mgr = MapleGuildResponse.NOT_FOUND_INVITE;
}
if (mgr != null && sender != null) {
sender.announce(mgr.getPacket(targetName));
}
return false;
}
public static Set<MapleCharacter> getEligiblePlayersForGuild(MapleCharacter guildLeader) {
Set<MapleCharacter> guildMembers = new HashSet<>();
guildMembers.add(guildLeader);
MapleMatchCheckerCoordinator mmce = guildLeader.getWorldServer().getMatchCheckerCoordinator();
for (MapleCharacter chr : guildLeader.getMap().getAllPlayers()) {
if (chr.getParty() == null && chr.getGuild() == null && mmce.getMatchConfirmationLeaderid(chr.getId()) == -1) {
guildMembers.add(chr);
}
}
return guildMembers;
}
public static void displayGuildRanks(MapleClient c, int npcid) {
try {
ResultSet rs;
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("SELECT `name`, `GP`, `logoBG`, `logoBGColor`, `logo`, `logoColor` FROM guilds ORDER BY `GP` DESC LIMIT 50")) {
rs = ps.executeQuery();
c.announce(MaplePacketCreator.showGuildRanks(npcid, rs));
}
rs.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("failed to display guild ranks. " + e);
}
}
public int getAllianceId() {
return allianceId;
}
public void setAllianceId(int aid) {
this.allianceId = aid;
try {
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("UPDATE guilds SET allianceId = ? WHERE guildid = ?")) {
ps.setInt(1, aid);
ps.setInt(2, id);
ps.executeUpdate();
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void resetAllianceGuildPlayersRank() {
try {
membersLock.lock();
try {
for(MapleGuildCharacter mgc: members) {
if(mgc.isOnline()) {
mgc.setAllianceRank(5);
}
}
} finally {
membersLock.unlock();
}
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET allianceRank = ? WHERE guildid = ?")) {
ps.setInt(1, 5);
ps.setInt(2, id);
ps.executeUpdate();
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static int getIncreaseGuildCost(int size) {
int cost = YamlConfig.config.server.EXPAND_GUILD_BASE_COST + Math.max(0, (size - 15) / 5) * YamlConfig.config.server.EXPAND_GUILD_TIER_COST;
if (size > 30) {
return Math.min(YamlConfig.config.server.EXPAND_GUILD_MAX_COST, Math.max(cost, 5000000));
} else {
return cost;
}
}
}

View File

@@ -0,0 +1,167 @@
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.server.guild;
import client.MapleCharacter;
public class MapleGuildCharacter {
private MapleCharacter character;
private int level;
private int id;
private int world, channel;
private int jobid;
private int guildrank;
private int guildid;
private int allianceRank;
private boolean online;
private String name;
public MapleGuildCharacter(MapleCharacter chr) {
this.character = chr;
this.name = chr.getName();
this.level = chr.getLevel();
this.id = chr.getId();
this.channel = chr.getClient().getChannel();
this.world = chr.getWorld();
this.jobid = chr.getJob().getId();
this.guildrank = chr.getGuildRank();
this.guildid = chr.getGuildId();
this.online = true;
this.allianceRank = chr.getAllianceRank();
}
public MapleGuildCharacter(MapleCharacter chr, int _id, int _lv, String _name, int _channel, int _world, int _job, int _rank, int _gid, boolean _on, int _allianceRank) {
this.character = chr;
this.level = _lv;
this.id = _id;
this.name = _name;
if (_on) {
this.channel = _channel;
this.world = _world;
}
this.jobid = _job;
this.online = _on;
this.guildrank = _rank;
this.guildid = _gid;
this.allianceRank = _allianceRank;
}
public void setCharacter(MapleCharacter ch) {
this.character = ch;
}
public MapleCharacter getCharacter() {
return character;
}
public int getLevel() {
return level;
}
public void setLevel(int l) {
level = l;
}
public int getId() {
return id;
}
public void setChannel(int ch) {
channel = ch;
}
public int getChannel() {
return channel;
}
public int getWorld() {
return world;
}
public int getJobId() {
return jobid;
}
public void setJobId(int job) {
jobid = job;
}
public int getGuildId() {
return guildid;
}
public void setGuildId(int gid) {
guildid = gid;
character.setGuildId(gid);
}
public int getGuildRank() {
return guildrank;
}
public void setOfflineGuildRank(int rank) {
guildrank = rank;
}
public void setGuildRank(int rank) {
guildrank = rank;
character.setGuildRank(rank);
}
public int getAllianceRank() {
return allianceRank;
}
public void setAllianceRank(int rank) {
allianceRank = rank;
character.setAllianceRank(rank);
}
public boolean isOnline() {
return online;
}
public void setOnline(boolean f) {
online = f;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof MapleGuildCharacter)) {
return false;
}
MapleGuildCharacter o = (MapleGuildCharacter) other;
return (o.getId() == id && o.getName().equals(name));
}
@Override
public int hashCode() {
int hash = 3;
hash = 19 * hash + this.id;
hash = 19 * hash + (this.name != null ? this.name.hashCode() : 0);
return hash;
}
}

View File

@@ -0,0 +1,47 @@
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.server.guild;
import tools.MaplePacketCreator;
public enum MapleGuildResponse {
NOT_IN_CHANNEL(0x2a),
ALREADY_IN_GUILD(0x28),
NOT_IN_GUILD(0x2d),
NOT_FOUND_INVITE(0x2e),
MANAGING_INVITE(0x36),
DENIED_INVITE(0x37);
private int value;
private MapleGuildResponse(int val) {
value = val;
}
public final byte[] getPacket(String targetName) {
if (value >= MANAGING_INVITE.value) {
return MaplePacketCreator.responseGuildMessage((byte) value, targetName);
} else {
return MaplePacketCreator.genericGuildMessage((byte) value);
}
}
}

View File

@@ -0,0 +1,64 @@
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.server.guild;
public class MapleGuildSummary {
private String name;
private short logoBG;
private byte logoBGColor;
private short logo;
private byte logoColor;
private int allianceId;
public MapleGuildSummary(MapleGuild g) {
this.name = g.getName();
this.logoBG = (short) g.getLogoBG();
this.logoBGColor = (byte) g.getLogoBGColor();
this.logo = (short) g.getLogo();
this.logoColor = (byte) g.getLogoColor();
this.allianceId = g.getAllianceId();
}
public String getName() {
return name;
}
public short getLogoBG() {
return logoBG;
}
public byte getLogoBGColor() {
return logoBGColor;
}
public short getLogo() {
return logo;
}
public byte getLogoColor() {
return logoColor;
}
public int getAllianceId() {
return allianceId;
}
}