Skip to content

Instantly share code, notes, and snippets.

@its-jackson
Created August 9, 2023 01:10
Show Gist options
  • Save its-jackson/0e2f255a54f7c6550faec1e1e4de272f to your computer and use it in GitHub Desktop.
Save its-jackson/0e2f255a54f7c6550faec1e1e4de272f to your computer and use it in GitHub Desktop.
package scripts.kotlin.api.framework.routine.api
import scripts.kotlin.api.DepositBoxAction
class DepositBoxManager(private val depositBoxAction: DepositBoxAction) {
private val logger = Logger("Deposit Box Manager")
fun depositAllItems(vararg items: Int): Boolean {
return depositAllItems(*items)
}
fun depositAllItems(vararg items: String): Boolean {
return depositAllItems(*items)
}
private fun depositAllItems(vararg items: Any): Boolean {
if (!depositBoxAction.ensureOpen()) {
logger.debug("Failed while opening deposit box")
return false
}
else {
logger.debug("Opened deposit box")
}
val success = when (items.firstOrNull()) {
is Int -> depositBoxAction.depositAll(*items.filterIsInstance<Int>().toIntArray())
is String -> depositBoxAction.depositAll(*items.filterIsInstance<String>().toTypedArray())
else -> false
}
if (!success) {
logger.debug("Failed while depositing inventory items")
return false
}
else {
logger.debug("Deposited inventory items")
}
logger.debug("Closing deposit box")
return depositBoxAction.close()
}
fun depositItem(item: String, amount: Int): Boolean {
return depositItem(item as Any, amount)
}
fun depositItem(item: Int, amount: Int): Boolean {
return depositItem(item as Any, amount)
}
private fun depositItem(item: Any, amount: Int): Boolean {
if (!depositBoxAction.ensureOpen()) {
logger.debug("Failed while opening deposit box")
return false
}
else {
logger.debug("Opened deposit box")
}
val success = when (item) {
is Int -> depositBoxAction.deposit(item, amount)
is String -> depositBoxAction.deposit(item, amount)
else -> false
}
if (!success) {
logger.debug("Failed while depositing inventory item: $item x $amount")
return false
}
else {
logger.debug("Deposited inventory item: $item x $amount")
}
logger.debug("Closing deposit box")
return depositBoxAction.close()
}
fun depositInventory(): Boolean {
if (!depositBoxAction.ensureOpen()) {
logger.debug("Failed while opening deposit box")
return false
}
else {
logger.debug("Opened deposit box")
}
if (!depositBoxAction.depositInventory()) {
logger.debug("Failed while depositing all inventory items")
return false
}
else {
logger.debug("Deposited all inventory items")
}
logger.debug("Closing deposit box")
return depositBoxAction.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment