MarketEngine.java
资源名称:src.rar [点击查看]
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:4k
源码类别:
游戏
开发平台:
Java
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor. Then suck a dick
- */
- package net.sf.odinms.server.market;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.LinkedHashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import net.sf.odinms.client.MapleCharacter;
- import net.sf.odinms.database.DatabaseConnection;
- import net.sf.odinms.net.channel.ChannelServer;
- /**
- *
- * @author David
- */
- public class MarketEngine {
- public static class ItemEntry {
- private int quantity;
- private int id;
- private int price;
- private int owner;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getQuantity() {
- return quantity;
- }
- public void setQuantity(int quantity) {
- this.quantity = quantity;
- }
- public int getPrice() {
- return price;
- }
- public void setPrice(int price) {
- this.price = price;
- }
- public int getOwner() {
- return owner;
- }
- public void setOwner(int owner) {
- this.owner = owner;
- }
- }
- private List<ItemEntry> items = new LinkedList<ItemEntry>();
- private Map<Integer, String> names = new LinkedHashMap<Integer, String>();
- public void addItem(int itemId, int quantity, int price, int charid) {
- //see if s/he's already put the same item up
- synchronized (items) {
- for (ItemEntry ie : items) {
- if (ie.getId() == itemId && ie.getOwner() == charid
- && ie.getPrice() == price) {
- ie.setQuantity(ie.getQuantity() + quantity);
- return;
- }
- }
- }
- ItemEntry ie = new ItemEntry();
- ie.setId(itemId);
- ie.setQuantity(quantity);
- ie.setOwner(charid);
- ie.setPrice(price);
- synchronized (items) {
- items.add(ie);
- }
- }
- public void removeItem(int itemId, int quantity, int charid) {
- synchronized (items) {
- for (int i = 0; i < items.size(); i++) {
- ItemEntry ie = items.get(i);
- if (ie.getOwner() == charid && ie.getId() == itemId &&
- ie.getQuantity() >= quantity) {
- if (ie.getQuantity() == quantity) {
- items.remove(items.indexOf(ie));
- } else {
- ie.setQuantity(ie.getQuantity() - quantity);
- }
- }
- }
- }
- }
- public ItemEntry getItem(int position) {
- return items.get(position);
- }
- public List<ItemEntry> getItems() {
- return items;
- }
- public String getCharacterName(int charId) {
- if (names.get(charId) != null) return names.get(charId);
- for (ChannelServer cs : ChannelServer.getAllInstances()) {
- for (MapleCharacter mc : cs.getPlayerStorage().getAllCharacters()) {
- if (mc.getId() == charId) {
- names.put(charId, mc.getName());
- return mc.getName();
- }
- }
- }
- //Not found.. look in SQL
- Connection con = DatabaseConnection.getConnection();
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- ps = con.prepareStatement("SELECT * FROM characters WHERE id = ?");
- ps.setInt(1, charId);
- rs = ps.executeQuery();
- if (rs.next()) {
- String name = rs.getString("name");
- names.put(charId, name);
- return name;
- }
- } catch (SQLException fuckthisissofuckinggay) {
- return "SQL Error fixmepl0x";
- } finally {
- try {
- if (ps !=null && !ps.isClosed())
- ps.close();
- if (rs !=null && !rs.isClosed())
- rs.close();
- } catch (SQLException fuck) {
- return "SQL Error fixmepl0x";
- }
- }
- return "No user";
- }
- @Override public String toString() {
- String ret = "";
- synchronized (items) {
- for (ItemEntry ie : items) {
- ret += "#v" + ie.getId() + //Item picture
- "# Price: #b" + ie.getPrice() + "#k" + //Price
- "Seller: #b" + getCharacterName(ie.getOwner()) + "#k" + //Seller
- "\r\n"; //Newline
- }
- }
- return ret;
- }
- }