Class: Osm::Badge::Data
Constant Summary collapse
- STAGES =
{ 'nightsaway' => [1, 2, 3, 4, 5, 10, 15, 20, 35, 50, 75, 100, 125, 150, 175, 200], 'hikes' => [1, 2, 5, 10, 15, 20, 35, 50], 'timeonthewater' => [1, 2, 5, 10, 15, 20, 35, 50], }
Instance Attribute Summary collapse
-
#awarded ⇒ Date
The last stage awarded.
-
#awarded_date ⇒ Date
When the badge was awarded.
-
#badge ⇒ Osm::Badge
The badge that the data belongs to.
-
#completed ⇒ Fixnum
Whether this badge has been completed (i.e. it is due?), number indicates stage if appropriate.
-
#first_name ⇒ Fixnum
The member’s first name.
-
#last_name ⇒ Fixnum
Ithe member’s last name.
-
#member_id ⇒ Fixnum
ID of the member this data relates to.
-
#requirements ⇒ DirtyHashy
The data for each badge requirement.
-
#section_id ⇒ Fixnum
The ID of the section the member belongs to.
Instance Method Summary collapse
-
#<=>(another) ⇒ Object
Compare Badge::Data based on badge, section_id then member_id.
-
#due? ⇒ Boolean
Check if this badge is due (according data retrieved from OSM).
-
#earnt ⇒ Fixnum
Get what stage which has most recently been earnt (using #earnt? will tell you if it’s still due (not yet awarded)).
-
#earnt? ⇒ Boolean
Check if this badge has been earnt.
-
#gained_in_sections ⇒ Hash
Get the number of requirements gained in each section.
- #inspect ⇒ Object
-
#mark_awarded(api, date = Date.today, level = completed, mark_as = :awarded) ⇒ Boolean
Mark the badge as awarded in OSM.
-
#mark_due(api, level) ⇒ Boolean
Mark the badge as due in OSM.
-
#sections_gained ⇒ Hash
Get the total number of sections gained.
-
#started ⇒ Fixnum
Get which stage has been started.
-
#started? ⇒ Boolean
Check if this badge has been started.
-
#total_gained ⇒ Fixnum
Get the total number of gained requirements.
-
#update(api) ⇒ Boolean
Update data in OSM.
Methods inherited from Model
#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i
Instance Attribute Details
#awarded ⇒ Date
Returns the last stage awarded.
273 |
# File 'lib/osm/badge.rb', line 273 attribute :member_id, :type => Integer |
#awarded_date ⇒ Date
Returns when the badge was awarded.
273 |
# File 'lib/osm/badge.rb', line 273 attribute :member_id, :type => Integer |
#badge ⇒ Osm::Badge
Returns the badge that the data belongs to.
273 |
# File 'lib/osm/badge.rb', line 273 attribute :member_id, :type => Integer |
#completed ⇒ Fixnum
Returns whether this badge has been completed (i.e. it is due?), number indicates stage if appropriate.
273 |
# File 'lib/osm/badge.rb', line 273 attribute :member_id, :type => Integer |
#first_name ⇒ Fixnum
Returns the member’s first name.
273 |
# File 'lib/osm/badge.rb', line 273 attribute :member_id, :type => Integer |
#last_name ⇒ Fixnum
Returns Ithe member’s last name.
273 |
# File 'lib/osm/badge.rb', line 273 attribute :member_id, :type => Integer |
#member_id ⇒ Fixnum
Returns ID of the member this data relates to.
273 |
# File 'lib/osm/badge.rb', line 273 attribute :member_id, :type => Integer |
#requirements ⇒ DirtyHashy
Returns the data for each badge requirement.
273 |
# File 'lib/osm/badge.rb', line 273 attribute :member_id, :type => Integer |
#section_id ⇒ Fixnum
Returns the ID of the section the member belongs to.
273 |
# File 'lib/osm/badge.rb', line 273 attribute :member_id, :type => Integer |
Instance Method Details
#<=>(another) ⇒ Object
Compare Badge::Data based on badge, section_id then member_id
541 542 543 544 545 546 |
# File 'lib/osm/badge.rb', line 541 def <=>(another) result = self.badge <=> another.try(:badge) result = self.section_id <=> another.try(:section_id) if result == 0 result = self.member_id <=> another.try(:member_id) if result == 0 return result end |
#due? ⇒ Boolean
Check if this badge is due (according data retrieved from OSM)
360 361 362 |
# File 'lib/osm/badge.rb', line 360 def due? completed > awarded end |
#earnt ⇒ Fixnum
Get what stage which has most recently been earnt (using #earnt? will tell you if it’s still due (not yet awarded))
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/osm/badge.rb', line 383 def earnt unless badge.type == :staged return earnt? ? 1 : 0 end if STAGES.keys.include?(badge.osm_key) total_done = requirements['y_01'] stages = STAGES[badge.osm_key] stages.reverse_each do |stage| return stage if total_done >= stage end else (awarded..5).reverse_each do |stage| group = 'abcde'[stage - 1] if gained_in_sections[group] >= badge.needed_from_section[group] return stage end end end return 0 end |
#earnt? ⇒ Boolean
Check if this badge has been earnt
367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/osm/badge.rb', line 367 def earnt? if badge.type == :staged return (earnt > awarded) end return false if (completed.eql?(1) && awarded.eql?(1)) return true if (completed.eql?(1) && awarded.eql?(0)) if badge.sections_needed == -1 # require all sections return (sections_gained == badge.needed_from_section.keys.size) else return (total_gained >= badge.total_needed) && (sections_gained >= badge.sections_needed) end end |
#gained_in_sections ⇒ Hash
Get the number of requirements gained in each section
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/osm/badge.rb', line 342 def gained_in_sections count = {} requirements.each do |field, data| field = field.split('_')[0] unless field.eql?('y') count[field] ||= 0 next unless reguiremet_met?(data) count[field] += 1 else # A total 'section' count['y'] = data.to_i end end return count end |
#inspect ⇒ Object
548 549 550 |
# File 'lib/osm/badge.rb', line 548 def inspect Osm.inspect_instance(self, ={:replace_with => {'badge' => :osm_key}}) end |
#mark_awarded(api, date = Date.today, level = completed, mark_as = :awarded) ⇒ Boolean
Mark the badge as awarded in OSM
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/osm/badge.rb', line 459 def mark_awarded(api, date=Date.today, level=completed, mark_as=:awarded) raise ArgumentError, 'date is not a Date' unless date.is_a?(Date) raise ArgumentError, 'mark_as is not an allowed value, use :awarded or :du' unless [:awarded, :due].include?(mark_as) raise ArgumentError, 'level can not be negative' if level < 0 section = Osm::Section.get(api, section_id) require_ability_to(api, :write, :badge, section) date_formatted = date.strftime(Osm::OSM_DATE_FORMAT) result = api.perform_query("challenges.php?action=award", { 'dateAwarded' => date_formatted, 'sectionid' => section_id, 'section' => section.type, 'chal' => badge.osm_key, 'type' => badge.type, 'stagedLevel' => level, 'due' => mark_as, }) updated = result.is_a?(Array) && result[0].is_a?(Hash) && (result[0]['sid'].to_i == member_id) && (result[0]['awarded'].to_i == level) && (result[0]['awardeddate'] == date_formatted) if updated awarded = level awarded_date = date end return updated end |
#mark_due(api, level) ⇒ Boolean
Mark the badge as due in OSM
494 495 496 |
# File 'lib/osm/badge.rb', line 494 def mark_due(api, level) mark_awarded(api, Date.today, level, :due) end |
#sections_gained ⇒ Hash
Get the total number of sections gained
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/osm/badge.rb', line 328 def sections_gained required = badge.needed_from_section gained = gained_in_sections count = 0 required.each do |section, needed| next if gained[section] < needed count += 1 end return count end |
#started ⇒ Fixnum
Get which stage has been started
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/osm/badge.rb', line 422 def started unless badge.type == :staged return started? ? 1 : 0 else # Staged badge if STAGES.keys.include?(badge.osm_key) # Special staged badges stages = STAGES[badge.osm_key] done = requirements['y_01'].to_i return 0 if done < stages[0] # Not started the first stage return 0 if done >= stages[stages.size - 1] # No more stages can be started (1..stages.size-1).reverse_each do |index| if (done < stages[index]) && (done > stages[index-1]) return stages[index] end end else # 'Normal' staged badge return 0 if completed == 5 || awarded == 5 # No more stages can be started start_group = 'abcde'[completed] # Requirements use the group letter to denote stage started = 'z' requirements.each do |key, value| next if key[0] < start_group # This stage is marked as completed next if key[0] > started # This stage is after the stage currently started started = key[0] unless value.blank? || value.to_s[0].downcase.eql?('x') end return started.eql?('z') ? 0 : 'abcde'.index(started)+1 end return 0 end end |
#started? ⇒ Boolean
Check if this badge has been started
406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/osm/badge.rb', line 406 def started? return (started > completed) if badge.type.eql?(:staged) # It's a staged badge return false if completed? requirements.each do |key, value| case key.split('_')[0] when 'a' return true if reguiremet_met?(value) when 'y' return true if (value.to_i > 0) end end return false end |
#total_gained ⇒ Fixnum
Get the total number of gained requirements
317 318 319 320 321 322 323 324 |
# File 'lib/osm/badge.rb', line 317 def total_gained count = 0 requirements.each do |field, data| next unless reguiremet_met?(data) count += 1 end return count end |
#update(api) ⇒ Boolean
Update data in OSM
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/osm/badge.rb', line 502 def update(api) raise Osm::ObjectIsInvalid, 'data is invalid' unless valid? section = Osm::Section.get(api, section_id) require_ability_to(api, :write, :badge, section) updated = true editable_fields = badge.requirements.select{ |r| r.editable }.map{ |r| r.field} requirements.changes.each do |field, (was,now)| if editable_fields.include?(field) result = api.perform_query("challenges.php?type=#{badge.class.type}§ion=#{section.type}", { 'action' => 'updatesingle', 'id' => member_id, 'col' => field, 'value' => now, 'chal' => badge.osm_key, 'sectionid' => section_id, }) updated = false unless result.is_a?(Hash) && (result['sid'].to_i == member_id) && (result[field] == now) end end if updated requirements.clean_up! end if changed_attributes.include?('awarded') || changed_attributes.include?('awarded_date') if mark_awarded(api, awarded_date, awarded) reset_changed_attributes else updated = false end end return updated end |