This commit is contained in:
parent
ab51b4b504
commit
e704a1e1c4
1 changed files with 33 additions and 27 deletions
|
|
@ -17,6 +17,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/heroiclabs/nakama-common/api"
|
||||
"github.com/heroiclabs/nakama-common/runtime"
|
||||
"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)
|
||||
}
|
||||
|
||||
// Create chess ELO leaderboard
|
||||
if err := nk.LeaderboardCreate(ctx, "chess-elo", true, "desc", "set", "", nil); err != nil {
|
||||
// Create chess ELO leaderboard (8 args for v1.44.0)
|
||||
if err := nk.LeaderboardCreate(ctx, "chess-elo", true, "desc", "set", "", nil, true); err != nil {
|
||||
logger.Info("Chess ELO leaderboard already exists or created")
|
||||
}
|
||||
|
||||
|
|
@ -216,8 +217,8 @@ func validateJwt(token string, secret string) (*AppJwtClaims, error) {
|
|||
return &claims, nil
|
||||
}
|
||||
|
||||
// Before authenticate custom hook
|
||||
func beforeAuthenticateCustom(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *runtime.AuthenticateCustomRequest) (*runtime.AuthenticateCustomRequest, error) {
|
||||
// Before authenticate custom hook - uses api.AuthenticateCustomRequest
|
||||
func beforeAuthenticateCustom(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateCustomRequest) (*api.AuthenticateCustomRequest, error) {
|
||||
appJwt := in.GetAccount().GetId()
|
||||
if appJwt == "" {
|
||||
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
|
||||
userColor := "#561D5E"
|
||||
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
|
||||
if err := json.Unmarshal([]byte(users[0].Metadata), &meta); err == nil {
|
||||
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
|
||||
}
|
||||
|
||||
// Validate and apply the move using notnil/chess
|
||||
// Validate and apply the move
|
||||
result := validateChessMove(s.FEN, moveData.From, moveData.To, moveData.Promotion)
|
||||
if !result.Valid {
|
||||
logger.Warn("Invalid move: %+v", moveData)
|
||||
|
|
@ -723,7 +724,7 @@ type MoveResult struct {
|
|||
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 {
|
||||
result := MoveResult{Turn: "w"}
|
||||
|
||||
|
|
@ -735,8 +736,8 @@ func validateChessMove(fenStr, from, to, promotion string) MoveResult {
|
|||
|
||||
game := chess.NewGame(fen)
|
||||
|
||||
// Find the move
|
||||
var targetMove *chess.Move
|
||||
// Find the move in valid moves
|
||||
found := false
|
||||
for _, move := range game.ValidMoves() {
|
||||
if move.S1().String() == from && move.S2().String() == to {
|
||||
// Check promotion
|
||||
|
|
@ -747,17 +748,21 @@ func validateChessMove(fenStr, from, to, promotion string) MoveResult {
|
|||
continue
|
||||
}
|
||||
}
|
||||
targetMove = move
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if targetMove == nil {
|
||||
if !found {
|
||||
return result
|
||||
}
|
||||
|
||||
// Make the move
|
||||
if err := game.Move(targetMove); err != nil {
|
||||
// Make the move using MoveStr (UCI format)
|
||||
moveStr := from + to
|
||||
if promotion != "" {
|
||||
moveStr += promotion
|
||||
}
|
||||
if err := game.MoveStr(moveStr); err != nil {
|
||||
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)
|
||||
|
||||
// Get current ratings
|
||||
winnerRecords, err := nk.LeaderboardRecordsList(ctx, "chess-elo", []string{winnerID}, 1, "", 0)
|
||||
// Get current ratings (5 return values)
|
||||
winnerRecords, _, _, _, err := nk.LeaderboardRecordsList(ctx, "chess-elo", []string{winnerID}, 1, "", 0)
|
||||
if err != nil {
|
||||
logger.Error("Failed to get winner ELO: %v", err)
|
||||
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 {
|
||||
logger.Error("Failed to get loser ELO: %v", err)
|
||||
return
|
||||
|
|
@ -826,11 +831,11 @@ func updateElo(ctx context.Context, nk runtime.NakamaModule, logger runtime.Logg
|
|||
|
||||
winnerElo := int64(defaultElo)
|
||||
loserElo := int64(defaultElo)
|
||||
if len(winnerRecords.Records) > 0 {
|
||||
winnerElo = winnerRecords.Records[0].Score
|
||||
if len(winnerRecords) > 0 {
|
||||
winnerElo = winnerRecords[0].Score
|
||||
}
|
||||
if len(loserRecords.Records) > 0 {
|
||||
loserElo = loserRecords.Records[0].Score
|
||||
if len(loserRecords) > 0 {
|
||||
loserElo = loserRecords[0].Score
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
var result []map[string]interface{}
|
||||
var resultList []map[string]interface{}
|
||||
for _, r := range records {
|
||||
result = append(result, map[string]interface{}{
|
||||
resultList = append(resultList, map[string]interface{}{
|
||||
"rank": r.Rank,
|
||||
"userId": r.OwnerId,
|
||||
"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
|
||||
}
|
||||
|
||||
|
|
@ -1052,10 +1057,10 @@ func cancelChessMatchRpc(ctx context.Context, logger runtime.Logger, db *sql.DB,
|
|||
return `{"success": false, "error": "Failed to find match"}`, nil
|
||||
}
|
||||
|
||||
var targetMatch *runtime.Match
|
||||
var targetMatch *api.Match
|
||||
for _, match := range matches {
|
||||
if match.MatchId == params.MatchID {
|
||||
targetMatch = &match
|
||||
targetMatch = match
|
||||
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
|
||||
}
|
||||
|
||||
// Signal the match to terminate
|
||||
if err := nk.MatchSignal(ctx, params.MatchID, "cancel"); err != nil {
|
||||
// Signal the match to terminate (returns 2 values)
|
||||
_, err = nk.MatchSignal(ctx, params.MatchID, "cancel")
|
||||
if err != nil {
|
||||
logger.Warn("matchSignal failed: %v", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue