Jump to content
  • 0
accerado

Grand Exchange problem

Question

I need help with my GrandExchange,
im using Eriwien 2 source,

Whenever i want to buy a item, it doesnt load
but.. when i sell a item thatt is the same as the buy item.
I get the item aswell as the money from sell item.
so its a economic system i guess, how can i delete tht so everyone can buy anything whenever they want?
thx in advance.
another little prob is character saving, i got both character and characterbackup folders, but il look later into tht.. want this prob solved ;/

[ATTACH]10357[/ATTACH]

GE Files:

[B]OFFER.JAVA[/B]

[CODE]package com.ew.game.player.content.grandExchange;

import com.ew.cache.loaders.ClientScriptMap;
import com.ew.cache.loaders.ItemDefinitions;
import com.ew.game.item.Item;
import com.ew.game.item.ItemsContainer;
import com.ew.game.player.Player;
import com.ew.utils.EconomyPrices;
import com.ew.utils.Utils;

public class Offer extends Item {

private static final long serialVersionUID = -4065899425889989474L;
public static final String SELL = null;

public Offer(int id, int ammount, int price, boolean buy) {
super(id, ammount);
this.price = price;
buying = buy;
receivedItems = new ItemsContainer<Item>(2, true);
}

private transient Player owner;
private transient int slot;

private int price; // price per item selling or buying
private int totalPriceSoFar; // total profit received so far or spent
private int totalAmmountSoFar; // amt of items sold or bought
private ItemsContainer<Item> receivedItems;
private boolean canceled;
private boolean buying;
[MENTION=19271]supp[/MENTION]ressWarnings("unused")
private long data;

public void link(int slot, Player owner) {
this.slot = slot;
this.owner = owner;
this.data = Utils.currentTimeMillis();
}

public void unlink() {
owner = null;
}

public void update() {
if (owner == null)
return;
owner.getPackets().sendGrandExchangeOffer(this);
sendItems();
}

public void sendItems() {
if (owner == null)
return;
owner.getPackets().sendItems(ClientScriptMap.getMap(1079).getIntValue(slot), receivedItems);
}

public int getPrice() {
return price;
}

public boolean forceRemove() {
return isCompleted() && !hasItemsWaiting();
}

protected boolean isCompleted() {
return canceled || totalAmmountSoFar >= getAmount();
}

public int getPercentage() {
return totalAmmountSoFar * getAmount() / 100;
}

public int getTotalAmmountSoFar() {
return totalAmmountSoFar;
}

public int getTotalPriceSoFar() {
return totalPriceSoFar;
}

public int getSlot() {
return slot;
}

public int getStage() {
if (forceRemove())
return 0;
if (isCompleted())
return buying ? 5 : 13;
return buying ? 2 : 10;
}

public boolean isBuying() {
return buying;
}

// TODO canceling message
public boolean cancel() {
if (isCompleted())
return false;
canceled = true;
if (buying)
receivedItems.add(new Item(995, (getAmount() - totalAmmountSoFar) * price));
else
receivedItems.add(new Item(getId(), getAmount() - totalAmmountSoFar));
update();
return true;
}

public void sendUpdateWarning() {
if (owner == null)
return;
owner.getPackets().sendGameMessage("One or more of your Grand Exchange offers have been updated.");
owner.getPackets().sendMusicEffect(284);
update();
}

public boolean isOfferTooHigh(Offer fromOffer) {
int left = getAmount() - totalAmmountSoFar;
int leftFrom = fromOffer.getAmount() - fromOffer.totalAmmountSoFar;
int exchangeAmt = left > leftFrom ? leftFrom : left;
int totalPrice = exchangeAmt * fromOffer.price;
int amtCoins = receivedItems.getNumberOf(995);

if (buying) {
if (fromOffer.receivedItems.getNumberOf(995) + totalPrice <= 0)
return true;
int leftcoins = exchangeAmt * price - totalPrice;
if (leftcoins > 0 && amtCoins + leftcoins <= 0)
return true;
} else {
if (amtCoins + totalPrice <= 0)
return true;
}
return false;

}

private int[] items = { 14484, 1048 };

public void updateOffer(Offer fromOffer) {
int left = getAmount() - totalAmmountSoFar;
int leftFrom = fromOffer.getAmount() - fromOffer.totalAmmountSoFar;
int exchangeAmt = left > leftFrom ? leftFrom : left;
int totalPrice = exchangeAmt * fromOffer.price;
if (buying) {
int leftcoins = exchangeAmt * price - totalPrice;
if (leftcoins > 0) {
receivedItems.add(new Item(995, leftcoins));
}
for (int i : items) {
if (totalPrice < EconomyPrices.getPrice(i)) {
owner.getPackets().sendGameMessage("You can't buy default set items for lower than their origional value!");
return;
}
receivedItems.add(buying ? new Item(getId(), exchangeAmt) : new Item(getId(), exchangeAmt));
}
receivedItems.add(buying ? new Item(getId(), exchangeAmt) : new Item(getId(), exchangeAmt));
fromOffer.receivedItems.add(new Item(995, totalPrice));
} else {
fromOffer.receivedItems.add(new Item(getId(), exchangeAmt));
receivedItems.add(new Item(995, totalPrice));
}
totalAmmountSoFar += exchangeAmt;
fromOffer.totalAmmountSoFar += exchangeAmt;
totalPriceSoFar += totalPrice;
fromOffer.totalPriceSoFar += totalPrice;
fromOffer.sendUpdateWarning();
}

public boolean collectItems(int slot, int option) {
if (owner == null)
return false;
int freeSlots = owner.getInventory().getFreeSlots();
if (freeSlots == 0) {
owner.getPackets().sendGameMessage("Not enough space in your inventory.");
return false;
}
Item item = receivedItems.get(slot);
if (item == null)
return false;
ItemDefinitions defs = item.getDefinitions();
if (!defs.isStackable() && option == (item.getAmount() == 1 ? 0 : 1)) {
Item add = new Item(item.getId(), item.getAmount() > freeSlots ? freeSlots : item.getAmount());
owner.getInventory().addItemMoneyPouch(add);
receivedItems.remove(add);
} else {
owner.getInventory().addItemMoneyPouch(new Item(defs.certId != -1 ? defs.certId : item.getId(), item.getAmount()));
receivedItems.remove(item);
}
update();
return true;
}

public boolean hasItemsWaiting() {
return receivedItems.getFreeSlots() != 2;
}

public Object getType() {
// TODO Auto-generated method stub
return null;
}

public void increase(int i) {
// TODO Auto-generated method stub

}

}
[/CODE]

[B]GRANDEXCHANGE.JAVA
[/B]
[CODE]package com.ew.game.player.content.grandExchange;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ew.cache.loaders.ItemDefinitions;
import com.ew.game.player.Player;
import com.ew.utils.EconomyPrices;
import com.ew.utils.SerializableFilesManager;
import com.ew.utils.Utils;

public class GrandExchange {

private static final Object LOCK = new Object();
// offer uid
private static HashMap<Long, Offer> OFFERS;
private static ArrayList<OfferHistory> OFFERS_TRACK;
private static HashMap<Integer, Integer> PRICES;


private static boolean edited;


public static void init() {
OFFERS = SerializableFilesManager.loadGEOffers();
OFFERS_TRACK = SerializableFilesManager.loadGEHistory();
PRICES = SerializableFilesManager.loadGEPrices();
}

public static void reset(boolean track, boolean price) {
if(track)
OFFERS_TRACK.clear();
if(price)
PRICES.clear();
recalcPrices();
}


public static void recalcPrices() {
ArrayList<OfferHistory> track = new ArrayList<OfferHistory>(OFFERS_TRACK);
HashMap<Integer, BigInteger> averagePrice = new HashMap<Integer, BigInteger>();
HashMap<Integer, BigInteger> averageQuantity = new HashMap<Integer, BigInteger>();
for (OfferHistory o : track) {
BigInteger price = averagePrice.get(o.getId());
if (price != null) {
BigInteger quantity = averageQuantity.get(o.getId());
averagePrice.put(o.getId(), price.add(BigInteger.valueOf(o.getPrice())));
averageQuantity.put(o.getId(), quantity.add(BigInteger.valueOf(o.getQuantity())));
} else {
averagePrice.put(o.getId(), BigInteger.valueOf(o.getPrice()));
averageQuantity.put(o.getId(), BigInteger.valueOf(o.getQuantity()));
}
}

for (int id : averagePrice.keySet()) {
BigInteger price = averagePrice.get(id);
BigInteger quantity = averageQuantity.get(id);

long oldPrice = getPrice(id);
long newPrice = price.divide(quantity).longValue();
long min = (long) ((double) oldPrice * 0.8D) - 100;
long max = (long) ((double) oldPrice * 1.2D) + 100;
if (newPrice < min)
newPrice = min;
else if (newPrice > max)
newPrice = max;

if (newPrice < 1)
newPrice = 1;
else if (newPrice > Integer.MAX_VALUE)
newPrice = Integer.MAX_VALUE;

PRICES.put(id, (int) newPrice);
}
savePrices();
}

private static void savePrices() {
SerializableFilesManager.saveGEPrices(PRICES);
}

public static final void save() {
if (!edited)
return;
SerializableFilesManager.saveGEOffers(OFFERS);
SerializableFilesManager.saveGEHistory(OFFERS_TRACK);
edited = false;
}

public static void linkOffers(Player player) {
boolean itemsWaiting = false;
for (int slot = 0; slot < player.getGEManager().getOfferUIds().length; slot++) {
Offer offer = getOffer(player, slot);
if (offer == null)
continue;
offer.link(slot, player);
offer.update();
if (!itemsWaiting && offer.hasItemsWaiting()) {
itemsWaiting = true;
player.getPackets().sendGameMessage("You have items from the Grand Exchange waiting in your collection box.");
}
}
}

public static Offer getOffer(Player player, int slot) {
synchronized (LOCK) {
long uid = player.getGEManager().getOfferUIds()[slot];
if (uid == 0)
return null;
Offer offer = OFFERS.get(uid);
if (offer == null) {
player.getGEManager().getOfferUIds()[slot] = 0; // offer
// disapeared
// within time
return null;
}
return offer;
}

}

public static void sendOffer(Player player, int slot, int itemId, int amount, int price, boolean buy) {
synchronized (LOCK) {
Offer offer = new Offer(itemId, amount, price, buy);
player.getGEManager().getOfferUIds()[slot] = createOffer(offer);
offer.link(slot, player);
findBuyerSeller(offer);
}
}

public static void abortOffer(Player player, int slot) {
synchronized (LOCK) {
Offer offer = getOffer(player, slot);
if (offer == null)
return;
edited = true;
if (offer.cancel() && offer.forceRemove())
deleteOffer(player, slot); // shouldnt here happen anyway
}
}

public static void collectItems(Player player, int slot, int invSlot, int option) {
synchronized (LOCK) {
Offer offer = getOffer(player, slot);
if (offer == null)
return;
edited = true;
if (offer.collectItems(invSlot, option) && offer.forceRemove()) {
deleteOffer(player, slot); // should happen after none left and
// offer completed
if (offer.getTotalAmmountSoFar() != 0) {
OfferHistory o = new OfferHistory(offer.getId(), offer.getTotalAmmountSoFar(), offer.getTotalPriceSoFar(), offer.isBuying());
OFFERS_TRACK.add(o);
player.getGEManager().addOfferHistory(o);
}
}
}
}

private static void deleteOffer(Player player, int slot) {
player.getGEManager().cancelOffer(); // sends back to original screen if
// seeing an offer
OFFERS.remove(player.getGEManager().getOfferUIds()[slot]);
player.getGEManager().getOfferUIds()[slot] = 0;
}

private static void findBuyerSeller(Offer offer) {
while (!offer.isCompleted()) {
Offer bestOffer = null;
for (Offer o : OFFERS.values()) {
if (o.isBuying() == offer.isBuying() || o.getId() != offer.getId() || o.isCompleted() || (offer.isBuying() && o.getPrice() > offer.getPrice()) || (!offer.isBuying() && o.getPrice() < offer.getPrice()) || offer.isOfferTooHigh(o))
continue;
if (bestOffer == null || (offer.isBuying() && o.getPrice() < bestOffer.getPrice()) || (!offer.isBuying() && o.getPrice() > bestOffer.getPrice()))
bestOffer = o;
}
if (bestOffer == null)
break;
offer.updateOffer(bestOffer);
}
offer.update();
}

private static long createOffer(Offer offer) {
edited = true;
long uid = getUId();
OFFERS.put(uid, offer);
return uid;
}

private static long getUId() {
while (true) {
long uid = Utils.RANDOM.nextLong();
if (OFFERS.containsKey(uid))
continue;
return uid;
}
}

public static int getPrice(int itemId) {
ItemDefinitions defs = ItemDefinitions.getItemDefinitions(itemId);
if (defs.isNoted())
itemId = defs.getCertId();
else if (defs.isLended())
itemId = defs.getLendId();
Integer price = PRICES.get(itemId);
return price != null && price != 0 ? price : EconomyPrices.getPrice(itemId);
}

// in order not to keep player saved on memory in offers after player leaves
// <.<
public static void unlinkOffers(Player player) {
for (int slot = 0; slot < player.getGEManager().getOfferUIds().length; slot++) {
Offer offer = getOffer(player, slot);
if (offer == null)
continue;
offer.unlink();
}
}


public static List<OfferHistory> getHistory() {
return OFFERS_TRACK;
}

public static void load() {
// TODO Auto-generated method stub

}
}
[/CODE]
[B]
HISTORY.JAVA
[/B]
[CODE]package com.ew.game.player.content.grandExchange;

import java.io.Serializable;

public class OfferHistory implements Serializable {

private static final long serialVersionUID = 7322642705393018764L;

private int id, quantity, price;
private boolean bought;

public OfferHistory(int id, int quantity, int price, boolean bought) {
this.id = id;
this.quantity = quantity;
this.price = price;
this.bought = bought;
}

public int getId() {
return id;
}

public int getQuantity() {
return quantity;
}

public int getPrice() {
return price;
}

public boolean isBought() {
return bought;
}
}
[/CODE] Edited by accerado

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0
bump.

[COLOR="silver"]- - - Updated - - -[/COLOR]

bump.

Share this post


Link to post
Share on other sites
  • 0
[quote name='Grish']Hey add my skype skillerlvl[/QUOTE]

Theres thing called private messaging. Thanks.

Share this post


Link to post
Share on other sites
  • 0
Need this
Bump

Share this post


Link to post
Share on other sites

×