MTSHandler.java
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:45k
源码类别:

游戏

开发平台:

Java

  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3. Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
  4. Matthias Butz <matze@odinms.de>
  5. Jan Christian Meyer <vimes@odinms.de>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License version 3
  8. as published by the Free Software Foundation. You may not use, modify
  9. or distribute this program under any other version of the
  10. GNU Affero General Public License.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18. package net.sf.odinms.net.channel.handler;
  19. import net.sf.odinms.client.*;
  20. import net.sf.odinms.database.DatabaseConnection;
  21. import net.sf.odinms.net.AbstractMaplePacketHandler;
  22. import net.sf.odinms.net.MaplePacket;
  23. import net.sf.odinms.net.channel.ChannelServer;
  24. import net.sf.odinms.server.MapleInventoryManipulator;
  25. import net.sf.odinms.server.MapleItemInformationProvider;
  26. import net.sf.odinms.server.MTSItemInfo;
  27. import net.sf.odinms.tools.MaplePacketCreator;
  28. import net.sf.odinms.tools.Pair;
  29. import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;
  30. import java.sql.Connection;
  31. import java.sql.PreparedStatement;
  32. import java.sql.ResultSet;
  33. import java.sql.SQLException;
  34. import java.util.*;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. public class MTSHandler extends AbstractMaplePacketHandler {
  38.     private static Logger log = LoggerFactory.getLogger(MTSHandler.class);
  39.     @Override
  40.     public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
  41.         if (!c.getPlayer().inMTS()) {
  42.             return; //hax
  43.         }
  44.         if (slea.available() > 0) {
  45.             byte op = slea.readByte();
  46.             if (op == 2) { //put item up for sale
  47.                 byte itemtype = slea.readByte();
  48.                 int itemid = slea.readInt();
  49.                 slea.readShort();
  50.                 slea.skip(7);
  51.                 short stars = 1;
  52.                 if (itemtype == 1) {
  53.                     slea.skip(32); //item stats, but we don't need them (can be exploited!)
  54.                 } else {
  55.                     stars = slea.readShort();
  56.                 }
  57.                 slea.readMapleAsciiString(); //another useless thing (owner)
  58.                 if (itemtype == 1) {
  59.                     slea.readInt();
  60.                 } else {
  61.                     slea.readShort();
  62.                 }
  63.                 byte slot = 1;
  64.                 short quantity = 1;
  65.                 if (itemtype != 1) {
  66.                     if (itemid / 10000 == 207 || itemid / 10000 == 233) {
  67.                         slea.skip(8);
  68.                     }
  69.                     slot = (byte) slea.readInt();
  70.                 } else {
  71.                     quantity = (short) slea.readInt();
  72.                 }
  73.                 if (itemtype == 1) {
  74.                     slea.readShort();
  75.                 }
  76.                 if (itemtype != 1) {
  77.                     if (itemid / 10000 == 207 || itemid / 10000 == 233) {
  78.                         quantity = stars;
  79.                         slea.skip(4);
  80.                     } else {
  81.                         quantity = (short) slea.readInt();
  82.                     }
  83.                 } else {
  84.                     slot = (byte) slea.readInt();
  85.                 }
  86.                 if (itemtype == 1) {
  87.                     slea.readInt();
  88.                 }
  89.                 int price = slea.readInt();
  90.                 if (itemtype == 1) {
  91.                     quantity = 1;
  92.                 }
  93.                 MapleInventoryType type = MapleItemInformationProvider.getInstance().getInventoryType(itemid);
  94.                 IItem i = c.getPlayer().getInventory(type).getItem(slot).copy();
  95.                 if (i != null && c.getPlayer().getMeso() >= 1337) {
  96.                     Connection con = DatabaseConnection.getConnection();
  97.                     try {
  98.                         PreparedStatement ps;
  99.                         Calendar calendar = Calendar.getInstance();
  100.                         int year;
  101.                         int month;
  102.                         int day;
  103.                         int oldmax = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  104.                         int oldday = calendar.get(Calendar.DAY_OF_MONTH) + 7;
  105.                         if (oldmax < oldday) {
  106.                             if (calendar.get(Calendar.MONTH) + 2 > 12) {
  107.                                 year = calendar.get(Calendar.YEAR) + 1;
  108.                                 month = 1;
  109.                                 calendar.set(year, month, 1);
  110.                                 day = oldday - oldmax;
  111.                             } else {
  112.                                 month = calendar.get(Calendar.MONTH) + 2;
  113.                                 year = calendar.get(Calendar.YEAR);
  114.                                 calendar.set(year, month, 1);
  115.                                 day = oldday - oldmax;
  116.                             }
  117.                         } else {
  118.                             day = calendar.get(Calendar.DAY_OF_MONTH) + 7;
  119.                             month = calendar.get(Calendar.MONTH);
  120.                             year = calendar.get(Calendar.YEAR);
  121.                         }
  122.                         String date = year + "-";
  123.                         if (month < 10) {
  124.                             date += "0" + month + "-";
  125.                         } else {
  126.                             date += month + "-";
  127.                         }
  128.                         if (day < 10) {
  129.                             date += "0" + day;
  130.                         } else {
  131.                             date += day + "";
  132.                         }
  133.                         ps = con.prepareStatement("SELECT count(*) FROM mts_items WHERE seller = " + c.getPlayer().getId());
  134.                         ResultSet rs = ps.executeQuery();
  135.                         rs.next();
  136.                         if (rs.getInt(1) >= 10) {
  137.                             c.getPlayer().dropMessage(1, "You cannot sell over 10 items");
  138.                             c.getSession().write(MaplePacketCreator.enableActions());
  139.                             rs.close();
  140.                             ps.close();
  141.                             return;
  142.                         }
  143.                         if (i.getType() == 2) {
  144.                             Item item = (Item) i;
  145.                             ps = con.prepareStatement("INSERT INTO mts_items (tab, type, itemid, quantity, seller, price, owner, sellername, sell_ends) VALUES " +
  146.                                     "(?, ?, ?, ?, ?, ?, ?, ?, ?)");
  147.                             ps.setInt(1, 1);
  148.                             ps.setInt(2, (int) type.getType());
  149.                             ps.setInt(3, item.getItemId());
  150.                             ps.setInt(4, quantity);
  151.                             ps.setInt(5, c.getPlayer().getId());
  152.                             ps.setInt(6, price);
  153.                             ps.setString(7, item.getOwner());
  154.                             ps.setString(8, c.getPlayer().getName());
  155.                             ps.setString(9, date);
  156.                         } else {
  157.                             Equip equip = (Equip) i;
  158.                             ps = con.prepareStatement("INSERT INTO mts_items (tab, type, itemid, quantity, seller, price, upgradeslots, level, str, dex, `int`, luk, hp, mp, watk, matk, wdef, mdef, acc, avoid, hands, speed, jump, locked, owner, sellername, sell_ends) VALUES " +
  159.                                     "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  160.                             ps.setInt(1, 1);
  161.                             ps.setInt(2, (int) type.getType());
  162.                             ps.setInt(3, equip.getItemId());
  163.                             ps.setInt(4, quantity);
  164.                             ps.setInt(5, c.getPlayer().getId());
  165.                             ps.setInt(6, price);
  166.                             ps.setInt(7, equip.getUpgradeSlots());
  167.                             ps.setInt(8, equip.getLevel());
  168.                             ps.setInt(9, equip.getStr());
  169.                             ps.setInt(10, equip.getDex());
  170.                             ps.setInt(11, equip.getInt());
  171.                             ps.setInt(12, equip.getLuk());
  172.                             ps.setInt(13, equip.getHp());
  173.                             ps.setInt(14, equip.getMp());
  174.                             ps.setInt(15, equip.getWatk());
  175.                             ps.setInt(16, equip.getMatk());
  176.                             ps.setInt(17, equip.getWdef());
  177.                             ps.setInt(18, equip.getMdef());
  178.                             ps.setInt(19, equip.getAcc());
  179.                             ps.setInt(20, equip.getAvoid());
  180.                             ps.setInt(21, equip.getHands());
  181.                             ps.setInt(22, equip.getSpeed());
  182.                             ps.setInt(23, equip.getJump());
  183.                             ps.setInt(24, equip.getLocked());
  184.                             ps.setString(25, equip.getOwner());
  185.                             ps.setString(26, c.getPlayer().getName());
  186.                             ps.setString(27, date);
  187.                         }
  188.                         ps.executeUpdate();
  189.                         ps.close();
  190.                         MapleInventoryManipulator.removeFromSlot(c, type, slot, quantity, false);
  191.                     } catch (SQLException e) {
  192.                         log.error("SQLErr4: " + e);
  193.                     }
  194.                     c.getPlayer().gainMeso(-1337, false);
  195.                     c.getSession().write(MaplePacketCreator.MTSConfirmSell());
  196.                     c.getSession().write(getMTS(1, 0, 0));
  197.                     c.getSession().write(MaplePacketCreator.enableMTS());
  198.                     c.getSession().write(MaplePacketCreator.TransferInventory(getTransfer(c.getPlayer().getId())));
  199.                     c.getSession().write(MaplePacketCreator.NotYetSoldInv(getNotYetSold(c.getPlayer().getId())));
  200.                 }
  201.             } else if (op == 3) { //send offer for wanted item
  202.             } else if (op == 4) { //list wanted item
  203.                 int itemid = slea.readInt();
  204.                 int price = slea.readInt();
  205.                 int quantity = slea.readInt();
  206.                 slea.readShort();
  207.                 String message = slea.readMapleAsciiString();
  208.             } else if (op == 5) { //change page/tab
  209.                 //tabs
  210.                 //1 = for sale
  211.                 //2 = wanted
  212.                 //3 = auction
  213.                 //4 = my page
  214.                 //5 = guide
  215.                 //types (wanted & auction)
  216.                 //0 = all
  217.                 //1 = equip
  218.                 //2 = use
  219.                 //3 = setup
  220.                 //4 = etc
  221.                 //types (my page)
  222.                 //0 = cart
  223.                 //1 = offers
  224.                 //2 = history
  225.                 //3 = auction
  226.                 int tab = slea.readInt();
  227.                 int type = slea.readInt();
  228.                 int page = slea.readInt();
  229.                 c.getPlayer().changeTab(tab);
  230.                 c.getPlayer().changeType(type);
  231.                 c.getPlayer().changePage(page);
  232.                 if (tab == 4 && type == 0) {
  233.                     c.getSession().write(getCart(c.getPlayer().getId()));
  234.                 } else {
  235.                     c.getSession().write(getMTS(tab, type, page));
  236.                 }
  237.                 c.getSession().write(MaplePacketCreator.enableMTS());
  238.                 c.getSession().write(MaplePacketCreator.TransferInventory(getTransfer(c.getPlayer().getId())));
  239.                 c.getSession().write(MaplePacketCreator.NotYetSoldInv(getNotYetSold(c.getPlayer().getId())));
  240.             } else if (op == 6) { //search
  241.                 int tab = slea.readInt();
  242.                 int type = slea.readInt();
  243.                 slea.readInt();
  244.                 int ci = slea.readInt();
  245.                 String search = slea.readMapleAsciiString();
  246.                 c.getSession().write(MaplePacketCreator.enableMTS());
  247.                 c.getSession().write(MaplePacketCreator.enableActions());
  248.                 c.getSession().write(getMTSSearch(tab, type, ci, search, c.getPlayer().getCurrentPage()));
  249.                 c.getSession().write(MaplePacketCreator.showMTSCash(c.getPlayer()));
  250.                 c.getSession().write(MaplePacketCreator.TransferInventory(getTransfer(c.getPlayer().getId())));
  251.                 c.getSession().write(MaplePacketCreator.NotYetSoldInv(getNotYetSold(c.getPlayer().getId())));
  252.             } else if (op == 7) { //cancel sale
  253.                 int id = slea.readInt(); //id of the item
  254.                 Connection con = DatabaseConnection.getConnection();
  255.                 try {
  256.                     PreparedStatement ps = con.prepareStatement("UPDATE mts_items SET transfer = 1 WHERE id = ? AND seller = ?");
  257.                     ps.setInt(1, id);
  258.                     ps.setInt(2, c.getPlayer().getId());
  259.                     ps.executeUpdate();
  260.                     ps.close();
  261.                     ps = con.prepareStatement("DELETE FROM mts_cart WHERE itemid = ?");
  262.                     ps.setInt(1, id);
  263.                     ps.executeUpdate();
  264.                     ps.close();
  265.                 } catch (SQLException e) {
  266.                     log.error("SQLErr4: " + e);
  267.                 }
  268.                 c.getSession().write(MaplePacketCreator.enableMTS());
  269.                 c.getSession().write(getMTS(c.getPlayer().getCurrentTab(), c.getPlayer().getCurrentType(), c.getPlayer().getCurrentPage()));
  270.                 c.getSession().write(MaplePacketCreator.NotYetSoldInv(getNotYetSold(c.getPlayer().getId())));
  271.                 c.getSession().write(MaplePacketCreator.TransferInventory(getTransfer(c.getPlayer().getId())));
  272.             } else if (op == 8) { //transfer item from transfer inv.
  273.                 int id = slea.readInt(); //id of the item
  274.                 Connection con = DatabaseConnection.getConnection();
  275.                 PreparedStatement ps;
  276.                 ResultSet rs;
  277.                 try {
  278.                     ps = con.prepareStatement("SELECT * FROM mts_items WHERE seller = ? AND transfer = 1  AND id= ? ORDER BY id DESC");
  279.                     ps.setInt(1, c.getPlayer().getId());
  280.                     ps.setInt(2, id);
  281.                     rs = ps.executeQuery();
  282.                     if (rs.next()) {
  283.                         IItem i;
  284.                         if (rs.getInt("type") != 1) {
  285.                             Item ii = new Item(rs.getInt("itemid"), (byte) 0, (short) rs.getInt("quantity"));
  286.                             ii.setOwner(rs.getString("owner"));
  287.                             ii.setPosition(c.getPlayer().getInventory(MapleItemInformationProvider.getInstance().getInventoryType(rs.getInt("itemid"))).getNextFreeSlot());
  288.                             i = ii.copy();
  289.                         } else {
  290.                             Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
  291.                             equip.setOwner(rs.getString("owner"));
  292.                             equip.setQuantity((short) 1);
  293.                             equip.setAcc((short) rs.getInt("acc"));
  294.                             equip.setAvoid((short) rs.getInt("avoid"));
  295.                             equip.setDex((short) rs.getInt("dex"));
  296.                             equip.setHands((short) rs.getInt("hands"));
  297.                             equip.setHp((short) rs.getInt("hp"));
  298.                             equip.setInt((short) rs.getInt("int"));
  299.                             equip.setJump((short) rs.getInt("jump"));
  300.                             equip.setLuk((short) rs.getInt("luk"));
  301.                             equip.setMatk((short) rs.getInt("matk"));
  302.                             equip.setMdef((short) rs.getInt("mdef"));
  303.                             equip.setMp((short) rs.getInt("mp"));
  304.                             equip.setSpeed((short) rs.getInt("speed"));
  305.                             equip.setStr((short) rs.getInt("str"));
  306.                             equip.setWatk((short) rs.getInt("watk"));
  307.                             equip.setWdef((short) rs.getInt("wdef"));
  308.                             equip.setUpgradeSlots((byte) rs.getInt("upgradeslots"));
  309.                             equip.setLocked((byte) rs.getInt("locked"));
  310.                             equip.setLevel((byte) rs.getInt("level"));
  311.                             equip.setPosition(c.getPlayer().getInventory(MapleItemInformationProvider.getInstance().getInventoryType(rs.getInt("itemid"))).getNextFreeSlot());
  312.                             i = equip.copy();
  313.                         }
  314.                         PreparedStatement pse = con.prepareStatement("DELETE FROM mts_items WHERE id = ? AND seller = ? AND transfer = 1");
  315.                         pse.setInt(1, id);
  316.                         pse.setInt(2, c.getPlayer().getId());
  317.                         pse.executeUpdate();
  318.                         pse.close();
  319.                         MapleInventoryManipulator.addFromDrop(c, i, false);
  320.                         c.getSession().write(MaplePacketCreator.enableMTS());
  321.                         c.getSession().write(getCart(c.getPlayer().getId()));
  322.                         c.getSession().write(getMTS(c.getPlayer().getCurrentTab(), c.getPlayer().getCurrentType(), c.getPlayer().getCurrentPage()));
  323.                         c.getSession().write(MaplePacketCreator.MTSConfirmTransfer(i.getQuantity(), i.getPosition()));
  324.                         c.getSession().write(MaplePacketCreator.TransferInventory(getTransfer(c.getPlayer().getId())));
  325.                         c.getPlayer().saveToDB(true, true);
  326.                     }
  327.                     rs.close();
  328.                     ps.close();
  329.                 } catch (SQLException e) {
  330.                     log.error("Err8: " + e);
  331.                 }
  332.             } else if (op == 9) { //add to cart
  333.                 int id = slea.readInt(); //id of the item
  334.                 Connection con = DatabaseConnection.getConnection();
  335.                 try {
  336.                     PreparedStatement ps1 = con.prepareStatement("SELECT * FROM mts_items WHERE id = ? AND seller <> ?");
  337.                     ps1.setInt(1, id);//Previene que agregues al cart tus propios items
  338.                     ps1.setInt(2, c.getPlayer().getId());
  339.                     ResultSet rs1 = ps1.executeQuery();
  340.                     if (rs1.next()) {
  341.                         PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_cart WHERE cid = ? AND itemid = ?");
  342.                         ps.setInt(1, c.getPlayer().getId());
  343.                         ps.setInt(2, id);
  344.                         ResultSet rs = ps.executeQuery();
  345.                         if (!rs.next()) {
  346.                             PreparedStatement pse = con.prepareStatement("INSERT INTO mts_cart (cid, itemid) VALUES (?, ?)");
  347.                             pse.setInt(1, c.getPlayer().getId());
  348.                             pse.setInt(2, id);
  349.                             pse.executeUpdate();
  350.                         //c.getSession().write(MaplePacketCreator.addToCartMessage(false, false));
  351.                         }// else
  352.                     // c.getSession().write(MaplePacketCreator.addToCartMessage(true, false));
  353.                     }
  354.                 } catch (SQLException e) {
  355.                     log.error("SqlErr12: ", e);
  356.                 }
  357.             /*c.getSession().write(getMTS(c.getPlayer().getCurrentTab(), c.getPlayer().getCurrentType(), c.getPlayer().getCurrentPage()));
  358.             c.getSession().write(MaplePacketCreator.enableMTS());
  359.             c.getSession().write(MaplePacketCreator.enableActions());
  360.             c.getSession().write(MaplePacketCreator.TransferInventory(getTransfer(c.getPlayer().getId())));
  361.             c.getSession().write(MaplePacketCreator.NotYetSoldInv(getNotYetSold(c.getPlayer().getId())));*/
  362.             } else if (op == 10) { //delete from cart
  363.                 int id = slea.readInt(); //id of the item
  364.                 Connection con = DatabaseConnection.getConnection();
  365.                 try {
  366.                     PreparedStatement ps = con.prepareStatement("DELETE FROM mts_cart WHERE itemid = ? AND cid = ?");
  367.                     ps.setInt(1, id);
  368.                     ps.setInt(2, c.getPlayer().getId());
  369.                     ps.executeUpdate();
  370.                 } catch (SQLException e) {
  371.                     log.error("SqlErr12: ", e);
  372.                 }
  373.             // c.getSession().write(MaplePacketCreator.addToCartMessage(false, true));
  374.              /*   c.getSession().write(getCart(c.getPlayer().getId()));
  375.             c.getSession().write(MaplePacketCreator.enableMTS());
  376.             c.getSession().write(MaplePacketCreator.TransferInventory(getTransfer(c.getPlayer().getId())));
  377.             c.getSession().write(MaplePacketCreator.NotYetSoldInv(getNotYetSold(c.getPlayer().getId())));*/
  378.             } else if (op == 12) { //put item up for auction
  379.             } else if (op == 12) { //cancel wanted cart thing
  380.             } else if (op == 14) { //buy auction item now
  381.             } else if (op == 16) { //buy
  382.                 //transaction fees: 100NX + 10% of item price
  383.                 int id = slea.readInt(); //id of the item
  384.                 Connection con = DatabaseConnection.getConnection();
  385.                 PreparedStatement ps;
  386.                 ResultSet rs;
  387.                 try {
  388.                     ps = con.prepareStatement("SELECT * FROM mts_items WHERE id = ? ORDER BY id DESC");
  389.                     ps.setInt(1, id);
  390.                     rs = ps.executeQuery();
  391.                     if (rs.next()) {
  392.                         int price = rs.getInt("price") + 100 + (int) (rs.getInt("price") * 0.1); //taxes
  393.                         if (c.getPlayer().getCSPoints(1) >= price) {
  394.                             boolean alwaysnull = true;
  395.                             for (ChannelServer cserv : ChannelServer.getAllInstances()) {
  396.                                 MapleCharacter victim = cserv.getPlayerStorage().getCharacterById(rs.getInt("seller"));
  397.                                 if (victim != null) {
  398.                                     victim.modifyCSPoints(1, rs.getInt("price"));
  399.                                     alwaysnull = false;
  400.                                 }
  401.                             }
  402.                             if (alwaysnull) {
  403.                                 PreparedStatement pse = con.prepareStatement("SELECT accountid FROM characters WHERE id = ?");
  404.                                 pse.setInt(1, rs.getInt("seller"));
  405.                                 ResultSet rse = pse.executeQuery();
  406.                                 if (rse.next()) {
  407.                                     PreparedStatement psee = con.prepareStatement("UPDATE accounts SET paypalNX = paypalNX + ? WHERE id = ?");
  408.                                     psee.setInt(1, rs.getInt("price"));
  409.                                     psee.setInt(2, rse.getInt("accountid"));
  410.                                     psee.executeUpdate();
  411.                                     psee.close();
  412.                                 }
  413.                                 pse.close();
  414.                                 rse.close();
  415.                             }
  416.                             PreparedStatement pse = con.prepareStatement("UPDATE mts_items SET seller = ?, transfer = 1 WHERE id = ?");
  417.                             pse.setInt(1, c.getPlayer().getId());
  418.                             pse.setInt(2, id);
  419.                             pse.executeUpdate();
  420.                             pse.close();
  421.                             pse = con.prepareStatement("DELETE FROM mts_cart WHERE itemid = ?");
  422.                             pse.setInt(1, id);
  423.                             pse.executeUpdate();
  424.                             pse.close();
  425.                             c.getPlayer().modifyCSPoints(1, -price);
  426.                             c.getSession().write(MaplePacketCreator.enableMTS());
  427.                             c.getSession().write(getMTS(c.getPlayer().getCurrentTab(), c.getPlayer().getCurrentType(), c.getPlayer().getCurrentPage()));
  428.                             c.getSession().write(MaplePacketCreator.MTSConfirmBuy());
  429.                             c.getSession().write(MaplePacketCreator.showMTSCash(c.getPlayer()));
  430.                             c.getSession().write(MaplePacketCreator.TransferInventory(getTransfer(c.getPlayer().getId())));
  431.                             c.getSession().write(MaplePacketCreator.NotYetSoldInv(getNotYetSold(c.getPlayer().getId())));
  432.                             c.getSession().write(MaplePacketCreator.enableActions());
  433.                         } else {
  434.                             c.getSession().write(MaplePacketCreator.MTSFailBuy());
  435.                         }
  436.                     }
  437.                     rs.close();
  438.                     ps.close();
  439.                 } catch (SQLException e) {
  440.                     c.getSession().write(MaplePacketCreator.MTSFailBuy());
  441.                     log.error("Err8: " + e);
  442.                 }
  443.             } else if (op == 17) { //buy from cart
  444.                 int id = slea.readInt(); //id of the item
  445.                 Connection con = DatabaseConnection.getConnection();
  446.                 PreparedStatement ps;
  447.                 ResultSet rs;
  448.                 try {
  449.                     ps = con.prepareStatement("SELECT * FROM mts_items WHERE id = ? ORDER BY id DESC");
  450.                     ps.setInt(1, id);
  451.                     rs = ps.executeQuery();
  452.                     if (rs.next()) {
  453.                         int price = rs.getInt("price") + 100 + (int) (rs.getInt("price") * 0.1);
  454.                         if (c.getPlayer().getCSPoints(1) >= price) {
  455.                             for (ChannelServer cserv : ChannelServer.getAllInstances()) {
  456.                                 MapleCharacter victim = cserv.getPlayerStorage().getCharacterById(rs.getInt("seller"));
  457.                                 if (victim != null) {
  458.                                     victim.modifyCSPoints(1, rs.getInt("price"));
  459.                                 } else {
  460.                                     PreparedStatement pse = con.prepareStatement("SELECT accountid FROM characters WHERE id = ?");
  461.                                     pse.setInt(1, rs.getInt("seller"));
  462.                                     ResultSet rse = pse.executeQuery();
  463.                                     if (rse.next()) {
  464.                                         PreparedStatement psee = con.prepareStatement("UPDATE accounts SET paypalNX = paypalNX + ? WHERE id = ?");
  465.                                         psee.setInt(1, rs.getInt("price"));
  466.                                         psee.setInt(2, rse.getInt("accountid"));
  467.                                         psee.executeUpdate();
  468.                                         psee.close();
  469.                                     }
  470.                                     pse.close();
  471.                                     rse.close();
  472.                                 }
  473.                             }
  474.                             PreparedStatement pse = con.prepareStatement("UPDATE mts_items SET seller = ?, transfer = 1 WHERE id = ?");
  475.                             pse.setInt(1, c.getPlayer().getId());
  476.                             pse.setInt(2, id);
  477.                             pse.executeUpdate();
  478.                             pse.close();
  479.                             pse = con.prepareStatement("DELETE FROM mts_cart WHERE itemid = ?");
  480.                             pse.setInt(1, id);
  481.                             pse.executeUpdate();
  482.                             pse.close();
  483.                             c.getPlayer().modifyCSPoints(1, -price);
  484.                             c.getSession().write(getCart(c.getPlayer().getId()));
  485.                             c.getSession().write(MaplePacketCreator.enableMTS());
  486.                             c.getSession().write(MaplePacketCreator.MTSConfirmBuy());
  487.                             c.getSession().write(MaplePacketCreator.showMTSCash(c.getPlayer()));
  488.                             c.getSession().write(MaplePacketCreator.TransferInventory(getTransfer(c.getPlayer().getId())));
  489.                             c.getSession().write(MaplePacketCreator.NotYetSoldInv(getNotYetSold(c.getPlayer().getId())));
  490.                         } else {
  491.                             c.getSession().write(MaplePacketCreator.MTSFailBuy());
  492.                         }
  493.                     }
  494.                     rs.close();
  495.                     ps.close();
  496.                 } catch (SQLException e) {
  497.                     c.getSession().write(MaplePacketCreator.MTSFailBuy());
  498.                     log.error("Err8: " + e);
  499.                 }
  500.             } else {
  501.                 log.info("Unhandled OP(MTS): " + op + " Packet: " + slea.toString());
  502.             }
  503.         } else {
  504.             c.getSession().write(MaplePacketCreator.showMTSCash(c.getPlayer()));
  505.         }
  506.     }
  507.     public List<MTSItemInfo> getNotYetSold(int cid) {
  508.         List<MTSItemInfo> items = new ArrayList<MTSItemInfo>();
  509.         Connection con = DatabaseConnection.getConnection();
  510.         PreparedStatement ps;
  511.         ResultSet rs;
  512.         try {
  513.             ps = con.prepareStatement("SELECT * FROM mts_items WHERE seller = ? AND transfer = 0 ORDER BY id DESC");
  514.             ps.setInt(1, cid);
  515.             rs = ps.executeQuery();
  516.             while (rs.next()) {
  517.                 if (rs.getInt("type") != 1) {
  518.                     Item i = new Item(rs.getInt("itemid"), (byte) 0, (short) rs.getInt("quantity"));
  519.                     i.setOwner(rs.getString("owner"));
  520.                     items.add(new MTSItemInfo((IItem) i, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
  521.                 } else {
  522.                     Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
  523.                     equip.setOwner(rs.getString("owner"));
  524.                     equip.setQuantity((short) 1);
  525.                     equip.setAcc((short) rs.getInt("acc"));
  526.                     equip.setAvoid((short) rs.getInt("avoid"));
  527.                     equip.setDex((short) rs.getInt("dex"));
  528.                     equip.setHands((short) rs.getInt("hands"));
  529.                     equip.setHp((short) rs.getInt("hp"));
  530.                     equip.setInt((short) rs.getInt("int"));
  531.                     equip.setJump((short) rs.getInt("jump"));
  532.                     equip.setLuk((short) rs.getInt("luk"));
  533.                     equip.setMatk((short) rs.getInt("matk"));
  534.                     equip.setMdef((short) rs.getInt("mdef"));
  535.                     equip.setMp((short) rs.getInt("mp"));
  536.                     equip.setSpeed((short) rs.getInt("speed"));
  537.                     equip.setStr((short) rs.getInt("str"));
  538.                     equip.setWatk((short) rs.getInt("watk"));
  539.                     equip.setWdef((short) rs.getInt("wdef"));
  540.                     equip.setUpgradeSlots((byte) rs.getInt("upgradeslots"));
  541.                     equip.setLocked((byte) rs.getInt("locked"));
  542.                     equip.setLevel((byte) rs.getInt("level"));
  543.                     items.add(new MTSItemInfo((IItem) equip, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
  544.                 }
  545.             }
  546.             rs.close();
  547.             ps.close();
  548.         } catch (SQLException e) {
  549.             log.error("Err8: " + e);
  550.         }
  551.         return items;
  552.     }
  553.     public MaplePacket getCart(int cid) {
  554.         List<MTSItemInfo> items = new ArrayList<MTSItemInfo>();
  555.         Connection con = DatabaseConnection.getConnection();
  556.         PreparedStatement ps;
  557.         ResultSet rs;
  558.         int pages = 0;
  559.         try {
  560.             ps = con.prepareStatement("SELECT * FROM mts_cart WHERE cid = ? ORDER BY id DESC");
  561.             ps.setInt(1, cid);
  562.             rs = ps.executeQuery();
  563.             while (rs.next()) {
  564.                 PreparedStatement pse = con.prepareStatement("SELECT * FROM mts_items WHERE id = ?");
  565.                 pse.setInt(1, rs.getInt("itemid"));
  566.                 ResultSet rse = pse.executeQuery();
  567.                 if (rse.next()) {
  568.                     if (rse.getInt("type") != 1) {
  569.                         Item i = new Item(rse.getInt("itemid"), (byte) 0, (short) rse.getInt("quantity"));
  570.                         i.setOwner(rse.getString("owner"));
  571.                         items.add(new MTSItemInfo((IItem) i, rse.getInt("price"), rse.getInt("id"), rse.getInt("seller"), rse.getString("sellername"), rse.getString("sell_ends")));
  572.                     } else {
  573.                         Equip equip = new Equip(rse.getInt("itemid"), (byte) rse.getInt("position"), -1);
  574.                         equip.setOwner(rse.getString("owner"));
  575.                         equip.setQuantity((short) 1);
  576.                         equip.setAcc((short) rse.getInt("acc"));
  577.                         equip.setAvoid((short) rse.getInt("avoid"));
  578.                         equip.setDex((short) rse.getInt("dex"));
  579.                         equip.setHands((short) rse.getInt("hands"));
  580.                         equip.setHp((short) rse.getInt("hp"));
  581.                         equip.setInt((short) rse.getInt("int"));
  582.                         equip.setJump((short) rse.getInt("jump"));
  583.                         equip.setLuk((short) rse.getInt("luk"));
  584.                         equip.setMatk((short) rse.getInt("matk"));
  585.                         equip.setMdef((short) rse.getInt("mdef"));
  586.                         equip.setMp((short) rse.getInt("mp"));
  587.                         equip.setSpeed((short) rse.getInt("speed"));
  588.                         equip.setStr((short) rse.getInt("str"));
  589.                         equip.setWatk((short) rse.getInt("watk"));
  590.                         equip.setWdef((short) rse.getInt("wdef"));
  591.                         equip.setUpgradeSlots((byte) rse.getInt("upgradeslots"));
  592.                         equip.setLocked((byte) rse.getInt("locked"));
  593.                         equip.setLevel((byte) rse.getInt("level"));
  594.                         items.add(new MTSItemInfo((IItem) equip, rse.getInt("price"), rse.getInt("id"), rse.getInt("seller"), rse.getString("sellername"), rse.getString("sell_ends")));
  595.                     }
  596.                 }
  597.             }
  598.             rs.close();
  599.             ps.close();
  600.             ps = con.prepareStatement("SELECT COUNT(*) FROM mts_cart WHERE cid = ?");
  601.             ps.setInt(1, cid);
  602.             rs = ps.executeQuery();
  603.             if (rs.next()) {
  604.                 pages = rs.getInt(1) / 16;
  605.                 if (rs.getInt(1) % 16 > 0) {
  606.                     pages += 1;
  607.                 }
  608.             }
  609.             rs.close();
  610.             ps.close();
  611.         } catch (SQLException e) {
  612.             log.error("Err8: " + e);
  613.         }
  614.         return MaplePacketCreator.sendMTS(items, 4, 0, 0, pages);
  615.     }
  616.     public List<MTSItemInfo> getTransfer(int cid) {
  617.         List<MTSItemInfo> items = new ArrayList<MTSItemInfo>();
  618.         Connection con = DatabaseConnection.getConnection();
  619.         PreparedStatement ps;
  620.         ResultSet rs;
  621.         try {
  622.             ps = con.prepareStatement("SELECT * FROM mts_items WHERE transfer = 1 AND seller = ? ORDER BY id DESC");
  623.             ps.setInt(1, cid);
  624.             rs = ps.executeQuery();
  625.             while (rs.next()) {
  626.                 if (rs.getInt("type") != 1) {
  627.                     Item i = new Item(rs.getInt("itemid"), (byte) 0, (short) rs.getInt("quantity"));
  628.                     i.setOwner(rs.getString("owner"));
  629.                     items.add(new MTSItemInfo((IItem) i, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
  630.                 } else {
  631.                     Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
  632.                     equip.setOwner(rs.getString("owner"));
  633.                     equip.setQuantity((short) 1);
  634.                     equip.setAcc((short) rs.getInt("acc"));
  635.                     equip.setAvoid((short) rs.getInt("avoid"));
  636.                     equip.setDex((short) rs.getInt("dex"));
  637.                     equip.setHands((short) rs.getInt("hands"));
  638.                     equip.setHp((short) rs.getInt("hp"));
  639.                     equip.setInt((short) rs.getInt("int"));
  640.                     equip.setJump((short) rs.getInt("jump"));
  641.                     equip.setLuk((short) rs.getInt("luk"));
  642.                     equip.setMatk((short) rs.getInt("matk"));
  643.                     equip.setMdef((short) rs.getInt("mdef"));
  644.                     equip.setMp((short) rs.getInt("mp"));
  645.                     equip.setSpeed((short) rs.getInt("speed"));
  646.                     equip.setStr((short) rs.getInt("str"));
  647.                     equip.setWatk((short) rs.getInt("watk"));
  648.                     equip.setWdef((short) rs.getInt("wdef"));
  649.                     equip.setUpgradeSlots((byte) rs.getInt("upgradeslots"));
  650.                     equip.setLocked((byte) rs.getInt("locked"));
  651.                     equip.setLevel((byte) rs.getInt("level"));
  652.                     items.add(new MTSItemInfo((IItem) equip, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
  653.                 }
  654.             }
  655.             rs.close();
  656.             ps.close();
  657.         } catch (SQLException e) {
  658.             log.error("Err7: " + e);
  659.         }
  660.         return items;
  661.     }
  662.     public MaplePacket getMTS(int tab, int type, int page) {
  663.         List<MTSItemInfo> items = new ArrayList<MTSItemInfo>();
  664.         Connection con = DatabaseConnection.getConnection();
  665.         PreparedStatement ps;
  666.         ResultSet rs;
  667.         int pages = 0;
  668.         try {
  669.             if (type != 0) {
  670.                 ps = con.prepareStatement("SELECT * FROM mts_items WHERE tab = ? AND type = ? AND transfer = 0 ORDER BY id DESC LIMIT ?, 16");
  671.             } else {
  672.                 ps = con.prepareStatement("SELECT * FROM mts_items WHERE tab = ? AND transfer = 0 ORDER BY id DESC LIMIT ?, 16");
  673.             }
  674.             ps.setInt(1, tab);
  675.             if (type != 0) {
  676.                 ps.setInt(2, type);
  677.                 ps.setInt(3, page * 16);
  678.             } else {
  679.                 ps.setInt(2, page * 16);
  680.             }
  681.             rs = ps.executeQuery();
  682.             while (rs.next()) {
  683.                 if (rs.getInt("type") != 1) {
  684.                     Item i = new Item(rs.getInt("itemid"), (byte) 0, (short) rs.getInt("quantity"));
  685.                     i.setOwner(rs.getString("owner"));
  686.                     items.add(new MTSItemInfo((IItem) i, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
  687.                 } else {
  688.                     Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
  689.                     equip.setOwner(rs.getString("owner"));
  690.                     equip.setQuantity((short) 1);
  691.                     equip.setAcc((short) rs.getInt("acc"));
  692.                     equip.setAvoid((short) rs.getInt("avoid"));
  693.                     equip.setDex((short) rs.getInt("dex"));
  694.                     equip.setHands((short) rs.getInt("hands"));
  695.                     equip.setHp((short) rs.getInt("hp"));
  696.                     equip.setInt((short) rs.getInt("int"));
  697.                     equip.setJump((short) rs.getInt("jump"));
  698.                     equip.setLuk((short) rs.getInt("luk"));
  699.                     equip.setMatk((short) rs.getInt("matk"));
  700.                     equip.setMdef((short) rs.getInt("mdef"));
  701.                     equip.setMp((short) rs.getInt("mp"));
  702.                     equip.setSpeed((short) rs.getInt("speed"));
  703.                     equip.setStr((short) rs.getInt("str"));
  704.                     equip.setWatk((short) rs.getInt("watk"));
  705.                     equip.setWdef((short) rs.getInt("wdef"));
  706.                     equip.setUpgradeSlots((byte) rs.getInt("upgradeslots"));
  707.                     equip.setLocked((byte) rs.getInt("locked"));
  708.                     equip.setLevel((byte) rs.getInt("level"));
  709.                     items.add(new MTSItemInfo((IItem) equip, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
  710.                 }
  711.             }
  712.             rs.close();
  713.             ps.close();
  714.             if (type != 0) {
  715.                 ps = con.prepareStatement("SELECT COUNT(*) FROM mts_items WHERE tab = ? AND type = ? AND transfer = 0");
  716.             } else {
  717.                 ps = con.prepareStatement("SELECT COUNT(*) FROM mts_items WHERE tab = ? AND transfer = 0");
  718.             }
  719.             ps.setInt(1, tab);
  720.             if (type != 0) {
  721.                 ps.setInt(2, type);
  722.             }
  723.             rs = ps.executeQuery();
  724.             if (rs.next()) {
  725.                 pages = rs.getInt(1) / 16;
  726.                 if (rs.getInt(1) % 16 > 0) {
  727.                     pages += 1;
  728.                 }
  729.             }
  730.             rs.close();
  731.             ps.close();
  732.         } catch (SQLException e) {
  733.             log.error("Err6: " + e);
  734.         }
  735.         return MaplePacketCreator.sendMTS(items, tab, type, page, pages);
  736.     }
  737.     public MaplePacket getMTSSearch(int tab, int type, int cOi, String search, int page) {
  738.         List<MTSItemInfo> items = new ArrayList<MTSItemInfo>();
  739.         MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
  740.         String listaitems = "";
  741.         if (cOi != 0) {
  742.             List<String> retItems = new ArrayList<String>();
  743.             for (Pair<Integer, String> itemPair : ii.getAllItems()) {
  744.                 if (itemPair.getRight().toLowerCase().contains(search.toLowerCase())) {
  745.                     retItems.add(" itemid=" + itemPair.getLeft() + " OR ");
  746.                 }
  747.             }
  748.             listaitems += " AND (";
  749.             if (retItems != null && retItems.size() > 0) {
  750.                 for (String singleRetItem : retItems) {
  751.                     listaitems += singleRetItem;
  752.                 }
  753.                 listaitems += " itemid=0 )";
  754.             }
  755.         } else {
  756.             listaitems = " AND sellername LIKE CONCAT('%','" + search + "', '%')";
  757.         }
  758.         Connection con = DatabaseConnection.getConnection();
  759.         PreparedStatement ps;
  760.         ResultSet rs;
  761.         int pages = 0;
  762.         try {
  763.             if (type != 0) {
  764.                 ps = con.prepareStatement("SELECT * FROM mts_items WHERE tab = ? " + listaitems + " AND type = ? AND transfer = 0 ORDER BY id DESC LIMIT ?, 16");
  765.             } else {
  766.                 ps = con.prepareStatement("SELECT * FROM mts_items WHERE tab = ? " + listaitems + " AND transfer = 0 ORDER BY id DESC LIMIT ?, 16");
  767.             }
  768.             ps.setInt(1, tab);
  769.             if (type != 0) {
  770.                 ps.setInt(2, type);
  771.                 ps.setInt(3, page * 16);
  772.             } else {
  773.                 ps.setInt(2, page * 16);
  774.             }
  775.             rs = ps.executeQuery();
  776.             while (rs.next()) {
  777.                 if (rs.getInt("type") != 1) {
  778.                     Item i = new Item(rs.getInt("itemid"), (byte) 0, (short) rs.getInt("quantity"));
  779.                     i.setOwner(rs.getString("owner"));
  780.                     items.add(new MTSItemInfo((IItem) i, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
  781.                 } else {
  782.                     Equip equip = new Equip(rs.getInt("itemid"), (byte) rs.getInt("position"), -1);
  783.                     equip.setOwner(rs.getString("owner"));
  784.                     equip.setQuantity((short) 1);
  785.                     equip.setAcc((short) rs.getInt("acc"));
  786.                     equip.setAvoid((short) rs.getInt("avoid"));
  787.                     equip.setDex((short) rs.getInt("dex"));
  788.                     equip.setHands((short) rs.getInt("hands"));
  789.                     equip.setHp((short) rs.getInt("hp"));
  790.                     equip.setInt((short) rs.getInt("int"));
  791.                     equip.setJump((short) rs.getInt("jump"));
  792.                     equip.setLuk((short) rs.getInt("luk"));
  793.                     equip.setMatk((short) rs.getInt("matk"));
  794.                     equip.setMdef((short) rs.getInt("mdef"));
  795.                     equip.setMp((short) rs.getInt("mp"));
  796.                     equip.setSpeed((short) rs.getInt("speed"));
  797.                     equip.setStr((short) rs.getInt("str"));
  798.                     equip.setWatk((short) rs.getInt("watk"));
  799.                     equip.setWdef((short) rs.getInt("wdef"));
  800.                     equip.setUpgradeSlots((byte) rs.getInt("upgradeslots"));
  801.                     equip.setLocked((byte) rs.getInt("locked"));
  802.                     equip.setLevel((byte) rs.getInt("level"));
  803.                     items.add(new MTSItemInfo((IItem) equip, rs.getInt("price"), rs.getInt("id"), rs.getInt("seller"), rs.getString("sellername"), rs.getString("sell_ends")));
  804.                 }
  805.             }
  806.             rs.close();
  807.             ps.close();
  808.             if (type != 0) {
  809.                 ps = con.prepareStatement("SELECT COUNT(*) FROM mts_items WHERE tab = ? " + listaitems + " AND type = ? AND transfer = 0");
  810.             } else {
  811.                 ps = con.prepareStatement("SELECT COUNT(*) FROM mts_items WHERE tab = ? " + listaitems + " AND transfer = 0");
  812.                 ps.setInt(1, tab);
  813.                 if (type != 0) {
  814.                     ps.setInt(2, type);
  815.                 }
  816.                 rs = ps.executeQuery();
  817.                 if (rs.next()) {
  818.                     pages = rs.getInt(1) / 16;
  819.                     if (rs.getInt(1) % 16 > 0) {
  820.                         pages += 1;
  821.                     }
  822.                 }
  823.                 rs.close();
  824.                 ps.close();
  825.             }
  826.         } catch (SQLException e) {
  827.             log.error("Err6: " + e);
  828.         }
  829.         return MaplePacketCreator.sendMTS(items, tab, type, page, pages);
  830.     }
  831. }