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,561 @@
/*
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>
Copyleft (L) 2016 - 2019 RonanLana (HeavenMS)
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 client.processor.npc;
import client.MapleCharacter;
import client.MapleClient;
import client.autoban.AutobanFactory;
import client.inventory.Item;
import client.inventory.ItemFactory;
import client.inventory.MapleInventory;
import client.inventory.MapleInventoryType;
import client.inventory.manipulator.MapleInventoryManipulator;
import client.inventory.manipulator.MapleKarmaManipulator;
import config.YamlConfig;
import constants.inventory.ItemConstants;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import net.server.channel.Channel;
import server.DueyPackage;
import server.MapleItemInformationProvider;
import server.MapleTrade;
import tools.DatabaseConnection;
import tools.FilePrinter;
import tools.MaplePacketCreator;
import tools.Pair;
/**
*
* @author RonanLana - synchronization of Duey modules
*/
public class DueyProcessor {
public enum Actions {
TOSERVER_RECV_ITEM(0x00),
TOSERVER_SEND_ITEM(0x02),
TOSERVER_CLAIM_PACKAGE(0x04),
TOSERVER_REMOVE_PACKAGE(0x05),
TOSERVER_CLOSE_DUEY(0x07),
TOCLIENT_OPEN_DUEY(0x08),
TOCLIENT_SEND_ENABLE_ACTIONS(0x09),
TOCLIENT_SEND_NOT_ENOUGH_MESOS(0x0A),
TOCLIENT_SEND_INCORRECT_REQUEST(0x0B),
TOCLIENT_SEND_NAME_DOES_NOT_EXIST(0x0C),
TOCLIENT_SEND_SAMEACC_ERROR(0x0D),
TOCLIENT_SEND_RECEIVER_STORAGE_FULL(0x0E),
TOCLIENT_SEND_RECEIVER_UNABLE_TO_RECV(0x0F),
TOCLIENT_SEND_RECEIVER_STORAGE_WITH_UNIQUE(0x10),
TOCLIENT_SEND_MESO_LIMIT(0x11),
TOCLIENT_SEND_SUCCESSFULLY_SENT(0x12),
TOCLIENT_RECV_UNKNOWN_ERROR(0x13),
TOCLIENT_RECV_ENABLE_ACTIONS(0x14),
TOCLIENT_RECV_NO_FREE_SLOTS(0x15),
TOCLIENT_RECV_RECEIVER_WITH_UNIQUE(0x16),
TOCLIENT_RECV_SUCCESSFUL_MSG(0x17),
TOCLIENT_RECV_PACKAGE_MSG(0x1B);
final byte code;
private Actions(int code) {
this.code = (byte) code;
}
public byte getCode() {
return code;
}
}
private static Pair<Integer, Integer> getAccountCharacterIdFromCNAME(String name) {
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT id,accountid FROM characters WHERE name = ?");
ps.setString(1, name);
Pair<Integer, Integer> id_ = null;
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
id_ = new Pair<>(rs.getInt("accountid"), rs.getInt("id"));
}
}
ps.close();
con.close();
return id_;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
private static void showDueyNotification(MapleClient c, MapleCharacter player) {
Connection con = null;
PreparedStatement ps = null;
PreparedStatement pss = null;
ResultSet rs = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT SenderName, Type FROM dueypackages WHERE ReceiverId = ? AND Checked = 1 ORDER BY Type DESC");
ps.setInt(1, player.getId());
rs = ps.executeQuery();
if (rs.next()) {
try {
Connection con2 = DatabaseConnection.getConnection();
pss = con2.prepareStatement("UPDATE dueypackages SET Checked = 0 where ReceiverId = ?");
pss.setInt(1, player.getId());
pss.executeUpdate();
pss.close();
con2.close();
c.announce(MaplePacketCreator.sendDueyParcelReceived(rs.getString("SenderName"), rs.getInt("Type") == 1));
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pss != null) {
pss.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
private static void deletePackageFromInventoryDB(Connection con, int packageId) throws SQLException {
ItemFactory.DUEY.saveItems(new LinkedList<Pair<Item, MapleInventoryType>>(), packageId, con);
}
private static void removePackageFromDB(int packageId) {
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM dueypackages WHERE PackageId = ?");
ps.setInt(1, packageId);
ps.executeUpdate();
ps.close();
deletePackageFromInventoryDB(con, packageId);
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static DueyPackage getPackageFromDB(ResultSet rs) {
try {
int packageId = rs.getInt("PackageId");
List<Pair<Item, MapleInventoryType>> dueyItems = ItemFactory.DUEY.loadItems(packageId, false);
DueyPackage dueypack;
if (!dueyItems.isEmpty()) { // in a duey package there's only one item
dueypack = new DueyPackage(packageId, dueyItems.get(0).getLeft());
} else {
dueypack = new DueyPackage(packageId);
}
dueypack.setSender(rs.getString("SenderName"));
dueypack.setMesos(rs.getInt("Mesos"));
dueypack.setSentTime(rs.getTimestamp("TimeStamp"), rs.getBoolean("Type"));
dueypack.setMessage(rs.getString("Message"));
return dueypack;
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
}
}
private static List<DueyPackage> loadPackages(MapleCharacter chr) {
List<DueyPackage> packages = new LinkedList<>();
try {
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM dueypackages dp WHERE ReceiverId = ?")) {
ps.setInt(1, chr.getId());
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
DueyPackage dueypack = getPackageFromDB(rs);
if (dueypack == null) continue;
packages.add(dueypack);
}
}
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return packages;
}
private static int createPackage(int mesos, String message, String sender, int toCid, boolean quick) {
try {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("INSERT INTO `dueypackages` (ReceiverId, SenderName, Mesos, TimeStamp, Message, Type, Checked) VALUES (?, ?, ?, ?, ?, ?, 1)", Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, toCid);
ps.setString(2, sender);
ps.setInt(3, mesos);
ps.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
ps.setString(5, message);
ps.setInt(6, quick ? 1 : 0);
int updateRows = ps.executeUpdate();
if (updateRows < 1) {
FilePrinter.printError(FilePrinter.INSERT_CHAR, "Error trying to create package [mesos: " + mesos + ", " + sender + ", quick: " + quick + ", to CharacterId: " + toCid + "]");
return -1;
}
int packageId;
rs = ps.getGeneratedKeys();
if (rs.next()) {
packageId = rs.getInt(1);
} else {
FilePrinter.printError(FilePrinter.INSERT_CHAR, "Failed inserting package [mesos: " + mesos + ", " + sender + ", quick: " + quick + ", to CharacterId: " + toCid + "]");
return -1;
}
return packageId;
} finally {
if (rs != null && !rs.isClosed()) {
rs.close();
}
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (con != null && !con.isClosed()) {
con.close();
}
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return -1;
}
private static boolean insertPackageItem(int packageId, Item item) {
try {
Pair<Item, MapleInventoryType> dueyItem = new Pair<>(item, MapleInventoryType.getByType(item.getItemType()));
Connection con = DatabaseConnection.getConnection();
ItemFactory.DUEY.saveItems(Collections.singletonList(dueyItem), packageId, con);
con.close();
return true;
} catch (SQLException sqle) {
sqle.printStackTrace();
return false;
}
}
private static int addPackageItemFromInventory(int packageId, MapleClient c, byte invTypeId, short itemPos, short amount) {
if (invTypeId > 0) {
MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
MapleInventoryType invType = MapleInventoryType.getByType(invTypeId);
MapleInventory inv = c.getPlayer().getInventory(invType);
Item item;
inv.lockInventory();
try {
item = inv.getItem(itemPos);
if (item != null && item.getQuantity() >= amount) {
if (item.isUntradeable() || ii.isUnmerchable(item.getItemId())) {
return -1;
}
if (ItemConstants.isRechargeable(item.getItemId())) {
MapleInventoryManipulator.removeFromSlot(c, invType, itemPos, item.getQuantity(), true);
} else {
MapleInventoryManipulator.removeFromSlot(c, invType, itemPos, amount, true, false);
}
item = item.copy();
} else {
return -2;
}
} finally {
inv.unlockInventory();
}
MapleKarmaManipulator.toggleKarmaFlagToUntradeable(item);
item.setQuantity(amount);
if (!insertPackageItem(packageId, item)) {
return 1;
}
}
return 0;
}
public static void dueySendItem(MapleClient c, byte invTypeId, short itemPos, short amount, int sendMesos, String sendMessage, String recipient, boolean quick) {
if (c.tryacquireClient()) {
try {
int fee = MapleTrade.getFee(sendMesos);
if (!quick) {
fee += 5000;
} else if (!c.getPlayer().haveItem(5330000)) {
AutobanFactory.PACKET_EDIT.alert(c.getPlayer(), c.getPlayer().getName() + " tried to packet edit with Quick Delivery on duey.");
FilePrinter.printError(FilePrinter.EXPLOITS + c.getPlayer().getName() + ".txt", c.getPlayer().getName() + " tried to use duey with Quick Delivery, mesos " + sendMesos + " and amount " + amount);
c.disconnect(true, false);
return;
}
long finalcost = (long) sendMesos + fee;
if (finalcost < 0 || finalcost > Integer.MAX_VALUE || (amount < 1 && sendMesos == 0)) {
AutobanFactory.PACKET_EDIT.alert(c.getPlayer(), c.getPlayer().getName() + " tried to packet edit with duey.");
FilePrinter.printError(FilePrinter.EXPLOITS + c.getPlayer().getName() + ".txt", c.getPlayer().getName() + " tried to use duey with mesos " + sendMesos + " and amount " + amount);
c.disconnect(true, false);
return;
}
Pair<Integer, Integer> accIdCid;
if (c.getPlayer().getMeso() >= finalcost) {
accIdCid = getAccountCharacterIdFromCNAME(recipient);
int recipientAccId = accIdCid.getLeft();
if (recipientAccId != -1) {
if (recipientAccId == c.getAccID()) {
c.announce(MaplePacketCreator.sendDueyMSG(DueyProcessor.Actions.TOCLIENT_SEND_SAMEACC_ERROR.getCode()));
return;
}
} else {
c.announce(MaplePacketCreator.sendDueyMSG(DueyProcessor.Actions.TOCLIENT_SEND_NAME_DOES_NOT_EXIST.getCode()));
return;
}
} else {
c.announce(MaplePacketCreator.sendDueyMSG(DueyProcessor.Actions.TOCLIENT_SEND_NOT_ENOUGH_MESOS.getCode()));
return;
}
int recipientCid = accIdCid.getRight();
if (recipientCid == -1) {
c.announce(MaplePacketCreator.sendDueyMSG(DueyProcessor.Actions.TOCLIENT_SEND_NAME_DOES_NOT_EXIST.getCode()));
return;
}
if (quick) {
MapleInventoryManipulator.removeById(c, MapleInventoryType.CASH, 5330000, (short) 1, false, false);
}
int packageId = createPackage(sendMesos, sendMessage, c.getPlayer().getName(), recipientCid, quick);
if (packageId == -1) {
c.announce(MaplePacketCreator.sendDueyMSG(DueyProcessor.Actions.TOCLIENT_SEND_ENABLE_ACTIONS.getCode()));
return;
}
c.getPlayer().gainMeso((int) -finalcost, false);
int res = addPackageItemFromInventory(packageId, c, invTypeId, itemPos, amount);
if (res == 0) {
c.announce(MaplePacketCreator.sendDueyMSG(DueyProcessor.Actions.TOCLIENT_SEND_SUCCESSFULLY_SENT.getCode()));
} else if (res > 0) {
c.announce(MaplePacketCreator.sendDueyMSG(DueyProcessor.Actions.TOCLIENT_SEND_ENABLE_ACTIONS.getCode()));
} else {
c.announce(MaplePacketCreator.sendDueyMSG(DueyProcessor.Actions.TOCLIENT_SEND_INCORRECT_REQUEST.getCode()));
}
MapleClient rClient = null;
int channel = c.getWorldServer().find(recipient);
if (channel > -1) {
Channel rcserv = c.getWorldServer().getChannel(channel);
if(rcserv != null) {
MapleCharacter rChr = rcserv.getPlayerStorage().getCharacterByName(recipient);
if(rChr != null) {
rClient = rChr.getClient();
}
}
}
if (rClient != null && rClient.isLoggedIn() && !rClient.getPlayer().isAwayFromWorld()) {
showDueyNotification(rClient, rClient.getPlayer());
}
} finally {
c.releaseClient();
}
}
}
public static void dueyRemovePackage(MapleClient c, int packageid, boolean playerRemove) {
if (c.tryacquireClient()) {
try {
removePackageFromDB(packageid);
c.announce(MaplePacketCreator.removeItemFromDuey(playerRemove, packageid));
} finally {
c.releaseClient();
}
}
}
public static void dueyClaimPackage(MapleClient c, int packageId) {
if (c.tryacquireClient()) {
try {
try {
DueyPackage dp = null;
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM dueypackages dp WHERE PackageId = ?")) {
ps.setInt(1, packageId);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
dp = getPackageFromDB(rs);
}
}
}
con.close();
if (dp == null) {
c.announce(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_RECV_UNKNOWN_ERROR.getCode()));
FilePrinter.printError(FilePrinter.EXPLOITS + c.getPlayer().getName() + ".txt", c.getPlayer().getName() + " tried to receive package from duey with id " + packageId);
return;
}
if (dp.isDeliveringTime()) {
c.announce(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_RECV_UNKNOWN_ERROR.getCode()));
return;
}
Item dpItem = dp.getItem();
if (dpItem != null) {
if (!c.getPlayer().canHoldMeso(dp.getMesos())) {
c.announce(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_RECV_UNKNOWN_ERROR.getCode()));
return;
}
if (!MapleInventoryManipulator.checkSpace(c, dpItem.getItemId(), dpItem.getQuantity(), dpItem.getOwner())) {
int itemid = dpItem.getItemId();
if(MapleItemInformationProvider.getInstance().isPickupRestricted(itemid) && c.getPlayer().getInventory(ItemConstants.getInventoryType(itemid)).findById(itemid) != null) {
c.announce(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_RECV_RECEIVER_WITH_UNIQUE.getCode()));
} else {
c.announce(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_RECV_NO_FREE_SLOTS.getCode()));
}
return;
} else {
MapleInventoryManipulator.addFromDrop(c, dpItem, false);
}
}
c.getPlayer().gainMeso(dp.getMesos(), false);
dueyRemovePackage(c, packageId, false);
} catch (SQLException e) {
e.printStackTrace();
}
} finally {
c.releaseClient();
}
}
}
public static void dueySendTalk(MapleClient c, boolean quickDelivery) {
if (c.tryacquireClient()) {
try {
long timeNow = System.currentTimeMillis();
if(timeNow - c.getPlayer().getNpcCooldown() < YamlConfig.config.server.BLOCK_NPC_RACE_CONDT) {
c.announce(MaplePacketCreator.enableActions());
return;
}
c.getPlayer().setNpcCooldown(timeNow);
if (quickDelivery) {
c.announce(MaplePacketCreator.sendDuey(0x1A, null));
} else {
c.announce(MaplePacketCreator.sendDuey(0x8, loadPackages(c.getPlayer())));
}
} finally {
c.releaseClient();
}
}
}
public static void dueyCreatePackage(Item item, int mesos, String sender, int recipientCid) {
int packageId = createPackage(mesos, null, sender, recipientCid, false);
if (packageId != -1) {
insertPackageItem(packageId, item);
}
}
public static void runDueyExpireSchedule() {
try {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -30);
Timestamp ts = new Timestamp(c.getTime().getTime());
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT `PackageId` FROM dueypackages WHERE `TimeStamp` < ?");
ps.setTimestamp(1, ts);
List<Integer> toRemove = new LinkedList<>();
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
toRemove.add(rs.getInt("PackageId"));
}
}
ps.close();
for (Integer pid : toRemove) {
removePackageFromDB(pid);
}
ps = con.prepareStatement("DELETE FROM dueypackages WHERE `TimeStamp` < ?");
ps.setTimestamp(1, ts);
ps.executeUpdate();
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,327 @@
/*
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>
Copyleft (L) 2016 - 2019 RonanLana (HeavenMS)
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 client.processor.npc;
import client.MapleCharacter;
import client.MapleClient;
import client.inventory.Item;
import client.inventory.ItemFactory;
import client.inventory.MapleInventory;
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 java.util.Collections;
import net.server.Server;
import net.server.world.World;
import server.MapleItemInformationProvider;
import server.maps.MapleHiredMerchant;
import tools.DatabaseConnection;
import tools.FilePrinter;
import tools.MaplePacketCreator;
import tools.Pair;
/**
*
* @author RonanLana - synchronization of Fredrick modules and operation results
*/
public class FredrickProcessor {
private static int[] dailyReminders = new int[]{2, 5, 10, 15, 30, 60, 90, Integer.MAX_VALUE};
private static byte canRetrieveFromFredrick(MapleCharacter chr, List<Pair<Item, MapleInventoryType>> items) {
if (!MapleInventory.checkSpotsAndOwnership(chr, items)) {
List<Integer> itemids = new LinkedList<>();
for (Pair<Item, MapleInventoryType> it : items) {
itemids.add(it.getLeft().getItemId());
}
if (chr.canHoldUniques(itemids)) {
return 0x22;
} else {
return 0x20;
}
}
int netMeso = chr.getMerchantNetMeso();
if (netMeso > 0) {
if (!chr.canHoldMeso(netMeso)) {
return 0x1F;
}
} else {
if (chr.getMeso() < -1 * netMeso) {
return 0x21;
}
}
return 0x0;
}
public static int timestampElapsedDays(Timestamp then, long timeNow) {
return (int) ((timeNow - then.getTime()) / (1000 * 60 * 60 * 24));
}
private static String fredrickReminderMessage(int daynotes) {
String msg;
if (daynotes < 4) {
msg = "Hi customer! I am Fredrick, the Union Chief of the Hired Merchant Union. A reminder that " + dailyReminders[daynotes] + " days have passed since you used our service. Please reclaim your stored goods at FM Entrance.";
} else {
msg = "Hi customer! I am Fredrick, the Union Chief of the Hired Merchant Union. " + dailyReminders[daynotes] + " days have passed since you used our service. Consider claiming back the items before we move them away for refund.";
}
return msg;
}
public static void removeFredrickLog(int cid) {
try {
Connection con = DatabaseConnection.getConnection();
removeFredrickLog(con, cid);
con.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
private static void removeFredrickLog(Connection con, int cid) throws SQLException {
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `fredstorage` WHERE `cid` = ?")) {
ps.setInt(1, cid);
ps.execute();
}
}
public static void insertFredrickLog(int cid) {
try {
Connection con = DatabaseConnection.getConnection();
removeFredrickLog(con, cid);
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `fredstorage` (`cid`, `daynotes`, `timestamp`) VALUES (?, 0, ?)")) {
ps.setInt(1, cid);
ps.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
ps.execute();
}
con.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
public static void removeFredrickReminders(int cid) {
removeFredrickReminders(Collections.singletonList(new Pair<>(cid, 0)));
}
private static void removeFredrickReminders(List<Pair<Integer, Integer>> expiredCids) {
List<String> expiredCnames = new LinkedList<>();
for (Pair<Integer, Integer> id : expiredCids) {
String name = MapleCharacter.getNameById(id.getLeft());
if (name != null) {
expiredCnames.add(name);
}
}
try {
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `notes` WHERE `from` LIKE ? AND `to` LIKE ?")) {
ps.setString(1, "FREDRICK");
for (String cname : expiredCnames) {
ps.setString(2, cname);
ps.executeBatch();
}
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void runFredrickSchedule() {
try {
Connection con = DatabaseConnection.getConnection();
List<Pair<Integer, Integer>> expiredCids = 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 (ResultSet rs = ps.executeQuery()) {
long curTime = System.currentTimeMillis();
while (rs.next()) {
int cid = rs.getInt("cid");
int world = rs.getInt("world");
Timestamp ts = rs.getTimestamp("timestamp");
int daynotes = Math.min(dailyReminders.length - 1, rs.getInt("daynotes"));
int elapsedDays = timestampElapsedDays(ts, curTime);
if (elapsedDays > 100) {
expiredCids.add(new Pair<>(cid, world));
} else {
int notifDay = dailyReminders[daynotes];
if (elapsedDays >= notifDay) {
do {
daynotes++;
notifDay = dailyReminders[daynotes];
} while (elapsedDays >= notifDay);
Timestamp logoutTs = rs.getTimestamp("lastLogoutTime");
int inactivityDays = timestampElapsedDays(logoutTs, curTime);
if (inactivityDays < 7 || daynotes >= dailyReminders.length - 1) { // don't spam inactive players
String name = rs.getString("name");
notifCids.add(new Pair<>(new Pair<>(cid, name), daynotes));
}
}
}
}
}
}
if (!expiredCids.isEmpty()) {
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `inventoryitems` WHERE `type` = ? AND `characterid` = ?")) {
ps.setInt(1, ItemFactory.MERCHANT.getValue());
for (Pair<Integer, Integer> cid : expiredCids) {
ps.setInt(2, cid.getLeft());
ps.addBatch();
}
ps.executeBatch();
}
try (PreparedStatement ps = con.prepareStatement("UPDATE `characters` SET `MerchantMesos` = 0 WHERE `id` = ?")) {
for (Pair<Integer, Integer> cid : expiredCids) {
ps.setInt(1, cid.getLeft());
ps.addBatch();
World wserv = Server.getInstance().getWorld(cid.getRight());
if (wserv != null) {
MapleCharacter chr = wserv.getPlayerStorage().getCharacterById(cid.getLeft());
if (chr != null) {
chr.setMerchantMeso(0);
}
}
}
ps.executeBatch();
}
removeFredrickReminders(expiredCids);
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `fredstorage` WHERE `cid` = ?")) {
for (Pair<Integer, Integer> cid : expiredCids) {
ps.setInt(1, cid.getLeft());
ps.addBatch();
}
ps.executeBatch();
}
}
if (!notifCids.isEmpty()) {
try (PreparedStatement ps = con.prepareStatement("UPDATE `fredstorage` SET `daynotes` = ? WHERE `cid` = ?")) {
for (Pair<Pair<Integer, String>, Integer> cid : notifCids) {
ps.setInt(1, cid.getRight());
ps.setInt(2, cid.getLeft().getLeft());
ps.addBatch();
String msg = fredrickReminderMessage(cid.getRight() - 1);
MapleCharacter.sendNote(cid.getLeft().getRight(), "FREDRICK", msg, (byte) 0);
}
ps.executeBatch();
}
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static boolean deleteFredrickItems(int cid) {
try {
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `inventoryitems` WHERE `type` = ? AND `characterid` = ?")) {
ps.setInt(1, ItemFactory.MERCHANT.getValue());
ps.setInt(2, cid);
ps.execute();
}
con.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static void fredrickRetrieveItems(MapleClient c) { // thanks Gustav for pointing out the dupe on Fredrick handling
if (c.tryacquireClient()) {
try {
MapleCharacter chr = c.getPlayer();
List<Pair<Item, MapleInventoryType>> items;
try {
items = ItemFactory.MERCHANT.loadItems(chr.getId(), false);
byte response = canRetrieveFromFredrick(chr, items);
if (response != 0) {
chr.announce(MaplePacketCreator.fredrickMessage(response));
return;
}
chr.withdrawMerchantMesos();
if (deleteFredrickItems(chr.getId())) {
MapleHiredMerchant merchant = chr.getHiredMerchant();
if(merchant != null)
merchant.clearItems();
for (Pair<Item, MapleInventoryType> it : items) {
Item item = it.getLeft();
MapleInventoryManipulator.addFromDrop(chr.getClient(), item, false);
String itemName = MapleItemInformationProvider.getInstance().getName(item.getItemId());
FilePrinter.print(FilePrinter.FREDRICK + chr.getName() + ".txt", chr.getName() + " gained " + item.getQuantity() + " " + itemName + " (" + item.getItemId() + ")");
}
chr.announce(MaplePacketCreator.fredrickMessage((byte) 0x1E));
removeFredrickLog(chr.getId());
} else {
chr.message("An unknown error has occured.");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} finally {
c.releaseClient();
}
}
}
}

View File

@@ -0,0 +1,207 @@
/*
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 client.processor.npc;
import client.MapleClient;
import client.MapleCharacter;
import client.autoban.AutobanFactory;
import client.inventory.Item;
import client.inventory.MapleInventory;
import client.inventory.MapleInventoryType;
import client.inventory.manipulator.MapleKarmaManipulator;
import config.YamlConfig;
import constants.inventory.ItemConstants;
import client.inventory.manipulator.MapleInventoryManipulator;
import server.MapleItemInformationProvider;
import server.MapleStorage;
import tools.FilePrinter;
import tools.MaplePacketCreator;
import tools.data.input.SeekableLittleEndianAccessor;
/**
*
* @author Matze
* @author Ronan - inventory concurrency protection on storing items
*/
public class StorageProcessor {
public static void storageAction(SeekableLittleEndianAccessor slea, MapleClient c) {
MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
MapleCharacter chr = c.getPlayer();
MapleStorage storage = chr.getStorage();
byte mode = slea.readByte();
if (chr.getLevel() < 15){
chr.dropMessage(1, "You may only use the storage once you have reached level 15.");
c.announce(MaplePacketCreator.enableActions());
return;
}
if (c.tryacquireClient()) {
try {
if (mode == 4) { // take out
byte type = slea.readByte();
byte slot = slea.readByte();
if (slot < 0 || slot > storage.getSlots()) { // removal starts at zero
AutobanFactory.PACKET_EDIT.alert(c.getPlayer(), c.getPlayer().getName() + " tried to packet edit with storage.");
FilePrinter.print(FilePrinter.EXPLOITS + c.getPlayer().getName() + ".txt", c.getPlayer().getName() + " tried to work with storage slot " + slot);
c.disconnect(true, false);
return;
}
slot = storage.getSlot(MapleInventoryType.getByType(type), slot);
Item item = storage.getItem(slot);
if (item != null) {
if (ii.isPickupRestricted(item.getItemId()) && chr.haveItemWithId(item.getItemId(), true)) {
c.announce(MaplePacketCreator.getStorageError((byte) 0x0C));
return;
}
int takeoutFee = storage.getTakeOutFee();
if (chr.getMeso() < takeoutFee) {
c.announce(MaplePacketCreator.getStorageError((byte) 0x0B));
return;
} else {
chr.gainMeso(-takeoutFee, false);
}
if (MapleInventoryManipulator.checkSpace(c, item.getItemId(), item.getQuantity(), item.getOwner())) {
if (storage.takeOut(item)) {
chr.setUsedStorage();
MapleKarmaManipulator.toggleKarmaFlagToUntradeable(item);
MapleInventoryManipulator.addFromDrop(c, item, false);
String itemName = ii.getName(item.getItemId());
FilePrinter.print(FilePrinter.STORAGE + c.getAccountName() + ".txt", c.getPlayer().getName() + " took out " + item.getQuantity() + " " + itemName + " (" + item.getItemId() + ")");
storage.sendTakenOut(c, item.getInventoryType());
} else {
c.announce(MaplePacketCreator.enableActions());
return;
}
} else {
c.announce(MaplePacketCreator.getStorageError((byte) 0x0A));
}
}
} else if (mode == 5) { // store
short slot = slea.readShort();
int itemId = slea.readInt();
short quantity = slea.readShort();
MapleInventoryType invType = ItemConstants.getInventoryType(itemId);
MapleInventory inv = chr.getInventory(invType);
if (slot < 1 || slot > inv.getSlotLimit()) { //player inv starts at one
AutobanFactory.PACKET_EDIT.alert(c.getPlayer(), c.getPlayer().getName() + " tried to packet edit with storage.");
FilePrinter.print(FilePrinter.EXPLOITS + c.getPlayer().getName() + ".txt", c.getPlayer().getName() + " tried to store item at slot " + slot);
c.disconnect(true, false);
return;
}
if (quantity < 1) {
c.announce(MaplePacketCreator.enableActions());
return;
}
if (storage.isFull()) {
c.announce(MaplePacketCreator.getStorageError((byte) 0x11));
return;
}
int storeFee = storage.getStoreFee();
if (chr.getMeso() < storeFee) {
c.announce(MaplePacketCreator.getStorageError((byte) 0x0B));
} else {
Item item;
inv.lockInventory(); // thanks imbee for pointing a dupe within storage
try {
item = inv.getItem(slot);
if (item != null && item.getItemId() == itemId && (item.getQuantity() >= quantity || ItemConstants.isRechargeable(itemId))) {
if (ItemConstants.isWeddingRing(itemId) || ItemConstants.isWeddingToken(itemId)) {
c.announce(MaplePacketCreator.enableActions());
return;
}
if (ItemConstants.isRechargeable(itemId)) {
quantity = item.getQuantity();
}
MapleInventoryManipulator.removeFromSlot(c, invType, slot, quantity, false);
} else {
c.announce(MaplePacketCreator.enableActions());
return;
}
item = item.copy(); // thanks Robin Schulz & BHB88 for noticing a inventory glitch when storing items
} finally {
inv.unlockInventory();
}
chr.gainMeso(-storeFee, false, true, false);
MapleKarmaManipulator.toggleKarmaFlagToUntradeable(item);
item.setQuantity(quantity);
storage.store(item); // inside a critical section, "!(storage.isFull())" is still in effect...
chr.setUsedStorage();
String itemName = ii.getName(item.getItemId());
FilePrinter.print(FilePrinter.STORAGE + c.getAccountName() + ".txt", c.getPlayer().getName() + " stored " + item.getQuantity() + " " + itemName + " (" + item.getItemId() + ")");
storage.sendStored(c, ItemConstants.getInventoryType(itemId));
}
} else if (mode == 6) { // arrange items
if(YamlConfig.config.server.USE_STORAGE_ITEM_SORT) storage.arrangeItems(c);
c.announce(MaplePacketCreator.enableActions());
} else if (mode == 7) { // meso
int meso = slea.readInt();
int storageMesos = storage.getMeso();
int playerMesos = chr.getMeso();
if ((meso > 0 && storageMesos >= meso) || (meso < 0 && playerMesos >= -meso)) {
if (meso < 0 && (storageMesos - meso) < 0) {
meso = Integer.MIN_VALUE + storageMesos;
if (meso < playerMesos) {
c.announce(MaplePacketCreator.enableActions());
return;
}
} else if (meso > 0 && (playerMesos + meso) < 0) {
meso = Integer.MAX_VALUE - playerMesos;
if (meso > storageMesos) {
c.announce(MaplePacketCreator.enableActions());
return;
}
}
storage.setMeso(storageMesos - meso);
chr.gainMeso(meso, false, true, false);
chr.setUsedStorage();
FilePrinter.print(FilePrinter.STORAGE + c.getPlayer().getName() + ".txt", c.getPlayer().getName() + (meso > 0 ? " took out " : " stored ") + Math.abs(meso) + " mesos");
storage.sendMeso(c);
} else {
c.announce(MaplePacketCreator.enableActions());
return;
}
} else if (mode == 8) {// close... unless the player decides to enter cash shop!
storage.close();
}
} finally {
c.releaseClient();
}
}
}
}