fixes lol
Some checks failed
Build and Push / build-all (push) Failing after 58s

This commit is contained in:
doomtube 2026-01-08 01:13:57 -05:00
parent ab51b4b504
commit e704a1e1c4

View file

@ -17,6 +17,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/heroiclabs/nakama-common/api"
"github.com/heroiclabs/nakama-common/runtime" "github.com/heroiclabs/nakama-common/runtime"
"github.com/corentings/chess/v2" "github.com/corentings/chess/v2"
) )
@ -88,8 +89,8 @@ func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runti
return fmt.Errorf("failed to register before authenticate custom: %w", err) return fmt.Errorf("failed to register before authenticate custom: %w", err)
} }
// Create chess ELO leaderboard // Create chess ELO leaderboard (8 args for v1.44.0)
if err := nk.LeaderboardCreate(ctx, "chess-elo", true, "desc", "set", "", nil); err != nil { if err := nk.LeaderboardCreate(ctx, "chess-elo", true, "desc", "set", "", nil, true); err != nil {
logger.Info("Chess ELO leaderboard already exists or created") logger.Info("Chess ELO leaderboard already exists or created")
} }
@ -216,8 +217,8 @@ func validateJwt(token string, secret string) (*AppJwtClaims, error) {
return &claims, nil return &claims, nil
} }
// Before authenticate custom hook // Before authenticate custom hook - uses api.AuthenticateCustomRequest
func beforeAuthenticateCustom(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *runtime.AuthenticateCustomRequest) (*runtime.AuthenticateCustomRequest, error) { func beforeAuthenticateCustom(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateCustomRequest) (*api.AuthenticateCustomRequest, error) {
appJwt := in.GetAccount().GetId() appJwt := in.GetAccount().GetId()
if appJwt == "" { if appJwt == "" {
return nil, errors.New("authentication token required") return nil, errors.New("authentication token required")
@ -377,7 +378,7 @@ func (m *ChessMatch) MatchJoin(ctx context.Context, logger runtime.Logger, db *s
// Get user color from metadata // Get user color from metadata
userColor := "#561D5E" userColor := "#561D5E"
users, err := nk.UsersGetId(ctx, []string{userID}, nil) users, err := nk.UsersGetId(ctx, []string{userID}, nil)
if err == nil && len(users) > 0 && users[0].Metadata != nil { if err == nil && len(users) > 0 && users[0].Metadata != "" {
var meta map[string]string var meta map[string]string
if err := json.Unmarshal([]byte(users[0].Metadata), &meta); err == nil { if err := json.Unmarshal([]byte(users[0].Metadata), &meta); err == nil {
if c, ok := meta["user_color"]; ok && c != "" { if c, ok := meta["user_color"]; ok && c != "" {
@ -539,7 +540,7 @@ func (m *ChessMatch) MatchLoop(ctx context.Context, logger runtime.Logger, db *s
continue continue
} }
// Validate and apply the move using notnil/chess // Validate and apply the move
result := validateChessMove(s.FEN, moveData.From, moveData.To, moveData.Promotion) result := validateChessMove(s.FEN, moveData.From, moveData.To, moveData.Promotion)
if !result.Valid { if !result.Valid {
logger.Warn("Invalid move: %+v", moveData) logger.Warn("Invalid move: %+v", moveData)
@ -723,7 +724,7 @@ type MoveResult struct {
Reason string Reason string
} }
// Validate chess move using notnil/chess library // Validate chess move using corentings/chess/v2 library
func validateChessMove(fenStr, from, to, promotion string) MoveResult { func validateChessMove(fenStr, from, to, promotion string) MoveResult {
result := MoveResult{Turn: "w"} result := MoveResult{Turn: "w"}
@ -735,8 +736,8 @@ func validateChessMove(fenStr, from, to, promotion string) MoveResult {
game := chess.NewGame(fen) game := chess.NewGame(fen)
// Find the move // Find the move in valid moves
var targetMove *chess.Move found := false
for _, move := range game.ValidMoves() { for _, move := range game.ValidMoves() {
if move.S1().String() == from && move.S2().String() == to { if move.S1().String() == from && move.S2().String() == to {
// Check promotion // Check promotion
@ -747,17 +748,21 @@ func validateChessMove(fenStr, from, to, promotion string) MoveResult {
continue continue
} }
} }
targetMove = move found = true
break break
} }
} }
if targetMove == nil { if !found {
return result return result
} }
// Make the move // Make the move using MoveStr (UCI format)
if err := game.Move(targetMove); err != nil { moveStr := from + to
if promotion != "" {
moveStr += promotion
}
if err := game.MoveStr(moveStr); err != nil {
return result return result
} }
@ -812,13 +817,13 @@ func updateElo(ctx context.Context, nk runtime.NakamaModule, logger runtime.Logg
logger.Info("updateElo called: winner=%s (%s), loser=%s (%s)", winnerName, winnerID, loserName, loserID) logger.Info("updateElo called: winner=%s (%s), loser=%s (%s)", winnerName, winnerID, loserName, loserID)
// Get current ratings // Get current ratings (5 return values)
winnerRecords, err := nk.LeaderboardRecordsList(ctx, "chess-elo", []string{winnerID}, 1, "", 0) winnerRecords, _, _, _, err := nk.LeaderboardRecordsList(ctx, "chess-elo", []string{winnerID}, 1, "", 0)
if err != nil { if err != nil {
logger.Error("Failed to get winner ELO: %v", err) logger.Error("Failed to get winner ELO: %v", err)
return return
} }
loserRecords, err := nk.LeaderboardRecordsList(ctx, "chess-elo", []string{loserID}, 1, "", 0) loserRecords, _, _, _, err := nk.LeaderboardRecordsList(ctx, "chess-elo", []string{loserID}, 1, "", 0)
if err != nil { if err != nil {
logger.Error("Failed to get loser ELO: %v", err) logger.Error("Failed to get loser ELO: %v", err)
return return
@ -826,11 +831,11 @@ func updateElo(ctx context.Context, nk runtime.NakamaModule, logger runtime.Logg
winnerElo := int64(defaultElo) winnerElo := int64(defaultElo)
loserElo := int64(defaultElo) loserElo := int64(defaultElo)
if len(winnerRecords.Records) > 0 { if len(winnerRecords) > 0 {
winnerElo = winnerRecords.Records[0].Score winnerElo = winnerRecords[0].Score
} }
if len(loserRecords.Records) > 0 { if len(loserRecords) > 0 {
loserElo = loserRecords.Records[0].Score loserElo = loserRecords[0].Score
} }
// Calculate expected scores // Calculate expected scores
@ -1002,9 +1007,9 @@ func getChessLeaderboardRpc(ctx context.Context, logger runtime.Logger, db *sql.
return "", fmt.Errorf("failed to list leaderboard: %w", err) return "", fmt.Errorf("failed to list leaderboard: %w", err)
} }
var result []map[string]interface{} var resultList []map[string]interface{}
for _, r := range records { for _, r := range records {
result = append(result, map[string]interface{}{ resultList = append(resultList, map[string]interface{}{
"rank": r.Rank, "rank": r.Rank,
"userId": r.OwnerId, "userId": r.OwnerId,
"username": r.Username.Value, "username": r.Username.Value,
@ -1012,7 +1017,7 @@ func getChessLeaderboardRpc(ctx context.Context, logger runtime.Logger, db *sql.
}) })
} }
response, _ := json.Marshal(map[string]interface{}{"records": result}) response, _ := json.Marshal(map[string]interface{}{"records": resultList})
return string(response), nil return string(response), nil
} }
@ -1052,10 +1057,10 @@ func cancelChessMatchRpc(ctx context.Context, logger runtime.Logger, db *sql.DB,
return `{"success": false, "error": "Failed to find match"}`, nil return `{"success": false, "error": "Failed to find match"}`, nil
} }
var targetMatch *runtime.Match var targetMatch *api.Match
for _, match := range matches { for _, match := range matches {
if match.MatchId == params.MatchID { if match.MatchId == params.MatchID {
targetMatch = &match targetMatch = match
break break
} }
} }
@ -1077,8 +1082,9 @@ func cancelChessMatchRpc(ctx context.Context, logger runtime.Logger, db *sql.DB,
return `{"success": false, "error": "Can only cancel waiting challenges"}`, nil return `{"success": false, "error": "Can only cancel waiting challenges"}`, nil
} }
// Signal the match to terminate // Signal the match to terminate (returns 2 values)
if err := nk.MatchSignal(ctx, params.MatchID, "cancel"); err != nil { _, err = nk.MatchSignal(ctx, params.MatchID, "cancel")
if err != nil {
logger.Warn("matchSignal failed: %v", err) logger.Warn("matchSignal failed: %v", err)
} }