dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 57b0e46cbe45128df1aa929635549ab265bcdd23
parent 4ff4c2f0ae5e91602e9096d58366bf28cbfbd52c
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 15 Jun 2023 12:14:22 -0700

cleanup

Diffstat:
Mpkg/web/handlers/chess.go | 10+++++++++-
Mpkg/web/handlers/interceptors/chess.go | 42++++++++----------------------------------
2 files changed, 17 insertions(+), 35 deletions(-)

diff --git a/pkg/web/handlers/chess.go b/pkg/web/handlers/chess.go @@ -606,6 +606,15 @@ func ChessGameHandler(c echo.Context) error { game := g.Game + piecesCache1 := make(map[string]chess.Square) + pos := game.Positions()[0] + for i := 0; i < 64; i++ { + sq := chess.Square(i) + if pos.Board().Piece(sq) != chess.NoPiece { + piecesCache1["piece_"+sq.String()] = sq + } + } + isFlipped := authUser.ID == g.Player2.ID isSpectator := !g.IsPlayer(authUser.ID) @@ -720,7 +729,6 @@ Loop: moves := game.Moves()[:moveIdx] lastMove := moves[len(moves)-1] piecesCache := interceptors.InitPiecesCache(moves) - piecesCache1 := g.PiecesCache1 squareMap := pos.Board().SquareMap() var bestMove *chess.Move diff --git a/pkg/web/handlers/interceptors/chess.go b/pkg/web/handlers/interceptors/chess.go @@ -49,15 +49,14 @@ type ChessPlayer struct { } type ChessGame struct { - DbChessGame *database.ChessGame - Key string - Game *chess.Game - lastUpdated time.Time - Player1 *ChessPlayer - Player2 *ChessPlayer - CreatedAt time.Time - piecesCache map[chess.Square]string - PiecesCache1 map[string]chess.Square + DbChessGame *database.ChessGame + Key string + Game *chess.Game + lastUpdated time.Time + Player1 *ChessPlayer + Player2 *ChessPlayer + CreatedAt time.Time + piecesCache map[chess.Square]string } func newChessPlayer(player database.User) *ChessPlayer { @@ -83,7 +82,6 @@ func newChessGame(gameKey string, player1, player2 database.User, dbChessGame *d g.Player1 = newChessPlayer(player1) g.Player2 = newChessPlayer(player2) g.piecesCache = InitPiecesCache(g.Game.Moves()) - g.PiecesCache1 = InitPiecesCache1(g.Game.Moves()) return g } @@ -648,7 +646,6 @@ func (b *Chess) SendMove(gameKey string, userID database.UserID, g *ChessGame, c } piecesCache := g.piecesCache - piecesCache1 := g.PiecesCache1 currentPlayer := player1 opponentPlayer := player2 @@ -716,7 +713,6 @@ func (b *Chess) SendMove(gameKey string, userID database.UserID, g *ChessGame, c } updatePiecesCache(piecesCache, mov) - updatePiecesCache1(piecesCache1, mov) var checkIDStr string if mov.HasTag(chess.Check) { @@ -767,7 +763,6 @@ func (g *ChessGame) MakeMoves(movesStr string, db *database.DkfDB) { func (g *ChessGame) MoveStr(m string) { game := g.Game piecesCache := g.piecesCache - piecesCache1 := g.PiecesCache1 validMoves := game.Position().ValidMoves() var mov chess.Move for _, move := range validMoves { @@ -779,7 +774,6 @@ func (g *ChessGame) MoveStr(m string) { } updatePiecesCache(piecesCache, mov) - updatePiecesCache1(piecesCache1, mov) _ = game.MoveStr(m) } @@ -809,22 +803,6 @@ func InitPiecesCache(moves []*chess.Move) map[chess.Square]string { return piecesCache } -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["piece_"+sq.String()] = sq - } - } - for _, m := range moves { - updatePiecesCache1(piecesCache1, *m) - } - return piecesCache1 -} - func updatePiecesCache(piecesCache map[chess.Square]string, mov chess.Move) { idStr1 := piecesCache[mov.S1()] delete(piecesCache, mov.S1()) @@ -850,10 +828,6 @@ func updatePiecesCache(piecesCache map[chess.Square]string, mov chess.Move) { } } -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 func pieceMap(board *chess.Board) map[chess.Piece]int { m := board.SquareMap()