commit 4ff4c2f0ae5e91602e9096d58366bf28cbfbd52c
parent 8460d2a386a5b579355357281224e28dbf7a6f94
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 15 Jun 2023 11:47:09 -0700
cleanup
Diffstat:
2 files changed, 12 insertions(+), 28 deletions(-)
diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go
@@ -842,22 +842,7 @@ Loop:
return nil
}
-var strToSquareMap = map[string]chess.Square{
- "a1": chess.A1, "a2": chess.A2, "a3": chess.A3, "a4": chess.A4, "a5": chess.A5, "a6": chess.A6, "a7": chess.A7, "a8": chess.A8,
- "b1": chess.B1, "b2": chess.B2, "b3": chess.B3, "b4": chess.B4, "b5": chess.B5, "b6": chess.B6, "b7": chess.B7, "b8": chess.B8,
- "c1": chess.C1, "c2": chess.C2, "c3": chess.C3, "c4": chess.C4, "c5": chess.C5, "c6": chess.C6, "c7": chess.C7, "c8": chess.C8,
- "d1": chess.D1, "d2": chess.D2, "d3": chess.D3, "d4": chess.D4, "d5": chess.D5, "d6": chess.D6, "d7": chess.D7, "d8": chess.D8,
- "e1": chess.E1, "e2": chess.E2, "e3": chess.E3, "e4": chess.E4, "e5": chess.E5, "e6": chess.E6, "e7": chess.E7, "e8": chess.E8,
- "f1": chess.F1, "f2": chess.F2, "f3": chess.F3, "f4": chess.F4, "f5": chess.F5, "f6": chess.F6, "f7": chess.F7, "f8": chess.F8,
- "g1": chess.G1, "g2": chess.G2, "g3": chess.G3, "g4": chess.G4, "g5": chess.G5, "g6": chess.G6, "g7": chess.G7, "g8": chess.G8,
- "h1": chess.H1, "h2": chess.H2, "h3": chess.H3, "h4": chess.H4, "h5": chess.H5, "h6": chess.H6, "h7": chess.H7, "h8": chess.H8,
-}
-
-func parseSq(s string) chess.Square {
- return strToSquareMap[s]
-}
-
-func renderShowVisiblePieceInPosition(styles *StylesBuilder, animationIdx *int, visiblePieces map[chess.Square]struct{}, squareMap map[chess.Square]chess.Piece, piecesCache map[chess.Square]string, piecesCache1 map[chess.Square]chess.Square, isFlipped bool) {
+func renderShowVisiblePieceInPosition(styles *StylesBuilder, animationIdx *int, visiblePieces map[chess.Square]struct{}, squareMap map[chess.Square]chess.Piece, piecesCache map[chess.Square]string, piecesCache1 map[string]chess.Square, isFlipped bool) {
animate := func(s1, s2 chess.Square, id string, p chess.Piece) {
x1, y1 := squareCoord(s1, isFlipped)
x2, y2 := squareCoord(s2, isFlipped)
@@ -873,12 +858,11 @@ func renderShowVisiblePieceInPosition(styles *StylesBuilder, animationIdx *int,
}
for newSq := range visiblePieces {
- sqID := piecesCache[newSq] // Get ID of piece on square newSq
- visiblePieceIDSqStr := parseSq(strings.TrimPrefix(sqID, "piece_")) // Get original board square
- currentSq := piecesCache1[visiblePieceIDSqStr] // Get current square location of the piece
- piece := squareMap[newSq] // Get the piece currently on the new square according to game data
- animate(currentSq, newSq, sqID, piece) // Move piece from current square to the new square where we want it to be
- piecesCache1[visiblePieceIDSqStr] = newSq // Update cache of location of the piece
+ sqID := piecesCache[newSq] // Get ID of piece on square newSq
+ currentSq := piecesCache1[sqID] // Get current square location of the piece
+ piece := squareMap[newSq] // Get the piece currently on the new square according to game data
+ animate(currentSq, newSq, sqID, piece) // Move piece from current square to the new square where we want it to be
+ piecesCache1[sqID] = newSq // Update cache of location of the piece
}
}
diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go
@@ -57,7 +57,7 @@ type ChessGame struct {
Player2 *ChessPlayer
CreatedAt time.Time
piecesCache map[chess.Square]string
- PiecesCache1 map[chess.Square]chess.Square
+ PiecesCache1 map[string]chess.Square
}
func newChessPlayer(player database.User) *ChessPlayer {
@@ -809,14 +809,14 @@ func InitPiecesCache(moves []*chess.Move) map[chess.Square]string {
return piecesCache
}
-func InitPiecesCache1(moves []*chess.Move) map[chess.Square]chess.Square {
- piecesCache1 := make(map[chess.Square]chess.Square)
+func InitPiecesCache1(moves []*chess.Move) map[string]chess.Square {
+ piecesCache1 := make(map[string]chess.Square)
game := chess.NewGame()
pos := game.Position()
for i := 0; i < 64; i++ {
sq := chess.Square(i)
if pos.Board().Piece(sq) != chess.NoPiece {
- piecesCache1[sq] = sq
+ piecesCache1["piece_"+sq.String()] = sq
}
}
for _, m := range moves {
@@ -850,8 +850,8 @@ func updatePiecesCache(piecesCache map[chess.Square]string, mov chess.Move) {
}
}
-func updatePiecesCache1(piecesCache1 map[chess.Square]chess.Square, mov chess.Move) {
- piecesCache1[mov.S1()] = mov.S2()
+func updatePiecesCache1(piecesCache1 map[string]chess.Square, mov chess.Move) {
+ piecesCache1["piece_"+mov.S1().String()] = mov.S2()
}
// Creates a map of pieces on the board and their count