diff --git a/Gemfile.lock b/Gemfile.lock index 4c78b8e..2284711 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,3 +40,6 @@ DEPENDENCIES pusher redis-objects sinatra + +BUNDLED WITH + 1.10.6 diff --git a/README.md b/README.md index 3615a7a..7743c0d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -Table Tennis Score Board -======================= +# Table Tennis Score Board Implements a table tennis scoreboard which can be updated by PUTing to particular URL's. This was part of a hack day project that involved buttons @@ -13,8 +12,7 @@ The back end is Sinatra and the front end is powered by Riot.js. Redis is used for data persistence. Pusher is used for communication between the back end and the front. -Instructions ------------- +## Instructions Install the bundle: @@ -27,3 +25,7 @@ Run the server: ```sh $ bundle exec foreman start ``` + +## Credits + +Ping Pong Paddle icon by Anbileru Adaleru from the Noun Project diff --git a/lib/match.rb b/lib/match.rb index 2482c8c..f1c32aa 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -1,4 +1,9 @@ class Match + include Redis::Objects + + value :initial_serve + value :serve + def initialize(name_one = nil, name_two = nil) @one, @two = Player.new(:one), Player.new(:two) @@ -10,16 +15,48 @@ class Match players[colour].score.increment if @one.has_beaten(@two) reset_scores + swap_initial_serve @one.games.increment elsif @two.has_beaten(@one) reset_scores + swap_initial_serve @two.games.increment end + set_serve + end + + def set_serve + total_points = @one.score.value + @two.score.value + initial_server = (total_points / 2).even? + case self.initial_serve.value + when 'blue' + self.serve = initial_server ? :blue : :red + when 'red' + self.serve = initial_server ? :red : :blue + end + end + + def swap_initial_serve + case self.initial_serve.value + when 'red' + set_initial_serve(:blue) + when 'blue' + set_initial_serve(:red) + end end def undo_point(colour) player = players[colour] - player.score.decrement if player.score.value > 0 + if player.score.value > 0 + player.score.decrement + elsif no_scores? + set_initial_serve(colour) + end + end + + def set_initial_serve(colour) + self.initial_serve = colour + self.serve = colour end def reset_scores @@ -30,6 +67,7 @@ class Match def reset_games @one.games.reset @two.games.reset + set_initial_serve(nil) end def total_games @@ -45,7 +83,16 @@ class Match end def scores - { red: players[:red].attributes, - blue: players[:blue].attributes } + { red: players[:red].attributes.merge(serve: self.serve == 'red'), + blue: players[:blue].attributes.merge(serve: self.serve == 'blue') } + end + + def no_scores? + @one.score == 0 && @two.score == 0 + end + + # This is required for Redis + def id + 1 end end diff --git a/public/images/bat.svg b/public/images/bat.svg new file mode 100644 index 0000000..0aec80e --- /dev/null +++ b/public/images/bat.svg @@ -0,0 +1,12 @@ + + + + Untitled + Created with Sketch. + + + + + + + \ No newline at end of file diff --git a/public/stylesheets/main.css b/public/stylesheets/main.css index 996c034..6d32d9e 100644 --- a/public/stylesheets/main.css +++ b/public/stylesheets/main.css @@ -36,6 +36,16 @@ body { top: 3rem; font-size: 2rem; } +.scores div .serve { + position: absolute; + top: 9rem; +} +.scores div .serve img { + display: none; +} +.scores div .serve img.show { + display: block; +} #red-score { background: #b00; } #blue-score { background: #00b; } #red-score .score { right: 5rem; } @@ -44,6 +54,8 @@ body { #blue-score .games { right: 5rem; } #red-score .name { right: 5rem; } #blue-score .name { left: 5rem; } +#red-score .serve { right: 5rem; } +#blue-score .serve { left: 5rem; } .controls { position: absolute; diff --git a/public/tags/scores.tag b/public/tags/scores.tag index 4f503f3..1a09af1 100644 --- a/public/tags/scores.tag +++ b/public/tags/scores.tag @@ -4,11 +4,13 @@ { players.blue.name } { players.blue.score } { players.blue.games } + service
{ players.red.name } { players.red.score } { players.red.games } + service
diff --git a/scoreboard.rb b/scoreboard.rb index d59fb9a..962a624 100644 --- a/scoreboard.rb +++ b/scoreboard.rb @@ -73,5 +73,4 @@ class Scoreboard < Sinatra::Base def match @match ||= Match.new end - end