toy_train 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +51 -0
  4. data/Rakefile +8 -0
  5. data/app/assets/config/toy_train_manifest.js +1 -0
  6. data/app/assets/stylesheets/toy_train/application.css +15 -0
  7. data/app/controllers/toy_train/application_controller.rb +4 -0
  8. data/app/helpers/toy_train/application_helper.rb +4 -0
  9. data/app/jobs/toy_train/application_job.rb +4 -0
  10. data/app/jobs/toy_train/update_rankings_job.rb +9 -0
  11. data/app/jobs/toy_train/update_rankings_job.rb~ +20 -0
  12. data/app/mailers/toy_train/application_mailer.rb +6 -0
  13. data/app/models/concerns/toy_train/ranking_updatable.rb +12 -0
  14. data/app/models/concerns/toy_train/ranking_updatable.rb~ +0 -0
  15. data/app/models/toy_train/application_record.rb +5 -0
  16. data/app/models/toy_train/game.rb +5 -0
  17. data/app/models/toy_train/game.rb~ +0 -0
  18. data/app/models/toy_train/game_ranking_position.rb +11 -0
  19. data/app/models/toy_train/game_ranking_position.rb~ +11 -0
  20. data/app/models/toy_train/game_record.rb +32 -0
  21. data/app/models/toy_train/game_record.rb~ +4 -0
  22. data/app/models/toy_train/overall_ranking_position.rb +8 -0
  23. data/app/models/toy_train/overall_ranking_position.rb~ +5 -0
  24. data/app/views/layouts/toy_train/application.html.erb +15 -0
  25. data/config/routes.rb +2 -0
  26. data/db/migrate/20220422150721_create_toy_train_game_records.rb +16 -0
  27. data/db/migrate/20220422150721_create_toy_train_game_records.rb~ +16 -0
  28. data/db/migrate/20220423074857_create_toy_train_overall_ranking_positions.rb +11 -0
  29. data/db/migrate/20220423074857_create_toy_train_overall_ranking_positions.rb~ +11 -0
  30. data/db/migrate/20220423074938_create_toy_train_game_ranking_positions.rb +12 -0
  31. data/db/migrate/20220423074938_create_toy_train_game_ranking_positions.rb~ +12 -0
  32. data/lib/tasks/toy_train_tasks.rake +4 -0
  33. data/lib/toy_train/engine.rb +5 -0
  34. data/lib/toy_train/version.rb +3 -0
  35. data/lib/toy_train/version.rb~ +3 -0
  36. data/lib/toy_train.rb +14 -0
  37. data/lib/toy_train.rb~ +6 -0
  38. metadata +135 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1a7b3a85f4a05519810a03f0e39ad7f4cfadeb480de88f7394f2000bda81869c
