add invite game mode
This commit is contained in:
@@ -30,6 +30,16 @@ RSpec.describe MatchmakingChannel, type: :channel do
|
||||
expect(subscription).to be_confirmed
|
||||
expect(transmissions.last).to eq(match_payload.as_json)
|
||||
end
|
||||
|
||||
it "transmits current invite code if one was active for the user" do
|
||||
allow(MatchmakingService).to receive(:claim).with(user.id).and_return(nil)
|
||||
allow(MatchmakingService).to receive(:current_invite_code).with(user.id).and_return("ABCDE")
|
||||
|
||||
subscribe
|
||||
|
||||
expect(subscription).to be_confirmed
|
||||
expect(transmissions.last).to eq({ "type" => "invite_created", "code" => "ABCDE" })
|
||||
end
|
||||
end
|
||||
|
||||
describe "#start" do
|
||||
@@ -57,6 +67,46 @@ RSpec.describe MatchmakingChannel, type: :channel do
|
||||
end
|
||||
end
|
||||
|
||||
describe "#create_invite" do
|
||||
it "calls MatchmakingService.create_invite and transmits invite_created" do
|
||||
subscribe
|
||||
expect(MatchmakingService).to receive(:create_invite).with(user.id).and_return("XYZAB")
|
||||
|
||||
perform :create_invite
|
||||
expect(transmissions.last).to eq({ "type" => "invite_created", "code" => "XYZAB" })
|
||||
end
|
||||
end
|
||||
|
||||
describe "#join_invite" do
|
||||
it "calls MatchmakingService.join_invite with code" do
|
||||
subscribe
|
||||
expect(MatchmakingService).to receive(:join_invite).with(user.id, "XYZAB").and_return({ ok: true })
|
||||
|
||||
perform :join_invite, { "code" => "XYZAB" }
|
||||
end
|
||||
|
||||
it "transmits invite_error if join fails" do
|
||||
subscribe
|
||||
expect(MatchmakingService).to receive(:join_invite).with(user.id, "WRONG").and_return(
|
||||
{ error: "not_found", message: "Приглашение не найдено или истекло" }
|
||||
)
|
||||
|
||||
perform :join_invite, { "code" => "WRONG" }
|
||||
expect(transmissions.last).to eq(
|
||||
{ "type" => "invite_error", "error" => "not_found", "message" => "Приглашение не найдено или истекло" }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#cancel_invite" do
|
||||
it "calls MatchmakingService.cancel for the current user" do
|
||||
subscribe
|
||||
|
||||
expect(MatchmakingService).to receive(:cancel).with(user.id)
|
||||
perform :cancel_invite
|
||||
end
|
||||
end
|
||||
|
||||
describe "#unsubscribed" do
|
||||
it "cancels matchmaking search when unsubscribing" do
|
||||
subscribe
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user