foot_stats_simulator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +40 -0
  4. data/app/assets/javascripts/foot_stats_simulator/application.js +15 -0
  5. data/app/assets/stylesheets/foot_stats_simulator/application.css +13 -0
  6. data/app/controllers/foot_stats_simulator/api_controller.rb +37 -0
  7. data/app/controllers/foot_stats_simulator/application_controller.rb +8 -0
  8. data/app/helpers/foot_stats_simulator/application_helper.rb +4 -0
  9. data/app/models/simulator_championship.rb +22 -0
  10. data/app/models/simulator_match.rb +56 -0
  11. data/app/models/simulator_player.rb +10 -0
  12. data/app/models/simulator_team.rb +10 -0
  13. data/app/models/simulator_timeline.rb +111 -0
  14. data/app/views/layouts/foot_stats_simulator/application.html.erb +14 -0
  15. data/config/routes.rb +14 -0
  16. data/lib/foot_stats_simulator.rb +4 -0
  17. data/lib/foot_stats_simulator/engine.rb +9 -0
  18. data/lib/foot_stats_simulator/migrate.rb +52 -0
  19. data/lib/foot_stats_simulator/tasks/foot_stats_simulator.rake +30 -0
  20. data/lib/foot_stats_simulator/version.rb +3 -0
  21. data/test/dummy/README.rdoc +261 -0
  22. data/test/dummy/Rakefile +7 -0
  23. data/test/dummy/app/assets/javascripts/application.js +15 -0
  24. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  25. data/test/dummy/app/controllers/application_controller.rb +3 -0
  26. data/test/dummy/app/helpers/application_helper.rb +2 -0
  27. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  28. data/test/dummy/config.ru +4 -0
  29. data/test/dummy/config/application.rb +59 -0
  30. data/test/dummy/config/boot.rb +10 -0
  31. data/test/dummy/config/database.yml +25 -0
  32. data/test/dummy/config/environment.rb +5 -0
  33. data/test/dummy/config/environments/development.rb +37 -0
  34. data/test/dummy/config/environments/production.rb +67 -0
  35. data/test/dummy/config/environments/test.rb +37 -0
  36. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  37. data/test/dummy/config/initializers/inflections.rb +15 -0
  38. data/test/dummy/config/initializers/mime_types.rb +5 -0
  39. data/test/dummy/config/initializers/secret_token.rb +7 -0
  40. data/test/dummy/config/initializers/session_store.rb +8 -0
  41. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  42. data/test/dummy/config/locales/en.yml +5 -0
  43. data/test/dummy/config/routes.rb +4 -0
  44. data/test/dummy/db/test.sqlite3 +0 -0
  45. data/test/dummy/log/test.log +3 -0
  46. data/test/dummy/public/404.html +26 -0
  47. data/test/dummy/public/422.html +26 -0
  48. data/test/dummy/public/500.html +25 -0
  49. data/test/dummy/public/favicon.ico +0 -0
  50. data/test/dummy/script/rails +6 -0
  51. data/test/foot_stats_simulator_test.rb +7 -0
  52. data/test/integration/navigation_test.rb +10 -0
  53. data/test/test_helper.rb +15 -0
  54. metadata +179 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
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.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = FootStatsSimulator
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'FootStatsSimulator'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
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 vendor/assets/stylesheets of plugins, if any, 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 top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,37 @@
1
+ module FootStatsSimulator
2
+ class ApiController < ::FootStatsSimulator::ApplicationController
3
+ skip_before_filter :verify_authenticity_token
4
+
5
+ def championship
6
+ render_foot_stats SimulatorChampionship.to_foot_stats
7
+ end
8
+
9
+ def championship_classification
10
+ render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
11
+ end
12
+
13
+ def championship_match
14
+ render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
15
+ end
16
+
17
+ def championship_round_match
18
+ render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
19
+ end
20
+
21
+ def championship_teams
22
+ render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
23
+ end
24
+
25
+ def live_match
26
+ render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
27
+ end
28
+
29
+ def match_narration
30
+ render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
31
+ end
32
+
33
+ def team_players
34
+ render text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ module FootStatsSimulator
2
+ class ApplicationController < ActionController::Base
3
+ protected
4
+ def render_foot_stats(data)
5
+ render text: "<string xmlns=\"http://tempuri.org/\">#{data.to_json}</string>"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module FootStatsSimulator
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,22 @@
1
+ class SimulatorChampionship < ActiveRecord::Base
2
+ def to_foot_stats
3
+ {
4
+ '@Id' => self.source_id.to_s,
5
+ '@Nome' => self.name,
6
+ '@TemClassificacao' => (self.has_classification ? 'True' : 'False'),
7
+ '@RodadaATual' => self.current_round.to_s,
8
+ '@Rodadas' => self.total_rounds.to_s
9
+ }
10
+ end
11
+
12
+ def self.to_foot_stats
13
+ all.map &:to_foot_stats
14
+ end
15
+
16
+ def self.dump!
17
+ FootStats::Championship.all.each do |foot_stats_championship|
18
+ championship = self.find_or_create_by_source_id foot_stats_championship.source_id
19
+ championship.update_attributes foot_stats_championship.attributes
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,56 @@
1
+ class SimulatorMatch < ActiveRecord::Base
2
+ STATUS_MAP = {
3
+ not_started: 'Partida não iniciada',
4
+ first_period: 'Primeiro tempo',
5
+ first_interval: 'Intervalo 1',
6
+ second_period: 'Segundo tempo',
7
+ second_interval: 'Intervalo 2',
8
+ first_prorrogation: 'Prorrogação 1',
9
+ third_interval: 'Intervalo 3',
10
+ second_prorrogation: 'Prorrogação 2',
11
+ penalties: 'Disputa de Pênaltis',
12
+ finished: 'Partida encerrada',
13
+ stopped_match: 'Partida interrompida',
14
+ canceled: 'Partida cancelada'
15
+ }
16
+
17
+ STATUS_SEQUENCE = {
18
+ not_started: 0,
19
+ first_period: 1,
20
+ first_interval: 2,
21
+ second_period: 3,
22
+ second_interval: 4,
23
+ first_prorrogation: 5,
24
+ third_interval: 6,
25
+ second_prorrogation: 7,
26
+ penalties: 8,
27
+ finished: 9,
28
+ stopped_match: 10,
29
+ canceled: 11
30
+ }
31
+ end
32
+
33
+ # t.integer :source_id
34
+ # t.string :date
35
+ # t.string :status
36
+ # t.string :referee
37
+ # t.string :stadium
38
+ # t.string :city
39
+ # t.string :state
40
+ # t.string :country
41
+ # t.string :has_statistic
42
+ # t.string :has_narration
43
+ # t.string :round
44
+ # t.string :phase
45
+ # t.string :cup
46
+ # t.string :group
47
+ # t.string :game_number
48
+ # t.boolean :live
49
+ # t.string :home_team
50
+ # t.string :home_team_name
51
+ # t.string :home_score
52
+ # t.string :home_penalties_score
53
+ # t.string :visitor_team
54
+ # t.string :visitor_team_name
55
+ # t.string :visitor_score
56
+ # t.string :visitor_penalties_score
@@ -0,0 +1,10 @@
1
+ class SimulatorPlayer < ActiveRecord::Base
2
+ def self.dump!
3
+ SimulatorTeam.all.each do |team|
4
+ FootStats::Player.all(team: team.source_id).each do |foot_stats_player|
5
+ player = self.find_or_create_by_source_id foot_stats_player.source_id
6
+ player.update_attributes foot_stats_player.attributes
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class SimulatorTeam < ActiveRecord::Base
2
+ def self.dump!
3
+ SimulatorChampionship.all.each do |championship|
4
+ FootStats::Team.all(championship: championship.source_id).each do |foot_stats_team|
5
+ team = self.find_or_create_by_source_id foot_stats_team.source_id
6
+ team.update_attributes foot_stats_team.attributes
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,111 @@
1
+ class SimulatorTimeline < ActiveRecord::Base
2
+ belongs_to :match
3
+
4
+ attr_accessor :states, :current_period, :current_minute
5
+ attr_accessor :narration, :home_goals, :home_cards, :home_players, :visitor_goals, :visitor_cards, :visitor_players
6
+
7
+ def period(period)
8
+ if block_given?
9
+ yield Period.new(self, period)
10
+ else
11
+ Period.new(self, period_time)
12
+ end
13
+ end
14
+
15
+ def current_period_time
16
+ SimulatorMatch::STATUS_SEQUENCE[current_period] || current_period.to_i
17
+ end
18
+
19
+ def transitions
20
+ states = States.new self
21
+ yield(states)
22
+ states.set_current
23
+ end
24
+
25
+ def setup
26
+ @narration = []
27
+ @home_goals = []
28
+ @home_cards = []
29
+ @home_players = []
30
+ @visitor_goals = []
31
+ @visitor_cards = []
32
+ @visitor_players = []
33
+ yield(self)
34
+ end
35
+
36
+ class States
37
+ def initialize(timeline)
38
+ @timeline = timeline
39
+ @timeline.states = []
40
+ end
41
+
42
+ def set_current
43
+ delta = (Time.now - @timeline.match.scheduled_start).to_i
44
+ period = @timeline.states.find{|minute, status| delta > minute }[1]
45
+
46
+ @timeline.current_period = period
47
+
48
+ @timeline.current_minute if period.to_s.match(/(period|prorrogation)/)
49
+ started_second = @timeline.states.find{ |minute, status| status == period }[0]
50
+ ((Time.now - @timeline.match.scheduled_start) + started_second).from_now
51
+ else
52
+ 0
53
+ end
54
+ end
55
+
56
+ def add_transition(status, time)
57
+ @timeline.states << [time, status]
58
+ @timeline.sort!
59
+ end
60
+
61
+ def method_missing(method_name, *args, &block)
62
+ if SimulatorMatch::STATUS_SEQUENCE[method_name]
63
+ add_transition(method_name, args.first)
64
+ else
65
+ super(method_name, *args, &block)
66
+ end
67
+ end
68
+ end
69
+
70
+ class Period
71
+ def initialize(timeline, period)
72
+ @timeline, @period = timeline, period
73
+ end
74
+
75
+ def period_time
76
+ SimulatorMatch::STATUS_SEQUENCE[@period] || @period.to_i
77
+ end
78
+
79
+ def at(minute)
80
+ if block_given?
81
+ yield Minute.new(@timeline, period_time, @minute)
82
+ else
83
+ Minute.new(@timeline, period_time, @minute)
84
+ end
85
+ end
86
+ end
87
+
88
+ class Minute
89
+ def initialize(timeline, minute)
90
+ @timeline, @minute = timeline, minute
91
+ end
92
+
93
+ def event(type, *args)
94
+ [@timeline, @minute, type, args]
95
+ end
96
+
97
+ def add_home_goal(player_source_id, type = 'Favor', reference = nil)
98
+ @timeline.home_goals << event(:goal, player_source_id, type, reference)
99
+ end
100
+
101
+ def add_visitor_goal(player_source_id, type = 'Favor', reference = nil)
102
+ @timeline.visitor_goals << event(:goal, player_source_id, type, reference)
103
+ end
104
+
105
+ def remove_goal(reference)
106
+ @timeline.home_goals.delete_all do |period, minute, type, *args|
107
+ type == goal && args[2] == reference
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>FootStatsSimulator</title>
5
+ <%= stylesheet_link_tag "foot_stats_simulator/application", :media => "all" %>
6
+ <%= javascript_include_tag "foot_stats_simulator/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,14 @@
1
+ FootStatsSimulator::Engine.routes.draw do
2
+
3
+
4
+
5
+ match 'ListaCampeonatos' => 'api#championship', via: :post, as: 'championship'
6
+ match 'ListaClassificacao' => 'api#championship_classification', via: :post, as: 'championship_classification'
7
+ match 'ListaPartidas' => 'api#championship_match', via: :post, as: 'championship_match'
8
+ match 'ListaPartidasRodada' => 'api#championship_round_match', via: :post, as: 'championship_round_match'
9
+ match 'ListaEquipesCampeonato' => 'api#championship_teams', via: :post, as: 'championship_teams'
10
+ match 'AoVivo' => 'api#live_match', via: :post, as: 'live_match'
11
+ match 'Narracao' => 'api#match_narration', via: :post, as: 'match_narration'
12
+ match 'ListaJogadoresEquipe' => 'api#team_players', via: :post, as: 'team_players'
13
+
14
+ end
@@ -0,0 +1,4 @@
1
+ require "foot_stats_simulator/engine"
2
+
3
+ module FootStatsSimulator
4
+ end
@@ -0,0 +1,9 @@
1
+ module FootStatsSimulator
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace FootStatsSimulator
4
+
5
+ rake_tasks do
6
+ load 'foot_stats_simulator/tasks/foot_stats_simulator.rake'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,52 @@
1
+ ActiveRecord::Migration.create_table :simulator_championship do |t|
2
+ t.integer :source_id
3
+ t.string :name
4
+ t.boolean :has_classification
5
+ t.integer :current_round
6
+ t.integer :total_rounds
7
+ end
8
+
9
+ ActiveRecord::Migration.create_table :simulator_team do |t|
10
+ t.integer :source_id
11
+ t.string :full_name
12
+ t.string :city
13
+ t.string :country
14
+ end
15
+
16
+ ActiveRecord::Migration.create_table :simulator_player do |t|
17
+ t.integer :source_id
18
+ t.string :full_name
19
+ t.string :nickname
20
+ end
21
+
22
+ ActiveRecord::Migration.create_table :simulator_match do |t|
23
+ t.integer :source_id
24
+ t.string :date
25
+ t.string :status
26
+ t.string :referee
27
+ t.string :stadium
28
+ t.string :city
29
+ t.string :state
30
+ t.string :country
31
+ t.string :has_statistic
32
+ t.string :has_narration
33
+ t.string :round
34
+ t.string :phase
35
+ t.string :cup
36
+ t.string :group
37
+ t.string :game_number
38
+ t.boolean :live
39
+ t.string :home_team
40
+ t.string :home_team_name
41
+ t.string :home_score
42
+ t.string :home_penalties_score
43
+ t.string :visitor_team
44
+ t.string :visitor_team_name
45
+ t.string :visitor_score
46
+ t.string :visitor_penalties_score
47
+ end
48
+
49
+ ActiveRecord::Migration.create_table :simulator_timeline do |t|
50
+ t.integer :simulator_match_id
51
+ t.string :timeline_name
52
+ end