diff --git a/app/channels/tournament_matches_channel.rb b/app/channels/tournament_matches_channel.rb index c9abf0e..4e2e78f 100644 --- a/app/channels/tournament_matches_channel.rb +++ b/app/channels/tournament_matches_channel.rb @@ -5,7 +5,7 @@ class TournamentMatchesChannel < ApplicationCable::Channel tournament = Tournament.find(params[:tournament_id]) state = params[:state].presence - stream_for TournamentMatchesBroadcast.stream_target(tournament, state: state) - transmit TournamentMatchesBroadcastPayload.build(tournament, state: state) + stream_for TournamentMatchesBroadcast.stream_target(tournament, state:) + transmit TournamentMatchesBroadcastPayload.build(tournament, state:) end end diff --git a/app/services/tournament_matches_broadcast.rb b/app/services/tournament_matches_broadcast.rb index d3c582e..792ebd3 100644 --- a/app/services/tournament_matches_broadcast.rb +++ b/app/services/tournament_matches_broadcast.rb @@ -10,8 +10,8 @@ class TournamentMatchesBroadcast STATES.each do |state| TournamentMatchesChannel.broadcast_to( - stream_target(tournament, state: state), - TournamentMatchesBroadcastPayload.build(tournament, state: state) + stream_target(tournament, state:), + TournamentMatchesBroadcastPayload.build(tournament, state:) ) end end diff --git a/app/services/tournament_matches_broadcast_payload.rb b/app/services/tournament_matches_broadcast_payload.rb index 70df665..1e044ed 100644 --- a/app/services/tournament_matches_broadcast_payload.rb +++ b/app/services/tournament_matches_broadcast_payload.rb @@ -6,8 +6,8 @@ class TournamentMatchesBroadcastPayload { type: 'tournament_matches.updated', tournament_id: tournament.id, - state: state, - matches: TournamentMatchesPayload.build(tournament, state: state) + state:, + matches: TournamentMatchesPayload.build(tournament, state:) } end end diff --git a/app/services/tournament_matches_payload.rb b/app/services/tournament_matches_payload.rb index 8fe43ab..c3e679c 100644 --- a/app/services/tournament_matches_payload.rb +++ b/app/services/tournament_matches_payload.rb @@ -3,7 +3,7 @@ class TournamentMatchesPayload class << self def build(tournament, state: nil) - serialized_matches(matches_for(tournament, state: state)) + serialized_matches(matches_for(tournament, state:)) end def matches_for(tournament, state: nil) @@ -16,22 +16,33 @@ class TournamentMatchesPayload private def upcoming_matches_for(tournament) + upcoming_group_matches(tournament).presence || upcoming_playoff_matches(tournament) + end + + def upcoming_group_matches(tournament) group_stage = tournament.stages.find { |stage| stage.level == -1 } - group_matches = group_stage&.groups&.map do |group| + return [] if group_stage.nil? + + group_stage.groups.filter_map do |group| group.matches.select { |match| match.state == 'not_started' }.min_by(&:position) - end&.compact - return group_matches unless group_matches.blank? - - next_level = nil - tournament.stages.reject { |stage| stage.level.nil? }.sort_by(&:level).reverse_each do |stage| - next unless stage.matches.any? - next unless stage.matches.all?(&:in_progress?) - - next_level = stage.level - 1 - break end + end - tournament.stages.find { |stage| stage.level == next_level }&.matches || [] + def upcoming_playoff_matches(tournament) + active_stage = playoff_stages(tournament).reverse.find do |stage| + stage_with_running_matches?(stage) + end + return [] if active_stage.nil? + + tournament.stages.find { |stage| stage.level == active_stage.level - 1 }&.matches || [] + end + + def stage_with_running_matches?(stage) + stage.matches.any? && stage.matches.all?(&:in_progress?) + end + + def playoff_stages(tournament) + tournament.stages.reject { |stage| stage.level.nil? }.sort_by(&:level) end def serialized_matches(matches) diff --git a/app/services/tournament_payload.rb b/app/services/tournament_payload.rb index 7516e85..4e6cf2d 100644 --- a/app/services/tournament_payload.rb +++ b/app/services/tournament_payload.rb @@ -7,7 +7,7 @@ class TournamentPayload tournament, serializer: TournamentSerializer, include: '**', - scope: scope + scope: ).as_json end end diff --git a/e2e/lib/action_cable_client.rb b/e2e/lib/action_cable_client.rb index 04f4b22..bc8f21d 100644 --- a/e2e/lib/action_cable_client.rb +++ b/e2e/lib/action_cable_client.rb @@ -53,7 +53,7 @@ module TurniereE2E def subscribe_tournament!(tournament_id:) identifier = { channel: 'TournamentChannel', - tournament_id: tournament_id + tournament_id: }.to_json subscribe!(identifier:) do |message| @@ -64,8 +64,8 @@ module TurniereE2E def subscribe_matches!(tournament_id:, state: nil) identifier = { channel: 'TournamentMatchesChannel', - tournament_id: tournament_id, - state: state + tournament_id:, + state: }.compact.to_json subscribe!(identifier:) do |message| diff --git a/spec/e2e/http/tournament_follow_sync_live_websocket_spec.rb b/spec/e2e/http/tournament_follow_sync_live_websocket_spec.rb index fa0f5e8..472ea29 100644 --- a/spec/e2e/http/tournament_follow_sync_live_websocket_spec.rb +++ b/spec/e2e/http/tournament_follow_sync_live_websocket_spec.rb @@ -26,16 +26,23 @@ RSpec.describe 'Tournament follower sync live websocket HTTP E2E' do let(:source_client) { login_client(base_url: source_base_url) } it 'broadcasts follower tournament and match updates after source match sync import' do - source = create_group_stage_tournament(client: source_client, base_url: source_base_url, name_prefix: 'Ws Live Source') + source = create_group_stage_tournament( + client: source_client, + base_url: source_base_url, + name_prefix: 'Ws Live Source' + ) follower = create_follower_tournament(name_prefix: 'Ws Live Follower') configure_sync = source_client.patch("/tournaments/#{source.fetch(:id)}", body: { - sync_target_url: "#{follower_base_url}/tournaments/#{follower.fetch(:id)}/sync_state", + sync_target_url: follower_sync_url(follower), sync_auth_token: sync_token }) expect(configure_sync[:status]).to eq(200) - follower = wait_for_follower_match_count!(follower.fetch(:id), count: source.fetch(:stages).first.fetch(:groups).flat_map { |group| group.fetch(:matches) }.count) + follower = wait_for_follower_match_count!( + follower.fetch(:id), + count: source_group_matches(source).count + ) source_match = source.fetch(:stages).first.fetch(:groups).first.fetch(:matches).first follower_match_id = follower_match_for_source!(follower, source_match).fetch(:id) @@ -48,22 +55,31 @@ RSpec.describe 'Tournament follower sync live websocket HTTP E2E' do play_match!(match_id: source_match.fetch(:id), first_score: 5, second_score: 2) - tournament_payload = wait_for_tournament_payload!(tournament_cable, tournament_id: follower.fetch(:id)) do |payload| + tournament_payload = wait_for_tournament_payload!( + tournament_cable, + tournament_id: follower.fetch(:id) + ) do |payload| payload.dig('tournament', 'stages').flat_map { |stage| stage.fetch('groups', []) } .flat_map { |group| group.fetch('matches', []) } - .any? { |candidate| candidate.fetch('id') == follower_match_id && candidate.fetch('state') == 'finished' } + .any? do |candidate| + candidate.fetch('id') == follower_match_id && candidate.fetch('state') == 'finished' + end end matches_payload = wait_for_matches_payload!(matches_cable, match_id: follower_match_id, state: 'finished') - expect(tournament_payload.fetch('tournament')).to eq(json_for_api(fetch_tournament(base_url: follower_base_url, tournament_id: follower.fetch(:id)))) - expect(matches_payload.fetch('matches')).to eq(json_for_api(fetch_matches(base_url: follower_base_url, tournament_id: follower.fetch(:id)))) + expect(tournament_payload.fetch('tournament')).to( + eq(json_for_api(fetch_tournament(base_url: follower_base_url, tournament_id: follower.fetch(:id)))) + ) + expect(matches_payload.fetch('matches')).to( + eq(json_for_api(fetch_matches(base_url: follower_base_url, tournament_id: follower.fetch(:id)))) + ) ensure tournament_cable&.close matches_cable&.close end def login_client(base_url:) - client = TurniereE2E::ApiClient.new(base_url: base_url) + client = TurniereE2E::ApiClient.new(base_url:) response = client.login!(email: owner_email, password: owner_password) expect(response[:status]).to eq(200) client @@ -77,7 +93,7 @@ RSpec.describe 'Tournament follower sync live websocket HTTP E2E' do group_stage: true, playoff_teams_amount: 2, teams: 2.times.flat_map do |group_index| - 2.times.map do |seed_index| + Array.new(2) do |seed_index| { name: "#{name_prefix.tr(' ', '')}-G#{group_index + 1}-S#{seed_index + 1}", group: group_index @@ -86,7 +102,7 @@ RSpec.describe 'Tournament follower sync live websocket HTTP E2E' do end }) expect(response[:status]).to eq(201) - fetch_tournament(base_url: base_url, tournament_id: response.dig(:json, :id)) + fetch_tournament(base_url:, tournament_id: response.dig(:json, :id)) end def create_follower_tournament(name_prefix:) @@ -111,7 +127,7 @@ RSpec.describe 'Tournament follower sync live websocket HTTP E2E' do ordered_scores = match_response.fetch(:json).fetch(:match_scores).sort_by { |score| score.fetch(:id) } [first_score, second_score].each_with_index do |points, index| - score_update = source_client.patch("/match_scores/#{ordered_scores[index].fetch(:id)}", body: { points: points }) + score_update = source_client.patch("/match_scores/#{ordered_scores[index].fetch(:id)}", body: { points: }) expect(score_update[:status]).to eq(200) end @@ -120,61 +136,75 @@ RSpec.describe 'Tournament follower sync live websocket HTTP E2E' do end def wait_for_follower_match_count!(tournament_id, count:, timeout: 20) - deadline = Time.now + timeout + deadline = monotonic_time + timeout loop do - follower = fetch_tournament(base_url: follower_base_url, tournament_id: tournament_id) + follower = fetch_tournament(base_url: follower_base_url, tournament_id:) matches = follower.fetch(:stages).flat_map do |stage| stage.fetch(:groups, []).flat_map { |group| group.fetch(:matches, []) } + stage.fetch(:matches, []) end return follower if matches.count == count - raise "follower tournament not synced within #{timeout}s" if Time.now >= deadline + raise "follower tournament not synced within #{timeout}s" if monotonic_time >= deadline sleep 0.5 end end def follower_match_for_source!(follower, source_match) - follower.fetch(:stages).flat_map do |stage| + follower_matches = follower.fetch(:stages).flat_map do |stage| stage.fetch(:groups, []).flat_map { |group| group.fetch(:matches, []) } + stage.fetch(:matches, []) - end.find do |candidate| + end + + follower_matches.find do |candidate| candidate.fetch(:position) == source_match.fetch(:position) end || raise('follower match missing') end def fetch_tournament(base_url:, tournament_id:) - response = TurniereE2E::ApiClient.new(base_url: base_url).get("/tournaments/#{tournament_id}") + response = TurniereE2E::ApiClient.new(base_url:).get("/tournaments/#{tournament_id}") expect(response[:status]).to eq(200) response.fetch(:json) end def fetch_matches(base_url:, tournament_id:) - response = TurniereE2E::ApiClient.new(base_url: base_url).get("/tournaments/#{tournament_id}/matches") + response = TurniereE2E::ApiClient.new(base_url:).get("/tournaments/#{tournament_id}/matches") expect(response[:status]).to eq(200) response.fetch(:json) end def wait_for_tournament_payload!(cable_client, tournament_id:, timeout: 20) - deadline = Time.now + timeout + deadline = monotonic_time + timeout loop do - payload = cable_client.wait_for_tournament_payload!(timeout: [deadline - Time.now, 0.1].max) + payload = cable_client.wait_for_tournament_payload!(timeout: [deadline - monotonic_time, 0.1].max) return payload if payload.fetch('tournament_id') == tournament_id && yield(payload) - raise "follower tournament websocket payload not received within #{timeout}s" if Time.now >= deadline + raise "follower tournament websocket payload not received within #{timeout}s" if monotonic_time >= deadline end end def wait_for_matches_payload!(cable_client, match_id:, state:, timeout: 20) - deadline = Time.now + timeout + deadline = monotonic_time + timeout loop do - payload = cable_client.wait_for_matches_payload!(timeout: [deadline - Time.now, 0.1].max) + payload = cable_client.wait_for_matches_payload!(timeout: [deadline - monotonic_time, 0.1].max) match = payload.fetch('matches').find { |candidate| candidate.fetch('id') == match_id } return payload if match&.fetch('state') == state - raise "follower matches websocket payload not received within #{timeout}s" if Time.now >= deadline + raise "follower matches websocket payload not received within #{timeout}s" if monotonic_time >= deadline end end + def follower_sync_url(follower) + "#{follower_base_url}/tournaments/#{follower.fetch(:id)}/sync_state" + end + + def source_group_matches(source) + source.fetch(:stages).first.fetch(:groups).flat_map { |group| group.fetch(:matches) } + end + + def monotonic_time + Process.clock_gettime(Process::CLOCK_MONOTONIC) + end + def json_for_api(value) JSON.parse(JSON.dump(value)) end diff --git a/spec/e2e/http/tournament_live_websocket_spec.rb b/spec/e2e/http/tournament_live_websocket_spec.rb index 599aa7a..bb3fe2a 100644 --- a/spec/e2e/http/tournament_live_websocket_spec.rb +++ b/spec/e2e/http/tournament_live_websocket_spec.rb @@ -32,15 +32,24 @@ RSpec.describe 'Tournament live websocket HTTP E2E' do tournament_cable.subscribe_tournament!(tournament_id: tournament.fetch(:id)) matches_cable.subscribe_matches!(tournament_id: tournament.fetch(:id)) - expect(tournament_cable.wait_for_tournament_payload!.fetch('tournament')).to eq(json_for_api(fetch_tournament(tournament.fetch(:id)))) - expect(matches_cable.wait_for_matches_payload!.fetch('matches')).to eq(json_for_api(fetch_matches(tournament.fetch(:id)))) + expect(tournament_cable.wait_for_tournament_payload!.fetch('tournament')).to( + eq(json_for_api(fetch_tournament(tournament.fetch(:id)))) + ) + expect(matches_cable.wait_for_matches_payload!.fetch('matches')).to( + eq(json_for_api(fetch_matches(tournament.fetch(:id)))) + ) play_match!(match.fetch(:id), first_score: 3, second_score: 1) - tournament_payload = wait_for_tournament_payload!(tournament_cable, tournament_id: tournament.fetch(:id)) do |payload| + tournament_payload = wait_for_tournament_payload!( + tournament_cable, + tournament_id: tournament.fetch(:id) + ) do |payload| payload.dig('tournament', 'stages').flat_map { |stage| stage.fetch('groups', []) } .flat_map { |group| group.fetch('matches', []) } - .any? { |candidate| candidate.fetch('id') == match.fetch(:id) && candidate.fetch('state') == 'finished' } + .any? do |candidate| + candidate.fetch('id') == match.fetch(:id) && candidate.fetch('state') == 'finished' + end end expect(tournament_payload.fetch('tournament')).to eq(json_for_api(fetch_tournament(tournament.fetch(:id)))) @@ -67,7 +76,7 @@ RSpec.describe 'Tournament live websocket HTTP E2E' do group_stage: true, playoff_teams_amount: 2, teams: 2.times.flat_map do |group_index| - 2.times.map do |seed_index| + Array.new(2) do |seed_index| { name: "#{name_prefix.tr(' ', '')}-G#{group_index + 1}-S#{seed_index + 1}", group: group_index @@ -88,7 +97,7 @@ RSpec.describe 'Tournament live websocket HTTP E2E' do ordered_scores = match_response.fetch(:json).fetch(:match_scores).sort_by { |score| score.fetch(:id) } [first_score, second_score].each_with_index do |points, index| - score_update = client.patch("/match_scores/#{ordered_scores[index].fetch(:id)}", body: { points: points }) + score_update = client.patch("/match_scores/#{ordered_scores[index].fetch(:id)}", body: { points: }) expect(score_update[:status]).to eq(200) end @@ -109,26 +118,30 @@ RSpec.describe 'Tournament live websocket HTTP E2E' do end def wait_for_tournament_payload!(cable_client, tournament_id:, timeout: 20) - deadline = Time.now + timeout + deadline = monotonic_time + timeout loop do - payload = cable_client.wait_for_tournament_payload!(timeout: [deadline - Time.now, 0.1].max) + payload = cable_client.wait_for_tournament_payload!(timeout: [deadline - monotonic_time, 0.1].max) return payload if payload.fetch('tournament_id') == tournament_id && yield(payload) - raise "tournament websocket payload not received within #{timeout}s" if Time.now >= deadline + raise "tournament websocket payload not received within #{timeout}s" if monotonic_time >= deadline end end def wait_for_matches_payload!(cable_client, match_id:, state:, timeout: 20) - deadline = Time.now + timeout + deadline = monotonic_time + timeout loop do - payload = cable_client.wait_for_matches_payload!(timeout: [deadline - Time.now, 0.1].max) + payload = cable_client.wait_for_matches_payload!(timeout: [deadline - monotonic_time, 0.1].max) match = payload.fetch('matches').find { |candidate| candidate.fetch('id') == match_id } return payload if match&.fetch('state') == state - raise "matches websocket payload not received within #{timeout}s" if Time.now >= deadline + raise "matches websocket payload not received within #{timeout}s" if monotonic_time >= deadline end end + def monotonic_time + Process.clock_gettime(Process::CLOCK_MONOTONIC) + end + def json_for_api(value) JSON.parse(JSON.dump(value)) end