use of com.skelril.skree.service.MarketService in project Skree by Skelril.
the class MarketVerifyCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
if (!optService.isPresent()) {
src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
return CommandResult.empty();
}
MarketService service = optService.get();
Task.builder().async().execute(() -> {
PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
List<Clause<String, BigDecimal>> profitMargins = new ArrayList<>();
for (IRecipe recipe : CraftingManager.getInstance().getRecipeList()) {
ItemStack output = recipe.getRecipeOutput();
if (output == null) {
continue;
}
Optional<BigDecimal> optResultPrice = service.getPrice(tf(output));
if (!optResultPrice.isPresent()) {
continue;
}
String name = service.getAlias(tf(output)).orElse(output.getItem().getRegistryName().toString());
Collection<ItemStack> items = new ArrayList<>();
if (recipe instanceof ShapedRecipes) {
items.addAll(Lists.newArrayList(((ShapedRecipes) recipe).recipeItems));
} else if (recipe instanceof ShapelessRecipes) {
items.addAll(((ShapelessRecipes) recipe).recipeItems);
} else {
src.sendMessage(Text.of(TextColors.RED, "Unsupported recipe for " + name));
continue;
}
items.removeAll(Collections.singleton(null));
BigDecimal creationCost = BigDecimal.ZERO;
try {
for (ItemStack stack : items) {
creationCost = creationCost.add(service.getPrice(tf(stack)).orElse(BigDecimal.ZERO));
}
} catch (Exception ex) {
src.sendMessage(Text.of(TextColors.RED, "Couldn't complete checks for " + name));
continue;
}
if (creationCost.equals(BigDecimal.ZERO)) {
src.sendMessage(Text.of(TextColors.RED, "No ingredients found on market for " + name));
continue;
}
BigDecimal sellPrice = optResultPrice.get();
sellPrice = sellPrice.multiply(service.getSellFactor(sellPrice));
profitMargins.add(new Clause<>(name, sellPrice.subtract(creationCost)));
}
List<Text> result = profitMargins.stream().sorted((a, b) -> b.getValue().subtract(a.getValue()).intValue()).map(a -> {
boolean profitable = a.getValue().compareTo(BigDecimal.ZERO) >= 0;
return Text.of(profitable ? TextColors.RED : TextColors.GREEN, a.getKey().toUpperCase(), " has a profit margin of ", profitable ? "+" : "", MarketImplUtil.format(a.getValue()));
}).collect(Collectors.toList());
pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Profit Margin Report")).padding(Text.of(" ")).sendTo(src);
}).submit(SkreePlugin.inst());
src.sendMessage(Text.of(TextColors.YELLOW, "Verification in progress..."));
return CommandResult.success();
}
use of com.skelril.skree.service.MarketService in project Skree by Skelril.
the class MarketSimulateCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
if (!optService.isPresent()) {
src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
return CommandResult.empty();
}
MarketService service = optService.get();
src.sendMessage(Text.of(TextColors.YELLOW, "Starting market update..."));
service.updatePrices();
src.sendMessage(Text.of(TextColors.YELLOW, "Update completed!"));
return CommandResult.success();
}
use of com.skelril.skree.service.MarketService in project Skree by Skelril.
the class MarketTrackItemCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("You must be a player to use this command!"));
return CommandResult.empty();
}
Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
if (!optService.isPresent()) {
src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
return CommandResult.empty();
}
MarketService service = optService.get();
Optional<ItemStack> held = ((Player) src).getItemInHand(HandTypes.MAIN_HAND);
if (!held.isPresent()) {
src.sendMessage(Text.of(TextColors.RED, "You are not holding an item."));
return CommandResult.empty();
}
ItemStack item = held.get();
if (service.addItem(item)) {
src.sendMessage(Text.of(TextColors.YELLOW, "Your held item is now being tracked."));
} else {
src.sendMessage(Text.of(TextColors.RED, "Your held item is already tracked."));
return CommandResult.empty();
}
return CommandResult.success();
}
use of com.skelril.skree.service.MarketService in project Skree by Skelril.
the class MarketBuyCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("You must be a player to use this command!"));
return CommandResult.empty();
}
Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
if (!optService.isPresent()) {
src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
return CommandResult.empty();
}
MarketService service = optService.get();
Player player = (Player) src;
if (!canBuyOrSell(player)) {
player.sendMessage(Text.of(TextColors.RED, "You cannot buy or sell from this area."));
return CommandResult.empty();
}
String itemName = args.<String>getOne("item").get();
List<String> targetItems;
if (itemName.endsWith("#armor")) {
String armorType = itemName.replace("#armor", " ");
targetItems = Lists.newArrayList(armorType + "helmet", armorType + "chestplate", armorType + "leggings", armorType + "boots");
} else {
targetItems = Lists.newArrayList(itemName);
}
BigDecimal price = BigDecimal.ZERO;
for (String anItem : targetItems) {
Optional<BigDecimal> optPrice = service.getPrice(anItem);
if (!optPrice.isPresent()) {
src.sendMessage(Text.of(TextColors.RED, "That item is not available for purchase."));
return CommandResult.empty();
}
price = price.add(optPrice.get());
}
Optional<Integer> optAmt = args.getOne("amount");
int amt = Math.max(1, optAmt.isPresent() ? optAmt.get() : 0);
price = price.multiply(BigDecimal.valueOf(amt));
BigDecimal funds = MarketImplUtil.getMoney(player);
BigDecimal newBalance = funds.subtract(price);
if (newBalance.compareTo(BigDecimal.ZERO) < 0) {
BigDecimal neededAmt = newBalance.multiply(BigDecimal.valueOf(-1));
src.sendMessage(Text.of(TextColors.RED, "You do not have enough money to purchase that item(s)."));
src.sendMessage(Text.of(TextColors.RED, "You need an additional ", TextColors.WHITE, format(neededAmt), TextColors.RED, "."));
return CommandResult.empty();
}
List<Clause<String, Integer>> newStocks = new ArrayList<>();
for (String anItem : targetItems) {
Optional<Integer> optStock = service.getStock(anItem);
if (optStock.orElse(0) < amt) {
src.sendMessage(Text.of(TextColors.RED, "There is not enough stock to satisfy your order."));
return CommandResult.empty();
}
newStocks.add(new Clause<>(anItem, optStock.get() - amt));
}
// Accumulate items
List<ItemStack> itemStacks = new ArrayList<>(targetItems.size());
for (String anItem : targetItems) {
Optional<ItemStack> stack = service.getItem(anItem);
if (!stack.isPresent()) {
// TODO Auto reporting
src.sendMessage(Text.of(TextColors.RED, "An item stack could not be resolved, please report this!"));
return CommandResult.empty();
}
int total = amt;
while (total > 0) {
int increment = Math.min(total, stack.get().getMaxStackQuantity());
total -= increment;
itemStacks.add(newItemStack(stack.get(), increment));
}
}
// Alright, all items have been found
if (!MarketImplUtil.setBalanceTo(player, newBalance, Cause.source(SkreePlugin.container()).build())) {
// TODO Auto reporting
src.sendMessage(Text.of(TextColors.RED, "Failed to adjust your balance, please report this!"));
return CommandResult.empty();
}
Clause<Boolean, List<Clause<ItemStack, Integer>>> transactions = MarketImplUtil.giveItems(player, itemStacks, Cause.source(SkreePlugin.container()).build());
if (!transactions.getKey()) {
// TODO Auto reporting
src.sendMessage(Text.of(TextColors.RED, "Failed to give all items, please report this!"));
return CommandResult.empty();
}
// Items have been given process stocks
for (Clause<String, Integer> stock : newStocks) {
service.setStock(stock.getKey(), stock.getValue());
}
if (!service.logTransactionByStack(player.getUniqueId(), transactions.getValue())) {
// TODO Auto reporting
// Not critical, continue
src.sendMessage(Text.of(TextColors.RED, "Failed to log transactions, please report this!"));
}
player.sendMessage(Text.of(TextColors.YELLOW, "Item(s) purchased for ", TextColors.WHITE, format(price), TextColors.YELLOW, "!"));
return CommandResult.success();
}
use of com.skelril.skree.service.MarketService in project Skree by Skelril.
the class MarketAddAliasCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("You must be a player to use this command!"));
return CommandResult.empty();
}
Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
if (!optService.isPresent()) {
src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
return CommandResult.empty();
}
MarketService service = optService.get();
Optional<ItemStack> held = ((Player) src).getItemInHand(HandTypes.MAIN_HAND);
if (!held.isPresent()) {
src.sendMessage(Text.of(TextColors.RED, "You are not holding an item."));
return CommandResult.empty();
}
ItemStack item = held.get();
String alias = args.<String>getOne("alias").get();
if (service.addAlias(alias, item)) {
src.sendMessage(Text.of(TextColors.YELLOW, alias + " added to the market."));
} else {
src.sendMessage(Text.of(TextColors.RED, "Your held item is not currently tracked, or the alias is already in use."));
return CommandResult.empty();
}
return CommandResult.success();
}
Aggregations