From 2d30fa33986a174cead3ff9ae4f95efb79da4bbb Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Thu, 14 May 2015 18:09:35 +0100 Subject: [PATCH] Add new_game route with ability to set names. --- lib/match.rb | 8 +++++--- lib/player.rb | 3 +-- scoreboard.rb | 10 +++++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/match.rb b/lib/match.rb index 82ba2c2..efb351a 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -1,7 +1,9 @@ class Match - def initialize - @one, @two = Player.new(:one, name: 'Player One'), - Player.new(:two, name: 'Player Two') + def initialize(name_one = nil, name_two = nil) + @one, @two = Player.new(:one), + Player.new(:two) + @one.name = name_one unless name_one.nil? + @two.name = name_two unless name_two.nil? end def add_point(colour) diff --git a/lib/player.rb b/lib/player.rb index 6aa3bc5..b779d2f 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -7,9 +7,8 @@ class Player counter :score counter :games - def initialize(id, attr = {}) + def initialize(id) @id = id - self.name = attr[:name] end def has_beaten(player) diff --git a/scoreboard.rb b/scoreboard.rb index d9152cd..d59fb9a 100644 --- a/scoreboard.rb +++ b/scoreboard.rb @@ -26,6 +26,14 @@ class Scoreboard < Sinatra::Base erb :index, locals: { scores: match.scores } end + post '/new_game' do + @match = Match.new(params[:one], params[:two]) + @match.reset_scores + @match.reset_games + push_scores + JSON match.scores + end + put '/reset_scores' do match.reset_scores match.reset_games @@ -63,7 +71,7 @@ class Scoreboard < Sinatra::Base private def match - Match.new + @match ||= Match.new end end