Class: Achievements::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/achievements/engine.rb

Overview

Triggering multiple at once

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Engine

Returns a new instance of Engine.



9
10
11
12
13
# File 'lib/achievements/engine.rb', line 9

def initialize(redis)
  @contexts = []
  @redis = redis
  @achievements = {}
end

Instance Attribute Details

#achievementsObject

Returns the value of attribute achievements.



5
6
7
# File 'lib/achievements/engine.rb', line 5

def achievements
  @achievements
end

#contextsObject

Returns the value of attribute contexts.



6
7
8
# File 'lib/achievements/engine.rb', line 6

def contexts
  @contexts
end

#redisObject

Returns the value of attribute redis.



7
8
9
# File 'lib/achievements/engine.rb', line 7

def redis
  @redis
end

Instance Method Details

#achieve(context, agent_id, name) ⇒ Object

The trigger method accepts: context, agent_id, name

And returns: context, name, threshold



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/achievements/engine.rb', line 29

def achieve(context, agent_id, name)
  achieved = []
  
  # Increment parent counter
  counter = Counter.make(context,agent_id,"parent")
  incr counter

  # Increment child counter
  counter = Counter.make(context,agent_id,name)
  result = incr counter

  # Check Threshold

  if  @redis.sismember("#{context}:#{name}:threshold", result) == true
    achieved << [context,name, result.to_s]
    return achieved
  else
    return []
  end
  
end

#achievement(context, name, threshold) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/achievements/engine.rb', line 15

def achievement(context,name,threshold)
  @contexts << context if !@contexts.include?(context)
  if achievement = Achievement.new(context,name,threshold)
    [threshold].flatten.each do |thresh|
      @redis.sadd "#{context}:#{name}:threshold", thresh.to_s
    end
  end
end

#deactiveate(counter) ⇒ Object



61
62
63
# File 'lib/achievements/engine.rb', line 61

def deactiveate(counter)
  @redis.set counter, "ACHIEVED"
end

#decr(counter) ⇒ Object

decr key



57
58
59
# File 'lib/achievements/engine.rb', line 57

def decr(counter)
  @redis.decr counter
end

#incr(counter) ⇒ Object

incr key



52
53
54
# File 'lib/achievements/engine.rb', line 52

def incr(counter)
  @redis.incr counter
end