diff --git a/Gemfile b/Gemfile index a3473e9..a01b799 100644 --- a/Gemfile +++ b/Gemfile @@ -6,3 +6,5 @@ gem 'json' gem 'sinatra' gem 'pusher' + +gem 'puma' diff --git a/Gemfile.lock b/Gemfile.lock index fec960b..daa2978 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,6 +5,8 @@ GEM httpclient (2.6.0.1) json (1.8.2) multi_json (1.11.0) + puma (2.11.2) + rack (>= 1.1, < 2.0) pusher (0.14.4) httpclient (~> 2.5) multi_json (~> 1.0) @@ -25,5 +27,6 @@ PLATFORMS DEPENDENCIES dotenv json + puma pusher sinatra diff --git a/lib/match.rb b/lib/match.rb index 590b146..e620abf 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -1,25 +1,43 @@ class Match - attr_reader :players - - def initialize(player1, player2) - @players = { one: player1, two: player2 } + def initialize + @one, @two = Player.new('Player One'), Player.new('Player Two') end - def add_point(player) - @players[player].add_point - if @players[:one].has_beaten(@players[:two]) - @players[:one].reset_score - @players[:two].reset_score - @players[:one].add_game - elsif @players[:two].has_beaten(@players[:one]) - @players[:one].reset_score - @players[:two].reset_score - @players[:two].add_game + def add_point(colour) + players[colour].add_point + if @one.has_beaten(@two) + reset_scores + @one.add_game + elsif @two.has_beaten(@one) + reset_scores + @two.add_game + end + end + + def reset_scores + @one.reset_score + @two.reset_score + end + + def reset_games + @one.reset_games + @two.reset_games + end + + def total_games + @one.games + @two.games + end + + def players + if total_games.even? + { red: @one, blue: @two } + else + { red: @two, blue: @one } end end def scores - { one: @players[:one].attributes, - two: @players[:two].attributes } + { red: players[:red].attributes, + blue: players[:blue].attributes } end end diff --git a/lib/player.rb b/lib/player.rb index aaa5255..9adcb86 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,7 +1,8 @@ class Player - attr_reader :score, :games + attr_reader :score, :games, :name - def initialize + def initialize(name) + @name = name @score = 0 @games = 0 end @@ -26,7 +27,13 @@ class Player @score = 0 end + def reset_games + @games = 0 + end + def attributes - { score: @score, games: @games } + { name: @name, + score: @score, + games: @games } end end diff --git a/public/javascripts/main.js b/public/javascripts/main.js new file mode 100644 index 0000000..1e95971 --- /dev/null +++ b/public/javascripts/main.js @@ -0,0 +1,10 @@ +var pusher = new Pusher('57dc9c12b6e6fa97febb'); +var channel = pusher.subscribe('scores'); + +resetScores = function () { + $.ajax({method: 'PUT', url: '/reset_scores', data: ''}); +} + +$(function() { + $('#reset').on('click', resetScores); +}) diff --git a/public/stylesheets/main.css b/public/stylesheets/main.css new file mode 100644 index 0000000..195ec74 --- /dev/null +++ b/public/stylesheets/main.css @@ -0,0 +1,43 @@ +body { + background: black; + color: white; + font-family: "Helvetica Neue", "Arial", sans-serif; +} +.scores { + position: absolute; + top: 0; bottom: 2em; left: 0; right: 0; + display: flex; + flex-direction: row; +} +.scores div { position: relative; flex-grow: 1; } +.scores div .score { + position: absolute; + bottom: 2rem; + font-weight: bold; + font-size: 20rem; +} +.scores div .games { + position: absolute; + top: 2rem; + font-weight: bold; + font-size: 5rem; +} +.scores div .name { + position: absolute; + top: 3rem; + font-size: 2rem; +} +#red-score { background: #b00; } +#blue-score { background: #00b; } +#red-score .score { right: 5rem; } +#blue-score .score { left: 5rem; } +#red-score .games { left: 5rem; } +#blue-score .games { right: 5rem; } +#red-score .name { right: 5rem; } +#blue-score .name { left: 5rem; } + +.controls { + position: absolute; + bottom: 0; left: 0; right: 0; + height: 2rem; +} diff --git a/score_board.rb b/score_board.rb index d47123e..9c3cf28 100755 --- a/score_board.rb +++ b/score_board.rb @@ -15,25 +15,25 @@ Pusher.url = "http://#{ENV['PUSHER_KEY']}:#{ENV['PUSHER_SECRET']}@api.pusherapp. class ScoreBoard < Sinatra::Base - @@match = Match.new(Player.new, Player.new) + @@match = Match.new get '/' do - erb :index, locals: { one: @@match.players[:one], - two: @@match.players[:two] } + erb :index, locals: { scores: @@match.scores } end put '/reset_scores' do - @@match = Match.new(Player.new, Player.new) + @@match.reset_scores + @@match.reset_games push_scores end - put '/player1_scores' do - @@match.add_point(:one) + put '/red_scores' do + @@match.add_point(:red) push_scores end - put '/player2_scores' do - @@match.add_point(:two) + put '/blue_scores' do + @@match.add_point(:blue) push_scores end diff --git a/views/index.erb b/views/index.erb index 0f31aa4..5b8858b 100644 --- a/views/index.erb +++ b/views/index.erb @@ -3,91 +3,25 @@ Table Tennis - - - - - - + + -
-
- <%= one.score %> - <%= one.games %> -
-
- <%= two.score %> - <%= two.games %> -
-
+ + + + +
+ + +