Skip to content

Commit

Permalink
fix loading bug, update leaderboards to show avatars too
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Apr 21, 2024
1 parent 8b1c963 commit b2d50c3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 36 deletions.
9 changes: 4 additions & 5 deletions src/main/kotlin/dartzee/bean/ScrollTablePlayers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import dartzee.core.bean.ScrollTable
import dartzee.core.util.TableUtil
import dartzee.db.PlayerEntity
import dartzee.utils.InjectedThings
import dartzee.utils.getAvatarImage
import dartzee.utils.resize
import javax.swing.ImageIcon
import dartzee.utils.getImageForTableRow

fun ScrollTable.getSelectedPlayer(): PlayerEntity? {
val row = table.selectedRow
Expand Down Expand Up @@ -34,7 +32,6 @@ fun ScrollTable.initPlayerTableModel(players: List<PlayerEntity> = listOf()) {
setColumnWidths("25")
}


addPlayers(players)

setRowName("player")
Expand All @@ -44,7 +41,9 @@ fun ScrollTable.initPlayerTableModel(players: List<PlayerEntity> = listOf()) {
fun ScrollTable.addPlayers(players: List<PlayerEntity>) = players.forEach(::addPlayer)

private fun ScrollTable.addPlayer(player: PlayerEntity) {
val flag = if (InjectedThings.partyMode) ImageIcon(player.getAvatarImage().resize(50, 50)) else player.getFlag()
val flag =
if (InjectedThings.partyMode) getImageForTableRow(player.playerImageId)
else player.getFlag()
val row = arrayOf(flag, player)

addRow(row)
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/dartzee/core/bean/ScrollTableHyperlink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dartzee.core.bean
import java.awt.Color
import java.awt.Component
import java.awt.Cursor
import java.awt.Font
import java.awt.event.MouseEvent
import java.awt.font.TextAttribute
import javax.swing.JTable
Expand Down Expand Up @@ -96,7 +95,7 @@ abstract class ScrollTableHyperlink(private val linkColumnName: String, testId:
}

override fun getTableCellRendererComponent(
table: JTable?,
table: JTable,
value: Any?,
isSelected: Boolean,
hasFocus: Boolean,
Expand All @@ -119,7 +118,8 @@ abstract class ScrollTableHyperlink(private val linkColumnName: String, testId:

val fontAttributes = HashMap<TextAttribute, Int>()
fontAttributes[TextAttribute.UNDERLINE] = TextAttribute.UNDERLINE_ON
val hyperlinkFont = Font("Tahoma", Font.BOLD, 12).deriveFont(fontAttributes)
fontAttributes[TextAttribute.SIZE] = table.font.size
val hyperlinkFont = table.font.deriveFont(fontAttributes)

font = hyperlinkFont

Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/dartzee/screen/game/DartsGameScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ class DartsGameScreen(game: GameEntity, private val participants: List<IWrappedP
ScreenCache.addDartsGameScreen(gameId, this)

title = gamePanel.gameTitle
contentPane.add(gamePanel)

if (InjectedThings.partyMode) {
contentPane.add(tutorialPanel)
size = Dimension(1000, 600)
extendedState = Frame.MAXIMIZED_BOTH
} else {
contentPane.add(gamePanel)
}
}

fun startNewGame() {
if (!InjectedThings.partyMode) {
gamePanel.startNewGame(participants)
} else {
contentPane.remove(gamePanel)
contentPane.add(tutorialPanel)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ import dartzee.preferences.Preferences
import dartzee.utils.InjectedThings.mainDatabase
import dartzee.utils.InjectedThings.partyMode
import dartzee.utils.InjectedThings.preferenceService
import dartzee.utils.ResourceCache
import dartzee.utils.combinePlayerFlags
import dartzee.utils.doesHighestWin
import dartzee.utils.getFilterPanel
import dartzee.utils.getImageForTableRow
import dartzee.utils.getRawImageForTableRow
import dartzee.utils.splitAvatar
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.Font
import java.awt.event.ActionListener
import javax.swing.JPanel
import javax.swing.JRadioButton
Expand Down Expand Up @@ -88,6 +93,14 @@ class LeaderboardTotalScore(private val gameType: GameType, gameParams: String?
model.addRows(rows)

table.model = model

if (partyMode) {
table.setRowHeight(50)

val font = ResourceCache.BASE_FONT.deriveFont(Font.PLAIN, 20f)
table.setTableFont(font)
}

table.setColumnWidths("35;50")
table.sortBy(0, false)
}
Expand All @@ -109,7 +122,7 @@ class LeaderboardTotalScore(private val gameType: GameType, gameParams: String?
val playerWhereSql = panelPlayerFilters.getWhereSql()

val sb = StringBuilder()
sb.append("SELECT p.Strategy, p.Name, g.LocalId, pt.FinalScore")
sb.append("SELECT p.Strategy, p.Name, p.PlayerImageId, g.LocalId, pt.FinalScore")
sb.append(" FROM Participant pt, Game g, Player p")
sb.append(" WHERE pt.GameId = g.RowId")
sb.append(" AND pt.PlayerId = p.RowId")
Expand All @@ -129,7 +142,10 @@ class LeaderboardTotalScore(private val gameType: GameType, gameParams: String?
val localId = rs.getLong("LocalId")
val score = rs.getInt("FinalScore")

val flag = PlayerEntity.getPlayerFlag(strategy.isEmpty())
val flag =
if (partyMode) getImageForTableRow(rs.getString("PlayerImageId"))
else PlayerEntity.getPlayerFlag(strategy.isEmpty())

LeaderboardEntry(score, listOf(flag, playerName, localId, score))
}
}
Expand All @@ -140,7 +156,8 @@ class LeaderboardTotalScore(private val gameType: GameType, gameParams: String?

val sb = StringBuilder()
sb.append(
"SELECT p1.Strategy, p2.Strategy AS Strategy2, p1.Name, p2.Name as Name2, g.LocalId, t.FinalScore"
"SELECT p1.Strategy, p2.Strategy AS Strategy2, p1.Name, p2.Name as Name2, " +
"p1.PlayerImageId, p2.PlayerImageId as PlayerImageId2, g.LocalId, t.FinalScore"
)
sb.append(" FROM Team t, Participant pt1, Participant pt2, Game g, Player p1, Player p2")
sb.append(" WHERE t.GameId = g.RowId")
Expand Down Expand Up @@ -169,12 +186,23 @@ class LeaderboardTotalScore(private val gameType: GameType, gameParams: String?
val localId = rs.getLong("LocalId")
val score = rs.getInt("FinalScore")

val playerFlag = PlayerEntity.getPlayerFlag(strategy.isEmpty())
val playerFlag2 = PlayerEntity.getPlayerFlag(strategy2.isEmpty())
val combinedFlag = combinePlayerFlags(playerFlag, playerFlag2)
val icon =
if (partyMode) {
val imageIdOne = rs.getString("PlayerImageId")
val imageIdTwo = rs.getString("PlayerImageId2")

val imgOne = getRawImageForTableRow(imageIdOne)
val imgTwo = getRawImageForTableRow(imageIdTwo)
splitAvatar(imgOne, imgTwo, null, true)
} else {
val playerFlag = PlayerEntity.getPlayerFlag(strategy.isEmpty())
val playerFlag2 = PlayerEntity.getPlayerFlag(strategy2.isEmpty())
combinePlayerFlags(playerFlag, playerFlag2)
}

val playerName = "$playerName1 & $playerName2"

LeaderboardEntry(score, listOf(combinedFlag, playerName, localId, score))
LeaderboardEntry(score, listOf(icon, playerName, localId, score))
}
}

Expand Down
54 changes: 36 additions & 18 deletions src/main/kotlin/dartzee/utils/PlayerImageUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dartzee.utils
import dartzee.core.bean.paint
import dartzee.core.bean.toBufferedImage
import dartzee.db.PlayerEntity
import dartzee.db.PlayerImageEntity
import java.awt.Color
import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
Expand All @@ -20,41 +21,59 @@ fun splitAvatar(
selectedPlayer: PlayerEntity?,
gameFinished: Boolean
): ImageIcon {
val firstImg = playerOne.getAvatarImage()
val secondImg = playerTwo.getAvatarImage()
val firstImg = getAvatarImage(playerOne.playerImageId)
val secondImg = getAvatarImage(playerTwo.playerImageId)

val selectedImg =
when (selectedPlayer) {
playerOne -> firstImg
playerTwo -> secondImg
else -> null
}

return splitAvatar(firstImg, secondImg, selectedImg, gameFinished)
}

fun splitAvatar(
firstImg: BufferedImage,
secondImg: BufferedImage,
selectedImg: BufferedImage?,
gameFinished: Boolean
): ImageIcon {
val diagonalOffset =
if (gameFinished) 1.0
else
when (selectedPlayer) {
playerOne -> 1.4
playerTwo -> 0.6
when (selectedImg) {
firstImg -> 1.4
secondImg -> 0.6
else -> 1.0
}

val diagonalCenter = (PLAYER_IMAGE_WIDTH * diagonalOffset).toInt()
val diagonalThickness = if (selectedPlayer != null) 1 else 0
val diagonalCenter = (firstImg.width * diagonalOffset).toInt()
val diagonalThickness = if (selectedImg != null) 1 else 0

val newImage =
BufferedImage(PLAYER_IMAGE_WIDTH, PLAYER_IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB)
val newImage = BufferedImage(firstImg.width, firstImg.height, BufferedImage.TYPE_INT_ARGB)
newImage.paint { pt ->
val manhattan = pt.x + pt.y
if (abs(manhattan - diagonalCenter) <= diagonalThickness) {
Color.BLACK
} else {
val playerForSection = if (manhattan < diagonalCenter) playerOne else playerTwo
val originalImg = if (playerForSection == playerOne) firstImg else secondImg
val rgb = originalImg.getRGB(pt.x, pt.y)
val rgbToUse =
if (selectedPlayer == playerForSection || gameFinished) rgb else greyscale(rgb)
val imgForSection = if (manhattan < diagonalCenter) firstImg else secondImg
val rgb = imgForSection.getRGB(pt.x, pt.y)
val rgbToUse = if (selectedImg == imgForSection || gameFinished) rgb else greyscale(rgb)
Color(rgbToUse)
}
}

return ImageIcon(newImage)
}

fun PlayerEntity.getAvatarImage() = getAvatar().toBufferedImage(BufferedImage.TYPE_INT_RGB)
fun getAvatarImage(imageId: String) =
PlayerImageEntity.retrieveImageIconForId(imageId).toBufferedImage(BufferedImage.TYPE_INT_RGB)

fun getImageForTableRow(imageId: String) = ImageIcon(getRawImageForTableRow(imageId))

fun getRawImageForTableRow(imageId: String) = getAvatarImage(imageId).resize(50, 50)

fun combinePlayerFlags(flagOne: ImageIcon, flagTwo: ImageIcon): ImageIcon {
val imageOne = flagOne.toBufferedImage(BufferedImage.TYPE_INT_ARGB)
Expand Down Expand Up @@ -120,13 +139,12 @@ fun convertImageToAvatarDimensions(imageBytes: ByteArray): ByteArray {
val yc = (image.height - minDimension) / 2

val croppedImage = image.getSubimage(xc, yc, minDimension, minDimension)
val resizedImage =
croppedImage.resize(PLAYER_IMAGE_WIDTH, PLAYER_IMAGE_HEIGHT)
val resizedImage = croppedImage.resize(PLAYER_IMAGE_WIDTH, PLAYER_IMAGE_HEIGHT)

val baos = ByteArrayOutputStream()
ImageIO.write(resizedImage, "jpg", baos)
return baos.toByteArray()
}

fun BufferedImage.resize(width: Int, height: Int = width) =
getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH).toBufferedImage(width, height)
getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH).toBufferedImage(width, height)

0 comments on commit b2d50c3

Please sign in to comment.