MapleShop.java
Upload User: gwt600
Upload Date: 2021-06-03
Package Size: 704k
Code Size: 8k
Category:

Games

Development Platform:

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. /*
  19.  * MapleShop.java
  20.  *
  21.  * Created on 28. November 2007, 17:35
  22.  */
  23. package net.sf.odinms.server;
  24. import java.sql.Connection;
  25. import java.sql.PreparedStatement;
  26. import java.sql.ResultSet;
  27. import java.sql.SQLException;
  28. import java.util.ArrayList;
  29. import java.util.LinkedHashSet;
  30. import java.util.LinkedList;
  31. import java.util.List;
  32. import java.util.Set;
  33. import net.sf.odinms.client.IItem;
  34. import net.sf.odinms.client.Item;
  35. import net.sf.odinms.client.MapleClient;
  36. import net.sf.odinms.client.MapleInventoryType;
  37. import net.sf.odinms.client.MaplePet;
  38. import net.sf.odinms.database.DatabaseConnection;
  39. import net.sf.odinms.net.PacketProcessor;
  40. import net.sf.odinms.tools.MaplePacketCreator;
  41. /**
  42.  *
  43.  * @author Matze
  44.  */
  45. public class MapleShop {
  46. private static final Set<Integer> rechargeableItems = new LinkedHashSet<Integer>();
  47. private int id;
  48. private int npcId;
  49. private List<MapleShopItem> items;
  50. private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PacketProcessor.class);
  51. static {
  52. for (int i = 2070000; i <= 2070018; i++) {
  53. rechargeableItems.add(i);
  54. }
  55. rechargeableItems.remove(2070014); // doesn't exist
  56. rechargeableItems.remove(2070017);
  57. for (int i = 2330000; i <= 2330005; i++) {
  58. rechargeableItems.add(i);
  59. }
  60.         rechargeableItems.add(2331000);//Blaze Capsule
  61. rechargeableItems.add(2332000);//Glaze Capsule
  62. }
  63. /** Creates a new instance of MapleShop */
  64. private MapleShop(int id, int npcId) {
  65. this.id = id;
  66. this.npcId = npcId;
  67. items = new LinkedList<MapleShopItem>();
  68. }
  69. public void addItem(MapleShopItem item) {
  70. items.add(item);
  71. }
  72. public void sendShop(MapleClient c) {
  73. c.getPlayer().setShop(this);
  74. c.getSession().write(MaplePacketCreator.getNPCShop(c, getNpcId(), items));
  75. }
  76. public void buy(MapleClient c, int itemId, short quantity) {
  77. if (quantity <= 0) {
  78. log.warn(c.getPlayer().getName() + " is buying an invalid amount: " + quantity + " of itemid: " + itemId);
  79. c.disconnect();
  80. return;
  81. }
  82. MapleShopItem item = findById(itemId);
  83. MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
  84. if (item != null && item.getPrice() > 0 && c.getPlayer().getMeso() >= item.getPrice() * quantity) {
  85. if (MapleInventoryManipulator.checkSpace(c, itemId, quantity, "")) {
  86. if (itemId >= 5000000 && itemId <= 5000100) {
  87. if (quantity > 1) {
  88. quantity = 1;
  89. }
  90. int petId = MaplePet.createPet(itemId);
  91. MapleInventoryManipulator.addById(c, itemId, quantity, null, petId);
  92. } else if (ii.isRechargable(itemId)) {
  93. short rechquantity = ii.getSlotMax(c, item.getItemId());
  94. MapleInventoryManipulator.addById(c, itemId, rechquantity, null, -1);
  95. } else {
  96. MapleInventoryManipulator.addById(c, itemId, quantity, null, -1);
  97. }
  98. c.getPlayer().gainMeso(-(item.getPrice() * quantity), false);
  99. c.getSession().write(MaplePacketCreator.confirmShopTransaction((byte) 0));
  100. } else {
  101. c.getSession().write(MaplePacketCreator.confirmShopTransaction((byte) 3));
  102. }
  103. }
  104. }
  105. public void sell(MapleClient c, MapleInventoryType type, byte slot, short quantity) {
  106. if (quantity == 0xFFFF || quantity == 0) {
  107. quantity = 1;
  108. }
  109. MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
  110. IItem item = c.getPlayer().getInventory(type).getItem(slot);
  111. if (ii.isThrowingStar(item.getItemId()) || ii.isBullet(item.getItemId())) {
  112. quantity = item.getQuantity();
  113. }
  114. if (quantity < 0) {
  115. log.warn(c.getPlayer().getName() + " is selling " + quantity + " " + item.getItemId() + " (" + type.name() + "/" + slot + ")");
  116. c.disconnect();
  117. return;
  118. }
  119. short iQuant = item.getQuantity();
  120. if (iQuant == 0xFFFF) {
  121. iQuant = 1;
  122. }
  123. if (quantity <= iQuant && iQuant > 0) {
  124. MapleInventoryManipulator.removeFromSlot(c, type, slot, quantity,false);
  125. double price;
  126. if (ii.isThrowingStar(item.getItemId()) || ii.isBullet(item.getItemId())) {
  127. price = ii.getWholePrice(item.getItemId()) / (double) ii.getSlotMax(c, item.getItemId());
  128. } else {
  129. price = ii.getPrice(item.getItemId());
  130. }
  131. int recvMesos = (int) Math.max(Math.ceil(price * quantity), 0);
  132. if (price != -1 && recvMesos > 0) {
  133. c.getPlayer().gainMeso(recvMesos, false);
  134. }
  135. c.getSession().write(MaplePacketCreator.confirmShopTransaction((byte) 0x8));
  136. }
  137. }
  138. public void recharge(MapleClient c, byte slot) {
  139. MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
  140. IItem item = c.getPlayer().getInventory(MapleInventoryType.USE).getItem(slot);
  141. if (item == null || (!ii.isThrowingStar(item.getItemId()) && !ii.isBullet(item.getItemId()))) {
  142. if (item != null && (!ii.isThrowingStar(item.getItemId()) || !ii.isBullet(item.getItemId()))) {
  143. log.warn(c.getPlayer().getName() + " is trying to recharge " + item.getItemId());
  144. }
  145. return;
  146. }
  147. short slotMax = ii.getSlotMax(c, item.getItemId());
  148. if (item.getQuantity() < 0) {
  149. log.warn(c.getPlayer().getName() + " is trying to recharge " + item.getItemId() + " with quantity " + item.getQuantity());
  150. }
  151. if (item.getQuantity() < slotMax) {
  152. // calc price ;_;
  153. int price = (int) Math.round(ii.getPrice(item.getItemId()) * (slotMax - item.getQuantity()));
  154. if (c.getPlayer().getMeso() >= price) {
  155. item.setQuantity(slotMax);
  156. c.getSession().write(MaplePacketCreator.updateInventorySlot(MapleInventoryType.USE, (Item) item));
  157. c.getPlayer().gainMeso(-price, false, true, false);
  158. c.getSession().write(MaplePacketCreator.confirmShopTransaction((byte) 0x8));
  159. }
  160. }
  161. }
  162. protected MapleShopItem findById(int itemId) {
  163. for (MapleShopItem item : items) {
  164. if (item.getItemId() == itemId) {
  165. return item;
  166. }
  167. }
  168. return null;
  169. }
  170. public static MapleShop createFromDB(int id, boolean isShopId) {
  171. MapleShop ret = null;
  172. MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
  173. int shopId;
  174. try {
  175. Connection con = DatabaseConnection.getConnection();
  176. PreparedStatement ps;
  177. if (isShopId) {
  178. ps = con.prepareStatement("SELECT * FROM shops WHERE shopid = ?");
  179. } else {
  180. ps = con.prepareStatement("SELECT * FROM shops WHERE npcid = ?");
  181. }
  182. ps.setInt(1, id);
  183. ResultSet rs = ps.executeQuery();
  184. if (rs.next()) {
  185. shopId = rs.getInt("shopid");
  186. ret = new MapleShop(shopId, rs.getInt("npcid"));
  187. rs.close();
  188. ps.close();
  189. } else {
  190. rs.close();
  191. ps.close();
  192. return null;
  193. }
  194. ps = con.prepareStatement("SELECT * FROM shopitems WHERE shopid = ? ORDER BY position ASC");
  195. ps.setInt(1, shopId);
  196. rs = ps.executeQuery();
  197. List<Integer> recharges = new ArrayList<Integer>(rechargeableItems);
  198. while (rs.next()) {
  199. if (ii.isThrowingStar(rs.getInt("itemid")) || ii.isBullet(rs.getInt("itemid"))) {
  200. MapleShopItem starItem = new MapleShopItem((short) 1, rs.getInt("itemid"), rs.getInt("price"));
  201. ret.addItem(starItem);
  202. if (rechargeableItems.contains(starItem.getItemId())) {
  203. recharges.remove(Integer.valueOf(starItem.getItemId()));
  204. }
  205. } else {
  206. ret.addItem(new MapleShopItem((short) 1000, rs.getInt("itemid"), rs.getInt("price")));
  207. }
  208. }
  209. for (Integer recharge : recharges) {
  210. ret.addItem(new MapleShopItem((short) 1000, recharge.intValue(), 0));
  211. }
  212. rs.close();
  213. ps.close();
  214. } catch (SQLException e) {
  215. log.error("Could not load shop", e);
  216. }
  217. return ret;
  218. }
  219. public int getNpcId() {
  220. return npcId;
  221. }
  222. public int getId() {
  223. return id;
  224. }
  225. }