Share Pastebin
Guest
Public paste!

v0.83 Cash Shop

By: a guest | Mar 20th, 2010 | Syntax: Java | Size: 8.90 KB | Hits: 175 | Expires: Never
Copy text to clipboard
  1. /* This file is part of the NidoMS Server.
  2.  * Copyright (C) 2009 - 2010  Flav <flav@nidoms.com>
  3.  *                            Hendi <hendi@nidoms.com>
  4.  *
  5.  *                            Patrick Huy <patrick.huy@frz.cc>
  6.  *                            Matthias Butz <matze@odinms.de>
  7.  *                            Jan Christian Meyer <vimes@odinms.de>
  8.  *
  9.  * This program is free software: you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation, either version 3 of the License, or
  12.  * (at your option) any later version.
  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 General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.  */
  22.  
  23. package server;
  24.  
  25. import client.IItem;
  26. import client.Item;
  27. import client.ItemFactory;
  28. import client.MapleInventoryType;
  29. import client.MaplePet;
  30. import constants.InventoryConstants;
  31. import java.io.File;
  32. import java.sql.Connection;
  33. import java.sql.PreparedStatement;
  34. import java.sql.ResultSet;
  35. import java.sql.SQLException;
  36. import java.util.ArrayList;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. import provider.MapleData;
  41. import provider.MapleDataProvider;
  42. import provider.MapleDataProviderFactory;
  43. import provider.MapleDataTool;
  44. import tools.DatabaseConnection;
  45. import tools.Pair;
  46.  
  47. public class CashShop {
  48.     public static class CashItem {
  49.         private int sn, itemId, price;
  50.         private short count;
  51.         private boolean onSale;
  52.  
  53.         private CashItem(int sn, int itemId, int price, short count, boolean onSale) {
  54.             this.sn = sn;
  55.             this.itemId = itemId;
  56.             this.price = price;
  57.             this.count = count;
  58.             this.onSale = onSale;
  59.         }
  60.  
  61.         public int getSN() {
  62.             return sn;
  63.         }
  64.  
  65.         public int getItemId() {
  66.             return itemId;
  67.         }
  68.  
  69.         public int getPrice() {
  70.             return price;
  71.         }
  72.  
  73.         public short getCount() {
  74.             return count;
  75.         }
  76.  
  77.         public boolean isOnSale() {
  78.             return onSale;
  79.         }
  80.  
  81.         public IItem toItem() {
  82.             MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
  83.             IItem item;
  84.             int petId = -1;
  85.  
  86.             if (InventoryConstants.isPet(itemId))
  87.                 petId = MaplePet.createPet(itemId);
  88.  
  89.             if (ii.getInventoryType(itemId).equals(MapleInventoryType.EQUIP))
  90.                 item = ii.getEquipById(itemId);
  91.             else
  92.                 item = new Item(itemId, (byte) 0, count, petId);
  93.  
  94.             item.setSN(sn);
  95.             return item;
  96.         }
  97.     }
  98.  
  99.     public static class CashItemFactory {
  100.         private static Map<Integer, CashItem> items = new HashMap<Integer, CashItem>();
  101.         private static Map<Integer, List<Integer>> packages = new HashMap<Integer, List<Integer>>();
  102.  
  103.         static {
  104.             System.out.println("loading");
  105.             MapleDataProvider etc = MapleDataProviderFactory.getDataProvider(new File("wz/Etc.wz"));
  106.  
  107.             for (MapleData item : etc.getData("Commodity.img").getChildren()) {
  108.                 int sn = MapleDataTool.getIntConvert("SN", item);
  109.                 int itemId = MapleDataTool.getIntConvert("ItemId", item);
  110.                 int price = MapleDataTool.getIntConvert("Price", item, 0);
  111.                 short count = (short) MapleDataTool.getIntConvert("Count", item, 1);
  112.                 boolean onSale = MapleDataTool.getIntConvert("OnSale", item, 0) == 1;
  113.                 items.put(sn, new CashItem(sn, itemId, price, count, onSale));
  114.             }
  115.  
  116.             for (MapleData cashPackage : etc.getData("CashPackage.img").getChildren()) {
  117.                 List<Integer> cPackage = new ArrayList<Integer>();
  118.  
  119.                 for (MapleData item : cashPackage.getChildByPath("SN").getChildren())
  120.                     cPackage.add(Integer.parseInt(item.getData().toString()));
  121.  
  122.                 packages.put(Integer.parseInt(cashPackage.getName()), cPackage);
  123.             }
  124.         }
  125.  
  126.         public static CashItem getItem(int sn) {
  127.             return items.get(sn);
  128.         }
  129.  
  130.         public static List<IItem> getPackage(int itemId) {
  131.             List<IItem> cashPackage = new ArrayList<IItem>();
  132.  
  133.             for (int sn : packages.get(itemId))
  134.                 cashPackage.add(getItem(sn).toItem());
  135.  
  136.             return cashPackage;
  137.         }
  138.     }
  139.  
  140.  
  141.     private int accountId, characterId, nxCredit, maplePoint, nxPrepaid;
  142.     private boolean opened;
  143.     private ItemFactory factory;
  144.     private List<IItem> inventory = new ArrayList<IItem>();
  145.     private List<Integer> wishList = new ArrayList<Integer>();
  146.  
  147.     public CashShop(int accountId, int characterId, int jobType) throws SQLException {
  148.         this.accountId = accountId;
  149.         this.characterId = characterId;
  150.  
  151.         if (jobType == 0)
  152.             factory = ItemFactory.CASH_EXPLORER;
  153.         else if (jobType == 1)
  154.             factory = ItemFactory.CASH_CYGNUS;
  155.         else if (jobType == 2)
  156.             factory = ItemFactory.CASH_ARAN;
  157.  
  158.         Connection con = DatabaseConnection.getConnection();
  159.         PreparedStatement ps = con.prepareStatement("SELECT `nxCredit`, `maplePoint`, `nxPrepaid` FROM `accounts` WHERE `id` = ?");
  160.         ps.setInt(1, accountId);
  161.         ResultSet rs = ps.executeQuery();
  162.  
  163.         if (rs.next()) {
  164.             this.nxCredit = rs.getInt("nxCredit");
  165.             this.maplePoint = rs.getInt("maplePoint");
  166.             this.nxPrepaid = rs.getInt("nxPrepaid");
  167.         }
  168.  
  169.         rs.close();
  170.         ps.close();
  171.  
  172.         for (Pair<IItem, MapleInventoryType> item : factory.loadItems(accountId, false))
  173.             inventory.add(item.getLeft());
  174.  
  175.         ps = con.prepareStatement("SELECT `serialNumber` FROM `wishlists` WHERE `characterId` = ?");
  176.         ps.setInt(1, characterId);
  177.         rs = ps.executeQuery();
  178.  
  179.         while (rs.next())
  180.             wishList.add(rs.getInt("serialNumber"));
  181.  
  182.         rs.close();
  183.         ps.close();
  184.     }
  185.  
  186.     public int getCash(int type) {
  187.         switch (type) {
  188.             case 1:
  189.                 return nxCredit;
  190.             case 2:
  191.                 return maplePoint;
  192.             case 4:
  193.                 return nxPrepaid;
  194.         }
  195.  
  196.         return 0;
  197.     }
  198.  
  199.     public void gainCash(int type, int cash) {
  200.         switch (type) {
  201.             case 1:
  202.                 nxCredit += cash;
  203.                 break;
  204.             case 2:
  205.                 maplePoint += cash;
  206.                 break;
  207.             case 4:
  208.                 nxPrepaid += cash;
  209.                 break;
  210.         }
  211.     }
  212.  
  213.     public boolean isOpened() {
  214.         return opened;
  215.     }
  216.  
  217.     public void open(boolean b) {
  218.         opened = b;
  219.     }
  220.  
  221.     public List<IItem> getInventory() {
  222.         return inventory;
  223.     }
  224.  
  225.     public IItem findByCashId(long cashId) {
  226.         for (IItem item : inventory) {
  227.             if (item.getCashId() == cashId)
  228.                 return item;
  229.         }
  230.  
  231.         return null;
  232.     }
  233.  
  234.     public void addToInventory(IItem item) {
  235.         inventory.add(item);
  236.     }
  237.  
  238.     public void removeFromInventory(IItem item) {
  239.         inventory.remove(item);
  240.     }
  241.  
  242.     public List<Integer> getWishList() {
  243.         return wishList;
  244.     }
  245.  
  246.     public void clearWishList() {
  247.         wishList.clear();
  248.     }
  249.  
  250.     public void addToWishList(int sn) {
  251.         wishList.add(sn);
  252.     }
  253.  
  254.     public void save() throws SQLException {
  255.         Connection con = DatabaseConnection.getConnection();
  256.         PreparedStatement ps = con.prepareStatement("UPDATE `accounts` SET `nxCredit` = ?, `maplePoint` = ?, `nxPrepaid` = ? WHERE `id` = ?");
  257.         ps.setInt(1, nxCredit);
  258.         ps.setInt(2, maplePoint);
  259.         ps.setInt(3, nxPrepaid);
  260.         ps.setInt(4, accountId);
  261.         ps.executeUpdate();
  262.         ps.close();
  263.         List<Pair<IItem, MapleInventoryType>> itemsWithType = new ArrayList<Pair<IItem, MapleInventoryType>>();
  264.  
  265.         for (IItem item : inventory)
  266.             itemsWithType.add(new Pair<IItem,
  267.                     MapleInventoryType>(item, MapleItemInformationProvider.getInstance().getInventoryType(item.getItemId())));
  268.  
  269.         factory.saveItems(itemsWithType, accountId);
  270.         ps = con.prepareStatement("DELETE FROM `wishlists` WHERE `characterId` = ?");
  271.         ps.setInt(1, characterId);
  272.         ps.executeUpdate();
  273.         ps = con.prepareStatement("INSERT INTO `wishlists` VALUES (DEFUALT, ?, ?)");
  274.         ps.setInt(1, characterId);
  275.  
  276.         for (int sn : wishList) {
  277.             ps.setInt(2, sn);
  278.             ps.executeUpdate();
  279.         }
  280.  
  281.         ps.close();
  282.     }
  283. }