Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: Sephi-Chan/Conquest-on-Rails
base: 9baa1d1777
...
head fork: Sephi-Chan/Conquest-on-Rails
compare: e3eeeaff10
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
17 app/contexts/game_begins.rb
@@ -0,0 +1,17 @@
+class GameBegins
+ attr_reader :game
+
+ def initialize(game)
+ @game = game
+ end
+
+
+ def process
+ raise GameIsNotFull unless game.full?
+ raise GameIsRunning if game.state == Game::RUNNING
+ end
+
+
+ class GameIsNotFull < StandardError; end
+ class GameIsRunning < StandardError; end
+end
View
10 app/contexts/user_joins_game.rb
@@ -8,11 +8,11 @@ def initialize(user, game)
def process
- raise GameIsRunning if game.state == Game::RUNNING
- raise GameIsFullError if game.full?
+ raise GameIsRunning if game.state == Game::RUNNING
+ raise GameIsFull if game.full?
game.participations.create(user: user, position: position).tap do
- game.start if game.full?
+ GameBegins.new(game).process if game.full?
end
end
@@ -22,6 +22,6 @@ def position
end
- class GameIsFullError < StandardError; end
- class GameIsRunning < StandardError; end
+ class GameIsFull < StandardError; end
+ class GameIsRunning < StandardError; end
end
View
27 app/models/user.rb
@@ -1,38 +1,27 @@
class User < ActiveRecord::Base
has_secure_password
-
+
validates :name, length: 2..20,
format: /^[a-z-]+$/i,
uniqueness: true,
presence: true
-
+
validates :email, format: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
presence: true,
uniqueness: true
-
+
validates_presence_of :password, :password_confirmation, on: :create
-
+
has_many :participations
has_many :games, through: :participations
-
-
+
+
# Generate a persistence token not yet used.
def self.generate_persistence_token
- persistence_tokens = User.scoped.value_of :persistence_token
-
begin
persistence_token = SecureRandom.hex
- end while persistence_tokens.include?(persistence_token)
-
+ end while User.where(persistence_token: persistence_token).any?
+
persistence_token
end
-
-
- # Find an appropriate game for the user.
- def send_in_game!
- User.transaction do
- game = Game.not_full.without(self).sample || Game.create
- game.add!(self)
- end
- end
end
View
40 spec/contexts/game_begins_spec.rb
@@ -0,0 +1,40 @@
+describe GameBegins do
+ let(:game) { stub_model(Game) }
+ subject { GameBegins.new(game) }
+
+ it "should have a game" do
+ subject.game.should be_a(Game)
+ end
+
+
+ describe "The game begins" do
+ context "when game is not full" do
+ let(:game) { stub_model(Game, full?: false) }
+
+ it "should fail" do
+ expect { subject.process }.to raise_error(GameBegins::GameIsNotFull)
+ end
+ end
+
+ context "when the game is already started" do
+ let(:game) { stub_model(Game, full?: true, state: Game::RUNNING)}
+
+ it "should fail" do
+ expect { subject.process }.to raise_error(GameBegins::GameIsRunning)
+ end
+ end
+
+ context "when the game is full" do
+ describe "Territories dispatch" do
+ it "should dispatch all the territories"
+ it "each player should have at least X territories"
+ it "X players should have Y more territories"
+ end
+
+ describe "Units deployment" do
+ it "no player should have units anymore"
+ it "each territory should have at least one unit"
+ end
+ end
+ end
+end
View
7 spec/contexts/user_joins_game_spec.rb
@@ -19,7 +19,7 @@
let(:game) { stub_model(Game, full?: true) }
it "should fail" do
- expect { subject.process }.to raise_error(UserJoinsGame::GameIsFullError)
+ expect { subject.process }.to raise_error(UserJoinsGame::GameIsFull)
end
end
@@ -68,9 +68,12 @@
let(:gamma) { FactoryGirl.create(:gamma) }
let(:delta) { FactoryGirl.create(:delta) }
let(:epsilon) { FactoryGirl.create(:epsilon) }
+ let(:game_begins) { GameBegins }
+ let(:game_begins_instance) { stub }
it "should start the game" do
- game.should_receive(:start)
+ game_begins_instance.should_receive(:process)
+ game_begins.should_receive(:new).with(game).and_return(game_begins_instance)
[ alpha, beta, gamma, delta, epsilon ].each do |user|
UserJoinsGame.new(user, game).process

No commit comments for this range

Something went wrong with that request. Please try again.