commit 9d1d08d3509a88633ed6b317d4199f9a042276cf
parent 968bfd689ed580c498918cf9d583fcd2ae7576cf
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 15 Jun 2023 00:47:43 -0700
simplify code
Diffstat:
1 file changed, 41 insertions(+), 24 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -637,9 +637,10 @@ Loop:
moveIdx := payload.MoveIdx
if moveIdx != 0 {
pos := game.Positions()[moveIdx]
- boardMap := pos.Board().SquareMap()
moves := game.Moves()[:moveIdx]
lastMove := moves[len(moves)-1]
+ piecesCache := interceptors.InitPiecesCache(moves)
+ squareMap := pos.Board().SquareMap()
var bestMove *chess.Move
bestMoveStr := payload.BestMove
@@ -657,34 +658,22 @@ Loop:
checkIDStr = interceptors.BlackKingID
}
- var styles StylesBuilder
- renderChecks(&styles, checkIDStr)
-
- for i := 0; i < 64; i++ {
- styles.Appendf(`#piece_%s { display: none !important; }`, chess.Square(i).String())
- }
-
- renderLastMove(&styles, *lastMove)
-
- // Render best move
- if bestMove != nil {
- styles.Appendf(`.square_%d, .square_%d { background-color: rgba(0, 0, 255, 0.2) !important; }`,
- int(bestMove.S1()), int(bestMove.S2()))
- }
-
- piecesCache := interceptors.InitPiecesCache(moves)
+ // Calc visible pieces
+ visiblePieces := make(map[chess.Square]struct{})
for i := 0; i < 64; i++ {
sq := chess.Square(i)
if pos.Board().Piece(sq) != chess.NoPiece {
- sqID := piecesCache[sq]
- x1, y1 := squareCoord(sq, isFlipped)
- p := boardMap[sq]
- styles.Appendf("#%s { display: block !important; "+
- "left: calc(%d*12.5%%) !important; top: calc(%d*12.5%%) !important; "+
- "background-image: url(/public/img/chess/"+p.Color().String()+strings.ToUpper(p.Type().String())+".png) !important; }",
- sqID, x1, y1)
+ visiblePieces[sq] = struct{}{}
}
}
+
+ var styles StylesBuilder
+ renderHideAllNonVisiblePieces(&styles, visiblePieces)
+ renderChecks(&styles, checkIDStr)
+ renderLastMove(&styles, *lastMove)
+ renderBestMove(&styles, bestMove)
+ renderShowVisiblePieceInPosition(&styles, visiblePieces, squareMap, piecesCache, isFlipped)
+
send(styles.Build())
c.Response().Flush()
}
@@ -778,6 +767,34 @@ Loop:
return nil
}
+func renderShowVisiblePieceInPosition(styles *StylesBuilder, visiblePieces map[chess.Square]struct{}, squareMap map[chess.Square]chess.Piece, piecesCache map[chess.Square]string, isFlipped bool) {
+ for sq := range visiblePieces {
+ sqID := piecesCache[sq]
+ x1, y1 := squareCoord(sq, isFlipped)
+ p := squareMap[sq]
+ styles.Appendf("#%s { display: block !important; "+
+ "left: calc(%d*12.5%%) !important; top: calc(%d*12.5%%) !important; "+
+ "background-image: url(/public/img/chess/"+p.Color().String()+strings.ToUpper(p.Type().String())+".png) !important; }",
+ sqID, x1, y1)
+ }
+}
+
+func renderHideAllNonVisiblePieces(styles *StylesBuilder, visiblePieces map[chess.Square]struct{}) {
+ for i := 0; i < 64; i++ {
+ sq := chess.Square(i)
+ if _, found := visiblePieces[sq]; !found {
+ styles.Appendf(`#piece_%s { display: none !important; }`, sq.String())
+ }
+ }
+}
+
+func renderBestMove(styles *StylesBuilder, bestMove *chess.Move) {
+ if bestMove != nil {
+ styles.Appendf(`.square_%d, .square_%d { background-color: rgba(0, 0, 255, 0.2) !important; }`,
+ int(bestMove.S1()), int(bestMove.S2()))
+ }
+}
+
func renderLastMove(styles *StylesBuilder, lastMove chess.Move) {
styles.Appendf(`.square { background-color: transparent !important; }`)
styles.Appendf(`.square_%d, .square_%d { background-color: %s !important; }`,