Advertisement
raptorjesusss

Untitled

Aug 23rd, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.12 KB | None | 0 0
  1. /*
  2. This file is part of the ZeroFusion MapleStory 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. ZeroFusion organized by "RMZero213" <RMZero213@hotmail.com>
  7.  
  8. This program is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU Affero General Public License version 3
  10. as published by the Free Software Foundation. You may not use, modify
  11. or distribute this program under any other version of the
  12. GNU Affero General Public 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 server;
  23.  
  24. import java.io.Serializable;
  25. import client.inventory.Equip;
  26. import java.sql.Connection;
  27. import java.sql.PreparedStatement;
  28. import java.sql.ResultSet;
  29. import java.sql.SQLException;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import constants.GameConstants;
  33. import client.inventory.MaplePet;
  34. import client.inventory.Item;
  35. import client.inventory.ItemLoader;
  36. import client.MapleClient;
  37. import client.inventory.MapleRing;
  38. import client.inventory.MapleInventoryIdentifier;
  39. import client.inventory.MapleInventoryType;
  40. import database.DatabaseConnection;
  41. import tools.FileoutputUtil;
  42. import tools.packet.MTSCSPacket;
  43. import tools.Pair;
  44.  
  45. public class CashShop implements Serializable {
  46.  
  47. private static final long serialVersionUID = 231541893513373579L;
  48. private int accountId, characterId;
  49. private ItemLoader factory = ItemLoader.CASHSHOP;
  50. private List<Item> inventory = new ArrayList<Item>();
  51. private List<Integer> uniqueids = new ArrayList<Integer>();
  52.  
  53. public CashShop(int accountId, int characterId, int jobType) throws SQLException {
  54. this.accountId = accountId;
  55. this.characterId = characterId;
  56. for (Pair<Item, MapleInventoryType> item : factory.loadItems(false, accountId).values()) {
  57. inventory.add(item.getLeft());
  58. }
  59. }
  60.  
  61. public int getItemsSize() {
  62. return inventory.size();
  63. }
  64.  
  65. public List<Item> getInventory() {
  66. return inventory;
  67. }
  68.  
  69. public Item findByCashId(int cashId) {
  70. for (Item item : inventory) {
  71. if (item.getUniqueId() == cashId) {
  72. return item;
  73. }
  74. }
  75.  
  76. return null;
  77. }
  78.  
  79. public void checkExpire(MapleClient c) {
  80. List<Item> toberemove = new ArrayList<Item>();
  81. for (Item item : inventory) {
  82. if (item != null && !GameConstants.isPet(item.getItemId()) && item.getExpiration() > 0 && item.getExpiration() < System.currentTimeMillis()) {
  83. toberemove.add(item);
  84. }
  85. }
  86. if (toberemove.size() > 0) {
  87. for (Item item : toberemove) {
  88. removeFromInventory(item);
  89. c.getSession().write(MTSCSPacket.cashItemExpired(item.getUniqueId()));
  90. }
  91. toberemove.clear();
  92. }
  93. }
  94.  
  95. public Item toItem(CashItemInfo cItem) {
  96. return toItem(cItem, MapleInventoryManipulator.getUniqueId(cItem.getId(), null), "");
  97. }
  98.  
  99. public Item toItem(CashItemInfo cItem, String gift) {
  100. return toItem(cItem, MapleInventoryManipulator.getUniqueId(cItem.getId(), null), gift);
  101. }
  102.  
  103. public Item toItem(CashItemInfo cItem, int uniqueid) {
  104. return toItem(cItem, uniqueid, "");
  105. }
  106.  
  107. public Item toItem(CashItemInfo cItem, int uniqueid, String gift) {
  108. if (uniqueid <= 0) {
  109. uniqueid = MapleInventoryIdentifier.getInstance();
  110. }
  111. long period = cItem.getPeriod();
  112. if ((period <= 0 && GameConstants.getInventoryType(cItem.getId()) != MapleInventoryType.EQUIP) || GameConstants.isPet(cItem.getId())) {
  113. period = GameConstants.GMS ? 90 : 45;
  114. }
  115. if (cItem.getId() >= 5000100 && cItem.getId() < 5000200) { //permanent pet
  116. period = 20000; //permanent time millis
  117. }
  118. Item ret = null;
  119. if (GameConstants.getInventoryType(cItem.getId()) == MapleInventoryType.EQUIP) {
  120. Equip eq = (Equip) MapleItemInformationProvider.getInstance().getEquipById(cItem.getId(), uniqueid);
  121. if (period > 0) {
  122. eq.setExpiration((long) (System.currentTimeMillis() + (long) (period * 24 * 60 * 60 * 1000)));
  123. }
  124. eq.setGMLog("Cash Shop: " + cItem.getSN() + " on " + FileoutputUtil.CurrentReadable_Date());
  125. eq.setGiftFrom(gift);
  126. if (GameConstants.isEffectRing(cItem.getId()) && uniqueid > 0) {
  127. MapleRing ring = MapleRing.loadFromDb(uniqueid);
  128. if (ring != null) {
  129. eq.setRing(ring);
  130. }
  131. }
  132. ret = eq.copy();
  133. } else {
  134. Item item = new Item(cItem.getId(), (byte) 0, (short) cItem.getCount(), (byte) 0, uniqueid);
  135. if (period > 0) {
  136. item.setExpiration((long) (System.currentTimeMillis() + (long) (period * 24 * 60 * 60 * 1000)));
  137. }
  138. item.setGMLog("Cash Shop: " + cItem.getSN() + " on " + FileoutputUtil.CurrentReadable_Date());
  139. item.setGiftFrom(gift);
  140. if (GameConstants.isPet(cItem.getId())) {
  141. final MaplePet pet = MaplePet.createPet(cItem.getId(), uniqueid);
  142. if (pet != null) {
  143. item.setPet(pet);
  144. }
  145. }
  146. ret = item.copy();
  147. }
  148. return ret;
  149. }
  150.  
  151. public void addToInventory(Item item) {
  152. inventory.add(item);
  153. }
  154.  
  155. public void removeFromInventory(Item item) {
  156. inventory.remove(item);
  157. }
  158.  
  159. public void gift(int recipient, String from, String message, int sn) {
  160. gift(recipient, from, message, sn, 0);
  161. }
  162.  
  163. public void gift(int recipient, String from, String message, int sn, int uniqueid) {
  164. try {
  165. PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("INSERT INTO `gifts` VALUES (DEFAULT, ?, ?, ?, ?, ?)");
  166. ps.setInt(1, recipient);
  167. ps.setString(2, from);
  168. ps.setString(3, message);
  169. ps.setInt(4, sn);
  170. ps.setInt(5, uniqueid);
  171. ps.executeUpdate();
  172. ps.close();
  173. } catch (SQLException sqle) {
  174. sqle.printStackTrace();
  175. }
  176. }
  177.  
  178. public List<Pair<Item, String>> loadGifts() {
  179. List<Pair<Item, String>> gifts = new ArrayList<Pair<Item, String>>();
  180. Connection con = DatabaseConnection.getConnection();
  181. try {
  182. PreparedStatement ps = con.prepareStatement("SELECT * FROM `gifts` WHERE `recipient` = ?");
  183. ps.setInt(1, characterId);
  184. ResultSet rs = ps.executeQuery();
  185.  
  186. while (rs.next()) {
  187. CashItemInfo cItem = CashItemFactory.getInstance().getItem(rs.getInt("sn"));
  188. if (cItem == null) {
  189. continue;
  190. }
  191. Item item = toItem(cItem, rs.getInt("uniqueid"), rs.getString("from"));
  192. gifts.add(new Pair<Item, String>(item, rs.getString("message")));
  193. uniqueids.add(item.getUniqueId());
  194. List<Integer> packages = CashItemFactory.getInstance().getPackageItems(cItem.getId());
  195. if (packages != null && packages.size() > 0) {
  196. for (int packageItem : packages) {
  197. CashItemInfo pack = CashItemFactory.getInstance().getSimpleItem(packageItem);
  198. if (pack != null) {
  199. addToInventory(toItem(pack, rs.getString("from")));
  200. }
  201. }
  202. } else {
  203. addToInventory(item);
  204. }
  205. }
  206.  
  207. rs.close();
  208. ps.close();
  209. ps = con.prepareStatement("DELETE FROM `gifts` WHERE `recipient` = ?");
  210. ps.setInt(1, characterId);
  211. ps.executeUpdate();
  212. ps.close();
  213. save();
  214. } catch (SQLException sqle) {
  215. sqle.printStackTrace();
  216. }
  217. return gifts;
  218. }
  219.  
  220. public boolean canSendNote(int uniqueid) {
  221. return uniqueids.contains(uniqueid);
  222. }
  223.  
  224. public void sendedNote(int uniqueid) {
  225. for (int i = 0; i < uniqueids.size(); i++) {
  226. if (uniqueids.get(i).intValue() == uniqueid) {
  227. uniqueids.remove(i);
  228. }
  229. }
  230. }
  231.  
  232. public void save() throws SQLException {
  233. List<Pair<Item, MapleInventoryType>> itemsWithType = new ArrayList<Pair<Item, MapleInventoryType>>();
  234.  
  235. for (Item item : inventory) {
  236. itemsWithType.add(new Pair<Item, MapleInventoryType>(item, GameConstants.getInventoryType(item.getItemId())));
  237. }
  238.  
  239. factory.saveItems(itemsWithType, accountId);
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement