mirror of
https://github.com/danbee/scoreboard
synced 2025-03-04 08:59:11 +00:00
Add game logic.
This commit is contained in:
parent
174242f824
commit
5661ed25c9
@ -7,6 +7,15 @@ class Match
|
||||
|
||||
def add_point(player)
|
||||
@players[player].add_point
|
||||
if @players[:one].has_beaten(@players[:two])
|
||||
@players[:one].reset_score
|
||||
@players[:two].reset_score
|
||||
@players[:one].add_game
|
||||
elsif @players[:two].has_beaten(@players[:one])
|
||||
@players[:one].reset_score
|
||||
@players[:two].reset_score
|
||||
@players[:two].add_game
|
||||
end
|
||||
end
|
||||
|
||||
def scores
|
||||
|
||||
@ -14,10 +14,18 @@ class Player
|
||||
@score += 1
|
||||
end
|
||||
|
||||
def add_game
|
||||
@games += 1
|
||||
end
|
||||
|
||||
def undo_point
|
||||
@score -= 1
|
||||
end
|
||||
|
||||
def reset_score
|
||||
@score = 0
|
||||
end
|
||||
|
||||
def attributes
|
||||
{ score: @score, games: @games }
|
||||
end
|
||||
|
||||
@ -22,6 +22,11 @@ class ScoreBoard < Sinatra::Base
|
||||
two: @@match.players[:two] }
|
||||
end
|
||||
|
||||
put '/reset_scores' do
|
||||
@@match = Match.new(Player.new, Player.new)
|
||||
push_scores
|
||||
end
|
||||
|
||||
put '/player1_scores' do
|
||||
@@match.add_point(:one)
|
||||
push_scores
|
||||
|
||||
@ -37,6 +37,12 @@
|
||||
#player2-score .score { left: 5rem; }
|
||||
#player1-score .games { left: 5rem; }
|
||||
#player2-score .games { right: 5rem; }
|
||||
|
||||
.controls {
|
||||
position: absolute;
|
||||
bottom: 0; left: 0; right: 0;
|
||||
height: 2rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script src="//js.pusher.com/2.2/pusher.min.js"></script>
|
||||
@ -57,15 +63,14 @@
|
||||
$('#player2-score .games').text(data.two.games);
|
||||
}
|
||||
|
||||
reset = function () {
|
||||
resetScores();
|
||||
resetGames();
|
||||
updateScores();
|
||||
resetScores = function () {
|
||||
$.ajax({method: 'PUT',
|
||||
url: '/reset_scores',
|
||||
data: ''});
|
||||
}
|
||||
|
||||
$(function() {
|
||||
updateScores(scores);
|
||||
$('#reset').on('click', reset);
|
||||
$('#reset').on('click', resetScores);
|
||||
})
|
||||
</script>
|
||||
</head>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user