Class: Osm::Badge::Data

Inherits:
Model
  • Object
show all
Defined in:
lib/osm/badge.rb

Constant Summary collapse

STAGES_NIGHTSAWAY =
[1, 5, 10, 20, 35, 50, 75, 100, 125, 150, 175, 200]
STAGES_HIKESAWAY =
[1, 5, 10, 20, 35, 50]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Instance Attribute Details

#awardedDate

Returns the last stage awarded.

Returns:

  • (Date)

    the last stage awarded



268
# File 'lib/osm/badge.rb', line 268

attribute :member_id, :type => Integer

#awarded_dateDate

Returns when the badge was awarded.

Returns:

  • (Date)

    when the badge was awarded



268
# File 'lib/osm/badge.rb', line 268

attribute :member_id, :type => Integer

#badgeOsm::Badge

Returns the badge that the data belongs to.

Returns:

  • (Osm::Badge)

    the badge that the data belongs to



268
# File 'lib/osm/badge.rb', line 268

attribute :member_id, :type => Integer

#completedFixnum

Returns whether this badge has been completed (i.e. it is due?), number indicates stage if appropriate.

Returns:

  • (Fixnum)

    whether this badge has been completed (i.e. it is due?), number indicates stage if appropriate



268
# File 'lib/osm/badge.rb', line 268

attribute :member_id, :type => Integer

#first_nameFixnum

Returns the member’s first name.

Returns:

  • (Fixnum)

    the member’s first name



268
# File 'lib/osm/badge.rb', line 268

attribute :member_id, :type => Integer

#last_nameFixnum

Returns Ithe member’s last name.

Returns:

  • (Fixnum)

    Ithe member’s last name



268
# File 'lib/osm/badge.rb', line 268

attribute :member_id, :type => Integer

#member_idFixnum

Returns ID of the member this data relates to.

Returns:

  • (Fixnum)

    ID of the member this data relates to



268
# File 'lib/osm/badge.rb', line 268

attribute :member_id, :type => Integer

#requirementsDirtyHashy

Returns the data for each badge requirement.

Returns:

  • (DirtyHashy)

    the data for each badge requirement



268
# File 'lib/osm/badge.rb', line 268

attribute :member_id, :type => Integer

#section_idFixnum

Returns the ID of the section the member belongs to.

Returns:

  • (Fixnum)

    the ID of the section the member belongs to



268
# File 'lib/osm/badge.rb', line 268

attribute :member_id, :type => Integer

Instance Method Details

#<=>(another) ⇒ Object

Compare Badge::Data based on badge, section_id then member_id



536
537
538
539
540
541
# File 'lib/osm/badge.rb', line 536

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)

Returns:

  • (Boolean)

    whether the badge is due to the member



353
354
355
# File 'lib/osm/badge.rb', line 353

def due?
  completed > awarded
end

#earntFixnum

Get what stage which has most recently been earnt (using #earnt? will tell you if it’s still due (not yet awarded))

Returns:

  • (Fixnum)

    the stage which has most recently been due



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/osm/badge.rb', line 376

def earnt
  unless badge.type == :staged
    return earnt? ? 1 : 0
  end
  if ['nightsaway', 'hikes'].include?(badge.osm_key)
    total_done = requirements['y_01']
    stages = STAGES_NIGHTSAWAY if badge.osm_key.eql?('nightsaway')
    stages = STAGES_HIKESAWAY if badge.osm_key.eql?('hikes')
    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

Returns:

  • (Boolean)

    whether the badge is due to the member



360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/osm/badge.rb', line 360

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_sectionsHash

Get the number of requirements gained in each section

Returns:

  • (Hash)


335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/osm/badge.rb', line 335

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

#inspectObject



543
544
545
# File 'lib/osm/badge.rb', line 543

def inspect
  Osm.inspect_instance(self, options={: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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • date (Date) (defaults to: Date.today)

    The date to mark the badge as awarded

  • level (Fixnum) (defaults to: completed)

    The level of the badge to award (1 for non-staged badges)

  • mark_as (Symbol) (defaults to: :awarded)

    :awarded or :due

Returns:

  • (Boolean)

    whether the data was updated in OSM

Raises:

  • (ArgumentError)


454
455
456
457
458
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
# File 'lib/osm/badge.rb', line 454

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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • level (Fixnum)

    The level of the badge to mark as due (1 for non-staged badges)

Returns:

  • (Boolean)

    whether the data was updated in OSM



489
490
491
# File 'lib/osm/badge.rb', line 489

def mark_due(api, level)
  mark_awarded(api, Date.today, level, :due)
end

#sections_gainedHash

Get the total number of sections gained

Returns:

  • (Hash)


321
322
323
324
325
326
327
328
329
330
331
# File 'lib/osm/badge.rb', line 321

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

#startedFixnum

Get which stage has been started

Returns:

  • (Fixnum)

    which stage of the badge has been started by the member (lowest)



416
417
418
419
420
421
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
# File 'lib/osm/badge.rb', line 416

def started
  unless badge.type == :staged
    return started? ? 1 : 0
  else
    # Staged badge
    if ['nightsaway', 'hikes'].include?(badge.osm_key) # Special staged badges
      stages = STAGES_NIGHTSAWAY if badge.osm_key.eql?('nightsaway')
      stages = STAGES_HIKESAWAY if badge.osm_key.eql?('hikes')
      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

Returns:

  • (Boolean)

    whether the badge has been started by the member (always false if the badge has been completed)



400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/osm/badge.rb', line 400

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_gainedFixnum

Get the total number of gained requirements

Returns:

  • (Fixnum)

    the total number of requirements considered gained



310
311
312
313
314
315
316
317
# File 'lib/osm/badge.rb', line 310

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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the data was updated in OSM

Raises:



497
498
499
500
501
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
# File 'lib/osm/badge.rb', line 497

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}&section=#{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