gioco 0.3.6 → 1.0.0
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 +32 -0
- data/lib/generators/gioco/migrations_generator.rb +18 -0
- data/lib/generators/gioco/model_generator.rb +63 -0
- data/lib/generators/gioco/rakes_generator.rb +101 -0
- data/lib/generators/gioco/setup_generator.rb +22 -169
- data/lib/generators/templates/badge.rb +32 -0
- data/lib/generators/templates/gioco.rb +1 -1
- data/lib/generators/templates/resource.rb +41 -0
- data/lib/gioco.rb +1 -3
- data/lib/gioco/core.rb +16 -8
- data/lib/gioco/rankings.rb +1 -1
- metadata +7 -3
- data/lib/gioco/badges.rb +0 -36
- data/lib/gioco/resources.rb +0 -15
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module GeneratorInstructions
|
3
|
+
def instructions
|
4
|
+
puts <<-EOS
|
5
|
+
|
6
|
+
=======================================================
|
7
|
+
|
8
|
+
Gioco successfully installed.
|
9
|
+
|
10
|
+
Now you are able to add Badges using:
|
11
|
+
rake gioco:add_badge[BADGE_NAME#{",POINTS" if options[:points]}#{",TYPE_NAME" if options[:types]},DEFAULT]
|
12
|
+
|
13
|
+
To remove Badges using:
|
14
|
+
rake gioco:remove_badge[BADGE_NAME#{",TYPE_NAME" if options[:types]}]
|
15
|
+
|
16
|
+
#{
|
17
|
+
if options[:types]
|
18
|
+
"And to remove Types using:
|
19
|
+
rake gioco:remove_type[TYPE_NAME]"
|
20
|
+
end
|
21
|
+
}
|
22
|
+
|
23
|
+
For usage and more infomation go to the documentation:
|
24
|
+
http://joaomdmoura.github.com/gioco/
|
25
|
+
|
26
|
+
By João Moura (a.k.a joaomdmoura)
|
27
|
+
|
28
|
+
=======================================================
|
29
|
+
|
30
|
+
EOS
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module MigrationsGenerator
|
2
|
+
def migrating
|
3
|
+
puts <<-EOS
|
4
|
+
|
5
|
+
=======================================
|
6
|
+
> Running rake db:migrate
|
7
|
+
=======================================
|
8
|
+
|
9
|
+
EOS
|
10
|
+
rake("db:migrate")
|
11
|
+
end
|
12
|
+
|
13
|
+
def configuring_seed
|
14
|
+
empty_directory "db/gioco"
|
15
|
+
create_file "db/gioco/db.rb"
|
16
|
+
append_file 'db/seeds.rb', 'require "#{Rails.root}/db/gioco/db.rb"'
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ModelGenerator
|
2
|
+
def generate_models
|
3
|
+
generate("model", "level badge_id:integer #{@model_name}_id:integer")
|
4
|
+
if options[:types]
|
5
|
+
generate("model", "point user_id:integer type_id:integer value:integer")
|
6
|
+
generate("model", "type name:string")
|
7
|
+
generate("model", "badge name:string type_id:integer #{(options[:points]) ? "points:integer" : ""} default:boolean")
|
8
|
+
else
|
9
|
+
generate("migration", "add_points_to_#{@model_name.pluralize} points:integer") if options[:points]
|
10
|
+
generate("model", "badge name:string #{(options[:points]) ? "points:integer" : ""} default:boolean")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def creating_templates
|
15
|
+
@points = (options[:points] ) ? true : false
|
16
|
+
@types = (options[:types] ) ? true : false
|
17
|
+
template "gioco.rb", "config/initializers/gioco.rb"
|
18
|
+
end
|
19
|
+
|
20
|
+
def adding_methods
|
21
|
+
resource = File.read find_in_source_paths("resource.rb")
|
22
|
+
badge = File.read find_in_source_paths("badge.rb")
|
23
|
+
inject_into_class "app/models/#{@model_name}.rb", @model_name.capitalize, "\n#{resource}\n"
|
24
|
+
inject_into_class "app/models/badge.rb", "Badge", "\n#{badge}\n"
|
25
|
+
end
|
26
|
+
|
27
|
+
def setup_relations
|
28
|
+
add_relationship("badge", "levels", "has_many", false, "destroy")
|
29
|
+
add_relationship("badge", @model_name.pluralize, "has_many", "levels")
|
30
|
+
|
31
|
+
add_relationship(@model_name, "levels", "has_many")
|
32
|
+
add_relationship(@model_name, "badges", "has_many", "levels")
|
33
|
+
|
34
|
+
add_relationship("level", @model_name, "belongs_to")
|
35
|
+
add_relationship("level", "badge", "belongs_to")
|
36
|
+
|
37
|
+
if options[:types]
|
38
|
+
add_relationship(@model_name, "points", "has_many")
|
39
|
+
add_relationship("type", "points", "has_many")
|
40
|
+
add_relationship("type", "badges", "has_many")
|
41
|
+
add_relationship("badge", "type", "belongs_to")
|
42
|
+
add_relationship("point", @model_name, "belongs_to")
|
43
|
+
add_relationship("point", "type", "belongs_to")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def add_validations
|
48
|
+
add_validation("badge", "name", [["presence", "true"]])
|
49
|
+
add_validation("type", "name", [["uniqueness", "true"], ["presence", "true"]]) if options[:types]
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def add_relationship (model, related, relation, through = false, dependent = false)
|
55
|
+
inject_into_class "app/models/#{model}.rb", model.capitalize, "#{relation} :#{related} #{(through) ? ", :through => :#{through}" : ""} #{(dependent) ? ", :dependent => :#{dependent}" : ""}\n"
|
56
|
+
end
|
57
|
+
|
58
|
+
def add_validation (model, field, validations = [])
|
59
|
+
validations.each do |validation|
|
60
|
+
inject_into_class "app/models/#{model}.rb", model.capitalize, "validates :#{field}, :#{validation[0]} => #{validation[1]}\n"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module RakesGenerator
|
2
|
+
def create_rakes
|
3
|
+
rakefile("gioco.rake") do
|
4
|
+
<<-EOS
|
5
|
+
# -*- encoding: utf-8 -*-
|
6
|
+
namespace :gioco do
|
7
|
+
|
8
|
+
desc "Used to add a new badge at Gioco scheme"
|
9
|
+
|
10
|
+
task :add_badge, [:name, #{":points, " if options[:points]}#{":type, " if options[:types]}:default] => :environment do |t, args|
|
11
|
+
arg_default = ( args.default ) ? eval(args.default) : false
|
12
|
+
|
13
|
+
|
14
|
+
if !args.name #{"|| !args.points" if options[:points]}#{" || !args.type" if options[:types]}
|
15
|
+
raise "There are missing some arguments"
|
16
|
+
else
|
17
|
+
badge_string = "#{options[:types] ? 'type = Type.find_or_create_by_name(\'#{args.type}\')\n' : ''}"
|
18
|
+
|
19
|
+
badge_string = badge_string + "badge = Badge.create({
|
20
|
+
:name => \'\#\{args.name\}\',
|
21
|
+
#{":points => \'\#\{args.points\}\'," if options[:points]}
|
22
|
+
#{":type_id => type.id," if options[:types]}
|
23
|
+
:default => \'\#\{arg_default\}\'
|
24
|
+
})\n"
|
25
|
+
|
26
|
+
if arg_default
|
27
|
+
badge_string = badge_string + 'resources = #{@model_name.capitalize}.find(:all)\n'
|
28
|
+
badge_string = badge_string + "resources.each do |r|
|
29
|
+
#{
|
30
|
+
if options[:points] && options[:types]
|
31
|
+
"r.points << Point.create({ :type_id => type.id, :value => \'\#\{args.points\}\'})"
|
32
|
+
elsif options[:points]
|
33
|
+
"r.points = \'\#\{args.points\}\'"
|
34
|
+
end
|
35
|
+
}
|
36
|
+
r.badges << badge
|
37
|
+
r.save!
|
38
|
+
end\n"
|
39
|
+
end
|
40
|
+
|
41
|
+
badge_string = badge_string + "puts '> Badge successfully created'"
|
42
|
+
|
43
|
+
eval badge_string
|
44
|
+
|
45
|
+
file_path = "/db/gioco/create_badge_\#\{args.name\}#{"_\#\{args.type\}" if options[:types]}.rb"
|
46
|
+
File.open("\#\{Rails.root\}\#\{file_path\}", 'w') { |f| f.write badge_string }
|
47
|
+
File.open("\#\{Rails.root\}/db/gioco/db.rb", 'a') { |f| f.write "require \\"\\#\\{Rails.root\\}\#\{file_path\}\\"\n" }
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
desc "Used to remove an old badge at Gioco scheme"
|
54
|
+
|
55
|
+
task :remove_badge, [:name#{", :type" if options[:types]}] => :environment do |t, args|
|
56
|
+
if !args.name#{" || !args.type" if options[:types]}
|
57
|
+
raise "There are missing some arguments"
|
58
|
+
else
|
59
|
+
badge_string = "#{"type = Type.find_by_name('\#\{args.type\}')" if options[:types]}
|
60
|
+
badge = Badge.where( :name => '\#\{args.name\}'#{", :type_id => type.id" if options[:types]} ).first
|
61
|
+
badge.destroy\n"
|
62
|
+
end
|
63
|
+
|
64
|
+
badge_string = badge_string + "puts '> Badge successfully removed'"
|
65
|
+
|
66
|
+
eval badge_string
|
67
|
+
|
68
|
+
file_path = "/db/gioco/remove_badge_\#\{args.name\}.rb"
|
69
|
+
File.open("\#\{Rails.root\}\#\{file_path\}", 'w') { |f| f.write badge_string }
|
70
|
+
File.open("\#\{Rails.root\}/db/gioco/db.rb", 'a') { |f| f.write "require \\"\\#\\{Rails.root\\}\#\{file_path\}\\"\n" }
|
71
|
+
end
|
72
|
+
#{
|
73
|
+
if options[:types]
|
74
|
+
'
|
75
|
+
desc "Removes a given type"
|
76
|
+
task :remove_type, [:name] => :environment do |t, args|
|
77
|
+
if !args.name
|
78
|
+
raise "There are missing some arguments"
|
79
|
+
else
|
80
|
+
type_string = "type = Type.find_by_name( \'#{args.name}\' )\n"
|
81
|
+
type_string = type_string + "if type.badges.empty?
|
82
|
+
type.destroy
|
83
|
+
else
|
84
|
+
raise \'Aborted! There are badges related with this type.\'
|
85
|
+
end\n"
|
86
|
+
end
|
87
|
+
type_string = type_string + "puts \'> Type successfully removed\'"
|
88
|
+
eval type_string
|
89
|
+
|
90
|
+
file_path = "/db/gioco/remove_type_#{args.name}.rb"
|
91
|
+
File.open("#{Rails.root}#{file_path}", "w") { |f| f.write type_string }
|
92
|
+
File.open("#{Rails.root}/db/gioco/db.rb", "a") { |f| f.write "require \\"\\#\\{Rails.root\\}#{file_path}\\"\n" }
|
93
|
+
end
|
94
|
+
'
|
95
|
+
end
|
96
|
+
}
|
97
|
+
end
|
98
|
+
EOS
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -1,181 +1,34 @@
|
|
1
|
-
|
1
|
+
require "generators/gioco/model_generator"
|
2
|
+
require "generators/gioco/rakes_generator"
|
3
|
+
require "generators/gioco/migrations_generator"
|
4
|
+
require "generators/gioco/generator_instructions"
|
5
|
+
|
6
|
+
class Gioco
|
7
|
+
class SetupGenerator < Rails::Generators::Base
|
8
|
+
include ModelGenerator
|
9
|
+
include RakesGenerator
|
10
|
+
include MigrationsGenerator
|
11
|
+
include GeneratorInstructions
|
2
12
|
|
3
|
-
class SetupGenerator < Rails::Generators::NamedBase
|
4
13
|
source_root File.expand_path("../../templates", __FILE__)
|
5
14
|
|
6
15
|
desc "Setup Gioco for some resource"
|
7
16
|
class_option :points, :type => :boolean, :default => false, :desc => "Setup gioco with points-system based"
|
8
17
|
class_option :types, :type => :boolean, :default => false, :desc => "Setup gioco with multiples types(categories) of badges."
|
9
18
|
|
10
|
-
def generate_models
|
11
|
-
generate("model", "level badge_id:integer #{file_name}_id:integer")
|
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
|
20
|
-
end
|
21
|
-
|
22
|
-
def creating_templates
|
23
|
-
@points = ( options[:points] ) ? true : false
|
24
|
-
@types = ( options[:types] ) ? true : false
|
25
|
-
template "gioco.rb", "config/initializers/gioco.rb"
|
26
|
-
end
|
27
19
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
20
|
+
def execute
|
21
|
+
@model_name = ask("What is your resource model? (eg. user)")
|
22
|
+
generate_models
|
23
|
+
creating_templates
|
24
|
+
adding_methods
|
25
|
+
add_validations
|
26
|
+
setup_relations
|
27
|
+
create_rakes
|
28
|
+
configuring_seed
|
29
|
+
migrating
|
30
|
+
instructions
|
46
31
|
end
|
47
|
-
|
48
|
-
def create_rakes
|
49
|
-
rakefile("gioco.rake") do
|
50
|
-
<<-EOS
|
51
|
-
# -*- encoding: utf-8 -*-
|
52
|
-
namespace :gioco do
|
53
32
|
|
54
|
-
desc "Used to add a new badge at Gioco scheme"
|
55
|
-
|
56
|
-
task :add_badge, [:name, #{":points, " if options[:points]}#{":type, " if options[:types]}:default] => :environment do |t, args|
|
57
|
-
args.default = ( args.default ) ? eval(args.default) : false
|
58
|
-
|
59
|
-
|
60
|
-
if !args.name #{"&& !args.points" if options[:points]}#{" && !args.type" if options[:types]}
|
61
|
-
puts "There are missing some arguments"
|
62
|
-
|
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
|
-
|
66
|
-
badge = Badge.create({
|
67
|
-
:name => args.name,
|
68
|
-
#{":points => args.points," if options[:points]}
|
69
|
-
#{":type_id => type.id," if options[:types]}
|
70
|
-
:default => args.default
|
71
|
-
})
|
72
|
-
|
73
|
-
if args.default
|
74
|
-
resources = #{file_name.capitalize}.find(:all)
|
75
|
-
resources.each do |r|
|
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
|
-
}
|
83
|
-
r.badges << badge
|
84
|
-
r.save!
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
91
|
-
|
92
|
-
desc "Used to remove an old badge at Gioco scheme"
|
93
|
-
|
94
|
-
task :remove_badge, [:name] => :environment do |t, args|
|
95
|
-
|
96
|
-
if !args.name
|
97
|
-
puts "There are missing some arguments"
|
98
|
-
|
99
|
-
else
|
100
|
-
badge = Badge.find_by_name( args.name )
|
101
|
-
badge.destroy
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
105
|
-
#{
|
106
|
-
if options[:types]
|
107
|
-
'task :remove_type, [:name] => :environment do |t, args|
|
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
33
|
end
|
123
|
-
}
|
124
|
-
end
|
125
|
-
EOS
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def migrating
|
130
|
-
puts <<-EOS
|
131
|
-
|
132
|
-
=======================================
|
133
|
-
> Now is time to run rake db:migrate
|
134
|
-
=======================================
|
135
|
-
|
136
|
-
EOS
|
137
|
-
rake("db:migrate")
|
138
|
-
end
|
139
|
-
|
140
|
-
def instructions
|
141
|
-
puts <<-EOS
|
142
|
-
|
143
|
-
=======================================================
|
144
|
-
|
145
|
-
Gioco successfully installed.
|
146
|
-
|
147
|
-
Now you are able to add Badges using:
|
148
|
-
rake gioco:add_badge[BADGE_NAME#{",POINTS" if options[:points]}#{",TYPE" if options[:types]},DEFAULT]
|
149
|
-
|
150
|
-
To remove Badges using:
|
151
|
-
rake gioco:remove_badge[BADGE_NAME]
|
152
|
-
|
153
|
-
#{
|
154
|
-
if options[:types]
|
155
|
-
"And to remove Types using:
|
156
|
-
rake gioco:remove_type[TYPE_NAME]"
|
157
|
-
end
|
158
|
-
}
|
159
|
-
|
160
|
-
For usage and more infomation go to the documentation:
|
161
|
-
http://joaomdmoura.github.com/gioco/
|
162
|
-
|
163
|
-
=======================================================
|
164
|
-
|
165
|
-
EOS
|
166
|
-
end
|
167
|
-
|
168
|
-
private
|
169
|
-
|
170
|
-
def add_relationship ( model, related, relation, through = false, dependent = false )
|
171
|
-
gsub_file "app/models/#{model}.rb", get_class_header(model), "#{get_class_header(model)}
|
172
|
-
#{relation} :#{related} #{(through) ? ", :through => :#{through}" : ""} #{(dependent) ? ", :dependent => :#{dependent}" : ""}"
|
173
|
-
end
|
174
|
-
|
175
|
-
def get_class_header ( model_name )
|
176
|
-
"class #{model_name.capitalize} < ActiveRecord::Base"
|
177
|
-
end
|
178
|
-
|
179
|
-
end
|
180
|
-
|
181
34
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
def add(resource_id)
|
2
|
+
resource = Gioco::Core.get_resource(resource_id)
|
3
|
+
|
4
|
+
if Gioco::Core::POINTS && !resource.badges.include?(self)
|
5
|
+
if Gioco::Core::TYPES
|
6
|
+
Gioco::Core.sync_resource_by_points(resource, self.points, self.type)
|
7
|
+
else
|
8
|
+
Gioco::Core.sync_resource_by_points(resource, self.points)
|
9
|
+
end
|
10
|
+
elsif !resource.badges.include?(self)
|
11
|
+
resource.badges << self
|
12
|
+
return self
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def remove(resource_id)
|
17
|
+
resource = Gioco::Core.get_resource(resource_id)
|
18
|
+
|
19
|
+
if Gioco::Core::POINTS && resource.badges.include?(self)
|
20
|
+
if Gioco::Core::TYPES
|
21
|
+
type = self.type
|
22
|
+
badges_gap = Badge.where( "points < #{self.points} AND type_id = #{type.id}" ).order('points DESC')[0]
|
23
|
+
Gioco::Core.sync_resource_by_points( resource, ( badges_gap.nil? ) ? 0 : badges_gap.points, type)
|
24
|
+
else
|
25
|
+
badges_gap = Badge.where( "points < #{self.points}" ).order('points DESC')[0]
|
26
|
+
Gioco::Core.sync_resource_by_points( resource, ( badges_gap.nil? ) ? 0 : badges_gap.points)
|
27
|
+
end
|
28
|
+
elsif resource.badges.include?(self)
|
29
|
+
resource.levels.where( :badge_id => self.id )[0].destroy
|
30
|
+
return self
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
def change_points(options)
|
2
|
+
if Gioco::Core::TYPES
|
3
|
+
type_id = options[:type]
|
4
|
+
points = options[:points]
|
5
|
+
else
|
6
|
+
points = options
|
7
|
+
end
|
8
|
+
type = (type_id) ? Type.find(type_id) : false
|
9
|
+
|
10
|
+
if Gioco::Core::TYPES
|
11
|
+
raise "Missing Type Identifier argument" if !type_id
|
12
|
+
old_pontuation = self.points.where(:type_id => type_id).sum(:value)
|
13
|
+
else
|
14
|
+
old_pontuation = self.points.to_i
|
15
|
+
end
|
16
|
+
new_pontuation = old_pontuation + points
|
17
|
+
Gioco::Core.sync_resource_by_points(self, new_pontuation, type)
|
18
|
+
end
|
19
|
+
|
20
|
+
def next_badge?(type_id = false)
|
21
|
+
type = (type_id) ? Type.find(type_id) : false
|
22
|
+
if Gioco::Core::TYPES
|
23
|
+
raise "Missing Type Identifier argument" if !type_id
|
24
|
+
old_pontuation = self.points.where(:type_id => type_id).sum(:value)
|
25
|
+
else
|
26
|
+
old_pontuation = self.points.to_i
|
27
|
+
end
|
28
|
+
next_badge = Badge.where("points > #{old_pontuation}").order("points ASC").first
|
29
|
+
last_badge_point = self.badges.last.try('points')
|
30
|
+
last_badge_point ||= 0
|
31
|
+
|
32
|
+
if next_badge
|
33
|
+
percentage = (old_pontuation - last_badge_point)*100/(next_badge.points - last_badge_point)
|
34
|
+
points = next_badge.points - old_pontuation
|
35
|
+
next_badge_info = {
|
36
|
+
:badge => next_badge,
|
37
|
+
:points => points,
|
38
|
+
:percentage => percentage
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
data/lib/gioco.rb
CHANGED
data/lib/gioco/core.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
class Gioco
|
2
2
|
class Core
|
3
3
|
def self.get_resource(rid)
|
4
4
|
RESOURCE_NAME.capitalize.constantize.find(rid)
|
@@ -6,29 +6,37 @@ module Gioco
|
|
6
6
|
|
7
7
|
def self.sync_resource_by_points(resource, points, type = false)
|
8
8
|
|
9
|
+
badges = {}
|
9
10
|
if TYPES && type
|
10
11
|
old_pontuation = resource.points.where(:type_id => type.id).sum(:value)
|
11
|
-
related_badges = Badge.where(
|
12
|
+
related_badges = Badge.where(((old_pontuation < points) ? "points <= #{points}" : "points > #{points} AND points <= #{old_pontuation}") + " AND type_id = #{type.id}")
|
12
13
|
else
|
13
14
|
old_pontuation = resource.points.to_i
|
14
|
-
related_badges = Badge.where((old_pontuation < points) ? "points <= #{points}" : "points > #{points} AND points <= #{old_pontuation}"
|
15
|
+
related_badges = Badge.where((old_pontuation < points) ? "points <= #{points}" : "points > #{points} AND points <= #{old_pontuation}")
|
15
16
|
end
|
16
17
|
|
17
|
-
new_pontuation = ( old_pontuation < points ) ? points :
|
18
|
+
new_pontuation = ( old_pontuation < points ) ? points - old_pontuation : - (old_pontuation - points)
|
18
19
|
|
19
20
|
Badge.transaction do
|
20
21
|
if TYPES && type
|
21
|
-
resource.points
|
22
|
-
elsif
|
23
|
-
resource.update_attribute( :points,
|
22
|
+
resource.points << Point.create({ :type_id => type.id, :value => new_pontuation })
|
23
|
+
elsif POINTS
|
24
|
+
resource.update_attribute( :points, points )
|
24
25
|
end
|
25
26
|
related_badges.each do |badge|
|
26
27
|
if old_pontuation < points
|
27
|
-
|
28
|
+
if !resource.badges.include?(badge)
|
29
|
+
resource.badges << badge
|
30
|
+
badges[:added] = [] if badges[:added].nil?
|
31
|
+
badges[:added] << badge
|
32
|
+
end
|
28
33
|
elsif old_pontuation > points
|
29
34
|
resource.levels.where( :badge_id => badge.id )[0].destroy
|
35
|
+
badges[:removed] = [] if badges[:removed].nil?
|
36
|
+
badges[:removed] << badge
|
30
37
|
end
|
31
38
|
end
|
39
|
+
badges
|
32
40
|
end
|
33
41
|
end
|
34
42
|
|
data/lib/gioco/rankings.rb
CHANGED
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: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -20,12 +20,16 @@ extensions: []
|
|
20
20
|
extra_rdoc_files: []
|
21
21
|
files:
|
22
22
|
- lib/gioco.rb
|
23
|
+
- lib/generators/gioco/generator_instructions.rb
|
24
|
+
- lib/generators/gioco/migrations_generator.rb
|
25
|
+
- lib/generators/gioco/model_generator.rb
|
26
|
+
- lib/generators/gioco/rakes_generator.rb
|
23
27
|
- lib/generators/gioco/setup_generator.rb
|
28
|
+
- lib/generators/templates/badge.rb
|
24
29
|
- lib/generators/templates/gioco.rb
|
25
|
-
- lib/
|
30
|
+
- lib/generators/templates/resource.rb
|
26
31
|
- lib/gioco/core.rb
|
27
32
|
- lib/gioco/rankings.rb
|
28
|
-
- lib/gioco/resources.rb
|
29
33
|
- init.rb
|
30
34
|
homepage: http://joaomdmoura.github.com/gioco/
|
31
35
|
licenses: []
|
data/lib/gioco/badges.rb
DELETED
@@ -1,36 +0,0 @@
|
|
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/resources.rb
DELETED
@@ -1,15 +0,0 @@
|
|
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
|