Search in sources :

Example 6 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class MarketQuickAddCommand 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();
    BigDecimal price;
    try {
        price = new BigDecimal(args.<String>getOne("price").get());
    } catch (NumberFormatException ex) {
        src.sendMessage(Text.of(TextColors.RED, "Invalid price specified"));
        return CommandResult.empty();
    }
    if (service.addItem(item)) {
        if (service.addAlias(alias, item)) {
            if (service.setBasePrice(alias, price)) {
                if (service.setPrimaryAlias(alias)) {
                    src.sendMessage(Text.of(TextColors.YELLOW, alias + " added to the market with a price of " + format(price)));
                    return CommandResult.success();
                }
            // Same error, fall through
            }
            src.sendMessage(Text.of(TextColors.RED, alias + " is not a valid alias"));
            return CommandResult.empty();
        }
        src.sendMessage(Text.of(TextColors.RED, "Your held item is not currently tracked, or the alias is already in use."));
        return CommandResult.empty();
    }
    src.sendMessage(Text.of(TextColors.RED, "Your held item is already tracked."));
    return CommandResult.empty();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ItemStack(org.spongepowered.api.item.inventory.ItemStack) BigDecimal(java.math.BigDecimal) MarketService(com.skelril.skree.service.MarketService)

Example 7 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class MarketRemoveAliasCommand 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();
    String alias = args.<String>getOne("alias").get();
    if (service.remAlias(alias)) {
        src.sendMessage(Text.of(TextColors.YELLOW, alias + " removed from the market."));
    } else {
        src.sendMessage(Text.of(TextColors.RED, "The alias was not in the market, and could not be removed."));
        return CommandResult.empty();
    }
    return CommandResult.success();
}
Also used : MarketService(com.skelril.skree.service.MarketService)

Example 8 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class MarketSellCommand 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.DARK_RED, "You cannot buy or sell from this area."));
        return CommandResult.empty();
    }
    Optional<ItemStack> filter = Optional.empty();
    QueryMode mode = QueryMode.HELD;
    if (args.hasAny("a")) {
        mode = QueryMode.EVERYTHING;
    } else if (args.hasAny("h")) {
        mode = QueryMode.HOT_BAR;
    }
    if (mode != QueryMode.HELD && !args.hasAny("u")) {
        filter = player.getItemInHand(HandTypes.MAIN_HAND);
        if (!filter.isPresent()) {
            src.sendMessage(Text.of(TextColors.RED, "You're not holding an item to filter with!"));
            return CommandResult.empty();
        }
    }
    Clause<BigDecimal, List<Integer>> changes = MarketImplUtil.getChanges(player, service, mode, filter.orElse(null));
    if (changes.getValue().isEmpty()) {
        src.sendMessage(Text.of(TextColors.RED, "No sellable items found" + (filter.isPresent() ? " that matched the filter" : "") + "!"));
        return CommandResult.empty();
    }
    List<Clause<ItemStack, Integer>> transactions = MarketImplUtil.removeAtPos(player, changes.getValue());
    Task.builder().execute(() -> {
        if (!service.logTransactionByStack(player.getUniqueId(), transactions)) {
            // TODO Auto reporting
            // Not critical, continue
            src.sendMessage(Text.of(TextColors.DARK_RED, "Failed to log transactions, please report this!"));
        }
        // Process new stocks
        for (Clause<ItemStack, Integer> transaction : transactions) {
            service.setStock(transaction.getKey(), service.getStock(transaction.getKey()).orElse(0) + -(transaction.getValue()));
        }
    }).async().submit(SkreePlugin.inst());
    BigDecimal newBalance = changes.getKey().add(MarketImplUtil.getMoney(player));
    if (!MarketImplUtil.setBalanceTo(player, newBalance, Cause.source(SkreePlugin.container()).build())) {
        // TODO Auto reporting
        src.sendMessage(Text.of(TextColors.DARK_RED, "Failed to adjust your balance, please report this!"));
        return CommandResult.empty();
    }
    player.sendMessage(Text.of(TextColors.YELLOW, "Item(s) sold for: ", TextColors.WHITE, MarketImplUtil.format(changes.getKey()), TextColors.YELLOW, "!"));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) BigDecimal(java.math.BigDecimal) QueryMode(com.skelril.skree.content.market.MarketImplUtil.QueryMode) List(java.util.List) Clause(com.skelril.nitro.Clause) ItemStack(org.spongepowered.api.item.inventory.ItemStack) MarketService(com.skelril.skree.service.MarketService)

Example 9 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class GoldRushInstance method payPlayer.

