From 28c159f63fa454fef299d4945d5b50ef259cc6af Mon Sep 17 00:00:00 2001 From: Douglas Berkley Date: Sun, 7 Jun 2026 15:45:38 +0900 Subject: [PATCH] Added minute to the to match and updating it in the API --- app/jobs/match_update_job.rb | 1 + app/jobs/schedule_daily_tasks_job.rb | 4 ++++ app/views/v1/matches/_match.json.jbuilder | 4 ++-- db/migrate/20260607063338_add_minute_to_matches.rb | 5 +++++ db/schema.rb | 3 ++- lib/tasks/match.rake | 13 +++++++++---- 6 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20260607063338_add_minute_to_matches.rb diff --git a/app/jobs/match_update_job.rb b/app/jobs/match_update_job.rb index 27fdc25..5e30509 100644 --- a/app/jobs/match_update_job.rb +++ b/app/jobs/match_update_job.rb @@ -34,6 +34,7 @@ def update_matches_future(url) match.group = Group.find_by(round: match.round, api_code: match_info["group"]) if match_info["group"] match.api_id = match_info['id'] match.location = match_info['venue'] + match.minute = match_info['minute'] match.kickoff_time = kickoff_time match.save p match.errors.full_messages if match.errors.any? diff --git a/app/jobs/schedule_daily_tasks_job.rb b/app/jobs/schedule_daily_tasks_job.rb index 5c7086c..70a2edc 100644 --- a/app/jobs/schedule_daily_tasks_job.rb +++ b/app/jobs/schedule_daily_tasks_job.rb @@ -8,6 +8,10 @@ def perform matches = competition.matches.where(kickoff_time: Date.today.all_day) matches.pluck(:kickoff_time).uniq.each do |kickoff_time| MatchStartedJob.set(wait_until: kickoff_time).perform_later(kickoff_time) + # Calls the API during the matches to get the current time + 150.times do |i| + MatchUpdateJob.set(wait_until: kickoff_time + i.minutes).perform_later(competition.id) + end end # Schedules notifications for rounds starting tomorrow rounds_starting_tomorrow = Round.joins(:matches) diff --git a/app/views/v1/matches/_match.json.jbuilder b/app/views/v1/matches/_match.json.jbuilder index f5d9efb..55e8004 100644 --- a/app/views/v1/matches/_match.json.jbuilder +++ b/app/views/v1/matches/_match.json.jbuilder @@ -1,4 +1,4 @@ -json.extract! match, :id, :kickoff_time, :status, :group_id, :next_match_id, :round_id, :location +json.extract! match, :id, :kickoff_time, :status, :group_id, :next_match_id, :round_id, :location, :minute json.round_number match.round.number json.team_home do json.partial! match.team_home @@ -11,7 +11,7 @@ end json.team_away do json.partial! match.team_away if %w[finished started].include?(match[:status]) - json.score match.team_away_score + json.score match.team_away_score json.et_score match.team_away_et_score json.ps_score match.team_away_ps_score end diff --git a/db/migrate/20260607063338_add_minute_to_matches.rb b/db/migrate/20260607063338_add_minute_to_matches.rb new file mode 100644 index 0000000..a1b5f9b --- /dev/null +++ b/db/migrate/20260607063338_add_minute_to_matches.rb @@ -0,0 +1,5 @@ +class AddMinuteToMatches < ActiveRecord::Migration[6.1] + def change + add_column :matches, :minute, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 69ad582..527c308 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2026_06_04_043212) do +ActiveRecord::Schema.define(version: 2026_06_07_063338) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -120,6 +120,7 @@ t.integer "team_home_ps_score" t.integer "team_away_ps_score" t.bigint "competition_id", null: false + t.integer "minute" t.index ["competition_id"], name: "index_matches_on_competition_id" t.index ["group_id"], name: "index_matches_on_group_id" t.index ["next_match_id"], name: "index_matches_on_next_match_id" diff --git a/lib/tasks/match.rake b/lib/tasks/match.rake index e5b4acd..60821df 100644 --- a/lib/tasks/match.rake +++ b/lib/tasks/match.rake @@ -4,16 +4,21 @@ namespace :match do return 'Not allowed in production' if Rails.env.production? @competition = Competition.last - completed_matches = @competition.matches.order(kickoff_time: :asc).first(5) - completed_matches.each do |match| + completed_matches = @competition.matches.order(kickoff_time: :asc).first(7) + completed_matches.each_with_index do |match, index| puts "#{match.team_home.name} (H) vs. #{match.team_away.name} (A)" - User.find_each do |user| + User.where(admin: true).each do |user| prediction = Prediction.find_or_initialize_by(user: user, match: match) prediction.choice = Prediction.choices.keys.sample prediction.save puts "- #{prediction.user.name} choose #{prediction.choice}" end - match.finished! + if [5, 6].include?(index) + match.started! + match.minute = rand(1..90) + else + match.finished! + end match.team_home_score = rand(0..3) match.team_away_score = rand(0..3) match.save