Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazaar Embargo #924

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ data class BazaarItem(
var price: Double,
var stock: Int,
var lastUpdated: Date,
var balance: Double
var balance: Double,
var embargoed: Boolean = false
) : DbObject {
companion object : OidDbObjectCompanion<BazaarItem>(BazaarItem::class, setup = {
ensureIndex(BazaarItem::cityTerritory)
Expand All @@ -55,7 +56,7 @@ data class BazaarItem(
}

val id = objId<BazaarItem>()
val item = BazaarItem(id, cityTerritory, seller, itemString, price, 0, Date.from(Instant.now()), 0.0)
val item = BazaarItem(id, cityTerritory, seller, itemString, price, 0, Date.from(Instant.now()), 0.0, false)
col.insertOne(sess, item)
return@trx id
}
Expand Down Expand Up @@ -108,6 +109,10 @@ data class BazaarItem(
return@trx total
}

fun embargo(itemId: Oid<BazaarItem>, embargoed: Boolean = true): Unit = trx { sess ->
col.updateOneById(sess, itemId, org.litote.kmongo.setValue(BazaarItem::embargoed, embargoed))
}

fun replaceLegacyMinerals(): Unit = trx { sess ->
val replacements = mutableMapOf(
"aluminum" to "ALUMINUM_INGOT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import net.kyori.adventure.text.format.NamedTextColor.DARK_PURPLE
import net.kyori.adventure.text.format.NamedTextColor.GRAY
import net.kyori.adventure.text.format.NamedTextColor.LIGHT_PURPLE
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.Bukkit
import org.bukkit.DyeColor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
Expand Down Expand Up @@ -233,6 +234,31 @@ object BazaarCommand : SLCommand() {
)
}

@Suppress("Unused")
@Subcommand("embargo")
@Description("Prevent a player's bazaar listing from appearing in this trade city")
@CommandCompletion("@nothing @bazaarItemStrings")
fun onEmbargo(sender: Player, target: String, itemString: String, @Optional embargo: Boolean = true) = asyncCommand(sender) {
val territory = requireTerritoryIn(sender)
val settlement = territory.settlement ?: fail { "You are not in a trade city" }
requireSettlementLeader(sender, settlement)

val cityName = cityName(territory)

val targetPlayer = Bukkit.getPlayer(resolveOfflinePlayer(target))!! // resolveOfflinePlayer will exit this function prematurely if UUID is not found
val item: BazaarItem = requireSelling(territory, targetPlayer, itemString)

BazaarItem.embargo(item._id, embargo)

Tasks.sync {
if (embargo) {
sender.success("Embargoed ${targetPlayer.name}'s $itemString at $cityName")
} else {
sender.success("Lifted embargo on ${targetPlayer.name}'s $itemString at $cityName")
}
}
}

@Suppress("Unused")
@Subcommand("setprice")
@Description("Update the price of the specific item")
Expand Down Expand Up @@ -278,14 +304,15 @@ object BazaarCommand : SLCommand() {
val stock = item.stock
val uncollected = item.balance.toCreditComponent()
val price = item.price.toCreditComponent()
val embargoed = item.embargoed

totalBalance += item.balance

ofChildren(
itemDisplayName,
text(" @ ", DARK_PURPLE),
text(city, LIGHT_PURPLE),
bracketed(template(text("stock: {0}, balance: {1}, price: {2}", GRAY), stock, uncollected, price))
bracketed(template(text("stock: {0}, balance: {1}, price: {2}, embargoed: {3}", GRAY), stock, uncollected, price, embargoed))
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ object Bazaars : IonServerComponent() {
}

private fun getCityItems(territoryId: Oid<Territory>): FindIterable<BazaarItem> = BazaarItem
.find(and(BazaarItem::cityTerritory eq territoryId, BazaarItem::stock gt 0))
.find(and(BazaarItem::cityTerritory eq territoryId, BazaarItem::stock gt 0, BazaarItem::embargoed ne true))

enum class SortingBy(val property: KProperty<*>, val displayType: Material) {
PRICE(BazaarItem::price, Material.GOLD_INGOT),
Expand All @@ -151,7 +151,7 @@ object Bazaars : IonServerComponent() {
} + guiButton(Material.IRON_DOOR) { openMainMenu(terrId, playerClicker, remote) }.setName("Go Back")

val items: List<GuiItem> = BazaarItem
.find(and(BazaarItem::cityTerritory eq terrId, BazaarItem::itemString eq item, BazaarItem::stock gt 0))
.find(and(BazaarItem::cityTerritory eq terrId, BazaarItem::itemString eq item, BazaarItem::stock gt 0, BazaarItem::embargoed ne true))
.let { if (descend) it.descendingSort(sort.property) else it.ascendingSort(sort.property) }
.map { bazaarItem ->
val itemStack = fromItemString(bazaarItem.itemString)
Expand Down
Loading