4
+ data.tar.gz: 9bb69f9ba0b0e2ed5b7dd8d86873be1476df6ce8a4425208879f6132122a0a36
5
+ SHA512:
6
+ metadata.gz: '016998f69513b255f3fdf6a622d02a599305e239557bebf1fb86b4a1d37213c5d8149d5c8d6fbc60d7fa307446f35ca92d8150f98c96b07fb4f7cff1efc3e427'
7
+ data.tar.gz: b7994c423a7d1985d8570af3480d56f94902bf3ea0ed55ccac5c01cdfa466778c3095bfaaa8a3be79ca1ee95fd0660273231df6eabca6b9d9c107e3137b65e54
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Julian Rubisch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # ToyTrain
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "toy_train"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install toy_train
22
+ ```
23
+
24
+ ## Installation
25
+
26
+ ### Models
27
+
28
+ #### Install Migrations
29
+
30
+ ```sh
31
+ bin/rails toy_train:install:migrations
32
+ ```
33
+
34
+ ## Configuration
35
+
36
+ In an initializer, do:
37
+
38
+ ```rb
39
+ ToyTrain.configure do |config|
40
+ config.games = [] # ActiveHash data
41
+ config.user_class = "Participant" # default "User"
42
+ end
43
+ ```
44
+
45
+
46
+
47
+ ## Contributing
48
+ Contribution directions go here.
49
+
50
+ ## License
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/toy_train .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module ToyTrain
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ToyTrain
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ToyTrain
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,9 @@
1
+ module ToyTrain
2
+ class UpdateRankingsJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ def perform(*args)
6
+ # Do something later
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module ToyTrain
2
+ class UpdateRankingsJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ def perform(game_id:)
6
+ @game_id = game_id
7
+ end
8
+
9
+ def game_record_scope(game_id = @game_id)
10
+ GameRecord.where(game_id: game_id)
11
+ end
12
+
13
+ def user_ranking_for_game(game_id = @game_id)
14
+ @user_ranking ||= game_record_scope(game_id)
15
+ .select("max(points) as max_points_per_user, max(max_points) as max_max_points, (100 * max(points) / max(max_points)) as score, user_id")
16
+ .group("user_id")
17
+ .order("score" => :desc)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ module ToyTrain
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ module ToyTrain
2
+ module RankingUpdatable
3
+ extend ActiveSupport::Concern
4
+
5
+ def update_ranking(index:, score: 0)
6
+ touch if persisted?
7
+ self.position = index
8
+ self.score = score
9
+ save(validate: false)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module ToyTrain
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module ToyTrain
2
+ class Game < ActiveHash::Base
3
+ self.data = ToyTrain.games
4
+ end
5
+ end
File without changes
@@ -0,0 +1,11 @@
1
+ module ToyTrain
2
+ class GameRankingPosition < ApplicationRecord
3
+ belongs_to :rankable, polymorphic: true
4
+
5
+ extend ActiveHash::Associations::ActiveRecordExtensions
6
+ belongs_to :game, class_name: "ToyTrain::Game"
7
+
8
+ validates :position, uniqueness: {scope: :game_id}
9
+ validates :game_id, uniqueness: {scope: [:rankable_id, :rankable_type]}
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module ToyTrain
2
+ class GameRankingPosition < ApplicationRecord
3
+ belongs_to :rankable, polymorphic: true
4
+
5
+ extend ActiveHash::Associations::ActiveRecordExtensions
6
+ belongs_to :game
7
+
8
+ validates :position, uniqueness: {scope: :game_id}
9
+ validates :team_id, uniqueness: {scope: :game_id}
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ module ToyTrain
2
+ class GameRecord < ApplicationRecord
3
+ before_save :calculate_points
4
+ after_save :calculate_ranking
5
+ after_destroy_commit :calculate_ranking
6
+
7
+ validate do |record|
8
+ errors.add(:base, "Only three attempts per game are allowed") if ToyTrain::GameRecord.where(user: record.user, game_id: record.game_id).count >= ToyTrain.max_attempts
9
+ end
10
+
11
+ belongs_to :user, class_name: ToyTrain.user_class
12
+
13
+ extend ActiveHash::Associations::ActiveRecordExtensions
14
+ belongs_to :game, class_name: "ToyTrain::Game"
15
+
16
+ def calculate_points
17
+ self.points = (units.to_f * 100 / max_points).round(2)
18
+ end
19
+
20
+ def calculate_ranking
21
+ UpdateRankingsJob.perform_later(game_id: game_id)
22
+ end
23
+
24
+ def data=(value)
25
+ super(JSON.parse(value))
26
+ end
27
+
28
+ def to_json
29
+ serializable_hash(only: %i[points max_points time max_time data]).map { |k, v| [k.camelize(:lower), v] }.to_h.to_json
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+ module ToyTrain
2
+ class GameRecord < ApplicationRecord
3
+ end
4
+ end
@@ -0,0 +1,8 @@
1
+ module ToyTrain
2
+ class OverallRankingPosition < ApplicationRecord
3
+ belongs_to :rankable, polymorphic: true
4
+
5
+ validates :position, uniqueness: true
6
+ validates :rankable_id, uniqueness: {scope: :rankable_type}
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module ToyTrain
2
+ class OverallRankingPosition < ApplicationRecord
3
+ belongs_to :rankable, polymorphic: true
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Toy train</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "toy_train/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ ToyTrain::Engine.routes.draw do
2
+ end
@@ -0,0 +1,16 @@
1
+ class CreateToyTrainGameRecords < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :toy_train_game_records do |t|
4
+ t.integer :max_points, default: 100
5
+ t.jsonb :data
6
+ t.float :max_time, default: 0.0
7
+ t.float :points
8
+ t.float :time, default: 0.0
9
+ t.integer :units
10
+ t.integer :game_id, null: false
11
+ t.bigint :user_id, null: false
12
+
13
+ t.timestamps
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ class CreateToyTrainGameRecords < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :toy_train_game_records do |t|
4
+ t.integer :max_points
5
+ t.jsonb :data
6
+ t.float :max_time
7
+ t.float :points
8
+ t.float :time
9
+ t.integer :units
10
+ t.integer :game_id
11
+ t.bigint :user_id
12
+
13
+ t.timestamps
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ class CreateToyTrainOverallRankingPositions < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :toy_train_overall_ranking_positions do |t|
4
+ t.references :rankable, polymorphic: true, null: false
5
+ t.integer :position, null: false
6
+ t.float :score, null: false
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class CreateToyTrainOverallRankingPositions < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :toy_train_overall_ranking_positions do |t|
4
+ t.references :rankable, polymorphic: true, null: false
5
+ t.integer :position
6
+ t.float :score
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ class CreateToyTrainGameRankingPositions < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :toy_train_game_ranking_positions do |t|
4
+ t.references :rankable, polymorphic: true, null: false
5
+ t.integer :position, null: false
6
+ t.float :score, null: false
7
+ t.integer :game_id, null: false
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ class CreateToyTrainGameRankingPositions < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :toy_train_game_ranking_positions do |t|
4
+ t.references :rankable, polymorphic: true, null: false
5
+ t.integer :position
6
+ t.float :score
7
+ t.integer :game_id
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :toy_train do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,5 @@
1
+ module ToyTrain
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ToyTrain
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module ToyTrain
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ module ToyTrain
2
+ VERSION = "0.1.0"
3
+ end
data/lib/toy_train.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "active_hash"
2
+
3
+ require "toy_train/version"
4
+ require "toy_train/engine"
5
+
6
+ module ToyTrain
7
+ mattr_accessor :user_class, default: "User"
8
+ mattr_accessor :games, default: []
9
+ mattr_accessor :max_attempts, default: 1
10
+
11
+ def self.configure
12
+ yield self
13
+ end
14
+ end
data/lib/toy_train.rb~ ADDED
@@ -0,0 +1,6 @@
1
+ require "toy_train/version"
2
+ require "toy_train/engine"
3
+
4
+ module ToyTrain
5
+ # Your code goes here...
6
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toy_train
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Julian Rubisch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_hash
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: standard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Description of ToyTrain.
70
+ email:
71
+ - julian@julianrubisch.at
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - app/assets/config/toy_train_manifest.js
80
+ - app/assets/stylesheets/toy_train/application.css
81
+ - app/controllers/toy_train/application_controller.rb
82
+ - app/helpers/toy_train/application_helper.rb
83
+ - app/jobs/toy_train/application_job.rb
84
+ - app/jobs/toy_train/update_rankings_job.rb
85
+ - app/jobs/toy_train/update_rankings_job.rb~
86
+ - app/mailers/toy_train/application_mailer.rb
87
+ - app/models/concerns/toy_train/ranking_updatable.rb
88
+ - app/models/concerns/toy_train/ranking_updatable.rb~
89
+ - app/models/toy_train/application_record.rb
90
+ - app/models/toy_train/game.rb
91
+ - app/models/toy_train/game.rb~
92
+ - app/models/toy_train/game_ranking_position.rb
93
+ - app/models/toy_train/game_ranking_position.rb~
94
+ - app/models/toy_train/game_record.rb
95
+ - app/models/toy_train/game_record.rb~
96
+ - app/models/toy_train/overall_ranking_position.rb
97
+ - app/models/toy_train/overall_ranking_position.rb~
98
+ - app/views/layouts/toy_train/application.html.erb
99
+ - config/routes.rb
100
+ - db/migrate/20220422150721_create_toy_train_game_records.rb
101
+ - db/migrate/20220422150721_create_toy_train_game_records.rb~
102
+ - db/migrate/20220423074857_create_toy_train_overall_ranking_positions.rb
103
+ - db/migrate/20220423074857_create_toy_train_overall_ranking_positions.rb~
104
+ - db/migrate/20220423074938_create_toy_train_game_ranking_positions.rb
105
+ - db/migrate/20220423074938_create_toy_train_game_ranking_positions.rb~
106
+ - lib/tasks/toy_train_tasks.rake
107
+ - lib/toy_train.rb
108
+ - lib/toy_train.rb~
109
+ - lib/toy_train/engine.rb
110
+ - lib/toy_train/version.rb
111
+ - lib/toy_train/version.rb~
112
+ homepage:
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubygems_version: 3.1.6
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Summary of ToyTrain.
135
+ test_files: []