Skip to content

Commit

Permalink
refactor: reduce code duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Attacktive committed May 24, 2024
1 parent 43d5456 commit 2251ce6
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.attacktive.troubleshootereditor.domain.common

interface IDiffResult<T> {
val id: T

fun insert(propertyIndex: Int, propertyValue: String)
fun update(propertyIndex: Int, propertyValue: String)
fun delete(propertyIndex: Int)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.github.attacktive.troubleshootereditor.domain.common

import java.util.function.Predicate
import com.github.attacktive.troubleshootereditor.extension.logger

data class Properties(private val list: List<Property> = mutableListOf()) {
open class Properties(private val list: List<Property> = mutableListOf()) {
constructor(map: Map<String, String>): this(map.map { Property(it.toPair()) }.toMutableList())

private val logger by logger()

fun containsKey(key: String) = keys().contains(key)
fun containsKeyThat(predicate: Predicate<String>) = keys().any { predicate.test(it) }

Expand Down Expand Up @@ -41,6 +44,22 @@ data class Properties(private val list: List<Property> = mutableListOf()) {
return Properties((withThese + withThose).toMutableList())
}

fun <T> applyPropertyChanges(diffResult: IDiffResult<T>, propertyMasterLookup: Map<String, Int>) {
forEach { property ->
val propertyIndex = propertyMasterLookup[property.key]
if (propertyIndex == null) {
logger.warn("Failed to find item property master index for \"${property.key}\"; ignoring. 😞")
} else {
when (property.diffType) {
DiffType.NONE -> {}
DiffType.ADDED -> diffResult.insert(propertyIndex, property.value)
DiffType.MODIFIED -> diffResult.update(propertyIndex, property.value)
DiffType.REMOVED -> diffResult.delete(propertyIndex)
}
}
}
}

private fun keys() = list.map { it.key }.toSet()

private fun findByKey(key: String) = list.find { it.key == key }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.github.attacktive.troubleshootereditor.domain.company

import com.fasterxml.jackson.annotation.JsonGetter
import com.github.attacktive.troubleshootereditor.domain.common.IDiffResult
import com.github.attacktive.troubleshootereditor.domain.common.Properties
import com.github.attacktive.troubleshootereditor.domain.company.table.CompanyProperties
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert

data class Company(val id: Int, val name: String, val vill: Long, val properties: Properties) {
constructor(id: Int, name: String, vill:Long, properties: Map<String, String>): this(id, name, vill, Properties(properties))
constructor(id: Int, name: String, vill: Long, properties: Map<String, String>): this(id, name, vill, Properties(properties))

@JsonGetter(value = "properties")
fun properties() = properties.toMap().toSortedMap()
Expand All @@ -17,5 +23,25 @@ data class Company(val id: Int, val name: String, val vill: Long, val properties
return DiffResult(id, name, vill, properties)
}

data class DiffResult(val id: Int, val name: String?, val vill: Long?, val properties: Properties)
data class DiffResult(override val id: Int, val name: String?, val vill: Long?, val properties: Properties): IDiffResult<Int> {
override fun insert(propertyIndex: Int, propertyValue: String) {
CompanyProperties.insert {
it[companyId] = id
it[masterIndex] = propertyIndex
it[cpValue] = propertyValue
}
}

override fun update(propertyIndex: Int, propertyValue: String) {
CompanyProperties.insert {
it[cpValue] = propertyValue
}
}

override fun delete(propertyIndex: Int) {
CompanyProperties.deleteWhere {
(companyId eq id) and (masterIndex eq propertyIndex)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package com.github.attacktive.troubleshootereditor.domain.company

import com.github.attacktive.troubleshootereditor.domain.common.DiffType
import com.github.attacktive.troubleshootereditor.domain.common.Property
import com.github.attacktive.troubleshootereditor.domain.company.table.Companies
import com.github.attacktive.troubleshootereditor.domain.company.table.CompanyProperties
import com.github.attacktive.troubleshootereditor.domain.company.table.CompanyPropertyMaster
import com.github.attacktive.troubleshootereditor.extension.logger
import com.github.attacktive.troubleshootereditor.extension.toProperties
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update

object CompanyObject {
private val logger by logger()

fun selectCompany(url: String): Company {
Database.connect(url)

Expand Down Expand Up @@ -66,28 +58,7 @@ object CompanyObject {
}
}

diffResult.properties
.forEach { property ->
val propertyIndex = companyMasterLookup[property.key]
if (propertyIndex == null) {
logger.warn("Failed to find property Company master index for \"${property.key}\"; ignoring. 😞")
} else {
when (property.diffType) {
DiffType.NONE -> {}
DiffType.ADDED -> CompanyProperties.insert {
it[companyId] = diffResult.id
it[masterIndex] = propertyIndex
it[cpValue] = property.value
}
DiffType.MODIFIED -> CompanyProperties.update({ (CompanyProperties.companyId eq diffResult.id) and (CompanyProperties.masterIndex eq propertyIndex) }) {
it[cpValue] = property.value
}
DiffType.REMOVED -> CompanyProperties.deleteWhere {
(companyId eq diffResult.id) and (masterIndex eq propertyIndex)
}
}
}
}
diffResult.properties.applyPropertyChanges(diffResult, companyMasterLookup)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package com.github.attacktive.troubleshootereditor.domain.item

import com.fasterxml.jackson.annotation.JsonGetter
import com.github.attacktive.troubleshootereditor.domain.common.Diffable
import com.github.attacktive.troubleshootereditor.domain.common.IDiffResult
import com.github.attacktive.troubleshootereditor.domain.common.Properties
import com.github.attacktive.troubleshootereditor.domain.item.table.ItemProperties
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert

data class Item(val id: Long, val type: String, val count: Long, val status: String, val properties: Properties, var equipmentPosition: EquipmentPosition? = null): Diffable<Item, Long, Item.DiffResult> {
constructor(id: Long, type: String, count: Long, status: String, properties: Map<String, String>, equipmentPosition: EquipmentPosition? = null): this(id, type, count, status, Properties(properties), equipmentPosition)
Expand All @@ -29,5 +35,25 @@ data class Item(val id: Long, val type: String, val count: Long, val status: Str
return properties.containsKeyThat { it.matches(Regex("Option/.+", RegexOption.IGNORE_CASE)) }
}

data class DiffResult(val id: Long, val type: String?, val count: Long?, val status: String?, val properties: Properties)
data class DiffResult(override val id: Long, val type: String?, val count: Long?, val status: String?, val properties: Properties): IDiffResult<Long> {
override fun insert(propertyIndex: Int, propertyValue: String) {
ItemProperties.insert {
it[itemId] = id
it[masterIndex] = propertyIndex
it[value] = propertyValue
}
}

override fun update(propertyIndex: Int, propertyValue: String) {
ItemProperties.insert {
it[value] = propertyValue
}
}

override fun delete(propertyIndex: Int) {
ItemProperties.deleteWhere {
(itemId eq id) and (masterIndex eq propertyIndex)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.attacktive.troubleshootereditor.domain.item

import com.github.attacktive.troubleshootereditor.domain.common.DiffType
import com.github.attacktive.troubleshootereditor.domain.common.Property
import com.github.attacktive.troubleshootereditor.domain.item.table.ItemEquippedInfos
import com.github.attacktive.troubleshootereditor.domain.item.table.ItemProperties
Expand All @@ -12,12 +11,9 @@ import com.github.attacktive.troubleshootereditor.extension.getDiffResults
import com.github.attacktive.troubleshootereditor.extension.logger
import com.github.attacktive.troubleshootereditor.extension.toProperties
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update

Expand Down Expand Up @@ -116,29 +112,8 @@ object ItemObject {
}
}

itemDiff.properties
.forEach { property ->
val propertyIndex = itemPropertyMasterLookup[property.key]
if (propertyIndex == null) {
logger.warn("Failed to find item property master index for \"${property.key}\"; ignoring. 😞")
} else {
when (property.diffType) {
DiffType.NONE -> {}
DiffType.ADDED -> ItemProperties.insert {
it[itemId] = itemDiff.id
it[masterIndex] = propertyIndex
it[value] = property.value
}
DiffType.MODIFIED -> ItemProperties.update({ (ItemProperties.itemId eq itemDiff.id) and (ItemProperties.masterIndex eq propertyIndex) }) {
it[value] = property.value
}
DiffType.REMOVED -> ItemProperties.deleteWhere {
(itemId eq itemDiff.id) and (masterIndex eq propertyIndex)
}
}
}
}
}
itemDiff.properties.applyPropertyChanges(itemDiff, itemPropertyMasterLookup)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.github.attacktive.troubleshootereditor.domain.roster

import com.github.attacktive.troubleshootereditor.domain.common.Diffable
import com.github.attacktive.troubleshootereditor.domain.common.IDiffResult
import com.github.attacktive.troubleshootereditor.domain.common.Properties
import com.github.attacktive.troubleshootereditor.domain.roster.table.RosterProperties
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert

data class Roster(val id: Long, val name: String, val `class`: String, val level: Long, val exp: Long, val properties: Properties): Diffable<Roster, Long, Roster.DiffResult> {
override fun getId() = id
Expand All @@ -16,5 +22,25 @@ data class Roster(val id: Long, val name: String, val `class`: String, val level
return DiffResult(id, name, `class`, level, exp, properties)
}

data class DiffResult(val id: Long, val name: String?, val `class`: String?, val level: Long?, val exp: Long?, val properties: Properties)
data class DiffResult(override val id: Long, val name: String?, val `class`: String?, val level: Long?, val exp: Long?, val properties: Properties): IDiffResult<Long> {
override fun insert(propertyIndex: Int, propertyValue: String) {
RosterProperties.insert {
it[rosterId] = id
it[masterIndex] = propertyIndex
it[value] = propertyValue
}
}

override fun update(propertyIndex: Int, propertyValue: String) {
RosterProperties.insert {
it[value] = propertyValue
}
}

override fun delete(propertyIndex: Int) {
RosterProperties.deleteWhere {
(rosterId eq id) and (masterIndex eq propertyIndex)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package com.github.attacktive.troubleshootereditor.domain.roster

import com.github.attacktive.troubleshootereditor.domain.common.DiffType
import com.github.attacktive.troubleshootereditor.domain.common.Property
import com.github.attacktive.troubleshootereditor.domain.item.table.ItemProperties
import com.github.attacktive.troubleshootereditor.domain.roster.table.RosterProperties
import com.github.attacktive.troubleshootereditor.domain.roster.table.RosterPropertyMaster
import com.github.attacktive.troubleshootereditor.domain.roster.table.Rosters
import com.github.attacktive.troubleshootereditor.extension.getDiffResults
import com.github.attacktive.troubleshootereditor.extension.logger
import com.github.attacktive.troubleshootereditor.extension.toProperties
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update

Expand Down Expand Up @@ -84,28 +79,7 @@ object RosterObject {
}
}

rosterDiff.properties
.forEach { property ->
val propertyIndex = rosterPropertyMasterLookup[property.key]
if (propertyIndex == null) {
logger.warn("Failed to find roster property master index for \"${property.key}\"; ignoring. 😞")
} else {
when (property.diffType) {
DiffType.NONE -> {}
DiffType.ADDED -> ItemProperties.insert {
it[itemId] = rosterDiff.id
it[masterIndex] = propertyIndex
it[value] = property.value
}
DiffType.MODIFIED -> ItemProperties.update({ (ItemProperties.itemId eq rosterDiff.id) and (ItemProperties.masterIndex eq propertyIndex) }) {
it[value] = property.value
}
DiffType.REMOVED -> ItemProperties.deleteWhere {
(itemId eq rosterDiff.id) and (masterIndex eq propertyIndex)
}
}
}
}
rosterDiff.properties.applyPropertyChanges(rosterDiff, rosterPropertyMasterLookup)
}
}
}
Expand Down

0 comments on commit 2251ce6

Please sign in to comment.