el_gato 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +3 -0
- data/Rakefile +1 -0
- data/el_gato.gemspec +27 -0
- data/lib/el_gato.rb +7 -0
- data/lib/el_gato/game_client.rb +33 -0
- data/lib/el_gato/game_server.rb +82 -0
- data/lib/el_gato/version.rb +3 -0
- data/spec/el_gato/game_client_spec.rb +47 -0
- data/spec/el_gato/game_server_spec.rb +20 -0
- data/spec/spec_helper.rb +3 -0
- metadata +149 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 chischaschos
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/el_gato.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'el_gato/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'el_gato'
|
8
|
+
spec.version = ElGato::VERSION
|
9
|
+
spec.authors = ['chischaschos']
|
10
|
+
spec.email = ['larin.s931@gmail.com']
|
11
|
+
spec.description = %q{A drb implementation of the tic tac toe game}
|
12
|
+
spec.summary = %q{A drb implementation of the tic tac toe game fot the CLI}
|
13
|
+
spec.homepage = 'https://github.com/chischaschos/el_gato'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'bare_gato'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'rspec'
|
26
|
+
spec.add_development_dependency 'debugger'
|
27
|
+
end
|
data/lib/el_gato.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module ElGato
|
2
|
+
class GameClient
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
SERVER_URI="druby://localhost:8787"
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
DRb.start_service
|
9
|
+
@game_server = DRbObject.new_with_uri(SERVER_URI)
|
10
|
+
@player_id = SecureRandom.uuid
|
11
|
+
end
|
12
|
+
|
13
|
+
def games
|
14
|
+
@game_server.games @player_id
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_player
|
18
|
+
@game_server.add_player @player_id
|
19
|
+
end
|
20
|
+
|
21
|
+
def play
|
22
|
+
result = @game_server.play @player_id
|
23
|
+
@current_game_id = result[:id] if result[:status] == :ready
|
24
|
+
result
|
25
|
+
end
|
26
|
+
|
27
|
+
def move args
|
28
|
+
raise 'Play a game first' unless @current_game_id
|
29
|
+
|
30
|
+
@game_server.move @current_game_id, @player_id, args
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module ElGato
|
2
|
+
class GameServer
|
3
|
+
require 'drb/drb'
|
4
|
+
|
5
|
+
# The URI for the server to connect to
|
6
|
+
URI="druby://localhost:8787"
|
7
|
+
|
8
|
+
|
9
|
+
def self.stop
|
10
|
+
Process.kill 'KILL', @proccess_id
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.start
|
14
|
+
@proccess_id = fork do
|
15
|
+
DRb.start_service(URI, new)
|
16
|
+
DRb.thread.join
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :waiting_players
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@games = []
|
24
|
+
@waiting_players = []
|
25
|
+
end
|
26
|
+
|
27
|
+
def play player_id
|
28
|
+
add_player player_id
|
29
|
+
|
30
|
+
if waiting_players.size == 2
|
31
|
+
@games << BareGato::Game.new(players: waiting_players.dup)
|
32
|
+
waiting_players.clear
|
33
|
+
status = { status: :ready, id: @games.last.id }
|
34
|
+
|
35
|
+
elsif (games = games(player_id)).size > 0
|
36
|
+
status = :ready
|
37
|
+
status = { status: :ready, id: games.last[:id] }
|
38
|
+
|
39
|
+
else
|
40
|
+
status = { status: :queued }
|
41
|
+
end
|
42
|
+
|
43
|
+
status
|
44
|
+
end
|
45
|
+
|
46
|
+
def games player_id
|
47
|
+
@games.find_all do |game|
|
48
|
+
game.includes_player? BareGato::Player.new player_id
|
49
|
+
end.map do |game|
|
50
|
+
{
|
51
|
+
id: game.id,
|
52
|
+
status: :on_progress
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def move game_id, player_id, args
|
58
|
+
game = find_game game_id
|
59
|
+
player = BareGato::Player.new player_id
|
60
|
+
|
61
|
+
raise 'Not your turn bro!' unless game && player && game.next?(player)
|
62
|
+
|
63
|
+
game.play BareGato::Move.new player, args
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def add_player player_id
|
69
|
+
player = BareGato::Player.new player_id
|
70
|
+
if !waiting_players.include?(player) && games(player_id).size == 0
|
71
|
+
waiting_players << BareGato::Player.new(player_id)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def find_game game_id
|
76
|
+
@games.find do |game|
|
77
|
+
game.id == game_id
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ElGato
|
4
|
+
describe GameClient do
|
5
|
+
before do
|
6
|
+
GameServer.start
|
7
|
+
end
|
8
|
+
|
9
|
+
specify do
|
10
|
+
game_client1 = GameClient.new
|
11
|
+
expect(game_client1.games).to have(0).items
|
12
|
+
expect(game_client1.play).to eq({
|
13
|
+
status: :queued
|
14
|
+
})
|
15
|
+
expect(game_client1.games).to eq []
|
16
|
+
|
17
|
+
expect { game_client1.move x: 0, y: 0 }.to raise_error 'Play a game first'
|
18
|
+
|
19
|
+
game_client2 = GameClient.new
|
20
|
+
expect(game_client2.games).to have(0).items
|
21
|
+
result = game_client2.play
|
22
|
+
expect(result[:status]).to eq :ready
|
23
|
+
expect(result).to have_key(:id)
|
24
|
+
|
25
|
+
expect(game_client2.games).to eq game_client1.games
|
26
|
+
expect(game_client2.games.first[:status]).to eq :on_progress
|
27
|
+
|
28
|
+
expect(game_client2.games).to have(1).item
|
29
|
+
|
30
|
+
result = game_client2.play
|
31
|
+
expect(result[:status]).to eq :ready
|
32
|
+
expect(result).to have_key(:id)
|
33
|
+
|
34
|
+
|
35
|
+
expect { game_client1.move x: 0, y: 0 }.to raise_error 'Play a game first'
|
36
|
+
expect(game_client1.play[:status]).to eq :ready
|
37
|
+
expect { game_client1.move x: 0, y: 0 }.to raise_error 'Not your turn bro!'
|
38
|
+
|
39
|
+
expect(game_client2.move x: 0, y: 0).to be_true
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
after do
|
44
|
+
GameServer.stop
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ElGato::GameServer do
|
4
|
+
it 'should allow only 1 game per player' do
|
5
|
+
expect(subject.play('P1')).to eq({ status: :queued })
|
6
|
+
expect(subject.games('P1')).to eq []
|
7
|
+
|
8
|
+
expect(subject.play('P1')).to eq({ status: :queued })
|
9
|
+
expect(subject.games('P1')).to eq []
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should allow a player returning to the active game' do
|
13
|
+
expect(subject.play('P1')).to eq({ status: :queued })
|
14
|
+
expect(subject.play('P2')[:status]).to eq :ready
|
15
|
+
expect(subject.play('P1')).to eq subject.play 'P2'
|
16
|
+
expect(subject.play('P2')[:id]).to_not be_nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: el_gato
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- chischaschos
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bare_gato
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: debugger
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: A drb implementation of the tic tac toe game
|
95
|
+
email:
|
96
|
+
- larin.s931@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- el_gato.gemspec
|
108
|
+
- lib/el_gato.rb
|
109
|
+
- lib/el_gato/game_client.rb
|
110
|
+
- lib/el_gato/game_server.rb
|
111
|
+
- lib/el_gato/version.rb
|
112
|
+
- spec/el_gato/game_client_spec.rb
|
113
|
+
- spec/el_gato/game_server_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
homepage: https://github.com/chischaschos/el_gato
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
segments:
|
129
|
+
- 0
|
130
|
+
hash: -1054788701632010869
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ! '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
segments:
|
138
|
+
- 0
|
139
|
+
hash: -1054788701632010869
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 1.8.23
|
143
|
+
signing_key:
|
144
|
+
specification_version: 3
|
145
|
+
summary: A drb implementation of the tic tac toe game fot the CLI
|
146
|
+
test_files:
|
147
|
+
- spec/el_gato/game_client_spec.rb
|
148
|
+
- spec/el_gato/game_server_spec.rb
|
149
|
+
- spec/spec_helper.rb
|