public boolean payPlayer(Player player) {
    BigDecimal fee = cofferRisk.get(player.getUniqueId());
    // They didn't pay, CHEATER!!!
    if (fee == null) {
        return false;
    }
    NonNullList<net.minecraft.item.ItemStack> itemStacks = tf(player).inventory.mainInventory;
    BigDecimal goldValue = BigDecimal.ZERO;
    BigDecimal itemValue = BigDecimal.ZERO;
    Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
    List<ItemStack> returned = new ArrayList<>();
    for (int i = 0; i < itemStacks.size(); ++i) {
        net.minecraft.item.ItemStack is = itemStacks.get(i);
        if (is == net.minecraft.item.ItemStack.EMPTY || is.getItem() != CustomItemTypes.PRIZE_BOX) {
            continue;
        }
        Optional<ItemStack> optOpened = PrizeBox.getPrizeStack(is);
        if (optService.isPresent() && optOpened.isPresent()) {
            MarketService service = optService.get();
            ItemStack opened = optOpened.get();
            Optional<BigDecimal> value = service.getPrice(opened);
            if (value.isPresent()) {
                BigDecimal quantity = new BigDecimal(opened.getQuantity());
                if (opened.getItem() == ItemTypes.GOLD_NUGGET || opened.getItem() == ItemTypes.GOLD_INGOT || opened.getItem() == BlockTypes.GOLD_BLOCK.getItem().get()) {
                    goldValue = goldValue.add(value.get().multiply(quantity));
                } else {
                    itemValue = itemValue.add(value.get().multiply(quantity));
                    returned.add(opened);
                }
            }
        }
        itemStacks.set(i, net.minecraft.item.ItemStack.EMPTY);
    }
    // Get the original grab amount (The Sum of Gold & Items)
    BigDecimal originalGrabbed = goldValue.add(itemValue);
    // Create a penalty value if the player was in a flood  (Time Taken / Time Per Penalty) * Penalty Increment Value
    // for instance (6000 milliseconds after / 3000 millisecond penality increment) * 9 coffer increment value
    // would result in 18 coffers being the penalty value
    BigDecimal penaltyValue = new BigDecimal(getTimeTakenAfterFlood()).divide(PENALTY_INCREMENT, 2, RoundingMode.UP).multiply(PENALTY_INCREMENT_VALUE);
    // Total grabbed is the original grab amount - the penalty value
    BigDecimal totalGrabbed = originalGrabbed.subtract(penaltyValue);
    // The ratio they would have recieved with no time penalty
    // Calculated as the multiplier * (Grab Ratio * (Value of stuff (grab amount) / Pivotal value (target amount)))
    BigDecimal originalGrabRatio = multiplier.multiply(GRAB_RATIO.multiply(originalGrabbed.divide(PIVOTAL_VALUE, 2, RoundingMode.DOWN)));
    // The same calculation as the original grab ratio, just using the time penalty modified grab amount
    BigDecimal grabRatio = multiplier.multiply(GRAB_RATIO.multiply(totalGrabbed.divide(PIVOTAL_VALUE, 2, RoundingMode.DOWN)));
    // The penalty ratio is the percentage value loss from the original grab ratio
    BigDecimal penaltyRatio = originalGrabRatio.subtract(grabRatio);
    // The loot split times the group modifier
    BigDecimal multipliedLootSplit = multiplier.multiply(lootSplit);
    // The amount of money they gain from the their boosted risk
    BigDecimal splitBoost = multipliedLootSplit.multiply(grabRatio);
    // The total amount of money they get, being the loot split + their boosted risk value
    // minus item value
    BigDecimal personalLootSplit = multipliedLootSplit.add(splitBoost);
    player.sendMessage(Text.of(TextColors.YELLOW, "You obtain: "));
    player.sendMessage(Text.of(TextColors.YELLOW, " - Bail: ", format(fee)));
    player.sendMessage(Text.of(TextColors.YELLOW, " - Split: ", format(multipliedLootSplit), ", Multiplied by: ", format(multiplier), "x, Boosted by: ", format(grabRatio.multiply(new BigDecimal(100))), "%"));
    if (penaltyRatio.compareTo(BigDecimal.ZERO) != 0) {
        player.sendMessage(Text.of(TextColors.YELLOW, "   - Boost time penalty: ", format(penaltyRatio.multiply(new BigDecimal(100))), "%"));
    }
    if (grabRatio.compareTo(BigDecimal.ZERO) != 0) {
        player.sendMessage(Text.of(TextColors.YELLOW, "   - Boost value: ", format(splitBoost)));
    }
    BigDecimal total = fee.add(personalLootSplit);
    player.sendMessage(Text.of(TextColors.YELLOW, "Total: ", format(total)));
    // Give the player their items
    Optional<PlayerStateService> optInvService = Sponge.getServiceManager().provide(PlayerStateService.class);
    if (optInvService.isPresent()) {
        PlayerStateService invService = optInvService.get();
        invService.loadInventoryIfStored(player);
    }
    returned.forEach(i -> player.getInventory().offer(i));
    MarketImplUtil.setBalanceTo(player, total.add(MarketImplUtil.getMoney(player)), Cause.source(this).build());
    Optional<HighScoreService> optHighScores = Sponge.getServiceManager().provide(HighScoreService.class);
    optHighScores.ifPresent(highScoreService -> highScoreService.update(player, ScoreTypes.GOLD_RUSH_ROBBERIES, 1));
    optHighScores.ifPresent(highScoreService -> highScoreService.update(player, ScoreTypes.GOLD_RUSH_LOOT_VALUE, total.toBigInteger().intValue()));
    remove(player);
    return true;
}
Also used : BigDecimal(java.math.BigDecimal) HighScoreService(com.skelril.skree.service.HighScoreService) PlayerStateService(com.skelril.skree.service.PlayerStateService) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemStackFactory.newItemStack(com.skelril.nitro.item.ItemStackFactory.newItemStack) MarketService(com.skelril.skree.service.MarketService)

