Created
April 15, 2019 13:10
-
-
Save aeshthetic/a2ad7880aaf34729e15419147aa90932 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cf.striking.strikonomy | |
import cf.striking.strikonomy.Main.Companion.db | |
import cf.striking.strikonomy.Main.Companion.log | |
import org.bukkit.Material | |
import org.bukkit.entity.EntityType | |
import org.bukkit.entity.Player | |
import org.bukkit.event.EventHandler | |
import org.bukkit.event.Listener | |
import org.bukkit.event.block.BlockBreakEvent | |
import org.bukkit.event.entity.EntityDeathEvent | |
import java.sql.SQLException | |
class GrowthListener : Listener { | |
val resourceTypes = mapOf( | |
Pair("crops", listOf( | |
Material.WHEAT, Material.POTATO, Material.CARROT, Material.SUGAR_CANE_BLOCK, Material.PUMPKIN, | |
Material.MELON_BLOCK, Material.CACTUS | |
)), | |
Pair("blocks",listOf( | |
Material.DIAMOND_ORE, Material.GOLD_ORE, Material.STONE, Material.IRON_ORE, Material.REDSTONE_ORE, | |
Material.GLOWING_REDSTONE_ORE, Material.QUARTZ_ORE, Material.LAPIS_ORE, Material.EMERALD_ORE, | |
Material.COAL_ORE, Material.SAND, Material.GRAVEL, Material.OBSIDIAN, Material.CLAY, Material.LOG, Material.LOG_2, Material.DIRT, Material.SOIL, Material.GRASS, Material.LONG_GRASS, | |
Material.GRASS_PATH, Material.DOUBLE_PLANT, Material.YELLOW_FLOWER, Material.RED_ROSE | |
)), | |
Pair("hostile_mobs", listOf( | |
EntityType.WITHER, EntityType.WITHER_SKELETON, EntityType.CAVE_SPIDER, EntityType.BLAZE, | |
EntityType.CREEPER, EntityType.ELDER_GUARDIAN, EntityType.ENDERMAN, EntityType.ENDERMITE, | |
EntityType.EVOKER, EntityType.GIANT, EntityType.GUARDIAN, EntityType.HUSK, EntityType.ZOMBIE_VILLAGER, | |
EntityType.ILLUSIONER, EntityType.PIG_ZOMBIE, EntityType.SILVERFISH, EntityType.SKELETON, EntityType.SPIDER, | |
EntityType.STRAY, EntityType.VEX, EntityType.VINDICATOR, EntityType.WITCH, EntityType.GHAST, EntityType.POLAR_BEAR, | |
EntityType.SHULKER, EntityType.SLIME, EntityType.MAGMA_CUBE | |
)), | |
Pair("peaceful_mobs", listOf( | |
EntityType.PIG, EntityType.COW, EntityType.HORSE, EntityType.DONKEY, EntityType.MULE, EntityType.MUSHROOM_COW, | |
EntityType.BAT, EntityType.SHEEP, EntityType.LLAMA, EntityType.OCELOT, EntityType.RABBIT, EntityType.SQUID | |
)) | |
) | |
//private val resourceTypes = mapOf(Pair(crops, 1), Pair(naturalResources, 2), Pair(overworldResources, 3)) | |
@EventHandler | |
fun onBlockBreak(event: BlockBreakEvent) { | |
try { | |
val type = when (event.block.type) { | |
in resourceTypes["crops"]!! -> 1 | |
in resourceTypes["blocks"]!! -> 2 | |
else -> 0 | |
} | |
val addNewResource = db.connection.prepareStatement("INSERT OR IGNORE INTO harvest (resource, harvests, type) VALUES (?, 1, ?)") | |
addNewResource.apply { | |
setString(1, event.block.type.name) | |
setInt(2, type) | |
executeUpdate() | |
} | |
val response = db.connection.createStatement().executeQuery("SELECT * FROM harvest WHERE (resource, type) = ('${event.block.type.name}', $type)") | |
while (response.next()) { | |
val currentHarvests = response.getInt("harvests") | |
val addHarvest = db.connection.prepareStatement("UPDATE harvest SET harvests = ? WHERE (resource, type) = (?, ?)") | |
addHarvest.apply{ | |
setInt(1, currentHarvests+1) | |
setString(2, event.block.type.name) | |
setInt(3, type) | |
executeUpdate() | |
} | |
} | |
var result = db.connection.createStatement().executeQuery("SELECT SUM(harvests) AS sm FROM harvest WHERE type = $type") | |
val total_harvests = if (result.next()) result.getInt("sm") else 0 | |
result = db.connection.createStatement().executeQuery("SELECT harvests FROM harvest WHERE resource = '${event.block.type.name}'") | |
val harvests = if (result.next()) result.getInt("harvests") else 0 | |
result = db.connection.createStatement().executeQuery("SELECT COUNT(resource) AS cunt FROM harvest WHERE type = $type") | |
val unique_harvests = if (result.next()) result.getInt("cunt") else 0 | |
val growth = calculateGrowth(total_harvests, harvests, unique_harvests) | |
result = db.connection.createStatement().executeQuery("SELECT net_worth FROM corporations WHERE id = 0") | |
val currentWorth = if (result.next()) result.getDouble("net_worth") else 0.0 | |
result = db.connection.createStatement().executeQuery("SELECT * FROM corporations WHERE id = 0") //The reserve has an id of 0 | |
while (result.next()) { | |
val updateNetWorth = db.connection.prepareStatement("UPDATE corporations SET net_worth = ? WHERE id = 0").apply { | |
setDouble(1, currentWorth + growth) | |
executeUpdate() | |
} | |
} | |
result = db.connection.createStatement().executeQuery("SELECT shares, share_value FROM corporations WHERE id = 0") | |
while (result.next()) { | |
val shares = result.getInt("shares") | |
val updateShareValue = db.connection.prepareStatement("UPDATE corporations SET share_value = ? WHERE id = 0").apply { | |
setDouble(1, currentWorth / shares.toDouble()) | |
executeUpdate() | |
} | |
} | |
result = db.connection.createStatement().executeQuery("SELECT net_worth FROM corporations WHERE id = 0") | |
while (result.next()) { | |
} | |
} | |
catch (exception: SQLException) { | |
log.severe(exception.message) | |
} | |
} | |
@EventHandler | |
fun onMobKill(event: EntityDeathEvent) { | |
val player: Player? | |
val victim = event.entity | |
when (victim.killer) { | |
null -> return | |
!is Player -> return | |
else -> player = victim.killer | |
} | |
val type = when (victim.type) { | |
in resourceTypes["hostile_mobs"]!! -> 3 | |
in resourceTypes["peaceful_mobs"]!! -> 4 | |
else -> 0 | |
} | |
try { | |
val insertNewHarvest = db.connection.prepareStatement("INSERT OR IGNORE INTO harvest (resource, harvests, type) VALUES (?, 1, ?)").apply { | |
setString(1, victim.type.name) | |
setInt(2, type) | |
executeUpdate() | |
} | |
val response = db.connection.createStatement().executeQuery("SELECT * FROM harvest WHERE (resource, type) = ('${victim.type.name}', $type)") | |
while (response.next()) { | |
val currentHarvests = response.getInt("harvests") | |
val addHarvest = db.connection.prepareStatement("UPDATE harvest SET harvests = ? WHERE (resource, type) = (?, ?)").apply { | |
setInt(1, currentHarvests + 1) | |
setString(2, victim.type.name) | |
setInt(3, type) | |
executeUpdate() | |
} | |
} | |
var result = db.connection.createStatement().executeQuery("SELECT SUM(harvests) AS sm FROM harvest WHERE type = $type") | |
val total_harvests = if (result.next()) result.getInt("sm") else 0 | |
result = db.connection.createStatement().executeQuery("SELECT harvests FROM harvest WHERE resource = '${victim.type.name}'") | |
val harvests = if (result.next()) result.getInt("harvests") else 0 | |
result = db.connection.createStatement().executeQuery("SELECT COUNT(resource) AS cunt FROM harvest WHERE type = $type") | |
val unique_harvests = if (result.next()) result.getInt("cunt") else 0 | |
val growth = calculateGrowth(total_harvests, harvests, unique_harvests) | |
result = db.connection.createStatement().executeQuery("SELECT * FROM corporations WHERE id = 0") //The reserve has an id of 0 | |
while (result.next()) { | |
val currentWorth = result.getDouble("net_worth") | |
val remainingShares = result.getInt("remaining_shares") | |
val updateNetWorth = db.connection.prepareStatement("UPDATE corporations SET net_worth = ? WHERE id = 0").apply { | |
setDouble(1, currentWorth + growth) | |
executeUpdate() | |
} | |
val updateShareValue = db.connection.prepareStatement("UPDATE corporations SET share_value = ? WHERE id = 0").apply { | |
setDouble(1, currentWorth / remainingShares.toDouble()) | |
executeUpdate() | |
} | |
} | |
} catch (exception: SQLException) { | |
log.severe(exception.message) | |
} | |
} | |
private fun calculateGrowth(total_harvests: Int, harvests: Int, unique_harvests: Int): Double = total_harvests.toDouble() / (harvests * unique_harvests + 1).toDouble() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment