gioco 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/generators/gioco/generator_instructions.rb +5 -5
- data/lib/generators/gioco/model_generator.rb +11 -11
- data/lib/generators/gioco/rakes_generator.rb +22 -22
- data/lib/generators/gioco/setup_generator.rb +2 -2
- data/lib/generators/templates/badge.rb +6 -6
- data/lib/generators/templates/gioco.rb +1 -1
- data/lib/generators/templates/resource.rb +17 -18
- data/lib/gioco/core.rb +18 -11
- data/lib/gioco/rankings.rb +29 -25
- metadata +3 -3
@@ -8,15 +8,15 @@ module GeneratorInstructions
|
|
8
8
|
Gioco successfully installed.
|
9
9
|
|
10
10
|
Now you are able to add Badges using:
|
11
|
-
rake gioco:add_badge[BADGE_NAME#{",POINTS" if options[:points]}#{",
|
11
|
+
rake gioco:add_badge[BADGE_NAME#{",POINTS" if options[:points]}#{",KIND_NAME" if options[:kinds]},DEFAULT]
|
12
12
|
|
13
13
|
To remove Badges using:
|
14
|
-
rake gioco:remove_badge[BADGE_NAME#{",
|
14
|
+
rake gioco:remove_badge[BADGE_NAME#{",KIND_NAME" if options[:kinds]}]
|
15
15
|
|
16
16
|
#{
|
17
|
-
if options[:
|
18
|
-
"And to remove
|
19
|
-
rake gioco:
|
17
|
+
if options[:kinds]
|
18
|
+
"And to remove Kinds using:
|
19
|
+
rake gioco:remove_kind[KIND_NAME]"
|
20
20
|
end
|
21
21
|
}
|
22
22
|
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module ModelGenerator
|
2
2
|
def generate_models
|
3
3
|
generate("model", "level badge_id:integer #{@model_name}_id:integer")
|
4
|
-
if options[:
|
5
|
-
generate("model", "point user_id:integer
|
6
|
-
generate("model", "
|
7
|
-
generate("model", "badge name:string
|
4
|
+
if options[:kinds]
|
5
|
+
generate("model", "point user_id:integer kind_id:integer value:integer")
|
6
|
+
generate("model", "kind name:string")
|
7
|
+
generate("model", "badge name:string kind_id:integer #{(options[:points]) ? "points:integer" : ""} default:boolean")
|
8
8
|
else
|
9
9
|
generate("migration", "add_points_to_#{@model_name.pluralize} points:integer") if options[:points]
|
10
10
|
generate("model", "badge name:string #{(options[:points]) ? "points:integer" : ""} default:boolean")
|
@@ -13,7 +13,7 @@ module ModelGenerator
|
|
13
13
|
|
14
14
|
def creating_templates
|
15
15
|
@points = (options[:points] ) ? true : false
|
16
|
-
@
|
16
|
+
@kinds = (options[:kinds] ) ? true : false
|
17
17
|
template "gioco.rb", "config/initializers/gioco.rb"
|
18
18
|
end
|
19
19
|
|
@@ -34,19 +34,19 @@ module ModelGenerator
|
|
34
34
|
add_relationship("level", @model_name, "belongs_to")
|
35
35
|
add_relationship("level", "badge", "belongs_to")
|
36
36
|
|
37
|
-
if options[:
|
37
|
+
if options[:kinds]
|
38
38
|
add_relationship(@model_name, "points", "has_many")
|
39
|
-
add_relationship("
|
40
|
-
add_relationship("
|
41
|
-
add_relationship("badge", "
|
39
|
+
add_relationship("kind", "points", "has_many")
|
40
|
+
add_relationship("kind", "badges", "has_many")
|
41
|
+
add_relationship("badge", "kind", "belongs_to")
|
42
42
|
add_relationship("point", @model_name, "belongs_to")
|
43
|
-
add_relationship("point", "
|
43
|
+
add_relationship("point", "kind", "belongs_to")
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
def add_validations
|
48
48
|
add_validation("badge", "name", [["presence", "true"]])
|
49
|
-
add_validation("
|
49
|
+
add_validation("kind", "name", [["uniqueness", "true"], ["presence", "true"]]) if options[:kinds]
|
50
50
|
end
|
51
51
|
|
52
52
|
private
|
@@ -7,19 +7,19 @@ namespace :gioco do
|
|
7
7
|
|
8
8
|
desc "Used to add a new badge at Gioco scheme"
|
9
9
|
|
10
|
-
task :add_badge, [:name, #{":points, " if options[:points]}#{":
|
10
|
+
task :add_badge, [:name, #{":points, " if options[:points]}#{":kind, " if options[:kinds]}:default] => :environment do |t, args|
|
11
11
|
arg_default = ( args.default ) ? eval(args.default) : false
|
12
12
|
|
13
13
|
|
14
|
-
if !args.name #{"|| !args.points" if options[:points]}#{" || !args.
|
14
|
+
if !args.name #{"|| !args.points" if options[:points]}#{" || !args.kind" if options[:kinds]}
|
15
15
|
raise "There are missing some arguments"
|
16
16
|
else
|
17
|
-
badge_string = "#{options[:
|
17
|
+
badge_string = "#{options[:kinds] ? 'kind = Kind.find_or_create_by(name: \'#{args.kind}\')\n' : ''}"
|
18
18
|
|
19
19
|
badge_string = badge_string + "badge = Badge.create({
|
20
20
|
:name => \'\#\{args.name\}\',
|
21
21
|
#{":points => \'\#\{args.points\}\'," if options[:points]}
|
22
|
-
#{":
|
22
|
+
#{":kind_id => kind.id," if options[:kinds]}
|
23
23
|
:default => \'\#\{arg_default\}\'
|
24
24
|
})\n"
|
25
25
|
|
@@ -27,8 +27,8 @@ namespace :gioco do
|
|
27
27
|
badge_string = badge_string + 'resources = #{@model_name.capitalize}.find(:all)\n'
|
28
28
|
badge_string = badge_string + "resources.each do |r|
|
29
29
|
#{
|
30
|
-
if options[:points] && options[:
|
31
|
-
"r.points << Point.create({ :
|
30
|
+
if options[:points] && options[:kinds]
|
31
|
+
"r.points << Point.create({ :kind_id => kinds.id, :value => \'\#\{args.points\}\'})"
|
32
32
|
elsif options[:points]
|
33
33
|
"r.points = \'\#\{args.points\}\'"
|
34
34
|
end
|
@@ -42,7 +42,7 @@ namespace :gioco do
|
|
42
42
|
|
43
43
|
eval badge_string
|
44
44
|
|
45
|
-
file_path = "/db/gioco/create_badge_\#\{args.name\}#{"_\#\{args.
|
45
|
+
file_path = "/db/gioco/create_badge_\#\{args.name\}#{"_\#\{args.kind\}" if options[:kinds]}.rb"
|
46
46
|
File.open("\#\{Rails.root\}\#\{file_path\}", 'w') { |f| f.write badge_string }
|
47
47
|
File.open("\#\{Rails.root\}/db/gioco/db.rb", 'a') { |f| f.write "require \\"\\#\\{Rails.root\\}\#\{file_path\}\\"\n" }
|
48
48
|
|
@@ -52,12 +52,12 @@ namespace :gioco do
|
|
52
52
|
|
53
53
|
desc "Used to remove an old badge at Gioco scheme"
|
54
54
|
|
55
|
-
task :remove_badge, [:name#{", :
|
56
|
-
if !args.name#{" || !args.
|
55
|
+
task :remove_badge, [:name#{", :kind" if options[:kinds]}] => :environment do |t, args|
|
56
|
+
if !args.name#{" || !args.kind" if options[:kinds]}
|
57
57
|
raise "There are missing some arguments"
|
58
58
|
else
|
59
|
-
badge_string = "#{"
|
60
|
-
badge = Badge.where( :name => '\#\{args.name\}'#{", :
|
59
|
+
badge_string = "#{"kind = Kind.find_by_name('\#\{args.kind\}')" if options[:kinds]}
|
60
|
+
badge = Badge.where( :name => '\#\{args.name\}'#{", :kind_id => kind.id" if options[:kinds]} ).first
|
61
61
|
badge.destroy\n"
|
62
62
|
end
|
63
63
|
|
@@ -70,25 +70,25 @@ namespace :gioco do
|
|
70
70
|
File.open("\#\{Rails.root\}/db/gioco/db.rb", 'a') { |f| f.write "require \\"\\#\\{Rails.root\\}\#\{file_path\}\\"\n" }
|
71
71
|
end
|
72
72
|
#{
|
73
|
-
if options[:
|
73
|
+
if options[:kinds]
|
74
74
|
'
|
75
|
-
desc "Removes a given
|
76
|
-
task :
|
75
|
+
desc "Removes a given kind"
|
76
|
+
task :remove_kind, [:name] => :environment do |t, args|
|
77
77
|
if !args.name
|
78
78
|
raise "There are missing some arguments"
|
79
79
|
else
|
80
|
-
|
81
|
-
|
82
|
-
|
80
|
+
kind_string = "kind = Kind.find_by_name( \'#{args.name}\' )\n"
|
81
|
+
kind_string = kind_string + "if kind.badges.empty?
|
82
|
+
kind.destroy
|
83
83
|
else
|
84
|
-
raise \'Aborted! There are badges related with this
|
84
|
+
raise \'Aborted! There are badges related with this kind.\'
|
85
85
|
end\n"
|
86
86
|
end
|
87
|
-
|
88
|
-
eval
|
87
|
+
kind_string = kind_string + "puts \'> Kind successfully removed\'"
|
88
|
+
eval kind_string
|
89
89
|
|
90
|
-
file_path = "/db/gioco/
|
91
|
-
File.open("#{Rails.root}#{file_path}", "w") { |f| f.write
|
90
|
+
file_path = "/db/gioco/remove_kind_#{args.name}.rb"
|
91
|
+
File.open("#{Rails.root}#{file_path}", "w") { |f| f.write kind_string }
|
92
92
|
File.open("#{Rails.root}/db/gioco/db.rb", "a") { |f| f.write "require \\"\\#\\{Rails.root\\}#{file_path}\\"\n" }
|
93
93
|
end
|
94
94
|
'
|
@@ -13,8 +13,8 @@ class Gioco
|
|
13
13
|
source_root File.expand_path("../../templates", __FILE__)
|
14
14
|
|
15
15
|
desc "Setup Gioco for some resource"
|
16
|
-
class_option :points, :
|
17
|
-
class_option :
|
16
|
+
class_option :points, :kind => :boolean, :default => false, :desc => "Setup gioco with points-system based"
|
17
|
+
class_option :kinds, :kind => :boolean, :default => false, :desc => "Setup gioco with multiples kinds(categories) of badges."
|
18
18
|
|
19
19
|
|
20
20
|
def execute
|
@@ -2,8 +2,8 @@ def add(resource_id)
|
|
2
2
|
resource = Gioco::Core.get_resource(resource_id)
|
3
3
|
|
4
4
|
if Gioco::Core::POINTS && !resource.badges.include?(self)
|
5
|
-
if Gioco::Core::
|
6
|
-
Gioco::Core.sync_resource_by_points(resource, self.points, self.
|
5
|
+
if Gioco::Core::KINDS
|
6
|
+
Gioco::Core.sync_resource_by_points(resource, self.points, self.kind)
|
7
7
|
else
|
8
8
|
Gioco::Core.sync_resource_by_points(resource, self.points)
|
9
9
|
end
|
@@ -17,10 +17,10 @@ def remove(resource_id)
|
|
17
17
|
resource = Gioco::Core.get_resource(resource_id)
|
18
18
|
|
19
19
|
if Gioco::Core::POINTS && resource.badges.include?(self)
|
20
|
-
if Gioco::Core::
|
21
|
-
|
22
|
-
badges_gap = Badge.where( "points < #{self.points} AND
|
23
|
-
Gioco::Core.sync_resource_by_points( resource, ( badges_gap.nil? ) ? 0 : badges_gap.points,
|
20
|
+
if Gioco::Core::KINDS
|
21
|
+
kind = self.kind
|
22
|
+
badges_gap = Badge.where( "points < #{self.points} AND kind_id = #{kind.id}" ).order('points DESC')[0]
|
23
|
+
Gioco::Core.sync_resource_by_points( resource, ( badges_gap.nil? ) ? 0 : badges_gap.points, kind)
|
24
24
|
else
|
25
25
|
badges_gap = Badge.where( "points < #{self.points}" ).order('points DESC')[0]
|
26
26
|
Gioco::Core.sync_resource_by_points( resource, ( badges_gap.nil? ) ? 0 : badges_gap.points)
|
@@ -1,29 +1,28 @@
|
|
1
1
|
def change_points(options)
|
2
|
-
if Gioco::Core::
|
3
|
-
|
4
|
-
|
2
|
+
if Gioco::Core::KINDS
|
3
|
+
points = options[:points]
|
4
|
+
kind = Kind.find(options[:kind])
|
5
5
|
else
|
6
|
-
points
|
6
|
+
points = options
|
7
|
+
kind = false
|
7
8
|
end
|
8
|
-
type = (type_id) ? Type.find(type_id) : false
|
9
9
|
|
10
|
-
if Gioco::Core::
|
11
|
-
raise "Missing
|
12
|
-
old_pontuation
|
10
|
+
if Gioco::Core::KINDS
|
11
|
+
raise "Missing Kind Identifier argument" if !kind
|
12
|
+
old_pontuation = self.points.where(:kind_id => kind.id).sum(:value)
|
13
13
|
else
|
14
|
-
old_pontuation
|
14
|
+
old_pontuation = self.points.to_i
|
15
15
|
end
|
16
|
-
new_pontuation
|
17
|
-
Gioco::Core.sync_resource_by_points(self, new_pontuation,
|
16
|
+
new_pontuation = old_pontuation + points
|
17
|
+
Gioco::Core.sync_resource_by_points(self, new_pontuation, kind)
|
18
18
|
end
|
19
19
|
|
20
|
-
def next_badge?(
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
old_pontuation = self.points.where(:type_id => type_id).sum(:value)
|
20
|
+
def next_badge?(kind_id = false)
|
21
|
+
if Gioco::Core::KINDS
|
22
|
+
raise "Missing Kind Identifier argument" if !kind_id
|
23
|
+
old_pontuation = self.points.where(:kind_id => kind_id).sum(:value)
|
25
24
|
else
|
26
|
-
old_pontuation
|
25
|
+
old_pontuation = self.points.to_i
|
27
26
|
end
|
28
27
|
next_badge = Badge.where("points > #{old_pontuation}").order("points ASC").first
|
29
28
|
last_badge_point = self.badges.last.try('points')
|
@@ -32,7 +31,7 @@ def next_badge?(type_id = false)
|
|
32
31
|
if next_badge
|
33
32
|
percentage = (old_pontuation - last_badge_point)*100/(next_badge.points - last_badge_point)
|
34
33
|
points = next_badge.points - old_pontuation
|
35
|
-
next_badge_info = {
|
34
|
+
next_badge_info = {
|
36
35
|
:badge => next_badge,
|
37
36
|
:points => points,
|
38
37
|
:percentage => percentage
|
data/lib/gioco/core.rb
CHANGED
@@ -4,28 +4,36 @@ class Gioco
|
|
4
4
|
RESOURCE_NAME.capitalize.constantize.find(rid)
|
5
5
|
end
|
6
6
|
|
7
|
-
def self.
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
old_pontuation = resource.points.where(:type_id => type.id).sum(:value)
|
12
|
-
related_badges = Badge.where(((old_pontuation < points) ? "points <= #{points}" : "points > #{points} AND points <= #{old_pontuation}") + " AND type_id = #{type.id}")
|
7
|
+
def self.related_info(resource, points, kind)
|
8
|
+
if KINDS && kind
|
9
|
+
old_pontuation = resource.points.where(:kind_id => kind.id).sum(:value)
|
10
|
+
related_badges = Badge.where(((old_pontuation < points) ? "points <= #{points}" : "points > #{points} AND points <= #{old_pontuation}") + " AND kind_id = #{kind.id}")
|
13
11
|
else
|
14
12
|
old_pontuation = resource.points.to_i
|
15
13
|
related_badges = Badge.where((old_pontuation < points) ? "points <= #{points}" : "points > #{points} AND points <= #{old_pontuation}")
|
16
14
|
end
|
17
|
-
|
18
15
|
new_pontuation = ( old_pontuation < points ) ? points - old_pontuation : - (old_pontuation - points)
|
19
16
|
|
17
|
+
{ old_pontuation: old_pontuation, related_badges: related_badges, new_pontuation: new_pontuation }
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.sync_resource_by_points(resource, points, kind = false)
|
21
|
+
|
22
|
+
badges = {}
|
23
|
+
info = self.related_info(resource, points, kind)
|
24
|
+
old_pontuation = info[:old_pontuation]
|
25
|
+
related_badges = info[:related_badges]
|
26
|
+
new_pontuation = info[:new_pontuation]
|
27
|
+
|
20
28
|
Badge.transaction do
|
21
|
-
|
22
|
-
resource.points << Point.create({ :
|
29
|
+
if KINDS && kind
|
30
|
+
resource.points << Point.create({ :kind_id => kind.id, :value => new_pontuation })
|
23
31
|
elsif POINTS
|
24
32
|
resource.update_attribute( :points, points )
|
25
33
|
end
|
26
34
|
related_badges.each do |badge|
|
27
35
|
if old_pontuation < points
|
28
|
-
|
36
|
+
unless resource.badges.include?(badge)
|
29
37
|
resource.badges << badge
|
30
38
|
badges[:added] = [] if badges[:added].nil?
|
31
39
|
badges[:added] << badge
|
@@ -39,6 +47,5 @@ class Gioco
|
|
39
47
|
badges
|
40
48
|
end
|
41
49
|
end
|
42
|
-
|
43
50
|
end
|
44
51
|
end
|
data/lib/gioco/rankings.rb
CHANGED
@@ -1,36 +1,40 @@
|
|
1
1
|
class Gioco
|
2
2
|
class Ranking < Core
|
3
3
|
|
4
|
-
|
4
|
+
def self.with_kind_and_points
|
5
5
|
ranking = []
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
.group("type_id, #{RESOURCE_NAME}_id")
|
15
|
-
.order("type_points DESC")
|
6
|
+
Kind.all.each do |t|
|
7
|
+
data = RESOURCE_NAME.capitalize.constantize
|
8
|
+
.select("#{RESOURCE_NAME.capitalize.constantize.table_name}.*,
|
9
|
+
points.kind_id, SUM(points.value) AS kind_points")
|
10
|
+
.where("points.kind_id = #{t.id}")
|
11
|
+
.joins(:points)
|
12
|
+
.group("kind_id, #{RESOURCE_NAME}_id")
|
13
|
+
.order("kind_points DESC")
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
elsif POINTS && !TYPES
|
22
|
-
ranking = RESOURCE_NAME.capitalize.constantize.order("points DESC")
|
15
|
+
ranking << { :kind => t, :ranking => data }
|
16
|
+
end
|
17
|
+
ranking
|
18
|
+
end
|
23
19
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
20
|
+
def self.without_kind_and_points
|
21
|
+
ranking = RESOURCE_NAME.capitalize.constantize
|
22
|
+
.select("#{RESOURCE_NAME.capitalize.constantize.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
|
+
end
|
31
28
|
|
29
|
+
def self.generate
|
30
|
+
ranking = []
|
31
|
+
if POINTS && KINDS
|
32
|
+
ranking = self.with_kind_and_points
|
33
|
+
elsif POINTS && !KINDS
|
34
|
+
ranking = RESOURCE_NAME.capitalize.constantize.order("points DESC")
|
35
|
+
elsif !POINTS && !KINDS
|
36
|
+
ranking = without_kind_and_points
|
32
37
|
end
|
33
|
-
|
34
38
|
ranking
|
35
39
|
end
|
36
40
|
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: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-09-13 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Gioco is a easy to implement gamification gem based on plug and play
|
15
15
|
concept. Doesn't matter if you already have a full and functional database, Gioco
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
version: '0'
|
52
52
|
requirements: []
|
53
53
|
rubyforge_project:
|
54
|
-
rubygems_version: 1.8.
|
54
|
+
rubygems_version: 1.8.15
|
55
55
|
signing_key:
|
56
56
|
specification_version: 3
|
57
57
|
summary: A gamification gem to Ruby on Rails applications.
|