Skip to content

Commit

Permalink
Merge pull request #293 from alyssaruth/ktfmt
Browse files Browse the repository at this point in the history
Add `ktfmt`
  • Loading branch information
alyssaruth authored Jan 10, 2024
2 parents 3c5c203 + 907a0ca commit 41a5ebe
Show file tree
Hide file tree
Showing 817 changed files with 17,546 additions and 17,617 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
- name: Verify tests
run: |
sh scripts/verify-test-files.sh
- name: Ktfmt
run: ./gradlew ktfmtCheck
- name: Detekt - main
run: ./gradlew detektMain
- name: Detekt - tests
Expand Down
7 changes: 6 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.net.URI

plugins {
kotlin("jvm") version "1.8.21"
kotlin("jvm") version "1.9.22"
id("java-library")
id("com.github.ben-manes.versions") version "0.44.0"
id("io.gitlab.arturbosch.detekt") version "1.23.0"
id("org.jetbrains.kotlinx.kover") version "0.6.1"
id("com.ncorti.ktfmt.gradle") version "0.15.1"
}

repositories {
Expand All @@ -26,6 +27,10 @@ detekt {
config.setFrom("$projectDir/detekt-config.yml")
}

ktfmt {
kotlinLangStyle()
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0")
implementation("org.apache.commons:commons-math3:3.6.1")
Expand Down
2 changes: 1 addition & 1 deletion scripts/verify-test-files.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -eu

badTestFiles=$(grep -r "class Test[^\:]*$" src/test || true)
badTestFiles=$(grep -r "class Test [^\:]*$" src/test || true)
if [ ! -z "$badTestFiles" ]; then
echo "The following test files do not correctly extend AbstractTest:"
echo $badTestFiles
Expand Down
191 changes: 79 additions & 112 deletions src/main/kotlin/dartzee/achievements/AbstractAchievement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ import java.net.URL
import javax.imageio.ImageIO
import javax.swing.table.DefaultTableModel

abstract class AbstractAchievement
{
abstract val name : String
abstract val desc : String
abstract val achievementType : AchievementType
abstract val redThreshold : Int
abstract val orangeThreshold : Int
abstract val yellowThreshold : Int
abstract val greenThreshold : Int
abstract val blueThreshold : Int
abstract val pinkThreshold : Int
abstract val maxValue : Int
abstract class AbstractAchievement {
abstract val name: String
abstract val desc: String
abstract val achievementType: AchievementType
abstract val redThreshold: Int
abstract val orangeThreshold: Int
abstract val yellowThreshold: Int
abstract val greenThreshold: Int
abstract val blueThreshold: Int
abstract val pinkThreshold: Int
abstract val maxValue: Int
abstract val gameType: GameType?
abstract val allowedForTeams: Boolean

Expand All @@ -41,41 +40,38 @@ abstract class AbstractAchievement
var gameIdEarned = ""
var localGameIdEarned = -1L
var dtLatestUpdate = DateStatics.START_OF_TIME
var player : PlayerEntity? = null
var player: PlayerEntity? = null

var tmBreakdown : DefaultTableModel? = null
var tmBreakdown: DefaultTableModel? = null

fun runConversion(playerIds: List<String>, database: Database = mainDatabase)
{
fun runConversion(playerIds: List<String>, database: Database = mainDatabase) {
val sb = StringBuilder()
sb.append(" DELETE FROM Achievement")
sb.append(" WHERE AchievementType = '$achievementType'")
appendPlayerSql(sb, playerIds, null)

if (!database.executeUpdate("" + sb))
{
if (!database.executeUpdate("" + sb)) {
return
}

populateForConversion(playerIds, database)
}

abstract fun populateForConversion(playerIds: List<String>, database: Database = mainDatabase)

abstract fun getIconURL(): URL?

/**
* Basic init will be the same for most achievements - get the value from the single row
*/
open fun initialiseFromDb(achievementRows : List<AchievementEntity>, player: PlayerEntity?)
{
if (achievementRows.isEmpty())
{
/** Basic init will be the same for most achievements - get the value from the single row */
open fun initialiseFromDb(achievementRows: List<AchievementEntity>, player: PlayerEntity?) {
if (achievementRows.isEmpty()) {
return
}

if (achievementRows.size > 1)
{
logger.error(CODE_SQL_EXCEPTION, "Got ${achievementRows.size} rows (expected 1) for achievement $achievementType and player ${achievementRows.first().playerId}")
if (achievementRows.size > 1) {
logger.error(
CODE_SQL_EXCEPTION,
"Got ${achievementRows.size} rows (expected 1) for achievement $achievementType and player ${achievementRows.first().playerId}"
)
}

val achievementRow = achievementRows.first()
Expand All @@ -87,11 +83,9 @@ abstract class AbstractAchievement
this.player = player
}

fun getScore() : Int
{
fun getScore(): Int {
val color = getColor(false)
return when (color)
{
return when (color) {
Color.MAGENTA -> 6
Color.CYAN -> 5
Color.GREEN -> 4
Expand All @@ -102,94 +96,74 @@ abstract class AbstractAchievement
}
}

fun getColor(highlighted : Boolean) : Color
{
val col = if (isDecreasing())
{
when (attainedValue)
{
-1 -> Color.GRAY
in redThreshold+1..Int.MAX_VALUE -> Color.GRAY
in orangeThreshold+1 until redThreshold+1 -> Color.RED
in yellowThreshold+1 until orangeThreshold+1 -> DartsColour.COLOUR_ACHIEVEMENT_ORANGE
in greenThreshold+1 until yellowThreshold+1 -> Color.YELLOW
in blueThreshold+1 until greenThreshold+1 -> Color.GREEN
in pinkThreshold+1 until blueThreshold+1 -> Color.CYAN
else -> Color.MAGENTA
}
}
else
{
when (attainedValue)
{
in Int.MIN_VALUE until redThreshold -> Color.GRAY
in redThreshold until orangeThreshold -> Color.RED
in orangeThreshold until yellowThreshold -> DartsColour.COLOUR_ACHIEVEMENT_ORANGE
in yellowThreshold until greenThreshold -> Color.YELLOW
in greenThreshold until blueThreshold -> Color.GREEN
in blueThreshold until pinkThreshold -> Color.CYAN
else -> Color.MAGENTA
fun getColor(highlighted: Boolean): Color {
val col =
if (isDecreasing()) {
when (attainedValue) {
-1 -> Color.GRAY
in redThreshold + 1..Int.MAX_VALUE -> Color.GRAY
in orangeThreshold + 1 until redThreshold + 1 -> Color.RED
in yellowThreshold + 1 until orangeThreshold + 1 ->
DartsColour.COLOUR_ACHIEVEMENT_ORANGE
in greenThreshold + 1 until yellowThreshold + 1 -> Color.YELLOW
in blueThreshold + 1 until greenThreshold + 1 -> Color.GREEN
in pinkThreshold + 1 until blueThreshold + 1 -> Color.CYAN
else -> Color.MAGENTA
}
} else {
when (attainedValue) {
in Int.MIN_VALUE until redThreshold -> Color.GRAY
in redThreshold until orangeThreshold -> Color.RED
in orangeThreshold until yellowThreshold ->
DartsColour.COLOUR_ACHIEVEMENT_ORANGE
in yellowThreshold until greenThreshold -> Color.YELLOW
in greenThreshold until blueThreshold -> Color.GREEN
in blueThreshold until pinkThreshold -> Color.CYAN
else -> Color.MAGENTA
}
}
}

if (highlighted
&& !isLocked())
{
if (highlighted && !isLocked()) {
return col.darker()
}

return col
}

fun getAngle() = getAngle(attainedValue)
fun getAngle(attainedValue : Int) : Double
{
if (attainedValue == -1)
{

fun getAngle(attainedValue: Int): Double {
if (attainedValue == -1) {
return 0.0
}

return if (!isDecreasing())
{
return if (!isDecreasing()) {
360 * attainedValue.toDouble() / maxValue
}
else
{
} else {
val denom = redThreshold - maxValue + 1
val num = Math.max(redThreshold - attainedValue + 1, 0)

360 * num / denom.toDouble()
}
}

fun isLocked() : Boolean
{
if (attainedValue == -1)
{
fun isLocked(): Boolean {
if (attainedValue == -1) {
return true
}

return if (isDecreasing())
{
return if (isDecreasing()) {
attainedValue > redThreshold
}
else
{
} else {
attainedValue < redThreshold
}
}

fun isClickable(): Boolean
{
return !gameIdEarned.isEmpty()
|| tmBreakdown != null
}
fun isClickable() = gameIdEarned.isNotEmpty() || tmBreakdown != null

fun getIcon() : BufferedImage?
{
fun getIcon(): BufferedImage? {
var iconURL = getIconURL()
if (isLocked())
{
if (isLocked()) {
iconURL = ResourceCache.URL_ACHIEVEMENT_LOCKED
}

Expand All @@ -198,28 +172,24 @@ abstract class AbstractAchievement

return bufferedImage
}
protected open fun changeIconColor(img : BufferedImage, newColor: Color)
{

protected open fun changeIconColor(img: BufferedImage, newColor: Color) {
img.paint {
val current = Color(img.getRGB(it.x, it.y), true)
if (current.red == current.blue && current.blue == current.green && current.red < 255) {
val alpha = if (current.alpha == 255) 255 - current.red else current.alpha
Color(newColor.red, newColor.green, newColor.blue, alpha)
}
else
current
} else current
}
}

override fun toString() = name

open fun isUnbounded() = false

fun getProgressDesc() : String
{
fun getProgressDesc(): String {
var progressStr = "$attainedValue"
if (!isUnbounded())
{
if (!isUnbounded()) {
progressStr += "/$maxValue"
}

Expand All @@ -228,24 +198,21 @@ abstract class AbstractAchievement

open fun isDecreasing() = false

fun getExtraDetails() : String
{
var ret = if (this is AbstractMultiRowAchievement)
{
"Last updated on ${dtLatestUpdate.formatAsDate()}"
}
else
{
"Earned on ${dtLatestUpdate.formatAsDate()}"
}
fun getExtraDetails(): String {
var ret =
if (this is AbstractMultiRowAchievement) {
"Last updated on ${dtLatestUpdate.formatAsDate()}"
} else {
"Earned on ${dtLatestUpdate.formatAsDate()}"
}

if (!gameIdEarned.isEmpty())
{
if (!gameIdEarned.isEmpty()) {
ret += " in Game #$localGameIdEarned"
}

return ret
}

open fun retrieveAllRows() = AchievementEntity().retrieveEntities("AchievementType = '$achievementType'")
open fun retrieveAllRows() =
AchievementEntity().retrieveEntities("AchievementType = '$achievementType'")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package dartzee.achievements
import dartzee.utils.Database
import dartzee.utils.doesHighestWin

abstract class AbstractAchievementBestGame : AbstractAchievement()
{
abstract class AbstractAchievementBestGame : AbstractAchievement() {
abstract val gameParams: String
override val allowedForTeams = false

override fun populateForConversion(playerIds: List<String>, database: Database)
{
override fun populateForConversion(playerIds: List<String>, database: Database) {
val sb = StringBuilder()
sb.append(" SELECT pt.PlayerId, g.RowId AS GameId, pt.FinalScore, pt.DtFinished AS DtAchieved")
sb.append(
" SELECT pt.PlayerId, g.RowId AS GameId, pt.FinalScore, pt.DtFinished AS DtAchieved"
)
sb.append(" FROM Participant pt, Game g")
sb.append(" WHERE pt.GameId = g.RowId")
sb.append(" AND g.GameType = '$gameType'")
Expand All @@ -27,13 +27,20 @@ abstract class AbstractAchievementBestGame : AbstractAchievement()
sb.append(" AND g2.GameParams = g.GameParams")
sb.append(" AND pt2.PlayerId = pt.PlayerId")
sb.append(" AND pt2.FinalScore > -1")
sb.append(" AND (pt2.FinalScore < pt.FinalScore OR (pt2.FinalScore = pt.FinalScore AND pt2.DtFinished < pt.DtFinished))")
sb.append(
" AND (pt2.FinalScore < pt.FinalScore OR (pt2.FinalScore = pt.FinalScore AND pt2.DtFinished < pt.DtFinished))"
)
sb.append(")")

database.executeQuery(sb).use { rs ->
bulkInsertFromResultSet(rs, database, achievementType, achievementCounterFn = { rs.getInt("FinalScore") })
bulkInsertFromResultSet(
rs,
database,
achievementType,
achievementCounterFn = { rs.getInt("FinalScore") }
)
}
}

override fun isDecreasing() = !doesHighestWin(gameType)
}
}
Loading

0 comments on commit 41a5ebe

Please sign in to comment.