initial nip64 implementation

This commit is contained in:
nrobi144
2025-12-29 12:17:40 +02:00
parent a70101f5e5
commit 19c36c2979
15 changed files with 2266 additions and 0 deletions
@@ -0,0 +1,145 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.commons.chess
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.vitorpamplona.quartz.nip64Chess.ChessPosition
import com.vitorpamplona.quartz.nip64Chess.PieceType
import com.vitorpamplona.quartz.nip64Chess.Color as ChessColor
/**
* Renders an 8x8 chess board with pieces
* Shared composable for Android and Desktop platforms
*
* @param position The chess position to display
* @param modifier Modifier for the board
* @param boardSize Total size of the board in dp
* @param showCoordinates Whether to show file labels (a-h) on bottom rank
*/
@Composable
fun ChessBoard(
position: ChessPosition,
modifier: Modifier = Modifier,
boardSize: Dp = 320.dp,
showCoordinates: Boolean = true,
) {
val squareSize = boardSize / 8
Column(modifier = modifier.size(boardSize)) {
// Render ranks 8 down to 1 (from white's perspective)
for (rank in 7 downTo 0) {
Row {
for (file in 0..7) {
val piece = position.pieceAt(file, rank)
val isLightSquare = (rank + file) % 2 == 0
ChessSquare(
piece = piece,
isLight = isLightSquare,
size = squareSize,
coordinate =
if (showCoordinates && rank == 0) {
('a' + file).toString()
} else {
null
},
)
}
}
}
}
}
/**
* Renders a single square on the chess board
*/
@Composable
private fun ChessSquare(
piece: com.vitorpamplona.quartz.nip64Chess.ChessPiece?,
isLight: Boolean,
size: Dp,
coordinate: String?,
) {
Box(
modifier =
Modifier
.size(size)
.background(
if (isLight) Color(0xFFF0D9B5) else Color(0xFFB58863),
),
contentAlignment = Alignment.Center,
) {
// Display piece using Unicode chess symbols
piece?.let {
Text(
text = it.toUnicode(),
fontSize = (size.value * 0.6).sp,
textAlign = TextAlign.Center,
color = MaterialTheme.colorScheme.onSurface,
)
}
// Show file coordinate (a-h) on bottom rank
coordinate?.let {
Text(
text = it,
fontSize = 10.sp,
fontWeight = FontWeight.Bold,
color = if (isLight) Color(0xFFB58863) else Color(0xFFF0D9B5),
modifier =
Modifier
.align(Alignment.BottomEnd)
.padding(2.dp),
)
}
}
}
/**
* Extension function to convert ChessPiece to Unicode chess symbol
*/
private fun com.vitorpamplona.quartz.nip64Chess.ChessPiece.toUnicode(): String {
val white = color == ChessColor.WHITE
return when (type) {
PieceType.KING -> if (white) "" else ""
PieceType.QUEEN -> if (white) "" else ""
PieceType.ROOK -> if (white) "" else ""
PieceType.BISHOP -> if (white) "" else ""
PieceType.KNIGHT -> if (white) "" else ""
PieceType.PAWN -> if (white) "" else ""
}
}
@@ -0,0 +1,172 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.commons.chess
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import com.vitorpamplona.quartz.nip64Chess.PGNParser
/**
* Complete chess game viewer with board, metadata, and navigation
* Shared composable for Android and Desktop platforms
*
* Parses PGN content and displays:
* - Game metadata (event, players, result)
* - Interactive chess board
* - Move navigation controls
*
* @param pgnContent PGN format string
* @param modifier Modifier for the viewer
*/
@Composable
fun ChessGameViewer(
pgnContent: String,
modifier: Modifier = Modifier,
) {
val gameResult =
remember(pgnContent) {
PGNParser.parse(pgnContent)
}
gameResult.fold(
onSuccess = { game ->
ChessGameDisplay(game, modifier)
},
onFailure = { error ->
ChessGameError(
errorMessage = error.message ?: "Failed to parse PGN",
pgnContent = pgnContent,
modifier = modifier,
)
},
)
}
/**
* Displays a successfully parsed chess game
*/
@Composable
private fun ChessGameDisplay(
game: com.vitorpamplona.quartz.nip64Chess.ChessGame,
modifier: Modifier = Modifier,
) {
var currentMoveIndex by remember { mutableStateOf(0) }
Card(
modifier = modifier.fillMaxWidth(),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
) {
Column(
modifier = Modifier.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
// Game metadata header
PGNMetadata(game = game)
// Chess board
ChessBoard(
position = game.positionAt(currentMoveIndex),
modifier =
Modifier
.fillMaxWidth()
.aspectRatio(1f),
boardSize = 320.dp,
)
// Move navigation
MoveNavigator(
currentMove = currentMoveIndex,
totalMoves = game.moves.size,
onMoveChange = { currentMoveIndex = it },
)
}
}
}
/**
* Displays error state when PGN parsing fails
*/
@Composable
private fun ChessGameError(
errorMessage: String,
pgnContent: String,
modifier: Modifier = Modifier,
) {
Card(
modifier = modifier.fillMaxWidth(),
colors =
CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.errorContainer,
),
) {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
// Error title
Text(
text = "Invalid Chess Game",
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.error,
)
// Error message
Text(
text = errorMessage,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onErrorContainer,
)
// PGN content label
Text(
text = "PGN Content:",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onErrorContainer,
modifier = Modifier.padding(top = 4.dp),
)
// Raw PGN content
Text(
text = pgnContent,
style = MaterialTheme.typography.bodySmall,
fontFamily = FontFamily.Monospace,
color = MaterialTheme.colorScheme.onErrorContainer,
)
}
}
}
@@ -0,0 +1,137 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.commons.chess
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowLeft
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.SkipNext
import androidx.compose.material.icons.filled.SkipPrevious
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
/**
* Move navigation controls for stepping through chess game positions
* Shared composable for Android and Desktop platforms
*
* @param currentMove Current move index (0 = starting position)
* @param totalMoves Total number of moves in the game
* @param onMoveChange Callback when user navigates to a different move
* @param modifier Modifier for the navigator
*/
@Composable
fun MoveNavigator(
currentMove: Int,
totalMoves: Int,
onMoveChange: (Int) -> Unit,
modifier: Modifier = Modifier,
) {
Row(
modifier = modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
) {
// First move (starting position)
IconButton(
onClick = { onMoveChange(0) },
enabled = currentMove > 0,
) {
Icon(
Icons.Default.SkipPrevious,
contentDescription = "Start position",
tint =
if (currentMove > 0) {
MaterialTheme.colorScheme.onSurface
} else {
MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)
},
)
}
// Previous move
IconButton(
onClick = { onMoveChange((currentMove - 1).coerceAtLeast(0)) },
enabled = currentMove > 0,
) {
Icon(
Icons.Default.KeyboardArrowLeft,
contentDescription = "Previous move",
tint =
if (currentMove > 0) {
MaterialTheme.colorScheme.onSurface
} else {
MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)
},
)
}
// Current position indicator
Text(
text = "Move $currentMove / $totalMoves",
style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.padding(horizontal = 16.dp),
)
// Next move
IconButton(
onClick = { onMoveChange((currentMove + 1).coerceAtMost(totalMoves)) },
enabled = currentMove < totalMoves,
) {
Icon(
Icons.Default.KeyboardArrowRight,
contentDescription = "Next move",
tint =
if (currentMove < totalMoves) {
MaterialTheme.colorScheme.onSurface
} else {
MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)
},
)
}
// Last move (final position)
IconButton(
onClick = { onMoveChange(totalMoves) },
enabled = currentMove < totalMoves,
) {
Icon(
Icons.Default.SkipNext,
contentDescription = "Final position",
tint =
if (currentMove < totalMoves) {
MaterialTheme.colorScheme.onSurface
} else {
MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)
},
)
}
}
}
@@ -0,0 +1,135 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.commons.chess
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.vitorpamplona.quartz.nip64Chess.ChessGame
/**
* Display PGN game metadata (Event, players, result, etc.)
* Shared composable for Android and Desktop platforms
*
* @param game The chess game with metadata to display
* @param modifier Modifier for the metadata display
*/
@Composable
fun PGNMetadata(
game: ChessGame,
modifier: Modifier = Modifier,
) {
Column(
modifier = modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
// Event name
game.event?.let { event ->
Text(
text = event,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onSurface,
)
}
// Site/location (if available)
game.site?.let { site ->
Text(
text = site,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
// Players
if (game.white != null || game.black != null) {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
game.white?.let { white ->
Text(
text = white,
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.onSurface,
)
}
Text(
text = "vs",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
game.black?.let { black ->
Text(
text = black,
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.onSurface,
)
}
}
}
// Date and Result
Row(
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically,
) {
game.date?.let { date ->
Text(
text = date,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
// Result
Text(
text = "Result: ${game.result.notation}",
style = MaterialTheme.typography.bodySmall,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
// Round (if available)
game.round?.let { round ->
Text(
text = "Round $round",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
}