Advertisement
Guest User

Untitled

a guest
Jul 11th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.32 KB | None | 0 0
  1. Auction.java
  2. [code]package server.model.content.auct;
  3.  
  4. import java.util.LinkedList;
  5. import java.util.List;
  6.  
  7. import server.model.players.Player;
  8. import server.util.Misc;
  9.  
  10. /**
  11. * BEAST ASS "Auction house" !!!!!!!!!!!!!!!!!!!
  12. *
  13. *
  14. * @author Nick/Women/asshole_rule
  15. * @author Rodrigo
  16. */
  17. public class Auction {
  18.  
  19. private static Auction auction;
  20. private AuctionHandler handler;
  21. private AuctionBinary binary;
  22.  
  23. public Auction() {
  24. handler = new AuctionHandler(this);
  25. binary = new AuctionBinary();
  26. offers = binary.loadOffers();
  27. handler.setCollections(binary.loadCollections());
  28. if(offers == null || handler.getCollections() == null) {
  29. offers = new LinkedList<Offer>();
  30. handler.setCollections(new LinkedList<Collect>());
  31. }
  32. }
  33.  
  34. public static Auction getAuction() {
  35. if(auction == null)
  36. auction = new Auction();
  37. return auction;
  38. }
  39.  
  40. public void saveInternally() {
  41. if(handler != null && binary != null) {
  42. binary.saveCollections((LinkedList<Collect>) handler.getCollections());
  43. binary.saveOffers((LinkedList<Offer>) offers);
  44. }
  45. }
  46.  
  47. public AuctionHandler getHandler() {
  48. return handler;
  49. }
  50.  
  51. /**
  52. * The list of offers which are waiting to be sold.
  53. */
  54. private static List<Offer> offers;
  55.  
  56. protected List<Offer> getOffers() {
  57. return offers;
  58. }
  59.  
  60. /**
  61. * Here we will display 4 offers, the sellers comment and name.
  62. */
  63. public void displayOffers(Player c, int[] values) {
  64. handler.resetInterface(c);
  65. Offer toShow[] = new Offer[4];
  66. if(values.length >= 4) {
  67. toShow[0] = offers.get(values[0]);
  68. toShow[1] = offers.get(values[1]);
  69. toShow[2] = offers.get(values[2]);
  70. toShow[3] = offers.get(values[3]);
  71. } else if (values.length == 3) {
  72. toShow[0] = offers.get(values[0]);
  73. toShow[1] = offers.get(values[1]);
  74. toShow[2] = offers.get(values[2]);
  75. } else if (values.length == 2) {
  76. toShow[0] = offers.get(values[0]);
  77. toShow[1] = offers.get(values[1]);
  78. } else if (values.length == 1) {
  79. toShow[0] = offers.get(values[0]);
  80. } else {
  81. toShow = null;
  82. }
  83.  
  84. if(toShow != null) {
  85. Offer first = toShow[0];
  86. Offer second = toShow[1];
  87. Offer third = toShow[2];
  88. Offer fourth = toShow[3];
  89. if(first != null) {
  90. display(first, 1, c);
  91. }
  92. if(second != null && first != null) {
  93. display(second, 2, c);
  94. } else if (second != null && first == null) {
  95. display(second, 1, c);
  96. }
  97. if(third != null && first != null && second != null) {
  98. display(third, 3, c);
  99. } else if (third != null && first != null && second == null) {
  100. display(third, 2, c);
  101. } else if (third != null && first == null && second == null) {
  102. display(third, 1, c);
  103. }
  104. if(fourth != null && third != null && second != null && first != null) {
  105. display(fourth, 4, c);
  106. } else if (fourth != null && third != null && second != null && first == null) {
  107. display(fourth, 3, c);
  108. } else if (fourth != null && third != null && second == null && first == null) {
  109. display(fourth, 2, c);
  110. } else if (fourth != null && third == null && second == null && first == null) {
  111. display(fourth, 1, c);
  112. }
  113. } else {
  114. //nothing to show...
  115. }
  116. }
  117.  
  118. private void display(Offer offer, int state, Player c) {
  119. String itemName = c.getItems().getItemName(offer.getItemId());
  120. switch(state) {
  121. case 1:
  122. //shows the offer in the first box.
  123. offer.setInterfaceId(43002);
  124. c.getPA().sendFrame126(offer.getComment(), 11019);
  125. c.getPA().sendFrame126(offer.getUsername(), 11023);
  126. c.getPA().sendFrame126(itemName, 11027);
  127. c.getPA().sendFrame126(""+Misc.formatGp(offer.getOfferedPrice())+"", 11031);
  128. break;
  129. case 2:
  130. offer.setInterfaceId(43003);
  131. c.getPA().sendFrame126(offer.getComment(), 11020);
  132. c.getPA().sendFrame126(offer.getUsername(), 11024);
  133. c.getPA().sendFrame126(itemName, 11028);
  134. c.getPA().sendFrame126(""+Misc.formatGp(offer.getOfferedPrice())+"", 11032);
  135. break;
  136. case 3:
  137. offer.setInterfaceId(43004);
  138. c.getPA().sendFrame126(offer.getComment(), 11021);
  139. c.getPA().sendFrame126(offer.getUsername(), 11025);
  140. c.getPA().sendFrame126(itemName, 11029);
  141. c.getPA().sendFrame126(""+Misc.formatGp(offer.getOfferedPrice())+"", 11033);
  142. break;
  143. case 4:
  144. offer.setInterfaceId(43005);
  145. c.getPA().sendFrame126(offer.getComment(), 11022);
  146. c.getPA().sendFrame126(offer.getUsername(), 11026);
  147. c.getPA().sendFrame126(itemName, 11030);
  148. c.getPA().sendFrame126(""+Misc.formatGp(offer.getOfferedPrice())+"", 11034);
  149. break;
  150. }
  151. }
  152.  
  153. private int restricted[] = {
  154. 995
  155. };
  156.  
  157. public void addOffer(Player c, String sellerComment, int itemId, int amount, int priceId) {
  158. for(int a : restricted) {
  159. if(itemId == a) {
  160. c.sendMessage("You cannot sell this item.");
  161. handler.resetInterface(c);
  162. return;
  163. }
  164. }
  165. if(!c.getItems().playerHasItem(itemId - 1, amount)) {
  166. System.out.println("Player ["+c.playerName+"] has tried to motherfucking dupe.");
  167. c.sendMessage("Bro, i will fucking rape you dont fucking dupe.");
  168. return;
  169. }
  170. c.getItems().deleteItem(itemId - 1, c.getItems().getItemSlot(itemId - 1),
  171. amount);
  172. Offer o = new Offer(itemId, amount, priceId, sellerComment, c.playerName);
  173. offers.add(o);
  174. System.out.println("adding player: "+c.playerName);
  175. saveInternally();
  176. changePage(c);
  177. c.getPA().sendFrame126(c.getItems().getItemName(itemId), 11018);
  178. c.getPA().sendFrame34(11035, itemId - 1, 1, 1);
  179. }
  180.  
  181. public void changePage(Player c) {
  182. if(c.page < 0) c.page = 0;
  183. int max = offers.size() / 4;
  184. if(c.page > max) c.page = max;
  185.  
  186. int first = (c.page * 4);
  187. int second = (c.page * 4) + 1;
  188. int third = (c.page * 4) + 2;
  189. int fourth = (c.page * 4) + 3;
  190.  
  191. if(offers.size() > fourth && offers.get(first) != null && offers.get(second) != null && offers.get(third) != null && offers.get(fourth) != null) {
  192. displayOffers(c, new int[] {first,second,third,fourth});
  193. } else if(offers.size() > third && offers.get(first) != null && offers.get(second) != null && offers.get(third) != null) {
  194. displayOffers(c, new int[] {first,second,third});
  195. } else if(offers.size() > second && offers.get(first) != null && offers.get(second) != null) {
  196. displayOffers(c, new int[] {first,second});
  197. } else if(offers.size() > first && offers.get(first) != null) {
  198. displayOffers(c, new int[] {first});
  199. } else {
  200. handler.resetInterface(c);
  201. }
  202. }
  203. }
  204. [/code]
  205.  
  206. AuctionBinary.java (lolol)
  207. [code]package server.model.content.auct;
  208.  
  209. import java.io.File;
  210. import java.io.FileInputStream;
  211. import java.io.FileOutputStream;
  212. import java.io.ObjectInputStream;
  213. import java.io.ObjectOutputStream;
  214. import java.util.LinkedList;
  215. import java.util.List;
  216.  
  217. /**
  218. * Here we will load/save the auction collection lists.
  219. *
  220. *
  221. * @author Nick/Women/asshole_rule
  222. * @author Rodrigo
  223. */
  224. public class AuctionBinary {
  225.  
  226. private final String OFFER_DIR = "./Data/auct/offers.dat";
  227. private final String COLLECT_DIR = "./Data/auct/collections.dat";
  228.  
  229. protected LinkedList<Offer> loadOffers() {
  230. try {
  231. ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(OFFER_DIR)));
  232. List<Offer> o = new LinkedList<Offer>();
  233. int size = in.readInt();
  234. for(int a = 0; a < size; a++) {
  235. o.add((Offer) in.readObject());
  236. }
  237. return (LinkedList<Offer>) o;
  238. } catch (Exception e) {
  239. return null;
  240. }
  241. }
  242.  
  243. protected LinkedList<Collect> loadCollections() {
  244. try {
  245. ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(COLLECT_DIR)));
  246. List<Collect> o = new LinkedList<Collect>();
  247. int size = in.readInt();
  248. for(int a = 0; a < size; a++) {
  249. o.add((Collect) in.readObject());
  250. }
  251. return (LinkedList<Collect>) o;
  252. } catch (Exception e) {
  253. return null;
  254. }
  255. }
  256.  
  257. protected void saveOffers(LinkedList<Offer> o) {
  258. try {
  259. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(OFFER_DIR)));
  260. out.writeInt(o.size());
  261. for(Offer offer : o) {
  262. out.writeObject(offer);
  263. }
  264. out.close();
  265. } catch (Exception e) {
  266. }
  267. }
  268.  
  269. protected void saveCollections(LinkedList<Collect> c) {
  270. try {
  271. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(COLLECT_DIR)));
  272. out.writeInt(c.size());
  273. for(Collect offer : c) {
  274. out.writeObject(offer);
  275. }
  276. out.close();
  277. } catch (Exception e) {
  278. }
  279. }
  280. }
  281. [/code]
  282.  
  283. AuctionHandler.java
  284. [code]package server.model.content.auct;
  285.  
  286. import java.util.List;
  287.  
  288. import server.model.players.Player;
  289. import server.model.players.PlayerHandler;
  290.  
  291. /**
  292. * Here we will need to handle the selling and buying of the items.
  293. *
  294. * Buying items is done.
  295. *
  296. * @author Nick/Women/asshole_rule
  297. * @author Rodrigo
  298. */
  299. public class AuctionHandler {
  300.  
  301. private Auction auction;
  302.  
  303. /**
  304. * A list of offers which have been sold, but not claimed by the seller.
  305. */
  306. private static List<Collect> toCollect;
  307.  
  308. protected void setCollections(List<Collect> c) {
  309. toCollect = c;
  310. }
  311.  
  312. protected List<Collect> getCollections() {
  313. return toCollect;
  314. }
  315.  
  316. public AuctionHandler(Auction auction) {
  317. this.auction = auction;
  318. }
  319.  
  320. public void openInterface(Player c) {
  321. resetInterface(c);
  322. auction.changePage(c);
  323. c.auctionOpen = true;
  324. c.getPA().showInterface(11000);//sec
  325. }
  326.  
  327. public void resetInterface(Player c) {
  328. c.getPA().sendFrame34(11035, -1/*lets try -1 lol*/, 1, 1);
  329. c.getPA().sendFrame126("No item.", 11018);
  330.  
  331. c.getPA().sendFrame126("No item name", 11019);
  332. c.getPA().sendFrame126("No seller", 11023);
  333. c.getPA().sendFrame126("No item name", 11027);
  334. c.getPA().sendFrame126("No price", 11031);
  335.  
  336. c.getPA().sendFrame126("No item name", 11020);
  337. c.getPA().sendFrame126("No seller", 11024);
  338. c.getPA().sendFrame126("No item name", 11028);
  339. c.getPA().sendFrame126("No price", 11032);
  340.  
  341. c.getPA().sendFrame126("No item name", 11021);
  342. c.getPA().sendFrame126("No seller", 11025);
  343. c.getPA().sendFrame126("No item name", 11029);
  344. c.getPA().sendFrame126("No price", 11033);
  345.  
  346. c.getPA().sendFrame126("No item name", 11022);
  347. c.getPA().sendFrame126("No seller", 11026);
  348. c.getPA().sendFrame126("No item name", 11030);
  349. c.getPA().sendFrame126("No price", 11034);
  350. }
  351.  
  352. public void clickToBuy(Player c, int interfaceId) {
  353. Offer clicked = null;
  354. for(Offer o : auction.getOffers()) {
  355. if(o.getInterfaceId() > 0) {
  356. if(o.getInterfaceId() == interfaceId) {
  357. clicked = o;
  358. break;
  359. }
  360. }
  361. }
  362.  
  363. if(clicked != null) {
  364. buyItem(clicked, c);
  365. } else {
  366. c.sendMessage("There is no item here.");
  367. }
  368. clicked = null;
  369. }
  370.  
  371.  
  372. public void buyItem(Offer o, Player c) {
  373. if(c.getItems().freeSlots() > 0) {
  374. if(o.getUsername().equalsIgnoreCase(c.playerName)) {
  375. c.sendMessage(""+o.getUsername()+" "+c.playerName+"");
  376. c.getItems().addItem(o.getItemId() - 1, o.getAmount());
  377. notifySeller(o, c);
  378. auction.changePage(c);
  379. return;
  380. }
  381. if(c.getItems().playerHasItem(995, o.getOfferedPrice())) {
  382. // they have enough money
  383. c.getItems().addItem(o.getItemId() - 1, o.getAmount());
  384. c.getItems().deleteItem(995, c.getItems().getItemSlot(995), o.getOfferedPrice());
  385. notifySeller(o, c);
  386. c.sendMessage("You buy "+o.getAmount()+" of "+c.getItems().getItemName(o.getItemId() - 1)+" from player: "+o.getUsername());
  387. auction.changePage(c);
  388. } else {
  389. c.sendMessage("You don't have enough coins to buy this item.");
  390. }
  391. } else {
  392. c.sendMessage("Not enough inventory space.");
  393. }
  394. }
  395.  
  396. private void notifySeller(Offer sold, Player buyer) {
  397. Player seller = null;
  398. for(int a = 0; a < PlayerHandler.players.length; a++) {
  399. if(PlayerHandler.players[a] != null) {
  400. if(PlayerHandler.players[a].playerName.equalsIgnoreCase(sold.getUsername())) {
  401. seller = (Player) PlayerHandler.players[a];
  402. break;
  403. }
  404. }
  405. }
  406. if(sold.getUsername().equalsIgnoreCase(buyer.playerName)) {
  407. this.auction.changePage(seller);
  408. this.auction.getOffers().remove(sold);
  409. return;
  410. }
  411. if(seller == null) {
  412. Collect c = getCollect(sold.getUsername());
  413. if(c != null) {
  414. c.addOffer(sold);
  415. } else {
  416. toCollect.add(new Collect(sold, sold.getUsername()));
  417. }
  418. //hes offline.
  419. } else {
  420. seller.sendMessage("Your item["+seller.getItems().getItemName(sold.getItemId() - 1)+"] has been bought by: "+buyer.playerName+"!");
  421. seller.sendMessage("You receive the money you offered in your bank.");
  422. this.auction.changePage(seller);
  423. if(seller.getItems().freeSlots() > 0) {
  424. seller.getItems().addItem(995, sold.getOfferedPrice());
  425. } else {
  426. if(seller.getItems().playerHasItem(995)) {
  427. seller.getItems().addItem(995, sold.getOfferedPrice());
  428. } else {
  429. seller.getItems().addItemToBank(995, sold.getOfferedPrice());
  430. }
  431. }
  432. }
  433. this.auction.getOffers().remove(sold);
  434. }
  435.  
  436. public Collect getCollect(String playerName) {
  437. Collect c = null;
  438. for(Collect o : toCollect) {
  439. if(o.getPlayername().equalsIgnoreCase(playerName)) {
  440. c = o;
  441. break;
  442. }
  443. }
  444. return c;
  445. }
  446.  
  447. //when player logs in, if they have offers to collect, then it will tell them
  448. public void login(Player c) {
  449. int offers = 0;
  450. for(Collect o : toCollect) {
  451. System.out.println(o.getPlayername());
  452. if(o.getPlayername().equalsIgnoreCase(c.playerName))
  453. offers++;
  454. }
  455. if(offers > 0)
  456. c.sendMessage("You have "+(offers == 1 ? "a" : "more than 1")+" offer"+(offers == 1 ? "" : "s")+" to be collected.");
  457. }
  458.  
  459. public void collectAll(Player c) {
  460. Collect col = getCollect(c.playerName);
  461. if(col != null) {
  462. for(int a= 0; a < col.getSoldOffers().length; a++) {
  463. if(col.getSoldOffers()[a] != null)
  464. giveToSeller(col.getSoldOffers()[a], c);
  465. if(a + 1 >= col.getSoldOffers().length)
  466. toCollect.remove(col);
  467. }
  468.  
  469. } else {
  470. c.sendMessage("You have nothing to collect.");
  471. }
  472. }
  473.  
  474. public void giveToSeller(Offer o, Player seller) {
  475. seller.sendMessage("Your item["+seller.getItems().getItemName(o.getItemId())+"] has been sold!");
  476. seller.sendMessage("You receive the money you offered in your bank.");
  477. if(seller.getItems().freeSlots() > 0) {
  478. seller.getItems().addItem(995, o.getOfferedPrice());
  479. } else {
  480. if(seller.getItems().playerHasItem(995)) {
  481. seller.getItems().addItem(995, o.getOfferedPrice());
  482. } else {
  483. seller.getItems().addItemToBank(995, o.getOfferedPrice());
  484. }
  485. }
  486. }
  487. }
  488. [/code]
  489.  
  490. [code]package server.model.content.auct;
  491.  
  492. import java.io.Serializable;
  493.  
  494. /**
  495. * Represents the collection button, which is at the top right hand corner.
  496. *
  497. *
  498. * @author Nick/Women/asshole_rule
  499. * @author Rodrigo
  500. */
  501. public class Collect implements Serializable {
  502.  
  503. private Offer[] soldOffers = new Offer[5];
  504. private String playername;
  505.  
  506. public Collect(Offer o, String playername) {
  507. getSoldOffers()[0] = o;
  508. this.setPlayername(playername);
  509. }
  510.  
  511. public Offer[] getSoldOffers() {
  512. return soldOffers;
  513. }
  514.  
  515. public void addOffer(Offer o) {
  516. int slot = getFreeSlot(soldOffers);
  517. if(slot != -1) {
  518. soldOffers[slot] = o;
  519. }
  520. }
  521.  
  522. public static int getFreeSlot(Object[] a) {
  523. for(int idx = 1; idx < a.length; idx++) {
  524. if(a[idx] == null)
  525. return idx;
  526. }
  527. return -1;
  528. }
  529.  
  530. public String getPlayername() {
  531. return playername;
  532. }
  533.  
  534. public void setPlayername(String playername) {
  535. this.playername = playername;
  536. }
  537. }
  538. [/code]
  539.  
  540. [code]package server.model.content.auct;
  541.  
  542. import java.io.Serializable;
  543.  
  544. /**
  545. * Represents an offer in the auction house.
  546. *
  547. *
  548. * @author Nick/Women/asshole_rule
  549. * @author Rodrigo
  550. */
  551. public class Offer implements Serializable {
  552.  
  553. private int itemId;
  554. private int amount;
  555. private int offeredPrice;
  556. private int interfaceId;
  557. private String comment;
  558. private String username;
  559.  
  560. public Offer(int itemId, int amount, int offeredPrice, String comment, String username) {
  561. this.setAmount(amount);
  562. this.setItemId(itemId);
  563. this.setOfferedPrice(offeredPrice);
  564. this.setComment(comment);
  565. this.setUsername(username);
  566. }
  567.  
  568. public int getItemId() {
  569. return itemId;
  570. }
  571. public void setItemId(int itemId) {
  572. this.itemId = itemId;
  573. }
  574. public int getAmount() {
  575. return amount;
  576. }
  577. public void setAmount(int amount) {
  578. this.amount = amount;
  579. }
  580. public String getUsername() {
  581. return username;
  582. }
  583. public void setUsername(String username) {
  584. this.username = username;
  585. }
  586.  
  587. public String getComment() {
  588. return comment;
  589. }
  590.  
  591. public void setComment(String comment) {
  592. this.comment = comment;
  593. }
  594.  
  595. public int getOfferedPrice() {
  596. return offeredPrice;
  597. }
  598.  
  599. public void setOfferedPrice(int offeredPrice) {
  600. this.offeredPrice = offeredPrice;
  601. }
  602.  
  603. public int getInterfaceId() {
  604. return interfaceId;
  605. }
  606.  
  607. public void setInterfaceId(int interfaceId) {
  608. this.interfaceId = interfaceId;
  609. }
  610.  
  611.  
  612. }
  613. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement