Files
ruby-sea-battle/main-server/app/services/matchmaking_service.rb
T
2026-11-15 19:19:35 +03:00

394 lines
14 KiB
Ruby

# Matchmaking via Redis.
#
# Data structures (all volatile, TTL-backed):
# mm:queue sorted set — waiting players, score = rating
# mm:since hash — user_id => enqueue timestamp (for dynamic rating range)
# mm:rating hash — user_id => rating (to requeue stale pending players)
# mm:pending sorted set — paired, awaiting finalize, score = pairing timestamp
# mm:match:<id> string — match payload (JSON)
# mm:user:<id> string — per-user pointer to the match payload (JSON)
#
# All state transitions happen inside Lua scripts, so every operation is
# atomic with respect to other Redis clients. Delivery to clients happens
# over Action Cable; the per-user pointer key guarantees at-most-once
# delivery even if the broadcast is missed (client reconnects and claims it).
class MatchmakingService
QUEUE_KEY = "mm:queue"
SINCE_KEY = "mm:since"
RATING_KEY = "mm:rating"
PENDING_KEY = "mm:pending"
MATCH_KEY_PREFIX = "mm:match:"
USER_KEY_PREFIX = "mm:user:"
INVITE_KEY_PREFIX = "mm:invite:"
USER_INVITE_KEY_PREFIX = "mm:user_invite:"
# Enqueues a player and tries to find an opponent immediately; in "sweep"
# mode pairs up everyone in the queue, expires timed-out waiters and
# requeues stale pending players. Returns an array of result strings:
# "P:<id1>:<id2>" — a pair was formed
# "E:<id>" — a player timed out waiting
PAIR_SCRIPT = <<~LUA
local queue_key = KEYS[1]
local since_key = KEYS[2]
local rating_key = KEYS[3]
local pending_key = KEYS[4]
local user_prefix = KEYS[5]
-- All timestamps are integer milliseconds: Lua turns large floats into
-- scientific notation strings which Redis rejects in ZADD/ZRANGEBYSCORE.
local mode = ARGV[1]
local now = tonumber(ARGV[2])
local queue_ttl = tonumber(ARGV[3]) * 1000
local pending_ttl = tonumber(ARGV[4]) * 1000
local base_range = tonumber(ARGV[5])
local range_per_sec = tonumber(ARGV[6])
local max_range = tonumber(ARGV[7])
local function tolerance(ts)
local waited = (now - ts) / 1000
local r = base_range + waited * range_per_sec
return r > max_range and max_range or r
end
local results = {}
if mode == "enqueue" then
local uid = ARGV[8]
local rating = tonumber(ARGV[9])
-- restart a fresh waiting session for this user
redis.call("ZREM", queue_key, uid)
redis.call("HDEL", since_key, uid)
redis.call("HDEL", rating_key, uid)
local candidates = redis.call("ZRANGEBYSCORE", queue_key,
rating - max_range, rating + max_range)
local best = nil
local best_diff = nil
for _, other in ipairs(candidates) do
if other ~= uid then
local other_ts = tonumber(redis.call("HGET", since_key, other) or now)
local other_rating = tonumber(redis.call("ZSCORE", queue_key, other))
local diff = math.abs(other_rating - rating)
if diff <= tolerance(other_ts) and (best_diff == nil or diff < best_diff) then
best = other
best_diff = diff
end
end
end
if best then
redis.call("ZREM", queue_key, uid, best)
redis.call("HDEL", since_key, uid, best)
redis.call("HDEL", rating_key, uid, best)
redis.call("ZADD", pending_key, now, uid, now, best)
results[#results + 1] = "P:" .. uid .. ":" .. best
else
redis.call("ZADD", queue_key, rating, uid)
redis.call("HSET", since_key, uid, now)
redis.call("HSET", rating_key, uid, rating)
end
else -- "sweep"
local all = redis.call("ZRANGE", queue_key, 0, -1, "WITHSCORES")
local items = {}
for i = 1, #all, 2 do
items[#items + 1] = {
id = all[i],
rating = tonumber(all[i + 1]),
ts = tonumber(redis.call("HGET", since_key, all[i]) or now)
}
end
table.sort(items, function(a, b) return a.rating < b.rating end)
local used = {}
for i = 1, #items do
local a = items[i]
if not used[a.id] then
if now - a.ts > queue_ttl then
used[a.id] = true
results[#results + 1] = "E:" .. a.id
else
local best = nil
for j = i + 1, #items do
local b = items[j]
-- tolerance is based on the longer-waiting player of the pair
if not used[b.id] and (b.rating - a.rating) <= tolerance(math.min(a.ts, b.ts)) then
best = b
break
end
end
if best then
used[a.id] = true
used[best.id] = true
results[#results + 1] = "P:" .. a.id .. ":" .. best.id
end
end
end
end
for _, res in ipairs(results) do
local parts = {}
for part in string.gmatch(res, "[^:]+") do parts[#parts + 1] = part end
if parts[1] == "P" then
redis.call("ZREM", queue_key, parts[2], parts[3])
redis.call("HDEL", since_key, parts[2], parts[3])
redis.call("HDEL", rating_key, parts[2], parts[3])
redis.call("ZADD", pending_key, now, parts[2], now, parts[3])
else
redis.call("ZREM", queue_key, parts[2])
redis.call("HDEL", since_key, parts[2])
redis.call("HDEL", rating_key, parts[2])
end
end
-- requeue players whose pairing was never finalized (e.g. the Rails
-- process died between pairing and match creation)
local stale = redis.call("ZRANGEBYSCORE", pending_key, 0, now - pending_ttl)
for _, uid in ipairs(stale) do
if redis.call("EXISTS", user_prefix .. uid) == 0 then
local r = redis.call("HGET", rating_key, uid)
if r then
redis.call("ZADD", queue_key, tonumber(r), uid)
redis.call("HSET", since_key, uid, now)
end
end
redis.call("ZREM", pending_key, uid)
end
end
redis.call("EXPIRE", queue_key, math.floor(queue_ttl / 1000) * 4)
redis.call("EXPIRE", pending_key, math.floor(queue_ttl / 1000) * 4)
redis.call("EXPIRE", since_key, math.floor(queue_ttl / 1000) * 4)
redis.call("EXPIRE", rating_key, math.floor(queue_ttl / 1000) * 4)
return results
LUA
# Atomically stores the match payload and clears the players' pending state.
FINALIZE_SCRIPT = <<~LUA
redis.call("SET", KEYS[1], ARGV[1], "EX", ARGV[2]) -- match payload
redis.call("SET", KEYS[2], ARGV[1], "EX", ARGV[2]) -- pointer: player 1
redis.call("SET", KEYS[3], ARGV[1], "EX", ARGV[2]) -- pointer: player 2
redis.call("ZREM", KEYS[4], ARGV[3], ARGV[4]) -- pending
redis.call("HDEL", KEYS[5], ARGV[3], ARGV[4]) -- rating
redis.call("HDEL", KEYS[6], ARGV[3], ARGV[4]) -- since
return 1
LUA
CANCEL_SCRIPT = <<~LUA
redis.call("ZREM", KEYS[1], ARGV[1])
redis.call("ZREM", KEYS[2], ARGV[1])
redis.call("HDEL", KEYS[3], ARGV[1])
redis.call("HDEL", KEYS[4], ARGV[1])
redis.call("DEL", KEYS[5] .. ARGV[1])
local user_invite_key = KEYS[6] .. ARGV[1]
local code = redis.call("GET", user_invite_key)
if code then
redis.call("DEL", KEYS[7] .. code)
redis.call("DEL", user_invite_key)
end
return 1
LUA
CLAIM_INVITE_SCRIPT = <<~LUA
local invite_key = KEYS[1]
local user_invite_prefix = KEYS[2]
local joiner_id = tostring(ARGV[1])
local inviter = redis.call("GET", invite_key)
if not inviter then
return { "err", "not_found" }
end
if tostring(inviter) == joiner_id then
return { "err", "cannot_join_self" }
end
redis.call("DEL", invite_key)
redis.call("DEL", user_invite_prefix .. inviter)
return { "ok", inviter }
LUA
class << self
# 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:)
results = call_pair_script("enqueue", extra: [ user_id, rating ])
process_results(results)
end
# Pairs up everyone possible, expires timed-out waiters, requeues stale
# pending players. Called periodically by MatchmakingSweepJob.
def sweep
results = call_pair_script("sweep")
process_results(results)
end
# Removes the user from the queue / pending state and cancels any active invite.
def cancel(user_id)
RedisConnection.with do |redis|
redis.eval(CANCEL_SCRIPT,
keys: [ QUEUE_KEY, PENDING_KEY, SINCE_KEY, RATING_KEY, USER_KEY_PREFIX, USER_INVITE_KEY_PREFIX, INVITE_KEY_PREFIX ],
argv: [ user_id ])
end
end
# Creates an invitation with a unique 5 uppercase latin letters code.
# Any previous matchmaking queue or invite session for this user is cancelled.
def create_invite(user_id)
cancel(user_id)
RedisConnection.with do |redis|
10.times do
code = generate_invite_code
invite_k = invite_key(code)
user_k = user_invite_key(user_id)
if redis.set(invite_k, user_id, nx: true, ex: config.invite_ttl)
redis.set(user_k, code, ex: config.invite_ttl)
return code
end
end
end
raise "Failed to generate unique invite code"
end
# Joins a game using an invite code.
# Returns { ok: true } on success, or { error: ..., message: ... } on failure.
def join_invite(user_id, raw_code)
code = raw_code.to_s.strip.upcase
unless code.match?(/\A[A-Z]{5}\z/)
return { error: "invalid_code", message: "Неверный формат кода" }
end
res = RedisConnection.with do |redis|
redis.eval(CLAIM_INVITE_SCRIPT,
keys: [ invite_key(code), USER_INVITE_KEY_PREFIX ],
argv: [ user_id.to_s ])
end
status, inviter_id = res
if status == "err"
case inviter_id
when "not_found"
{ error: "not_found", message: "Приглашение не найдено или истекло" }
when "cannot_join_self"
{ error: "cannot_join_self", message: "Нельзя играть с самим собой" }
else
{ error: "unknown", message: "Ошибка присоединения к игре" }
end
else
cancel(user_id)
create_match(inviter_id.to_i, user_id.to_i)
{ ok: true }
end
end
def current_invite_code(user_id)
RedisConnection.with { |redis| redis.get(user_invite_key(user_id)) }
end
def invite_key(code) = "#{INVITE_KEY_PREFIX}#{code}"
def user_invite_key(user_id) = "#{USER_INVITE_KEY_PREFIX}#{user_id}"
def generate_invite_code
Array.new(5) { ("A".."Z").to_a.sample }.join
end
# Atomically reads and clears the match pointer for the user.
# Used on channel subscribe to deliver a match that was found while
# the user had no active subscription (at-most-once delivery).
def claim(user_id)
payload = RedisConnection.with { |redis| redis.getdel(user_key(user_id)) }
JSON.parse(payload, symbolize_names: true) if payload
end
def config
Rails.application.config.x.matchmaking
end
private
def call_pair_script(mode, extra: [])
RedisConnection.with do |redis|
redis.eval(PAIR_SCRIPT,
keys: [ QUEUE_KEY, SINCE_KEY, RATING_KEY, PENDING_KEY, USER_KEY_PREFIX ],
argv: [ mode, (Time.current.to_f * 1000).to_i, config.queue_ttl, config.pending_ttl,
config.base_range, config.range_per_second, config.max_range, *extra ])
end
end
def process_results(results)
results.each do |result|
kind, id1, id2 = result.split(":")
if kind == "P"
create_match(id1.to_i, id2.to_i)
else
broadcast(id1.to_i, { type: "timeout" })
end
end
end
# JWT generation happens in Ruby, so pairing (done in Lua) and match
# creation are two steps. Players sit in the "pending" set between them;
# the sweep requeues anyone stuck there longer than pending_ttl.
def create_match(player1_id, player2_id)
match_id = SecureRandom.uuid
game_token = JwtService.encode(
match_id: match_id,
player1_id: player1_id,
player2_id: player2_id,
game_server: config.game_server_url,
exp: config.token_ttl.seconds.from_now.to_i
)
# Persist the match before finalizing in Redis: if the DB write fails,
# the players stay in "pending" and the sweep requeues them, so no
# match is announced without a corresponding DB record.
player1 = User.find(player1_id)
player2 = User.find(player2_id)
Match.create!(
uuid: match_id,
player1: player1,
player2: player2,
status: :ongoing,
game_server: config.game_server_url,
started_at: Time.current,
player1_rating_before: player1.rating,
player2_rating_before: player2.rating,
player1_rating_change: 0,
player2_rating_change: 0
)
payload = {
type: "matched",
match_id: match_id,
game_token: game_token,
game_server: config.game_server_url,
players: [ player1_id, player2_id ]
}.to_json
RedisConnection.with do |redis|
redis.eval(FINALIZE_SCRIPT,
keys: [ match_key(match_id), user_key(player1_id), user_key(player2_id),
PENDING_KEY, RATING_KEY, SINCE_KEY ],
argv: [ payload, config.match_ttl, player1_id, player2_id ])
end
[ player1_id, player2_id ].each do |uid|
# Deliver and clear the pointer so a later subscribe-claim
# cannot duplicate the match.
pointer = RedisConnection.with { |redis| redis.getdel(user_key(uid)) }
broadcast(uid, JSON.parse(pointer, symbolize_names: true)) if pointer
end
end
def broadcast(user_id, payload)
MatchmakingChannel.broadcast_to(user_id, payload)
end
def match_key(match_id) = "#{MATCH_KEY_PREFIX}#{match_id}"
def user_key(user_id) = "#{USER_KEY_PREFIX}#{user_id}"
end
end