mirror of
https://github.com/danbee/scoreboard
synced 2025-03-04 08:59:11 +00:00
Move state server side.
This commit is contained in:
parent
0a5ece1d00
commit
174242f824
2
Gemfile
2
Gemfile
@ -2,5 +2,7 @@ source 'https://rubygems.org'
|
|||||||
|
|
||||||
gem 'dotenv'
|
gem 'dotenv'
|
||||||
|
|
||||||
|
gem 'json'
|
||||||
|
|
||||||
gem 'sinatra'
|
gem 'sinatra'
|
||||||
gem 'pusher'
|
gem 'pusher'
|
||||||
|
|||||||
@ -3,6 +3,7 @@ GEM
|
|||||||
specs:
|
specs:
|
||||||
dotenv (2.0.1)
|
dotenv (2.0.1)
|
||||||
httpclient (2.6.0.1)
|
httpclient (2.6.0.1)
|
||||||
|
json (1.8.2)
|
||||||
multi_json (1.11.0)
|
multi_json (1.11.0)
|
||||||
pusher (0.14.4)
|
pusher (0.14.4)
|
||||||
httpclient (~> 2.5)
|
httpclient (~> 2.5)
|
||||||
@ -23,5 +24,6 @@ PLATFORMS
|
|||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
dotenv
|
dotenv
|
||||||
|
json
|
||||||
pusher
|
pusher
|
||||||
sinatra
|
sinatra
|
||||||
|
|||||||
@ -1,2 +1,6 @@
|
|||||||
require './scores'
|
require 'bundler'
|
||||||
run Sinatra::Application
|
|
||||||
|
Bundler.setup
|
||||||
|
require './score_board'
|
||||||
|
|
||||||
|
run ScoreBoard
|
||||||
|
|||||||
16
lib/match.rb
Normal file
16
lib/match.rb
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
class Match
|
||||||
|
attr_reader :players
|
||||||
|
|
||||||
|
def initialize(player1, player2)
|
||||||
|
@players = { one: player1, two: player2 }
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_point(player)
|
||||||
|
@players[player].add_point
|
||||||
|
end
|
||||||
|
|
||||||
|
def scores
|
||||||
|
{ one: @players[:one].attributes,
|
||||||
|
two: @players[:two].attributes }
|
||||||
|
end
|
||||||
|
end
|
||||||
24
lib/player.rb
Normal file
24
lib/player.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
class Player
|
||||||
|
attr_reader :score, :games
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@score = 0
|
||||||
|
@games = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def has_beaten(player)
|
||||||
|
@score > 10 && @score > player.score + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_point
|
||||||
|
@score += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def undo_point
|
||||||
|
@score -= 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def attributes
|
||||||
|
{ score: @score, games: @games }
|
||||||
|
end
|
||||||
|
end
|
||||||
43
score_board.rb
Executable file
43
score_board.rb
Executable file
@ -0,0 +1,43 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require 'dotenv'
|
||||||
|
Dotenv.load
|
||||||
|
|
||||||
|
require 'json'
|
||||||
|
|
||||||
|
require 'sinatra'
|
||||||
|
require 'pusher'
|
||||||
|
|
||||||
|
require './lib/match'
|
||||||
|
require './lib/player'
|
||||||
|
|
||||||
|
Pusher.url = "http://#{ENV['PUSHER_KEY']}:#{ENV['PUSHER_SECRET']}@api.pusherapp.com/apps/#{ENV['PUSHER_APP']}"
|
||||||
|
|
||||||
|
class ScoreBoard < Sinatra::Base
|
||||||
|
|
||||||
|
@@match = Match.new(Player.new, Player.new)
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
erb :index, locals: { one: @@match.players[:one],
|
||||||
|
two: @@match.players[:two] }
|
||||||
|
end
|
||||||
|
|
||||||
|
put '/player1_scores' do
|
||||||
|
@@match.add_point(:one)
|
||||||
|
push_scores
|
||||||
|
end
|
||||||
|
|
||||||
|
put '/player2_scores' do
|
||||||
|
@@match.add_point(:two)
|
||||||
|
push_scores
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/scores' do
|
||||||
|
JSON @@match.scores
|
||||||
|
end
|
||||||
|
|
||||||
|
def push_scores
|
||||||
|
Pusher['scores'].trigger('update_scores', @@match.scores.to_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
25
scores.rb
25
scores.rb
@ -1,25 +0,0 @@
|
|||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
require 'dotenv'
|
|
||||||
Dotenv.load
|
|
||||||
|
|
||||||
require 'sinatra'
|
|
||||||
require 'pusher'
|
|
||||||
|
|
||||||
Pusher.url = "http://#{ENV['PUSHER_KEY']}:#{ENV['PUSHER_SECRET']}@api.pusherapp.com/apps/#{ENV['PUSHER_APP']}"
|
|
||||||
|
|
||||||
get '/' do
|
|
||||||
erb :index
|
|
||||||
end
|
|
||||||
|
|
||||||
put '/player1_scores' do
|
|
||||||
Pusher['scores'].trigger('player1_scores', {
|
|
||||||
message: 'Player 1 scores'
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
put '/player2_scores' do
|
|
||||||
Pusher['scores'].trigger('player2_scores', {
|
|
||||||
message: 'Player 2 scores'
|
|
||||||
})
|
|
||||||
end
|
|
||||||
@ -46,50 +46,38 @@
|
|||||||
var pusher = new Pusher('57dc9c12b6e6fa97febb');
|
var pusher = new Pusher('57dc9c12b6e6fa97febb');
|
||||||
var channel = pusher.subscribe('scores');
|
var channel = pusher.subscribe('scores');
|
||||||
|
|
||||||
var scores = { player1: 0, player2: 0 }
|
channel.bind('update_scores', function(data) {
|
||||||
var games = { player1: 0, player2: 0 }
|
updateDisplay(data);
|
||||||
|
|
||||||
channel.bind('player1_scores', function(data) {
|
|
||||||
scores.player1++;
|
|
||||||
updateScores();
|
|
||||||
});
|
|
||||||
channel.bind('player2_scores', function(data) {
|
|
||||||
scores.player2++;
|
|
||||||
updateScores();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
updateScores = function () {
|
updateDisplay = function (data) {
|
||||||
updateDisplay();
|
$('#player1-score .score').text(data.one.score);
|
||||||
|
$('#player2-score .score').text(data.two.score);
|
||||||
|
$('#player1-score .games').text(data.one.games);
|
||||||
|
$('#player2-score .games').text(data.two.games);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateDisplay = function () {
|
reset = function () {
|
||||||
$('#player1-score .score').text(scores.player1);
|
resetScores();
|
||||||
$('#player2-score .score').text(scores.player2);
|
resetGames();
|
||||||
$('#player1-score .games').text(games.player1);
|
|
||||||
$('#player2-score .games').text(games.player2);
|
|
||||||
}
|
|
||||||
|
|
||||||
resetScores = function () {
|
|
||||||
scores = { player1: 0, player2: 0 }
|
|
||||||
games = { player1: 0, player2: 0 }
|
|
||||||
updateScores();
|
updateScores();
|
||||||
}
|
}
|
||||||
|
|
||||||
$(function() {
|
$(function() {
|
||||||
updateScores(scores);
|
updateScores(scores);
|
||||||
$('#reset').on('click', resetScores);
|
$('#reset').on('click', reset);
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="scores">
|
<div class="scores">
|
||||||
<div id="player1-score">
|
<div id="player1-score">
|
||||||
<span class="score"></span>
|
<span class="score"><%= one.score %></span>
|
||||||
<span class="games"></span>
|
<span class="games"><%= one.games %></span>
|
||||||
</div>
|
</div>
|
||||||
<div id="player2-score">
|
<div id="player2-score">
|
||||||
<span class="score"></span>
|
<span class="score"><%= two.score %></span>
|
||||||
<span class="games"></span>
|
<span class="games"><%= two.games %></span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user