gioco 0.1.8 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/generators/gioco/setup_generator.rb +62 -13
- data/lib/generators/templates/gioco.rb +3 -2
- data/lib/gioco.rb +4 -110
- data/lib/gioco/badges.rb +36 -0
- data/lib/gioco/core.rb +36 -0
- data/lib/gioco/rankings.rb +37 -0
- data/lib/gioco/resources.rb +15 -0
- metadata +8 -4
@@ -5,15 +5,23 @@ module Gioco
|
|
5
5
|
|
6
6
|
desc "Setup Gioco for some resource"
|
7
7
|
class_option :points, :type => :boolean, :default => false, :desc => "Setup gioco with points-system based"
|
8
|
+
class_option :types, :type => :boolean, :default => false, :desc => "Setup gioco with multiples types(categories) of badges."
|
8
9
|
|
9
10
|
def generate_models
|
10
|
-
generate("model", "badge name:string #{(options[:points]) ? "points:integer" : ""} default:boolean")
|
11
11
|
generate("model", "level badge_id:integer #{file_name}_id:integer")
|
12
|
-
|
12
|
+
if options[:types]
|
13
|
+
generate("model", "point user_id:integer type_id:integer value:integer")
|
14
|
+
generate("model", "type name:string")
|
15
|
+
generate("model", "badge name:string type_id:integer #{(options[:points]) ? "points:integer" : ""} default:boolean")
|
16
|
+
else
|
17
|
+
generate("migration", "add_points_to_#{file_name.pluralize} points:integer") if options[:points]
|
18
|
+
generate("model", "badge name:string #{(options[:points]) ? "points:integer" : ""} default:boolean")
|
19
|
+
end
|
13
20
|
end
|
14
21
|
|
15
22
|
def creating_templates
|
16
23
|
@points = ( options[:points] ) ? true : false
|
24
|
+
@types = ( options[:types] ) ? true : false
|
17
25
|
template "gioco.rb", "config/initializers/gioco.rb"
|
18
26
|
end
|
19
27
|
|
@@ -26,6 +34,15 @@ module Gioco
|
|
26
34
|
|
27
35
|
add_relationship( "level", file_name, "belongs_to" )
|
28
36
|
add_relationship( "level", "badge", "belongs_to" )
|
37
|
+
|
38
|
+
if options[:types]
|
39
|
+
add_relationship( file_name, "points", "has_many" )
|
40
|
+
add_relationship( "type", "points", "has_many" )
|
41
|
+
add_relationship( "type", "badges", "has_many" )
|
42
|
+
add_relationship( "badge", "type", "belongs_to" )
|
43
|
+
add_relationship( "point", file_name, "belongs_to" )
|
44
|
+
add_relationship( "point", "type", "belongs_to" )
|
45
|
+
end
|
29
46
|
end
|
30
47
|
|
31
48
|
def create_rakes
|
@@ -36,25 +53,35 @@ namespace :gioco do
|
|
36
53
|
|
37
54
|
desc "Used to add a new badge at Gioco scheme"
|
38
55
|
|
39
|
-
task :add_badge, [:name, #{
|
56
|
+
task :add_badge, [:name, #{":points, " if options[:points]}#{":type, " if options[:types]}:default] => :environment do |t, args|
|
40
57
|
args.default = ( args.default ) ? eval(args.default) : false
|
41
58
|
|
42
|
-
|
59
|
+
|
60
|
+
if !args.name #{"&& !args.points" if options[:points]}#{" && !args.type" if options[:types]}
|
43
61
|
puts "There are missing some arguments"
|
44
62
|
|
45
63
|
else
|
64
|
+
#{"type = ( Type.find_by_name(args.type) ) ? Type.find_by_name(args.type) : Type.create({ :name => args.type })" if options[:types]}
|
65
|
+
|
46
66
|
badge = Badge.create({
|
47
67
|
:name => args.name,
|
48
|
-
#{
|
68
|
+
#{":points => args.points," if options[:points]}
|
69
|
+
#{":type_id => type.id," if options[:types]}
|
49
70
|
:default => args.default
|
50
71
|
})
|
51
|
-
|
72
|
+
|
52
73
|
if args.default
|
53
74
|
resources = #{file_name.capitalize}.find(:all)
|
54
75
|
resources.each do |r|
|
55
|
-
#{
|
76
|
+
#{
|
77
|
+
if options[:points] && options[:types]
|
78
|
+
"r.points << Point.create({ :type_id => type.id, :value => args.points })"
|
79
|
+
elsif options[:points]
|
80
|
+
"r.points = args.points"
|
81
|
+
end
|
82
|
+
}
|
56
83
|
r.badges << badge
|
57
|
-
r.save
|
84
|
+
r.save!
|
58
85
|
end
|
59
86
|
end
|
60
87
|
|
@@ -75,7 +102,25 @@ namespace :gioco do
|
|
75
102
|
end
|
76
103
|
|
77
104
|
end
|
105
|
+
#{
|
106
|
+
if options[:types]
|
107
|
+
'task :remove_type, [:name] => :environment do |t, args|
|
78
108
|
|
109
|
+
if !args.name
|
110
|
+
puts "There are missing some arguments"
|
111
|
+
|
112
|
+
else
|
113
|
+
type = Type.find_by_name( args.name )
|
114
|
+
|
115
|
+
if type.badges.nil?
|
116
|
+
type.destroy
|
117
|
+
else
|
118
|
+
puts "Aborted! There are badges related with this type."
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end'
|
122
|
+
end
|
123
|
+
}
|
79
124
|
end
|
80
125
|
EOS
|
81
126
|
end
|
@@ -100,14 +145,18 @@ end
|
|
100
145
|
Gioco successfully installed.
|
101
146
|
|
102
147
|
Now you are able to add Badges using:
|
103
|
-
rake gioco:add_badge[BADGE_NAME,POINTS,DEFAULT]
|
148
|
+
rake gioco:add_badge[BADGE_NAME#{",POINTS" if options[:points]}#{",TYPE" if options[:types]},DEFAULT]
|
104
149
|
|
105
|
-
|
106
|
-
rake gioco:add_badge[BADGE_NAME,DEFAULT]
|
107
|
-
|
108
|
-
And to remove Badges using:
|
150
|
+
To remove Badges using:
|
109
151
|
rake gioco:remove_badge[BADGE_NAME]
|
110
152
|
|
153
|
+
#{
|
154
|
+
if options[:types]
|
155
|
+
"And to remove Types using:
|
156
|
+
rake gioco:remove_type[TYPE_NAME]"
|
157
|
+
end
|
158
|
+
}
|
159
|
+
|
111
160
|
For usage and more infomation go to the documentation:
|
112
161
|
http://joaomdmoura.github.com/gioco/
|
113
162
|
|
@@ -1,2 +1,3 @@
|
|
1
|
-
Gioco::Core::RESOURCE_NAME
|
2
|
-
Gioco::Core::POINTS
|
1
|
+
Gioco::Core::RESOURCE_NAME = "<%= file_name %>"
|
2
|
+
Gioco::Core::POINTS = <%= @points %>
|
3
|
+
Gioco::Core::TYPES = <%= @types %>
|
data/lib/gioco.rb
CHANGED
@@ -1,112 +1,6 @@
|
|
1
1
|
module Gioco
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
eval(RESOURCE_NAME.capitalize).find( rid )
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.get_badge( bid )
|
10
|
-
Badge.find( bid )
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.ranking
|
14
|
-
|
15
|
-
if POINTS
|
16
|
-
|
17
|
-
eval(RESOURCE_NAME.capitalize).order( "points DESC" )
|
18
|
-
|
19
|
-
else
|
20
|
-
|
21
|
-
eval(RESOURCE_NAME.capitalize).all(
|
22
|
-
:select => "#{eval(RESOURCE_NAME.capitalize).table_name}.*,
|
23
|
-
COUNT(levels.badge_id) AS number_of_levels",
|
24
|
-
:joins => :levels,
|
25
|
-
:group => "#{RESOURCE_NAME}_id",
|
26
|
-
:order => "number_of_levels DESC")
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.sync_resource_by_points( resource, points )
|
33
|
-
|
34
|
-
old_pontuation = resource.points.to_i
|
35
|
-
|
36
|
-
related_badges = Badge.where( ( old_pontuation < points ) ? "points <= #{points}" : "points > #{points} AND points <= #{old_pontuation}" )
|
37
|
-
|
38
|
-
Badge.transaction do
|
39
|
-
|
40
|
-
resource.update_attributes( {:points => points } )
|
41
|
-
|
42
|
-
related_badges.each do |badge|
|
43
|
-
|
44
|
-
if old_pontuation < points
|
45
|
-
|
46
|
-
resource.badges << badge if !resource.badges.include?(badge)
|
47
|
-
|
48
|
-
elsif old_pontuation > points
|
49
|
-
|
50
|
-
resource.levels.where( :badge_id => badge.id )[0].destroy
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
class Resources < Core
|
63
|
-
|
64
|
-
def self.change_points( rid, points, resource = false )
|
65
|
-
|
66
|
-
resource = get_resource( rid ) if !resource
|
67
|
-
new_pontuation = resource.points.to_i + points
|
68
|
-
|
69
|
-
sync_resource_by_points( resource, new_pontuation )
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
class Badges < Core
|
76
|
-
|
77
|
-
def self.add( rid, badge_id, resource = false, badge = false )
|
78
|
-
resource = get_resource( rid ) if !resource
|
79
|
-
badge = get_badge( badge_id ) if !badge
|
80
|
-
|
81
|
-
if POINTS && !resource.badges.include?(badge)
|
82
|
-
|
83
|
-
sync_resource_by_points( resource, badge.points )
|
84
|
-
|
85
|
-
elsif !resource.badges.include?(badge)
|
86
|
-
|
87
|
-
resource.badges << badge
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
def self.remove( rid, badge_id, resource = false, badge = false )
|
94
|
-
|
95
|
-
resource = get_resource( rid ) if !resource
|
96
|
-
badge = get_badge( badge_id ) if !badge
|
97
|
-
|
98
|
-
if POINTS && resource.badges.include?(badge)
|
99
|
-
|
100
|
-
sync_resource_by_points( resource, Badge.where( "points < #{badge.points}" )[0].points )
|
101
|
-
|
102
|
-
elsif resource.badges.include?(badge)
|
103
|
-
|
104
|
-
resource.levels.where( :badge_id => badge.id )[0].destroy
|
105
|
-
|
106
|
-
end
|
107
|
-
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
|
2
|
+
require "gioco/core"
|
3
|
+
require "gioco/resources"
|
4
|
+
require "gioco/badges"
|
5
|
+
require "gioco/rankings"
|
112
6
|
end
|
data/lib/gioco/badges.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Gioco
|
2
|
+
class Badges < Core
|
3
|
+
def self.add(rid, badge_id)
|
4
|
+
resource = get_resource( rid )
|
5
|
+
badge = Badge.find( badge_id )
|
6
|
+
|
7
|
+
if POINTS && !resource.badges.include?(badge)
|
8
|
+
if TYPES
|
9
|
+
sync_resource_by_points( resource, badge.points, badge.type)
|
10
|
+
else
|
11
|
+
sync_resource_by_points( resource, badge.points)
|
12
|
+
end
|
13
|
+
elsif !resource.badges.include?(badge)
|
14
|
+
resource.badges << badge
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.remove( rid, badge_id )
|
19
|
+
resource = get_resource( rid )
|
20
|
+
badge = Badge.find( badge_id )
|
21
|
+
type = badge.type
|
22
|
+
|
23
|
+
if POINTS && resource.badges.include?(badge)
|
24
|
+
if TYPES
|
25
|
+
badges_gap = Badge.where( "points < #{badge.points} AND type_id = #{type.id}" )[0]
|
26
|
+
sync_resource_by_points( resource, ( badges_gap.nil? ) ? 0 : badges_gap.points, type)
|
27
|
+
else
|
28
|
+
badges_gap = Badge.where( "points < #{badge.points}" )[0]
|
29
|
+
sync_resource_by_points( resource, ( badges_gap.nil? ) ? 0 : badges_gap.points)
|
30
|
+
end
|
31
|
+
elsif resource.badges.include?(badge)
|
32
|
+
resource.levels.where( :badge_id => badge.id )[0].destroy
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/gioco/core.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Gioco
|
2
|
+
class Core
|
3
|
+
def self.get_resource(rid)
|
4
|
+
RESOURCE_NAME.capitalize.constantize.find(rid)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.sync_resource_by_points(resource, points, type = false)
|
8
|
+
|
9
|
+
if TYPES && type
|
10
|
+
old_pontuation = resource.points.where(:type_id => type.id).sum(:value)
|
11
|
+
related_badges = Badge.where( ((old_pontuation < points) ? "points <= #{points}" : "points > #{points} AND points <= #{old_pontuation}") + " AND type_id = #{type.id}" )
|
12
|
+
else
|
13
|
+
old_pontuation = resource.points.to_i
|
14
|
+
related_badges = Badge.where((old_pontuation < points) ? "points <= #{points}" : "points > #{points} AND points <= #{old_pontuation}" )
|
15
|
+
end
|
16
|
+
|
17
|
+
new_pontuation = ( old_pontuation < points ) ? points : - (old_pontuation - points)
|
18
|
+
|
19
|
+
Badge.transaction do
|
20
|
+
if TYPES && type
|
21
|
+
resource.points << Point.create({ :type_id => type.id, :value => new_pontuation })
|
22
|
+
elsif options[:points]
|
23
|
+
resource.update_attribute( :points, new_pontuation )
|
24
|
+
end
|
25
|
+
related_badges.each do |badge|
|
26
|
+
if old_pontuation < points
|
27
|
+
resource.badges << badge if !resource.badges.include?(badge)
|
28
|
+
elsif old_pontuation > points
|
29
|
+
resource.levels.where( :badge_id => badge.id )[0].destroy
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Gioco
|
2
|
+
class Ranking < Core
|
3
|
+
|
4
|
+
def self.generate
|
5
|
+
ranking = []
|
6
|
+
|
7
|
+
if POINTS && TYPES
|
8
|
+
Type.find(:all).each do |t|
|
9
|
+
data = RESOURCE_NAME.capitalize.constantize
|
10
|
+
.select("#{RESOURCE_NAME.capitalize.constantize.table_name}.*,
|
11
|
+
points.type_id, SUM(points.value) AS type_points")
|
12
|
+
.where("points.type_id = #{t.id}")
|
13
|
+
.joins(:points)
|
14
|
+
.group("type_id, #{RESOURCE_NAME}_id")
|
15
|
+
.order("type_points DESC")
|
16
|
+
|
17
|
+
ranking << { :type => t, :ranking => data }
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
elsif POINTS && !TYPES
|
22
|
+
ranking = RESOURCE_NAME.capitalize.constantize.order("points DESC")
|
23
|
+
|
24
|
+
elsif !POINTS && !TYPES
|
25
|
+
ranking = RESOURCE_NAME.capitalize.constantize
|
26
|
+
.select("#{RESOURCE_NAME.capitalize.constantize.table_name}.*,
|
27
|
+
COUNT(levels.badge_id) AS number_of_levels")
|
28
|
+
.joins(:levels)
|
29
|
+
.group("#{RESOURCE_NAME}_id")
|
30
|
+
.order("number_of_levels DESC")
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
ranking
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Gioco
|
2
|
+
class Resources < Core
|
3
|
+
def self.change_points( rid, points, tid = false )
|
4
|
+
resource = get_resource( rid )
|
5
|
+
type = ( tid ) ? Type.find(tid) : false
|
6
|
+
if TYPES && tid
|
7
|
+
old_pontuation = resource.points.where(:type_id => tid).sum(:value)
|
8
|
+
else
|
9
|
+
old_pontuation = resource.points.to_i
|
10
|
+
end
|
11
|
+
new_pontuation = old_pontuation + points
|
12
|
+
sync_resource_by_points(resource, new_pontuation, type)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gioco
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-21 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Gioco is a easy to implement gamification gem based on plug and play
|
15
|
-
concept.Doesn't matter if you already have a full and functional database, Gioco
|
16
|
-
will smoothly
|
15
|
+
concept. Doesn't matter if you already have a full and functional database, Gioco
|
16
|
+
will smoothly integrate everything and provide all methods that you might need.
|
17
17
|
email: joaomdmoura@gmail.com
|
18
18
|
executables: []
|
19
19
|
extensions: []
|
@@ -22,6 +22,10 @@ files:
|
|
22
22
|
- lib/gioco.rb
|
23
23
|
- lib/generators/gioco/setup_generator.rb
|
24
24
|
- lib/generators/templates/gioco.rb
|
25
|
+
- lib/gioco/badges.rb
|
26
|
+
- lib/gioco/core.rb
|
27
|
+
- lib/gioco/rankings.rb
|
28
|
+
- lib/gioco/resources.rb
|
25
29
|
- init.rb
|
26
30
|
homepage: http://joaomdmoura.github.com/gioco/
|
27
31
|
licenses: []
|