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:
parent
cd631a5c77
commit
a25f39308c
@ -1,6 +1,7 @@
|
|||||||
class Match
|
class Match
|
||||||
def initialize
|
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
|
end
|
||||||
|
|
||||||
def add_point(colour)
|
def add_point(colour)
|
||||||
@ -14,6 +15,11 @@ class Match
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def undo_point(colour)
|
||||||
|
player = players[colour]
|
||||||
|
player.score.decrement if player.score.value > 0
|
||||||
|
end
|
||||||
|
|
||||||
def reset_scores
|
def reset_scores
|
||||||
@one.score.reset
|
@one.score.reset
|
||||||
@two.score.reset
|
@two.score.reset
|
||||||
|
|||||||
@ -1,11 +1,15 @@
|
|||||||
class Player
|
class Player
|
||||||
include Redis::Objects
|
include Redis::Objects
|
||||||
|
|
||||||
|
attr_reader :id
|
||||||
|
|
||||||
|
value :name
|
||||||
counter :score
|
counter :score
|
||||||
counter :games
|
counter :games
|
||||||
|
|
||||||
def initialize(name)
|
def initialize(id, attr = {})
|
||||||
@name = name
|
@id = id
|
||||||
|
self.name = attr[:name]
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_beaten(player)
|
def has_beaten(player)
|
||||||
@ -13,12 +17,8 @@ class Player
|
|||||||
end
|
end
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
{ name: @name,
|
{ name: self.name.value,
|
||||||
score: self.score.value,
|
score: self.score.value,
|
||||||
games: self.games.value }
|
games: self.games.value }
|
||||||
end
|
end
|
||||||
|
|
||||||
def id
|
|
||||||
@name.downcase.gsub(/[^a-z]/, '-')
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -13,6 +13,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
this.players = opts.players;
|
this.players = opts.players;
|
||||||
|
|
||||||
channel.bind('update_scores', function(data) {
|
channel.bind('update_scores', function(data) {
|
||||||
|
|||||||
@ -5,7 +5,7 @@ Dotenv.load
|
|||||||
|
|
||||||
require 'json'
|
require 'json'
|
||||||
|
|
||||||
require 'sinatra'
|
require 'sinatra/base'
|
||||||
require 'pusher'
|
require 'pusher'
|
||||||
|
|
||||||
require 'redis'
|
require 'redis'
|
||||||
@ -22,34 +22,48 @@ Pusher.url = "http://#{ENV['PUSHER_KEY']}:#{ENV['PUSHER_SECRET']}@api.pusherapp.
|
|||||||
|
|
||||||
class Scoreboard < Sinatra::Base
|
class Scoreboard < Sinatra::Base
|
||||||
|
|
||||||
@@match = Match.new
|
|
||||||
|
|
||||||
get '/' do
|
get '/' do
|
||||||
erb :index, locals: { scores: @@match.scores }
|
erb :index, locals: { scores: match.scores }
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/reset_scores' do
|
put '/reset_scores' do
|
||||||
@@match.reset_scores
|
match.reset_scores
|
||||||
@@match.reset_games
|
match.reset_games
|
||||||
push_scores
|
push_scores
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/red_scores' do
|
put '/red_scores' do
|
||||||
@@match.add_point(:red)
|
match.add_point(:red)
|
||||||
push_scores
|
push_scores
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/blue_scores' do
|
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
|
push_scores
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/scores' do
|
get '/scores' do
|
||||||
JSON @@match.scores
|
JSON match.scores
|
||||||
end
|
end
|
||||||
|
|
||||||
def push_scores
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user