diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -240,6 +240,7 @@ if passed flash[:notice] = 'Correct solution.' + user.update_codejom_status else flash[:notice] = 'Incorrect solution.' end diff --git a/app/models/codejom_status.rb b/app/models/codejom_status.rb new file mode 100644 --- /dev/null +++ b/app/models/codejom_status.rb @@ -0,0 +1,5 @@ +class CodejomStatus < ActiveRecord::Base + + belongs_to :user + +end diff --git a/app/models/problem.rb b/app/models/problem.rb --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -29,6 +29,11 @@ find(:all, :conditions => {:available => true}, :order => "date_added DESC") end + # TODO: may try to optimize this using cache + def self.available_problem_count + return Problem.find_available_problems.length + end + def self.create_from_import_form_params(params, old_problem=nil) problem = old_problem || Problem.new import_params = Problem.extract_params_and_check(params, problem) diff --git a/app/models/user.rb b/app/models/user.rb --- a/app/models/user.rb +++ b/app/models/user.rb @@ -24,6 +24,9 @@ belongs_to :site belongs_to :country + # For Code Jom + has_one :codejom_status + named_scope :activated_users, :conditions => {:activated => true} validates_presence_of :login @@ -221,6 +224,24 @@ end end + # For Code Jom + def update_codejom_status + status = codejom_status || CodejomStatus.new(:user => self) + problem_count = Problem.available_problem_count + status.num_problems_passed = (self.submission_statuses.find_all {|s| s.passed}).length + status.alive = (problem_count - (status.num_problems_passed)) <= CODEJOM_MAX_ALIVE_LEVEL + status.save + end + + def codejom_level + problem_count = Problem.available_problem_count + if codejom_status!=nil + return problem_count - codejom_status.num_problems_passed + else + return problem_count + end + end + protected def encrypt_new_password return if password.blank? diff --git a/config/environment.rb.SAMPLE b/config/environment.rb.SAMPLE --- a/config/environment.rb.SAMPLE +++ b/config/environment.rb.SAMPLE @@ -103,3 +103,7 @@ # Uncomment so that configuration is read only once when the server is loaded # Configuration.enable_caching + +# OPTIONS FOR CODE JOM +# -------------------- +CODEJOM_MAX_ALIVE_LEVEL = 10 diff --git a/db/migrate/20100124191250_create_codejom_statuses.rb b/db/migrate/20100124191250_create_codejom_statuses.rb new file mode 100644 --- /dev/null +++ b/db/migrate/20100124191250_create_codejom_statuses.rb @@ -0,0 +1,15 @@ +class CreateCodejomStatuses < ActiveRecord::Migration + def self.up + create_table :codejom_statuses do |t| + t.integer :user_id + t.boolean :alive + t.integer :num_problems_passed + + t.timestamps + end + end + + def self.down + drop_table :codejom_statuses + end +end diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20100124054458) do +ActiveRecord::Schema.define(:version => 20100124191250) do create_table "announcements", :force => true do |t| t.string "author" @@ -22,6 +22,14 @@ t.string "title" end + create_table "codejom_statuses", :force => true do |t| + t.integer "user_id" + t.boolean "alive" + t.integer "num_problems_passed" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "configurations", :force => true do |t| t.string "key" t.string "value_type" diff --git a/test/fixtures/codejom_statuses.yml b/test/fixtures/codejom_statuses.yml new file mode 100644 --- /dev/null +++ b/test/fixtures/codejom_statuses.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + user_id: 1 + alive: false + num_problems_passed: 1 + +two: + user_id: 1 + alive: false + num_problems_passed: 1 diff --git a/test/unit/codejom_status_test.rb b/test/unit/codejom_status_test.rb new file mode 100644 --- /dev/null +++ b/test/unit/codejom_status_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class CodejomStatusTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end