mirror of
https://github.com/danbee/scoreboard
synced 2025-03-04 08:59:11 +00:00
Use redis-objects for persistence.
This has also simplified the code!
This commit is contained in:
parent
5e5a67fe56
commit
7e487167d1
3
Gemfile
3
Gemfile
@ -8,5 +8,8 @@ gem 'json'
|
|||||||
gem 'sinatra'
|
gem 'sinatra'
|
||||||
gem 'pusher'
|
gem 'pusher'
|
||||||
|
|
||||||
|
gem 'redis-objects'
|
||||||
|
gem 'connection_pool'
|
||||||
|
|
||||||
gem 'puma'
|
gem 'puma'
|
||||||
gem 'foreman', group: :development
|
gem 'foreman', group: :development
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
GEM
|
GEM
|
||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
|
connection_pool (2.2.0)
|
||||||
dotenv (2.0.1)
|
dotenv (2.0.1)
|
||||||
foreman (0.78.0)
|
foreman (0.78.0)
|
||||||
thor (~> 0.19.1)
|
thor (~> 0.19.1)
|
||||||
@ -16,6 +17,9 @@ GEM
|
|||||||
rack (1.6.0)
|
rack (1.6.0)
|
||||||
rack-protection (1.5.3)
|
rack-protection (1.5.3)
|
||||||
rack
|
rack
|
||||||
|
redis (3.2.1)
|
||||||
|
redis-objects (1.2.0)
|
||||||
|
redis (>= 3.0.2)
|
||||||
signature (0.1.8)
|
signature (0.1.8)
|
||||||
sinatra (1.4.6)
|
sinatra (1.4.6)
|
||||||
rack (~> 1.4)
|
rack (~> 1.4)
|
||||||
@ -28,9 +32,11 @@ PLATFORMS
|
|||||||
ruby
|
ruby
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
|
connection_pool
|
||||||
dotenv
|
dotenv
|
||||||
foreman
|
foreman
|
||||||
json
|
json
|
||||||
puma
|
puma
|
||||||
pusher
|
pusher
|
||||||
|
redis-objects
|
||||||
sinatra
|
sinatra
|
||||||
|
|||||||
16
lib/match.rb
16
lib/match.rb
@ -4,28 +4,28 @@ class Match
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_point(colour)
|
def add_point(colour)
|
||||||
players[colour].add_point
|
players[colour].score.increment
|
||||||
if @one.has_beaten(@two)
|
if @one.has_beaten(@two)
|
||||||
reset_scores
|
reset_scores
|
||||||
@one.add_game
|
@one.games.increment
|
||||||
elsif @two.has_beaten(@one)
|
elsif @two.has_beaten(@one)
|
||||||
reset_scores
|
reset_scores
|
||||||
@two.add_game
|
@two.games.increment
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_scores
|
def reset_scores
|
||||||
@one.reset_score
|
@one.score.reset
|
||||||
@two.reset_score
|
@two.score.reset
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_games
|
def reset_games
|
||||||
@one.reset_games
|
@one.games.reset
|
||||||
@two.reset_games
|
@two.games.reset
|
||||||
end
|
end
|
||||||
|
|
||||||
def total_games
|
def total_games
|
||||||
@one.games + @two.games
|
@one.games.value + @two.games.value
|
||||||
end
|
end
|
||||||
|
|
||||||
def players
|
def players
|
||||||
|
|||||||
@ -1,39 +1,24 @@
|
|||||||
class Player
|
class Player
|
||||||
attr_reader :score, :games, :name
|
include Redis::Objects
|
||||||
|
|
||||||
|
counter :score
|
||||||
|
counter :games
|
||||||
|
|
||||||
def initialize(name)
|
def initialize(name)
|
||||||
@name = name
|
@name = name
|
||||||
@score = 0
|
|
||||||
@games = 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_beaten(player)
|
def has_beaten(player)
|
||||||
@score > 10 && @score > player.score + 1
|
self.score.value > 10 && self.score.value > player.score.value + 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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
{ name: @name,
|
{ name: @name,
|
||||||
score: @score,
|
score: self.score.value,
|
||||||
games: @games }
|
games: self.games.value }
|
||||||
|
end
|
||||||
|
|
||||||
|
def id
|
||||||
|
@name.downcase.gsub(/[^a-z]/, '-')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,6 +8,13 @@ require 'json'
|
|||||||
require 'sinatra'
|
require 'sinatra'
|
||||||
require 'pusher'
|
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/match'
|
||||||
require './lib/player'
|
require './lib/player'
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user