Skip to content

Add ending time to challenges #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/lib/flag_challenge_share_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def solved?(times = 1)
end

def first_solve_time
first_solve&.created_at || Game.instance.defense_end
first_solve&.created_at || challenge&.close_challenge_at || Game.instance.defense_end
end

# Calculates how many points each team should get for solving a specific teams flag
Expand Down Expand Up @@ -82,11 +82,11 @@ def convert_shares_to_points_for(num_shares, total_shares, challenge_point_value
private

def calc_points_for_first_solve
first_capture_point_bonus + calc_point_value(start_calculation_at, Game.instance.defense_end)
first_capture_point_bonus + calc_point_value(start_calculation_at, stop_calculation_at)
end

def calc_points_for_solve
potential_shares = calc_shares(first_solve_time, Game.instance.defense_end)
potential_shares = calc_shares(first_solve_time, stop_calculation_at)
convert_shares_to_points_for(
potential_shares,
calc_offensive_shares_for_solved_challenges.values.sum + potential_shares,
Expand Down
14 changes: 13 additions & 1 deletion app/models/challenge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,20 @@ def challenge_open?
state.eql? 'open'
end

def before_close?
return Time.now.utc < close_challenge_at unless close_challenge_at.nil?

true
end

def after_open?
return Time.now.utc > open_challenge_at unless open_challenge_at.nil?

true
end

def open?
challenge_open? && game.open?
challenge_open? && before_close? && after_open? && game.open?
end

def display_point_value(_ = nil)
Expand Down
6 changes: 5 additions & 1 deletion app/models/defense_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def challenge_open?
end

def start_calculation_at
super || Game.instance.start
super || challenge&.open_challenge_at || Game.instance.start
end

def stop_calculation_at
challenge&.close_challenge_at || Game.instance.stop
end

def open?
Expand Down
6 changes: 5 additions & 1 deletion app/models/share_challenge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class ShareChallenge < StandardChallenge
validates :unsolved_increment_period, numericality: { greater_than: 0 }

def start_calculation_at
game&.start
open_challenge_at || game&.start
end

def stop_calculation_at
close_challenge_at || game&.stop
end

# Share Challenges do not have a concept of teams, however the FlagChallengeShareModule
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddOpenChallengeAtAndCloseChallengeAt < ActiveRecord::Migration[6.0]
def change
add_column :challenges, :open_challenge_at, :datetime
add_column :challenges, :close_challenge_at, :datetime
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_06_23_173255) do
ActiveRecord::Schema.define(version: 2020_06_26_135801) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -52,6 +52,8 @@
t.text "sponsor", default: ""
t.text "sponsor_logo", default: "", null: false
t.text "sponsor_description", default: "", null: false
t.datetime "open_challenge_at"
t.datetime "close_challenge_at"
t.index ["game_id"], name: "index_challenges_on_game_id"
end

Expand Down
8 changes: 8 additions & 0 deletions test/models/challenge_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ class ChallengeTest < ActiveSupport::TestCase
assert_equal chal2, chal1.next_challenge
assert_nil chal3.next_challenge
end

test 'challenge is closed when its passed closing time or before opening time' do
game = create(:active_game)
challenge = create(:standard_challenge, close_challenge_at: Time.current - 1.days, open_challenge_at: Time.current + 1.days)

assert_not challenge.before_close?
assert_not challenge.after_open?
end
end
1 change: 1 addition & 0 deletions test/models/pentest_flag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def setup
unsolve_team = create(:team)
solved_challenge = create(:pentest_solved_challenge, team: solve_team, challenge: @challenge)
point_value = solved_challenge.flag.display_point_value(unsolve_team)
puts solved_challenge.flag.display_point_value(solve_team)
assert_equal 50, point_value
end

Expand Down