add player rating to the db

This commit is contained in:
2026-10-25 17:10:40 +03:00
parent acdaebd491
commit 765a3365bd
9 changed files with 180 additions and 147 deletions
@@ -3,7 +3,7 @@
# Client flow:
# 1. Connect: ws(s)://host/cable?token=<JWT>
# 2. Subscribe to "MatchmakingChannel"
# 3. Send { action: "start", rating: <optional int> } to enter the queue
# 3. Send { action: "start" } to enter the queue
# 4. Receive { type: "matched", ... } or { type: "timeout" }
# 5. Disconnecting (or sending { action: "stop" }) cancels the search.
class MatchmakingChannel < ApplicationCable::Channel
@@ -17,8 +17,8 @@ class MatchmakingChannel < ApplicationCable::Channel
end
end
def start(data)
MatchmakingService.enqueue(current_user.id, rating: data["rating"])
def start(_data)
MatchmakingService.enqueue(current_user.id, rating: current_user.rating)
end
def stop
+1
View File
@@ -7,6 +7,7 @@ class User < ApplicationRecord
normalizes :nickname, with: ->(n) { n.strip }
validates :nickname, presence: true, uniqueness: { case_sensitive: false }, length: { minimum: 3, maximum: 24 }
validates :rating, presence: true
validates :password, presence: true, length: { minimum: 6 }, if: -> { new_record? || password_digest_changed? }
validate :avatar_content_type, if: -> { avatar.attached? }
@@ -186,8 +186,7 @@ class MatchmakingService
# Adds the user to the queue and immediately tries to pair them.
# The result (match or timeout) is delivered asynchronously via
# MatchmakingChannel broadcasts.
def enqueue(user_id, rating: nil)
rating ||= config.default_rating
def enqueue(user_id, rating:)
results = call_pair_script("enqueue", extra: [ user_id, rating ])
process_results(results)
end