Skip to content

Commit

Permalink
fix: スコア登録が正しくサれるように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
KinjiKawaguchi committed Apr 3, 2024
1 parent a0a7172 commit 2a052b1
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions typing-server/api/repository/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,20 @@ func GetScoresRanking(ctx context.Context, client *ent.Client, sortBy string, st

return rankings, nil
}

func CreateScore(ctx context.Context, client *ent.Client, userID uuid.UUID, keystrokes int, accuracy float64) error {
// Create a new score
createdScore, err := client.Score.Create().
// トランザクションを開始
tx, err := client.Tx(ctx)
if err != nil {
return err
}
defer func() {
if r := recover(); r != nil {
tx.Rollback()

Check failure on line 101 in typing-server/api/repository/score.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `tx.Rollback` is not checked (errcheck)
}
}()

// 新しいスコアを作成
createdScore, err := tx.Score.Create().
SetKeystrokes(keystrokes).
SetAccuracy(accuracy).
SetUserID(userID).
Expand All @@ -102,46 +112,70 @@ func CreateScore(ctx context.Context, client *ent.Client, userID uuid.UUID, keys
return err
}

// キーストロークと精度のチェック
if keystrokes < 120 || accuracy < 0.95 {
return nil
return tx.Commit()
}

// ユーザーを取得
user, err := tx.User.Query().Where(user.ID(userID)).Only(ctx)
if err != nil {
return err
}

// 最大キーストロークと最大精度のスコアを取得
maxKeystrokeScore, maxAccuracyScore, err := getMaxScores(ctx, user)
if err != nil {
return err
}

// Get the user
user, err := client.User.Query().Where(user.ID(userID)).Only(ctx)
// 以前の最大スコアのフラグを更新
err = updateMaxScoreFlags(ctx, maxKeystrokeScore, maxAccuracyScore, createdScore)
if err != nil {
return err
}

// Check if the new score has the maximum keystrokes
// トランザクションをコミット
return tx.Commit()
}

func getMaxScores(ctx context.Context, user *ent.User) (*ent.Score, *ent.Score, error) {
// 最大キーストロークのスコアを取得
maxKeystrokeScore, err := user.QueryScores().Where(score.IsMaxKeystrokes(true)).Only(ctx)
if err != nil && !ent.IsNotFound(err) {
return err
return nil, nil, err
}
isMaxKeystrokes := maxKeystrokeScore == nil || createdScore.Keystrokes >= maxKeystrokeScore.Keystrokes

// Check if the new score has the maximum accuracy
// 最大精度のスコアを取得
maxAccuracyScore, err := user.QueryScores().Where(score.IsMaxAccuracy(true)).Only(ctx)
if err != nil && !ent.IsNotFound(err) {
return err
return nil, nil, err
}

return maxKeystrokeScore, maxAccuracyScore, nil
}

func updateMaxScoreFlags(ctx context.Context, maxKeystrokeScore, maxAccuracyScore, createdScore *ent.Score) error {
isMaxKeystrokes := maxKeystrokeScore == nil || createdScore.Keystrokes >= maxKeystrokeScore.Keystrokes
isMaxAccuracy := maxAccuracyScore == nil || createdScore.Accuracy >= maxAccuracyScore.Accuracy

// Update the flags of the previous maximum scores
if maxKeystrokeScore != nil && !isMaxKeystrokes {
err = maxKeystrokeScore.Update().SetIsMaxKeystrokes(false).Exec(ctx)
// 以前の最大スコアのフラグを更新
if maxKeystrokeScore != nil && isMaxKeystrokes {
err := maxKeystrokeScore.Update().SetIsMaxKeystrokes(false).Exec(ctx)
if err != nil {
return err
}
}
if maxAccuracyScore != nil && !isMaxAccuracy {
err = maxAccuracyScore.Update().SetIsMaxAccuracy(false).Exec(ctx)

if maxAccuracyScore != nil && isMaxAccuracy {
err := maxAccuracyScore.Update().SetIsMaxAccuracy(false).Exec(ctx)
if err != nil {
return err
}
}

// Update the score with the maximum flags
err = createdScore.Update().
// 作成したスコアの最大フラグを更新
err := createdScore.Update().
SetIsMaxKeystrokes(isMaxKeystrokes).
SetIsMaxAccuracy(isMaxAccuracy).
Exec(ctx)
Expand Down

0 comments on commit 2a052b1

Please sign in to comment.