public boolean marketBottom(CommandSender sender) { // gather the list of commodities List<Commodities> commodities = plugin.getDatabase().find(Commodities.class).findList(); // sort 'em and return only the top 10 List<Commodities> top10 = plugin .getDatabase() .filter(Commodities.class) .sort("value asc") .maxRows(10) .filter(commodities); // calculate elasticity List<Integer> elasticities = new LinkedList<Integer>(); for (int index = 0; index < top10.size(); index++) { Commodities c = top10.get(index); double value = c.getValue(); double changeRate = c.getChangeRate(); double minValue = c.getMinValue(); if (value < minValue) { double newValue = Math.abs(value - minValue); double elasticity = (newValue / changeRate); int el = (int) Math.round(elasticity); elasticities.add(index, el); c.setValue(minValue); } else { elasticities.add(index, 0); } } // Send them to the player for (int x = 0; x < top10.size(); x++) { int rank = x + 1; sender.sendMessage( ChatColor.GREEN + String.valueOf(rank) + ". " + ChatColor.WHITE + top10.get(x).getName() + " " + ChatColor.GRAY + top10.get(x).getValue() + " " + ChatColor.DARK_GREEN + elasticities.get(x)); } return true; }
/** * Determine the cost of a given number of an item and calculate a new value for the item * accordingly. * * @param oper 1 for buying, 0 for selling. * @param commodity the commodity in question * @param amount the desired amount of the item in question * @return the total cost and the calculated new value as an Invoice */ public Invoice generateInvoice(int oper, Commodities commodity, int amount) { // get the initial value of the item, 0 for not found Invoice inv = new Invoice(0.0, 0.0); inv.value = commodity.getValue(); // get the spread so we can do one initial decrement of the value if we are selling BigDecimal spread = BigDecimal.valueOf(commodity.getSpread()); // determine the total cost inv.total = 0.0; for (int x = 1; x <= amount; x++) { BigDecimal minValue = BigDecimal.valueOf(commodity.getMinValue()); BigDecimal maxValue = BigDecimal.valueOf(commodity.getMaxValue()); BigDecimal changeRate = BigDecimal.valueOf(commodity.getChangeRate()); // work the spread on the first one. if ((oper == 0) && (x == 1)) { inv.subtractValue(spread.doubleValue()); } else if ((oper == 0) && (x > 1)) { // otherwise, do the usual decriment. inv.subtractValue(changeRate.doubleValue()); } // check the current value if (inv.getValue() >= minValue.doubleValue()) { // current value is @ or above minValue // be sure value is not above maxValue if (inv.getValue() < maxValue.doubleValue()) { // current value is "just right" inv.addTotal(inv.getValue()); // add current value to total } else { // current value is above the max inv.addTotal(maxValue.doubleValue()); // add maxValue to total } } else { // current value is below the minimum inv.addTotal(minValue.doubleValue()); // add the minimum to total if ((oper == 0) && (x == 1)) { inv.subtractTotal( spread .doubleValue()); // subtract the spread if we're selling and this is the first run } } // Change our stored value for the item // we don't care about min/maxValue here because we don't want the value to 'bounce' off of // them. if (oper == 1) { inv.addValue(changeRate.doubleValue()); } } return inv; }