diff options
author | Dico <dico.karssiens@gmail.com> | 2018-08-12 18:07:43 +0100 |
---|---|---|
committer | Dico <dico.karssiens@gmail.com> | 2018-08-12 18:07:43 +0100 |
commit | 5bd0970c54a843c897126116d5eaff88014360fb (patch) | |
tree | 54ce2c17247377024c67b1dacb528fa46615b9d5 /src/main/kotlin/io/dico/parcels2/storage | |
parent | 957d6f2434f9223107605a8115b6e868de772018 (diff) |
Work on a couple of the todos
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/storage')
5 files changed, 81 insertions, 9 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/storage/Backing.kt b/src/main/kotlin/io/dico/parcels2/storage/Backing.kt index b658d10..6bef483 100644 --- a/src/main/kotlin/io/dico/parcels2/storage/Backing.kt +++ b/src/main/kotlin/io/dico/parcels2/storage/Backing.kt @@ -6,6 +6,7 @@ import kotlinx.coroutines.experimental.Deferred import kotlinx.coroutines.experimental.Job import kotlinx.coroutines.experimental.channels.ReceiveChannel import kotlinx.coroutines.experimental.channels.SendChannel +import org.joda.time.DateTime import java.util.UUID interface Backing { @@ -30,8 +31,14 @@ interface Backing { fun shutdown() + fun getWorldCreationTime(worldId: ParcelWorldId): DateTime? + + fun setWorldCreationTime(worldId: ParcelWorldId, time: DateTime) + fun getPlayerUuidForName(name: String): UUID? + fun updatePlayerName(uuid: UUID, name: String) + fun transmitParcelData(channel: SendChannel<DataPair>, parcels: Sequence<ParcelId>) fun transmitAllParcelData(channel: SendChannel<DataPair>) @@ -47,6 +54,8 @@ interface Backing { fun setParcelOwner(parcel: ParcelId, owner: PlayerProfile?) + fun setParcelOwnerSignOutdated(parcel: ParcelId, outdated: Boolean) + fun setLocalPlayerStatus(parcel: ParcelId, player: PlayerProfile, status: AddedStatus) fun setParcelAllowsInteractInventory(parcel: ParcelId, value: Boolean) diff --git a/src/main/kotlin/io/dico/parcels2/storage/Storage.kt b/src/main/kotlin/io/dico/parcels2/storage/Storage.kt index 2116b46..a0e94e0 100644 --- a/src/main/kotlin/io/dico/parcels2/storage/Storage.kt +++ b/src/main/kotlin/io/dico/parcels2/storage/Storage.kt @@ -8,6 +8,7 @@ import kotlinx.coroutines.experimental.Job import kotlinx.coroutines.experimental.channels.ReceiveChannel import kotlinx.coroutines.experimental.channels.SendChannel import kotlinx.coroutines.experimental.launch +import org.joda.time.DateTime import java.util.UUID typealias DataPair = Pair<ParcelId, ParcelData?> @@ -22,8 +23,14 @@ interface Storage { fun shutdown(): Job + fun getWorldCreationTime(worldId: ParcelWorldId): Deferred<DateTime?> + + fun setWorldCreationTime(worldId: ParcelWorldId, time: DateTime): Job + fun getPlayerUuidForName(name: String): Deferred<UUID?> + fun updatePlayerName(uuid: UUID, name: String): Job + fun readParcelData(parcel: ParcelId): Deferred<ParcelData?> fun transmitParcelData(parcels: Sequence<ParcelId>): ReceiveChannel<DataPair> @@ -39,6 +46,8 @@ interface Storage { fun setParcelOwner(parcel: ParcelId, owner: PlayerProfile?): Job + fun setParcelOwnerSignOutdated(parcel: ParcelId, outdated: Boolean): Job + fun setParcelPlayerStatus(parcel: ParcelId, player: PlayerProfile, status: AddedStatus): Job fun setParcelAllowsInteractInventory(parcel: ParcelId, value: Boolean): Job @@ -65,8 +74,14 @@ class BackedStorage internal constructor(val b: Backing) : Storage { override fun shutdown() = launch(b.dispatcher) { b.shutdown() } + override fun getWorldCreationTime(worldId: ParcelWorldId): Deferred<DateTime?> = b.launchFuture { b.getWorldCreationTime(worldId) } + + override fun setWorldCreationTime(worldId: ParcelWorldId, time: DateTime): Job = b.launchJob { b.setWorldCreationTime(worldId, time) } + override fun getPlayerUuidForName(name: String): Deferred<UUID?> = b.launchFuture { b.getPlayerUuidForName(name) } + override fun updatePlayerName(uuid: UUID, name: String): Job = b.launchJob { b.updatePlayerName(uuid, name) } + override fun readParcelData(parcel: ParcelId) = b.launchFuture { b.readParcelData(parcel) } override fun transmitParcelData(parcels: Sequence<ParcelId>) = b.openChannel<DataPair> { b.transmitParcelData(it, parcels) } @@ -81,6 +96,8 @@ class BackedStorage internal constructor(val b: Backing) : Storage { override fun setParcelOwner(parcel: ParcelId, owner: PlayerProfile?) = b.launchJob { b.setParcelOwner(parcel, owner) } + override fun setParcelOwnerSignOutdated(parcel: ParcelId, outdated: Boolean): Job = b.launchJob { b.setParcelOwnerSignOutdated(parcel, outdated) } + override fun setParcelPlayerStatus(parcel: ParcelId, player: PlayerProfile, status: AddedStatus) = b.launchJob { b.setLocalPlayerStatus(parcel, player, status) } override fun setParcelAllowsInteractInventory(parcel: ParcelId, value: Boolean) = b.launchJob { b.setParcelAllowsInteractInventory(parcel, value) } diff --git a/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt b/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt index 11d26c4..8cd2804 100644 --- a/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt +++ b/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt @@ -4,10 +4,12 @@ package io.dico.parcels2.storage.exposed import com.zaxxer.hikari.HikariDataSource import io.dico.parcels2.* +import io.dico.parcels2.PlayerProfile.Star.name import io.dico.parcels2.storage.AddedDataPair import io.dico.parcels2.storage.Backing import io.dico.parcels2.storage.DataPair import io.dico.parcels2.util.synchronized +import io.dico.parcels2.util.toByteArray import io.dico.parcels2.util.toUUID import kotlinx.coroutines.experimental.* import kotlinx.coroutines.experimental.channels.ArrayChannel @@ -114,11 +116,28 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource, val poolSi else -> throw InternalError("Case should not be reached") } + + override fun getWorldCreationTime(worldId: ParcelWorldId): DateTime? { + return WorldsT.getWorldCreationTime(worldId) + } + + override fun setWorldCreationTime(worldId: ParcelWorldId, time: DateTime) { + WorldsT.setWorldCreationTime(worldId, time) + } + override fun getPlayerUuidForName(name: String): UUID? { return ProfilesT.slice(ProfilesT.uuid).select { ProfilesT.name.upperCase() eq name.toUpperCase() } .firstOrNull()?.let { it[ProfilesT.uuid]?.toUUID() } } + override fun updatePlayerName(uuid: UUID, name: String) { + val binaryUuid = uuid.toByteArray() + ProfilesT.upsert(ProfilesT.uuid) { + it[ProfilesT.uuid] = binaryUuid + it[ProfilesT.name] = name + } + } + override fun transmitParcelData(channel: SendChannel<DataPair>, parcels: Sequence<ParcelId>) { for (parcel in parcels) { val data = readParcelData(parcel) @@ -193,6 +212,14 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource, val poolSi ParcelsT.update({ ParcelsT.id eq id }) { it[ParcelsT.owner_id] = owner_id it[claim_time] = time + it[sign_oudated] = false + } + } + + override fun setParcelOwnerSignOutdated(parcel: ParcelId, outdated: Boolean) { + val id = ParcelsT.getId(parcel) ?: return + ParcelsT.update({ ParcelsT.id eq id }) { + it[sign_oudated] = outdated } } @@ -203,16 +230,16 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource, val poolSi override fun setParcelAllowsInteractInventory(parcel: ParcelId, value: Boolean) { val id = ParcelsT.getOrInitId(parcel) ParcelOptionsT.upsert(ParcelOptionsT.parcel_id) { - it[ParcelOptionsT.parcel_id] = id - it[ParcelOptionsT.interact_inventory] = value + it[parcel_id] = id + it[interact_inventory] = value } } override fun setParcelAllowsInteractInputs(parcel: ParcelId, value: Boolean) { val id = ParcelsT.getOrInitId(parcel) ParcelOptionsT.upsert(ParcelOptionsT.parcel_id) { - it[ParcelOptionsT.parcel_id] = id - it[ParcelOptionsT.interact_inputs] = value + it[parcel_id] = id + it[interact_inputs] = value } } @@ -231,7 +258,8 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource, val poolSi private fun rowToParcelData(row: ResultRow) = ParcelDataHolder().apply { owner = row[ParcelsT.owner_id]?.let { ProfilesT.getItem(it) } - since = row[ParcelsT.claim_time] + lastClaimTime = row[ParcelsT.claim_time] + ownerSignOutdated = row[ParcelsT.sign_oudated] val id = row[ParcelsT.id] ParcelOptionsT.select { ParcelOptionsT.parcel_id eq id }.firstOrNull()?.let { optrow -> diff --git a/src/main/kotlin/io/dico/parcels2/storage/exposed/IdTables.kt b/src/main/kotlin/io/dico/parcels2/storage/exposed/IdTables.kt index 98e209d..d8315fd 100644 --- a/src/main/kotlin/io/dico/parcels2/storage/exposed/IdTables.kt +++ b/src/main/kotlin/io/dico/parcels2/storage/exposed/IdTables.kt @@ -9,6 +9,7 @@ import io.dico.parcels2.util.toByteArray import io.dico.parcels2.util.toUUID import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.statements.UpdateBuilder +import org.joda.time.DateTime import java.util.UUID sealed class IdTransactionsTable<TableT : IdTransactionsTable<TableT, QueryObj>, QueryObj>(tableName: String, columnName: String) @@ -24,7 +25,8 @@ sealed class IdTransactionsTable<TableT : IdTransactionsTable<TableT, QueryObj>, } internal inline fun getOrInitId(getId: () -> Int?, noinline body: TableT.(UpdateBuilder<*>) -> Unit, objName: () -> String): Int { - return getId() ?: table.insertIgnore(body)[id] ?: getId() ?: throw ExposedDatabaseException("This should not happen - failed to insert ${objName()} and get its id") + return getId() ?: table.insertIgnore(body)[id] ?: getId() + ?: throw ExposedDatabaseException("This should not happen - failed to insert ${objName()} and get its id") } abstract fun getId(obj: QueryObj): Int? @@ -35,9 +37,10 @@ sealed class IdTransactionsTable<TableT : IdTransactionsTable<TableT, QueryObj>, fun getId(obj: QueryObj, init: Boolean): Int? = if (init) getOrInitId(obj) else getId(obj) } -object WorldsT : IdTransactionsTable<WorldsT, ParcelWorldId>("parcel_worlds", "world_id") { +object WorldsT : IdTransactionsTable<WorldsT, ParcelWorldId>("parcels_worlds", "world_id") { val name = varchar("name", 50) val uid = binary("uid", 16).nullable() + val creation_time = datetime("creation_time").nullable() val index_name = uniqueIndexR("index_name", name) val index_uid = uniqueIndexR("index_uid", uid) @@ -56,6 +59,18 @@ object WorldsT : IdTransactionsTable<WorldsT, ParcelWorldId>("parcel_worlds", "w override fun getItem(row: ResultRow): ParcelWorldId { return ParcelWorldId(row[name], row[uid]?.toUUID()) } + + fun getWorldCreationTime(worldId: ParcelWorldId): DateTime? { + val id = getId(worldId) ?: return null + return select { WorldsT.id eq id }.firstOrNull()?.let { it[WorldsT.creation_time] } + } + + fun setWorldCreationTime(worldId: ParcelWorldId, time: DateTime) { + val id = getOrInitId(worldId) + update({ WorldsT.id eq id }) { + it[WorldsT.creation_time] = time + } + } } object ParcelsT : IdTransactionsTable<ParcelsT, ParcelId>("parcels", "parcel_id") { @@ -63,6 +78,7 @@ object ParcelsT : IdTransactionsTable<ParcelsT, ParcelId>("parcels", "parcel_id" val px = integer("px") val pz = integer("pz") val owner_id = integer("owner_id").references(ProfilesT.id).nullable() + val sign_oudated = bool("sign_outdated").default(false) val claim_time = datetime("claim_time").nullable() val index_location = uniqueIndexR("index_location", world_id, px, pz) @@ -89,7 +105,7 @@ object ParcelsT : IdTransactionsTable<ParcelsT, ParcelId>("parcels", "parcel_id" } } -object ProfilesT : IdTransactionsTable<ProfilesT, PlayerProfile>("parcel_profiles", "owner_id") { +object ProfilesT : IdTransactionsTable<ProfilesT, PlayerProfile>("parcels_profiles", "owner_id") { val uuid = binary("uuid", 16).nullable() val name = varchar("name", 32).nullable() @@ -103,7 +119,8 @@ object ProfilesT : IdTransactionsTable<ProfilesT, PlayerProfile>("parcel_profile private inline fun getId(nameIn: String) = getId { uuid.isNull() and (name.lowerCase() eq nameIn.toLowerCase()) } private inline fun getRealId(nameIn: String) = getId { uuid.isNotNull() and (name.lowerCase() eq nameIn.toLowerCase()) } - private inline fun getOrInitId(uuid: UUID, name: String?) = uuid.toByteArray().let { binaryUuid -> getOrInitId( + private inline fun getOrInitId(uuid: UUID, name: String?) = uuid.toByteArray().let { binaryUuid -> + getOrInitId( { getId(binaryUuid) }, { it[this@ProfilesT.uuid] = binaryUuid; it[this@ProfilesT.name] = name }, { "profile(uuid = $uuid, name = $name)" }) diff --git a/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt b/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt index f0c0cd8..9921268 100644 --- a/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt +++ b/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt @@ -83,6 +83,7 @@ class PlotmeMigration(val options: PlotmeMigrationOptions) : Migration { val parcel = getParcelId(PlotmePlotsT, row) ?: return@forEach val owner = PlayerProfile.safe(row[PlotmePlotsT.owner_uuid]?.toUUID(), row[PlotmePlotsT.owner_name]) target.setParcelOwner(parcel, owner) + target.setParcelOwnerSignOutdated(parcel, true) } } |