Skip to content

Commit

Permalink
Merge pull request #309 from alyssaruth/party-e2e
Browse files Browse the repository at this point in the history
Party mode - more tweaks and E2E test
  • Loading branch information
alyssaruth authored Mar 7, 2024
2 parents 7b31b8c + 2473549 commit 397690c
Show file tree
Hide file tree
Showing 20 changed files with 308 additions and 71 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/dartzee/bean/AbstractPlayerSelector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ abstract class AbstractPlayerSelector<S : ScrollTable> :
protected fun render() {
layout = MigLayout("al center center", "[452px][100px][452px]", "[407px]")

tablePlayersToSelectFrom.name = "TableUnselected"

val panelMovementOptions = JPanel()
add(tablePlayersToSelectFrom, "cell 0 0,alignx left,growy")
panelMovementOptions.minimumSize = Dimension(50, 10)
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/dartzee/game/state/X01PlayerState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ data class X01PlayerState(
override var isActive: Boolean = false
) : AbstractPlayerState<X01PlayerState>() {
override fun getScoreSoFar(): Int {
val hasFinished = completedRounds.isNotEmpty() && isFinishRound(completedRounds.last())
val hasFinished =
completedRounds.isNotEmpty() && isFinishRound(completedRounds.last(), config.finishType)
if (!hasFinished) {
return (completedRounds.size * 3) + currentRound.size
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/dartzee/screen/UtilitiesScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dartzee.core.util.dumpThreadStacks
import dartzee.core.util.getAllChildComponentsForType
import dartzee.core.util.runInOtherThread
import dartzee.db.sanity.DatabaseSanityCheck
import dartzee.logging.CODE_PARTY_MODE
import dartzee.`object`.DartsClient
import dartzee.utils.DARTS_VERSION_NUMBER
import dartzee.utils.DartsDatabaseUtil
Expand All @@ -26,6 +27,7 @@ class UtilitiesScreen : EmbeddedScreen() {
private val btnViewLogs = JButton("View Logs")
private val btnThreadStacks = JButton("Thread Stacks")
private val btnAchievementConversion = JButton("Run Achievement Conversion")
private val btnPartyMode = JButton("Enter Party Mode")

init {
val panel = JPanel()
Expand All @@ -39,6 +41,7 @@ class UtilitiesScreen : EmbeddedScreen() {
panel.add(btnCheckForUpdates, "cell 0 8,alignx center")
panel.add(btnViewLogs, "cell 0 10,alignx center")
panel.add(btnAchievementConversion, "cell 0 11,alignx center")
panel.add(btnPartyMode, "cell 0 12,alignx center")

val buttons = panel.getAllChildComponentsForType<AbstractButton>()
for (button in buttons) {
Expand Down Expand Up @@ -66,10 +69,18 @@ class UtilitiesScreen : EmbeddedScreen() {
}
btnThreadStacks -> dumpThreadStacks()
btnAchievementConversion -> runAchievementConversion()
btnPartyMode -> enterPartyMode()
else -> super.actionPerformed(arg0)
}
}

private fun enterPartyMode() {
InjectedThings.logger.info(CODE_PARTY_MODE, "Entering party mode!")
InjectedThings.partyMode = true

ScreenCache.switch<MenuScreen>()
}

private fun runAchievementConversion() {
val dlg = AchievementConversionDialog()
dlg.setLocationRelativeTo(ScreenCache.mainScreen)
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/dartzee/screen/game/DartsGamePanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import dartzee.screen.game.golf.GamePanelGolf
import dartzee.screen.game.rtc.GamePanelRoundTheClock
import dartzee.screen.game.scorer.AbstractDartsScorer
import dartzee.screen.game.x01.GamePanelX01
import dartzee.utils.InjectedThings
import dartzee.utils.InjectedThings.mainDatabase
import dartzee.utils.PREFERENCES_INT_AI_SPEED
import dartzee.utils.PreferenceUtil
Expand Down Expand Up @@ -619,9 +620,11 @@ abstract class DartsGamePanel<
}

fun achievementUnlocked(playerId: String, achievement: AbstractAchievement) {
scorersOrdered
.find { it.playerIds.contains(playerId) }
?.achievementUnlocked(achievement, playerId)
if (!InjectedThings.partyMode) {
scorersOrdered
.find { it.playerIds.contains(playerId) }
?.achievementUnlocked(achievement, playerId)
}
}

fun hideInputButtons() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class AbstractDartsScorerPausable<PlayerState : AbstractPlayerState<Pla
panelSouth.add(btnResume, BorderLayout.EAST)
btnResume.isVisible = false
btnResume.icon = ICON_RESUME
btnResume.toolTipText = "Resume throwing"

btnResume.addActionListener(this)
}
Expand All @@ -45,12 +46,14 @@ abstract class AbstractDartsScorerPausable<PlayerState : AbstractPlayerState<Pla
"Paused player ${participant.getUniqueParticipantName()}"
)
btnResume.icon = ICON_RESUME
btnResume.toolTipText = "Resume throwing"
} else {
logger.info(
CODE_PLAYER_UNPAUSED,
"Unpaused player ${participant.getUniqueParticipantName()}"
)
btnResume.icon = ICON_PAUSE
btnResume.toolTipText = "Pause throwing"
updateResultColourForPosition(-1)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package dartzee.screen.game.x01

import dartzee.core.util.minOrZero
import dartzee.game.X01Config
import dartzee.utils.isFinishRound
import dartzee.utils.sumScore

class MatchStatisticsPanelX01(gameParams: String) : GameStatisticsPanelX01(gameParams) {
private val config = X01Config.fromJson(gameParams)

override fun addRowsToTable() {
super.addRowsToTable()

Expand All @@ -17,7 +20,7 @@ class MatchStatisticsPanelX01(gameParams: String) : GameStatisticsPanelX01(gameP
private fun getHighestFinishRow() =
prepareRow("Best Finish") { playerName ->
val rounds = hmPlayerToDarts[playerName].orEmpty()
val finishRounds = rounds.filter { r -> isFinishRound(r) }
val finishRounds = rounds.filter { r -> isFinishRound(r, config.finishType) }
finishRounds.maxOfOrNull { r -> sumScore(r) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ package dartzee.screen.preference
import dartzee.bean.SliderAiSpeed
import dartzee.core.bean.NumberField
import dartzee.core.util.setFontSize
import dartzee.logging.CODE_PARTY_MODE
import dartzee.screen.MenuScreen
import dartzee.screen.ScreenCache
import dartzee.utils.InjectedThings
import dartzee.utils.InjectedThings.logger
import dartzee.utils.PREFERENCES_BOOLEAN_AI_AUTO_CONTINUE
import dartzee.utils.PREFERENCES_BOOLEAN_CHECK_FOR_UPDATES
import dartzee.utils.PREFERENCES_BOOLEAN_SHOW_ANIMATIONS
Expand All @@ -19,7 +14,6 @@ import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.beans.PropertyChangeEvent
import java.beans.PropertyChangeListener
import javax.swing.JButton
import javax.swing.JCheckBox
import javax.swing.JLabel
import javax.swing.JPanel
Expand All @@ -39,7 +33,6 @@ class PreferencesPanelMisc :
private val lblRowsToShow = JLabel("Rows to show on Leaderboards")
val nfLeaderboardSize = NumberField(10, 200)
val chckbxShowAnimations = JCheckBox("Play sounds/animations")
private val btnPartyMode = JButton("Enter Party Mode")

init {
add(panelCenter, BorderLayout.CENTER)
Expand All @@ -60,14 +53,12 @@ class PreferencesPanelMisc :
panelCenter.add(chckbxAiAutomaticallyFinish, "flowx,cell 0 2")
panelCenter.add(chckbxCheckForUpdates, "flowx,cell 0 3")
panelCenter.add(chckbxShowAnimations, "cell 0 4")
panelCenter.add(btnPartyMode, "cell 0 5")

slider.addChangeListener(this)
nfLeaderboardSize.addPropertyChangeListener(this)
chckbxAiAutomaticallyFinish.addActionListener(this)
chckbxCheckForUpdates.addActionListener(this)
chckbxShowAnimations.addActionListener(this)
btnPartyMode.addActionListener(this)
}

override fun refreshImpl(useDefaults: Boolean) {
Expand Down Expand Up @@ -99,13 +90,6 @@ class PreferencesPanelMisc :
PreferenceUtil.saveBoolean(PREFERENCES_BOOLEAN_SHOW_ANIMATIONS, showAnimations)
}

private fun enterPartyMode() {
logger.info(CODE_PARTY_MODE, "Entering party mode!")
InjectedThings.partyMode = true

ScreenCache.switch<MenuScreen>()
}

override fun hasOutstandingChanges() =
slider.value != PreferenceUtil.getIntValue(PREFERENCES_INT_AI_SPEED) ||
nfLeaderboardSize.value !=
Expand All @@ -122,10 +106,6 @@ class PreferencesPanelMisc :
override fun propertyChange(evt: PropertyChangeEvent?) = stateChanged()

override fun actionPerformed(e: ActionEvent?) {
if (e?.source == btnPartyMode) {
enterPartyMode()
} else {
stateChanged()
}
stateChanged()
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/dartzee/utils/X01Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ fun getCheckoutScores(): MutableList<Int> {

fun getCheckoutSingles(): List<Int> = getCheckoutScores().map { it / 2 }

fun isFinishRound(round: List<Dart>): Boolean {
fun isFinishRound(round: List<Dart>, finishType: FinishType): Boolean {
val drt = round.last()
return drt.isDouble() && drt.getTotal() == drt.startingScore
return drt.getTotal() == drt.startingScore && !isBust(drt, finishType)
}

/** Refactored out of GameWrapper for use in game stats panel */
Expand Down
26 changes: 26 additions & 0 deletions src/test/kotlin/dartzee/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import io.kotest.matchers.shouldBe
import io.mockk.MockKMatcherScope
import java.awt.Color
import java.awt.Component
import java.awt.Container
import java.awt.Dimension
import java.awt.Point
import java.io.File
Expand Down Expand Up @@ -300,3 +301,28 @@ fun ImageIcon.toLabel(): JLabel {
label.repaint()
return label
}

fun <T> waitForAssertionWithReturn(timeout: Int = 10000, assertion: (() -> T)): T {
val startTime = System.currentTimeMillis()
while (true) {
try {
return assertion()
} catch (e: AssertionError) {
Thread.sleep(200)

val elapsed = System.currentTimeMillis() - startTime
if (elapsed > timeout) {
throw AssertionError("Timed out waiting for assertion - see cause for details", e)
}
}
}
}

fun Container.clickButton(
name: String? = null,
text: String? = null,
async: Boolean = false,
filterFn: ((JButton) -> Boolean)? = null,
) {
clickChild<JButton>(name, text, async, filterFn)
}
2 changes: 1 addition & 1 deletion src/test/kotlin/dartzee/e2e/AbstractE2ETest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open class AbstractE2ETest : AbstractRegistryTest() {

@BeforeEach
open fun beforeEach() {
PreferenceUtil.saveInt(PREFERENCES_INT_AI_SPEED, 0)
PreferenceUtil.saveInt(PREFERENCES_INT_AI_SPEED, 50)
PreferenceUtil.saveBoolean(PREFERENCES_BOOLEAN_AI_AUTO_CONTINUE, true)
PreferenceUtil.saveBoolean(PREFERENCES_BOOLEAN_SHOW_ANIMATIONS, false)
}
Expand Down
27 changes: 25 additions & 2 deletions src/test/kotlin/dartzee/e2e/GameHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import dartzee.achievements.runConversionsWithProgressBar
import dartzee.ai.AI_DARTBOARD
import dartzee.ai.DartsAiModel
import dartzee.ai.getPointForScore
import dartzee.bean.GameSetupPlayerSelector
import dartzee.bean.ParticipantAvatar
import dartzee.clickButton
import dartzee.core.bean.ScrollTable
import dartzee.core.util.DateStatics
import dartzee.core.util.getSortedValues
import dartzee.db.DartEntity
Expand Down Expand Up @@ -101,12 +104,32 @@ fun DartsGamePanel<*, *>.startGame(players: List<PlayerEntity>): List<IWrappedPa
return participants
}

fun GameSetupPlayerSelector.selectTopPlayer() {
getChild<ScrollTable>("TableUnselected").selectRow(0)
clickButton("Select")
}

fun DartsGamePanel<*, *>.throwHumanRound(vararg darts: Dart) {
darts.forEach { throwHumanDart(it.score, it.segmentType) }

confirmRound()
}

fun DartsGamePanel<*, *>.throwHumanDart(score: Int, segmentType: SegmentType) {
val singleTwentyPt = getPointForScore(score, segmentType)
val computedPt = AI_DARTBOARD.toComputedPoint(singleTwentyPt)
val computedPt =
if (segmentType == SegmentType.MISS) AI_DARTBOARD.getDeliberateMissPoint()
else {
val singleTwentyPt = getPointForScore(score, segmentType)
AI_DARTBOARD.toComputedPoint(singleTwentyPt)
}

getChild<GameplayDartboard>().dartThrown(computedPt)
}

fun DartsGamePanel<*, *>.confirmRound() {
clickChild<JButton> { it.toolTipText == "Confirm round" }
}

fun DartsGamePanel<*, *>.awaitTurn(participant: IWrappedParticipant) {
val scorer = getChild<AbstractDartsScorer<*>> { it.participant == participant }
waitForAssertion { scorer.lblAvatar.shouldBeSelected() }
Expand Down
Loading

0 comments on commit 397690c

Please sign in to comment.