Advertisement
Guest User

bla

a guest
Feb 28th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  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.  
  7.     This program is free software: you can redistribute it and/or modify
  8.     it under the terms of the GNU Affero General Public License as
  9.     published by the Free Software Foundation version 3 as published by
  10.     the Free Software Foundation. You may not use, modify or distribute
  11.     this program under any other version of the GNU Affero General Public
  12.     License.
  13.  
  14.     This program is distributed in the hope that it will be useful,
  15.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.     GNU Affero General Public License for more details.
  18.  
  19.     You should have received a copy of the GNU Affero General Public License
  20.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package net.server.channel.handlers;
  23.  
  24. import client.MapleClient;
  25. import net.AbstractMaplePacketHandler;
  26. import server.MapleInventoryManipulator;
  27. import tools.DatabaseConnection;
  28. import tools.MaplePacketCreator;
  29. import tools.data.input.SeekableLittleEndianAccessor;
  30.  
  31. import java.sql.Connection;
  32. import java.sql.PreparedStatement;
  33. import java.sql.ResultSet;
  34. import java.sql.SQLException;
  35.  
  36. /**
  37.  * @author Penguins (Acrylic)
  38.  */
  39. public final class CouponCodeHandler extends AbstractMaplePacketHandler {
  40.  
  41.     public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
  42.         slea.skip(2);
  43.         String code = slea.readMapleAsciiString();
  44.         boolean validcode = false;
  45.         int type = -1;
  46.         int item = -1;
  47.         validcode = getNXCodeValid(code.toUpperCase(), validcode);
  48.         if (validcode) {
  49.             type = getNXCode(code, "type");
  50.             item = getNXCode(code, "item");
  51.             if (type != 5) {
  52.                 try {
  53.                     Connection con = DatabaseConnection.getConnection();
  54.                     PreparedStatement ps = con.prepareStatement("UPDATE nxcode SET `valid` = 0 WHERE code = " + code);
  55.                     ps.executeUpdate();
  56.                     ps.close();
  57.                     ps = con.prepareStatement("UPDATE nxcode SET `user` = ? WHERE code = ?");
  58.                     ps.setString(1, c.getPlayer().getName());
  59.                     ps.setString(2, code);
  60.                     ps.executeUpdate();
  61.                     ps.close();
  62.                     con.close();
  63.                 } catch (SQLException e) {
  64.                     e.printStackTrace();
  65.                 }
  66.             }
  67.             switch (type) {
  68.                 case 0:
  69.                 case 1:
  70.                 case 2:
  71.                     c.getPlayer().getCashShop().gainCash(type, item);
  72.                     break;
  73.                 case 3:
  74.                     c.getPlayer().getCashShop().gainCash(0, item);
  75.                     c.getPlayer().getCashShop().gainCash(2, (item / 5000));
  76.                     break;
  77.                 case 4:
  78.                     MapleInventoryManipulator.addById(c, item, (short) 1, null, -1, -1);
  79.                     c.announce(MaplePacketCreator.showCouponRedeemedItem(item));
  80.                     break;
  81.                 case 5:
  82.                     c.getPlayer().getCashShop().gainCash(0, item);
  83.                     break;
  84.             }
  85.             c.announce(MaplePacketCreator.showCash(c.getPlayer()));
  86.         } else {
  87.             //c.announce(MaplePacketCreator.wrongCouponCode());
  88.         }
  89.         c.announce(MaplePacketCreator.enableCSUse());
  90.     }
  91.  
  92.     private int getNXCode(String code, String type) {
  93.         int item = -1;
  94.         try {
  95.             Connection con = DatabaseConnection.getConnection();
  96.             PreparedStatement ps = con.prepareStatement("SELECT `" + type + "` FROM nxcode WHERE code = ?");
  97.             ps.setString(1, code);
  98.             ResultSet rs = ps.executeQuery();
  99.             while (rs.next()) {
  100.                 item = rs.getInt(type);
  101.             }
  102.             rs.close();
  103.             ps.close();
  104.             con.close();
  105.         } catch (SQLException ex) {
  106.             ex.printStackTrace();
  107.         }
  108.         return item;
  109.     }
  110.  
  111.     private boolean getNXCodeValid(String code, boolean validcode) {
  112.         try {
  113.             Connection con = DatabaseConnection.getConnection();
  114.             PreparedStatement ps = con.prepareStatement("SELECT `valid` FROM nxcode WHERE code = ?");
  115.             ps.setString(1, code);
  116.             ResultSet rs = ps.executeQuery();
  117.             while (rs.next()) {
  118.                 validcode = rs.getInt("valid") != 0;
  119.             }
  120.             rs.close();
  121.             ps.close();
  122.             con.close();
  123.         } catch (SQLException ex) {
  124.             ex.printStackTrace();
  125.         }
  126.         return validcode;
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement