add invite game mode

This commit is contained in:
2026-11-15 19:19:35 +03:00
parent 8f70bb639d
commit a91eccbeaa
12 changed files with 600 additions and 58 deletions
@@ -170,6 +170,111 @@ RSpec.describe MatchmakingService do
expect(redis.zrange(described_class::QUEUE_KEY, 0, -1)).to be_empty
expect(redis.hget(described_class::SINCE_KEY, a.to_s)).to be_nil
end
it "removes the player's active invite and code mapping" do
a = uid(1)
code = described_class.create_invite(a)
expect(redis.get(described_class.invite_key(code))).to eq(a.to_s)
described_class.cancel(a)
expect(redis.get(described_class.invite_key(code))).to be_nil
expect(redis.get(described_class.user_invite_key(a))).to be_nil
expect(described_class.current_invite_code(a)).to be_nil
end
end
describe ".create_invite" do
it "generates a 5-letter uppercase code and stores it in Redis with TTL" do
a = uid(1)
code = described_class.create_invite(a)
expect(code).to match(/\A[A-Z]{5}\z/)
expect(redis.get(described_class.invite_key(code))).to eq(a.to_s)
expect(redis.get(described_class.user_invite_key(a))).to eq(code)
expect(described_class.current_invite_code(a)).to eq(code)
expect(redis.ttl(described_class.invite_key(code))).to be > 0
end
it "clears previous matchmaking queue or invite when creating a new one" do
a = uid(1)
described_class.enqueue(a, rating: 1000)
code1 = described_class.create_invite(a)
expect(redis.zrange(described_class::QUEUE_KEY, 0, -1)).to be_empty
code2 = described_class.create_invite(a)
expect(code2).to be_present
expect(redis.get(described_class.invite_key(code1))).to be_nil
expect(redis.get(described_class.invite_key(code2))).to eq(a.to_s)
end
end
describe ".join_invite" do
it "rejects invalid code formats" do
a = uid(1)
expect(described_class.join_invite(a, "12345")[:error]).to eq("invalid_code")
expect(described_class.join_invite(a, "ABC")[:error]).to eq("invalid_code")
expect(described_class.join_invite(a, "TOOLONG")[:error]).to eq("invalid_code")
expect(described_class.join_invite(a, "")[:error]).to eq("invalid_code")
end
it "returns not_found if code does not exist" do
a = uid(1)
result = described_class.join_invite(a, "ABCDE")
expect(result[:error]).to eq("not_found")
end
it "returns cannot_join_self if player attempts to join their own invite" do
a = uid(1)
code = described_class.create_invite(a)
result = described_class.join_invite(a, code)
expect(result[:error]).to eq("cannot_join_self")
end
it "pairs the players, creates a Match record, broadcasts to both, and cleans up Redis" do
host = uid(1)
guest = uid(2)
code = described_class.create_invite(host)
result = described_class.join_invite(guest, code.downcase) # should be case-insensitive
expect(result[:ok]).to be true
# Redis invite keys are removed
expect(redis.get(described_class.invite_key(code))).to be_nil
expect(redis.get(described_class.user_invite_key(host))).to be_nil
# Neither player is in the public matchmaking queue
expect(redis.zrange(described_class::QUEUE_KEY, 0, -1)).to be_empty
# Match record created in DB
host_broadcast = broadcast_payloads(host).first
guest_broadcast = broadcast_payloads(guest).first
expect(host_broadcast[:type]).to eq("matched")
expect(guest_broadcast[:type]).to eq("matched")
expect(host_broadcast[:match_id]).to eq(guest_broadcast[:match_id])
expect(host_broadcast[:players]).to contain_exactly(host, guest)
match = Match.find_by!(uuid: host_broadcast[:match_id])
expect(match.player1_id).to eq(host)
expect(match.player2_id).to eq(guest)
expect(match).to be_ongoing
expect(match.match_players.pluck(:player_id)).to contain_exactly(host, guest)
end
it "handles concurrent join attempts so only one guest succeeds" do
host = uid(1)
guest1 = uid(2)
guest2 = uid(3)
code = described_class.create_invite(host)
res1 = described_class.join_invite(guest1, code)
res2 = described_class.join_invite(guest2, code)
expect(res1[:ok]).to be true
expect(res2[:error]).to eq("not_found")
end
end
describe ".claim" do