Skip to content

Commit

Permalink
simplified game launch in party mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Mar 5, 2024
1 parent a1b0d43 commit 7837b85
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/dartzee/bean/AbstractPlayerSelector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class AbstractPlayerSelector<S : ScrollTable> :
private val btnUnselect = JButton("")

protected fun render() {
layout = MigLayout("", "[452px][100px][452px]", "[407px]")
layout = MigLayout("al center center", "[452px][100px][452px]", "[407px]")

val panelMovementOptions = JPanel()
add(tablePlayersToSelectFrom, "cell 0 0,alignx left,growy")
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/dartzee/bean/GameSetupPlayerSelector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ class GameSetupPlayerSelector : AbstractPlayerSelector<ScrollTableOrdered>() {
val selectedPlayers = getSelectedPlayers()
val rowCount = selectedPlayers.size
if (rowCount < 1) {
DialogUtil.showErrorOLD("You must select at least 1 player.")
DialogUtil.showError("You must select at least 1 player.")
return false
}

val playerOrTeamDesc = if (btnPairs.isSelected) "teams" else "players"
val matchMinimum = if (btnPairs.isSelected) 4 else 2
if (match && rowCount < matchMinimum) {
DialogUtil.showErrorOLD("You must select at least 2 $playerOrTeamDesc for a match.")
DialogUtil.showError("You must select at least 2 $playerOrTeamDesc for a match.")
return false
}

val maxPlayers = if (btnPairs.isSelected) MAX_PLAYERS * 2 else MAX_PLAYERS
if (rowCount > maxPlayers) {
DialogUtil.showErrorOLD("You cannot select more than $MAX_PLAYERS $playerOrTeamDesc.")
DialogUtil.showError("You cannot select more than $MAX_PLAYERS $playerOrTeamDesc.")
return false
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/dartzee/screen/EmbeddedScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract class EmbeddedScreen : JPanel(), ActionListener {
ScreenCache.switch(getBackTarget(), false)
}

fun toggleNextVisibility(visible: Boolean) {
protected fun toggleNextVisibility(visible: Boolean) {
btnNext.isVisible = visible
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/dartzee/screen/GameSetupScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GameSetupScreen : EmbeddedScreen() {
private val panelGameType = JPanel()
private val panelPlayers = JPanel()
private val launchPanel = JPanel()
val btnLaunch = JButton("Launch Game")
private val btnLaunch = JButton("Launch Game")
val playerSelector = GameSetupPlayerSelector()
val gameTypeComboBox = ComboBoxGameType()
private val panelGameTypeCb = JPanel()
Expand Down Expand Up @@ -93,6 +93,7 @@ class GameSetupScreen : EmbeddedScreen() {
spinners.forEachIndexed { ix, spinner ->
panelPointBreakdown.add(spinner, "cell $ix 0,alignx center")
}
btnLaunch.name = "LaunchButton"

matchConfigPanel.addActionListener(this)
gameTypeComboBox.addActionListener(this)
Expand Down
10 changes: 9 additions & 1 deletion src/main/kotlin/dartzee/screen/MenuScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,21 @@ class MenuScreen : EmbeddedScreen() {
}
}

private fun newGame() {
if (InjectedThings.partyMode) {
ScreenCache.switch<SimplePlayerSelectionScreen>()
} else {
ScreenCache.switch<GameSetupScreen>()
}
}

override fun showBackButton() = false

override fun actionPerformed(arg0: ActionEvent) {
when (arg0.source) {
btnPreferences -> ScreenCache.switch<PreferencesScreen>()
btnSyncSummary -> ScreenCache.switch<SyncManagementScreen>()
btnNewGame -> ScreenCache.switch<GameSetupScreen>()
btnNewGame -> newGame()
btnManagePlayers -> ScreenCache.switch<PlayerManagementScreen>()
btnGameReport -> ScreenCache.switch<ReportingSetupScreen>()
btnLeaderboards -> ScreenCache.switch<LeaderboardsScreen>()
Expand Down
43 changes: 43 additions & 0 deletions src/main/kotlin/dartzee/screen/SimplePlayerSelectionScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dartzee.screen

import dartzee.bean.GameSetupPlayerSelector
import dartzee.game.FinishType
import dartzee.game.GameLaunchParams
import dartzee.game.GameType
import dartzee.game.X01Config
import dartzee.utils.InjectedThings.gameLauncher
import java.awt.BorderLayout

class SimplePlayerSelectionScreen : EmbeddedScreen() {
private val playerSelector = GameSetupPlayerSelector()

init {
add(playerSelector, BorderLayout.CENTER)
}

override fun initialise() {
playerSelector.init()
}

override fun nextPressed() {
if (!playerSelector.valid(false)) {
return
}

val launchParams =
GameLaunchParams(
playerSelector.getSelectedPlayers(),
GameType.X01,
X01Config(301, FinishType.Any).toJson(),
playerSelector.pairMode(),
)

gameLauncher.launchNewGame(launchParams)
}

override fun getNextText() = "Launch Game"

override fun showNextButton() = true

override fun getScreenName() = "Select Players"
}
15 changes: 15 additions & 0 deletions src/test/kotlin/dartzee/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.github.alyssaburlton.swingtest.findAll
import com.github.alyssaburlton.swingtest.findWindow
import com.github.alyssaburlton.swingtest.flushEdt
import com.github.alyssaburlton.swingtest.getChild
import com.github.alyssaburlton.swingtest.purgeWindows
import com.github.alyssaburlton.swingtest.typeText
import dartzee.bean.ComboBoxGameType
import dartzee.bean.InteractiveDartboard
Expand Down Expand Up @@ -251,6 +252,20 @@ fun <T> runAsync(block: () -> T?): T? {
return result
}

fun runExpectingError(errorText: String, block: () -> Boolean) {
var result: Boolean? = null
SwingUtilities.invokeLater { result = block() }

flushEdt()
getErrorDialog().getDialogMessage() shouldBe errorText

getErrorDialog().clickOk()
flushEdt()
purgeWindows()

result shouldBe false
}

fun purgeGameAndConfirm(localId: Long): String {
runAsync { DevUtilities.purgeGame(localId) }

Expand Down
42 changes: 11 additions & 31 deletions src/test/kotlin/dartzee/bean/TestGameSetupPlayerSelector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import com.github.alyssaburlton.swingtest.clickChild
import dartzee.db.PlayerEntity
import dartzee.helper.AbstractTest
import dartzee.helper.insertPlayer
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.collections.shouldContainExactly
import dartzee.runExpectingError
import io.kotest.matchers.shouldBe
import javax.swing.JToggleButton
import org.junit.jupiter.api.Test
Expand All @@ -17,10 +15,8 @@ class TestGameSetupPlayerSelector : AbstractTest() {
val selector = GameSetupPlayerSelector()
selector.init()

selector.valid(false) shouldBe false
selector.valid(true) shouldBe false

dialogFactory.errorsShown.shouldContain("You must select at least 1 player.")
runExpectingError("You must select at least 1 player.") { selector.valid(false) }
runExpectingError("You must select at least 1 player.") { selector.valid(true) }
}

@Test
Expand All @@ -29,9 +25,7 @@ class TestGameSetupPlayerSelector : AbstractTest() {

val selector = GameSetupPlayerSelector()
selector.init(listOf(alex))

selector.valid(false) shouldBe true
dialogFactory.errorsShown.shouldBeEmpty()
}

@Test
Expand All @@ -41,10 +35,9 @@ class TestGameSetupPlayerSelector : AbstractTest() {
val selector = GameSetupPlayerSelector()
selector.init(listOf(alex))

selector.valid(true) shouldBe false
dialogFactory.errorsShown.shouldContainExactly(
"You must select at least 2 players for a match."
)
runExpectingError("You must select at least 2 players for a match.") {
selector.valid(true)
}
}

@Test
Expand All @@ -55,10 +48,7 @@ class TestGameSetupPlayerSelector : AbstractTest() {
selector.init(listOf(alex, alyssa))
selector.clickChild<JToggleButton>()

selector.valid(true) shouldBe false
dialogFactory.errorsShown.shouldContainExactly(
"You must select at least 2 teams for a match."
)
runExpectingError("You must select at least 2 teams for a match.") { selector.valid(true) }
}

@Test
Expand All @@ -73,7 +63,6 @@ class TestGameSetupPlayerSelector : AbstractTest() {

selector.valid(true) shouldBe true
selector.valid(false) shouldBe true
dialogFactory.errorsShown.shouldBeEmpty()

val p = insertPlayer()
players.add(p)
Expand All @@ -90,7 +79,6 @@ class TestGameSetupPlayerSelector : AbstractTest() {

selector.valid(true) shouldBe true
selector.valid(false) shouldBe true
dialogFactory.errorsShown.shouldBeEmpty()

val p = insertPlayer()
players.add(p)
Expand All @@ -107,12 +95,8 @@ class TestGameSetupPlayerSelector : AbstractTest() {
val selector = GameSetupPlayerSelector()
selector.init(players)

selector.valid(true) shouldBe false
dialogFactory.errorsShown.shouldContainExactly("You cannot select more than 6 players.")

dialogFactory.errorsShown.clear()
selector.valid(false) shouldBe false
dialogFactory.errorsShown.shouldContainExactly("You cannot select more than 6 players.")
runExpectingError("You cannot select more than 6 players.") { selector.valid(true) }
runExpectingError("You cannot select more than 6 players.") { selector.valid(false) }
}

@Test
Expand All @@ -124,12 +108,8 @@ class TestGameSetupPlayerSelector : AbstractTest() {
selector.init(players)
selector.clickChild<JToggleButton>()

selector.valid(true) shouldBe false
dialogFactory.errorsShown.shouldContainExactly("You cannot select more than 6 teams.")

dialogFactory.errorsShown.clear()
selector.valid(false) shouldBe false
dialogFactory.errorsShown.shouldContainExactly("You cannot select more than 6 teams.")
runExpectingError("You cannot select more than 6 teams.") { selector.valid(true) }
runExpectingError("You cannot select more than 6 teams.") { selector.valid(false) }
}

@Test
Expand Down
Loading

0 comments on commit 7837b85

Please sign in to comment.