Class: Achievements::Engine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Engine

Initialize an Achievements Engine by passing a connected redis instance



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

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

Instance Attribute Details

#achievements(achievement_array) ⇒ Object

Bind multiple achievements at a time. Accepts an array of objects which respond to the context, name, and threshold methods.



26
27
28
# File 'lib/achievements/engine.rb', line 26

def achievements
  @achievements
end

#contextsObject

Returns the value of attribute contexts.



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

def contexts
  @contexts
end

#redisObject

Returns the value of attribute redis.



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

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/achievements/engine.rb', line 38

def achieve(context, agent_id, name)
  achieved = []
  
  # Increment user counter
  counter = "agent:#{agent_id}"
  incr counter
  
  # 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

Bind one achievement at a time. Accepts context, name, and threshold.



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

#achieves(achievements) ⇒ Object

Submit multiple achievements to the engine at one time, as an array of arrays.



64
65
66
67
68
69
70
# File 'lib/achievements/engine.rb', line 64

def achieves(achievements)
  response = []
  achievements.each do |a|
    response << achieve(a[0],a[1],a[2]).flatten
  end
  response
end

#deactiveate(counter) ⇒ Object

Deactivate a counter by setting it to “ACHIEVED,” this making it incapable of being incremented or decremented



84
85
86
# File 'lib/achievements/engine.rb', line 84

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

#decr(counter) ⇒ Object

Decrement a given counter



78
79
80
# File 'lib/achievements/engine.rb', line 78

def decr(counter)
  @redis.decr counter
end

#incr(counter) ⇒ Object

Increment a given counter



73
74
75
# File 'lib/achievements/engine.rb', line 73

def incr(counter)
  @redis.incr counter
end

#score(user_id, context = nil, name = nil) ⇒ Object

Retrieve the score of:

  • specific counter (provide user_id, context, name)

  • context counter (provide user_id, context)

  • user counter (provide user_id)



92
93
94
95
96
97
98
# File 'lib/achievements/engine.rb', line 92

def score(user_id, context = nil, name = nil)
  scores = []
  scores << @redis.get("agent:#{user_id}")
  scores << @redis.get("#{context}:agent:#{user_id}:parent") unless context.nil?
  scores << @redis.get("#{context}:agent:#{user_id}:#{name}") unless name.nil?
  scores
end