Example 10 with MarketService

use of com.skelril.skree.service.MarketService in project Skree by Skelril.

the class MarketListCommand 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();
    PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    Optional<String> optFilter = args.getOne("name");
    String filter = optFilter.isPresent() ? optFilter.get() : "";
    if (!filter.matches(MarketService.VALID_ALIAS_REGEX)) {
        src.sendMessage(Text.of(TextColors.RED, "Invalid filter supplied."));
        return CommandResult.empty();
    }
    List<ItemDescriptor> prices = filter.isEmpty() ? service.getPrices() : service.getPrices(filter + "%");
    if (prices.isEmpty()) {
        src.sendMessage(Text.of(TextColors.YELLOW, "No items matched."));
        return CommandResult.success();
    }
    List<Text> result = prices.stream().filter(a -> filter.isEmpty() || a.getName().startsWith(filter)).sorted(Comparator.comparing(ItemDescriptor::getCurrentValue)).map(a -> createLine(a, service)).collect(Collectors.toList());
    pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Item List")).padding(Text.of(" ")).sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) CommandSource(org.spongepowered.api.command.CommandSource) GenericArguments.optional(org.spongepowered.api.command.args.GenericArguments.optional) Sponge(org.spongepowered.api.Sponge) MarketImplUtil.format(com.skelril.skree.content.market.MarketImplUtil.format) GenericArguments.remainingJoinedStrings(org.spongepowered.api.command.args.GenericArguments.remainingJoinedStrings) MarketService(com.skelril.skree.service.MarketService) PaginationService(org.spongepowered.api.service.pagination.PaginationService) ItemDescriptor(com.skelril.skree.service.internal.market.ItemDescriptor) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) BigDecimal(java.math.BigDecimal) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Optional(java.util.Optional) Comparator(java.util.Comparator) TextColors(org.spongepowered.api.text.format.TextColors) ItemDescriptor(com.skelril.skree.service.internal.market.ItemDescriptor) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService) MarketService(com.skelril.skree.service.MarketService)

Aggregations

MarketService (com.skelril.skree.service.MarketService)14 ItemStack (org.spongepowered.api.item.inventory.ItemStack)9 BigDecimal (java.math.BigDecimal)8 Player (org.spongepowered.api.entity.living.player.Player)8 Clause (com.skelril.nitro.Clause)3 List (java.util.List)3 Text (org.spongepowered.api.text.Text)3 ItemStackFactory.newItemStack (com.skelril.nitro.item.ItemStackFactory.newItemStack)2 ArrayList (java.util.ArrayList)2 Collectors (java.util.stream.Collectors)2 Sponge (org.spongepowered.api.Sponge)2 CommandException (org.spongepowered.api.command.CommandException)2 CommandResult (org.spongepowered.api.command.CommandResult)2 CommandSource (org.spongepowered.api.command.CommandSource)2 CommandContext (org.spongepowered.api.command.args.CommandContext)2 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)2 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)2 PaginationService (org.spongepowered.api.service.pagination.PaginationService)2 TextColors (org.spongepowered.api.text.format.TextColors)2 Lists (com.google.common.collect.Lists)1