1
0
mirror of https://github.com/danbee/scoreboard synced 2025-03-04 08:59:11 +00:00

Refactor and add undo.

This commit is contained in:
Daniel Barber 2015-05-11 16:51:09 +01:00
parent cd631a5c77
commit a25f39308c
4 changed files with 39 additions and 18 deletions

View File

@ -1,6 +1,7 @@
class Match
def initialize
@one, @two = Player.new('Player One'), Player.new('Player Two')
@one, @two = Player.new(:one, name: 'Player One'),
Player.new(:two, name: 'Player Two')
end
def add_point(colour)
@ -14,6 +15,11 @@ class Match
end
end
def undo_point(colour)
player = players[colour]
player.score.decrement if player.score.value > 0
end
def reset_scores
@one.score.reset
@two.score.reset

View File

@ -1,11 +1,15 @@
class Player
include Redis::Objects
attr_reader :id
value :name
counter :score
counter :games
def initialize(name)
@name = name
def initialize(id, attr = {})
@id = id
self.name = attr[:name]
end
def has_beaten(player)
@ -13,12 +17,8 @@ class Player
end
def attributes
{ name: @name,
{ name: self.name.value,
score: self.score.value,
games: self.games.value }
end
def id
@name.downcase.gsub(/[^a-z]/, '-')
end
end

View File

@ -13,6 +13,7 @@
</div>
<script>
this.players = opts.players;
channel.bind('update_scores', function(data) {

View File

@ -5,7 +5,7 @@ Dotenv.load
require 'json'
require 'sinatra'
require 'sinatra/base'
require 'pusher'
require 'redis'
@ -22,34 +22,48 @@ Pusher.url = "http://#{ENV['PUSHER_KEY']}:#{ENV['PUSHER_SECRET']}@api.pusherapp.
class Scoreboard < Sinatra::Base
@@match = Match.new
get '/' do
erb :index, locals: { scores: @@match.scores }
erb :index, locals: { scores: match.scores }
end
put '/reset_scores' do
@@match.reset_scores
@@match.reset_games
match.reset_scores
match.reset_games
push_scores
end
put '/red_scores' do
@@match.add_point(:red)
match.add_point(:red)
push_scores
end
put '/blue_scores' do
@@match.add_point(:blue)
match.add_point(:blue)
push_scores
end
put '/red_undo' do
match.undo_point(:red)
push_scores
end
put '/blue_undo' do
match.undo_point(:blue)
push_scores
end
get '/scores' do
JSON @@match.scores
JSON match.scores
end
def push_scores
Pusher['scores'].trigger('update_scores', @@match.scores.to_json)
Pusher['scores'].trigger('update_scores', match.scores.to_json)
end
private
def match
Match.new
end
end