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



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

attribute :member_id, :type => Integer

#awarded_dateDate

Returns when the badge was awarded.

Returns:

  • (Date)

    when the badge was awarded



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

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



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

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



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

attribute :member_id, :type => Integer

#first_nameFixnum

Returns the member’s first name.

Returns:

  • (Fixnum)

    the member’s first name



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

attribute :member_id, :type => Integer

#last_nameFixnum

Returns Ithe member’s last name.

Returns:

  • (Fixnum)

    Ithe member’s last name



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

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



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

attribute :member_id, :type => Integer

#requirementsDirtyHashy

Returns the data for each badge requirement.

Returns:

  • (DirtyHashy)

    the data for each badge requirement



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

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



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

attribute :member_id, :type => Integer

Instance Method Details

#<=>(another) ⇒ Object

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



530
531
532
533
534
535
# File 'lib/osm/badge.rb', line 530

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



347
348
349
# File 'lib/osm/badge.rb', line 347

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



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/osm/badge.rb', line 370

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



354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/osm/badge.rb', line 354

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)


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/osm/badge.rb', line 329

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



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

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)


448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/osm/badge.rb', line 448

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



483
484
485
# File 'lib/osm/badge.rb', line 483

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

#sections_gainedHash

Get the total number of sections gained

Returns:

  • (Hash)


315
316
317
318
319
320
321
322
323
324
325
# File 'lib/osm/badge.rb', line 315

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)



410
411
412
413
414
415
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
# File 'lib/osm/badge.rb', line 410

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)



394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/osm/badge.rb', line 394

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



304
305
306
307
308
309
310
311
# File 'lib/osm/badge.rb', line 304

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:



491
492
493
494
495
496
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
# File 'lib/osm/badge.rb', line 491

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