From 7e487167d12761e1245ebcd3f7b0d21e7e0aedf7 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sun, 10 May 2015 14:55:37 +0100 Subject: [PATCH] Use redis-objects for persistence. This has also simplified the code! --- Gemfile | 3 +++ Gemfile.lock | 6 ++++++ lib/match.rb | 16 ++++++++-------- lib/player.rb | 37 +++++++++++-------------------------- score_board.rb | 7 +++++++ 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/Gemfile b/Gemfile index 4ebaec5..1198748 100644 --- a/Gemfile +++ b/Gemfile @@ -8,5 +8,8 @@ gem 'json' gem 'sinatra' gem 'pusher' +gem 'redis-objects' +gem 'connection_pool' + gem 'puma' gem 'foreman', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 4371f8e..0d8ccb8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + connection_pool (2.2.0) dotenv (2.0.1) foreman (0.78.0) thor (~> 0.19.1) @@ -16,6 +17,9 @@ GEM rack (1.6.0) rack-protection (1.5.3) rack + redis (3.2.1) + redis-objects (1.2.0) + redis (>= 3.0.2) signature (0.1.8) sinatra (1.4.6) rack (~> 1.4) @@ -28,9 +32,11 @@ PLATFORMS ruby DEPENDENCIES + connection_pool dotenv foreman json puma pusher + redis-objects sinatra diff --git a/lib/match.rb b/lib/match.rb index e620abf..743f98b 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -4,28 +4,28 @@ class Match end def add_point(colour) - players[colour].add_point + players[colour].score.increment if @one.has_beaten(@two) reset_scores - @one.add_game + @one.games.increment elsif @two.has_beaten(@one) reset_scores - @two.add_game + @two.games.increment end end def reset_scores - @one.reset_score - @two.reset_score + @one.score.reset + @two.score.reset end def reset_games - @one.reset_games - @two.reset_games + @one.games.reset + @two.games.reset end def total_games - @one.games + @two.games + @one.games.value + @two.games.value end def players diff --git a/lib/player.rb b/lib/player.rb index 9adcb86..ac7bd7f 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,39 +1,24 @@ class Player - attr_reader :score, :games, :name + include Redis::Objects + + counter :score + counter :games def initialize(name) @name = name - @score = 0 - @games = 0 end def has_beaten(player) - @score > 10 && @score > player.score + 1 - end - - def add_point - @score += 1 - end - - def add_game - @games += 1 - end - - def undo_point - @score -= 1 - end - - def reset_score - @score = 0 - end - - def reset_games - @games = 0 + self.score.value > 10 && self.score.value > player.score.value + 1 end def attributes { name: @name, - score: @score, - games: @games } + score: self.score.value, + games: self.games.value } + end + + def id + @name.downcase.gsub(/[^a-z]/, '-') end end diff --git a/score_board.rb b/score_board.rb index 9c3cf28..99baa4e 100644 --- a/score_board.rb +++ b/score_board.rb @@ -8,6 +8,13 @@ require 'json' require 'sinatra' require 'pusher' +require 'redis' +require 'redis-objects' + +require 'connection_pool' +redis_url = ENV['REDISCLOUD_URL'] +Redis::Objects.redis = ConnectionPool.new(size: 5, timeout: 5) { Redis.new(url: redis_url) } + require './lib/match' require './lib/player'