drsi 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94aae14ff4de08a20726540d5b43141bbbdc8d24
4
- data.tar.gz: b63c09ef0f0299d85f3a65fd0cb4bf1c6de39c87
3
+ metadata.gz: f34d6bd83bc21f9c63c83c31c552e1f48dc42acf
4
+ data.tar.gz: 28785a5247013c7ddf5d0da3bae4d01f60308c42
5
5
  SHA512:
6
- metadata.gz: b1399b95a5dc17b649cd11da703f255278b54ecd6b1088be0a06be14b6666b0a0e5856ce3a89332631ed0daf65aaa3eab388e3679d49cef406ab195dcd2a78eb
7
- data.tar.gz: be8217e6295ed0731fc35e93cec412ffb15200627eab2afcb32a73f6cefd4c2e6f36b1c0e59f7769b639d7878bdf2caf34167a32a455f33e6427c7f20f0f9ffc
6
+ metadata.gz: 9b422c3925e5055ddf1053684ec51220d7580a0c67cb0c0f2788d42864f5937a44d80f7f74421330b19a4bc304ca1b0f25a96633692962c786b6a237c72d08de
7
+ data.tar.gz: a099dbfa5b9de7ded07c1aed28487ef3242d9eaf9374229ad11a48b53140833ef512cbb5a477f643db089670c2c78ec17c6335e2b58c36b5b4b727933213f50e
@@ -97,8 +97,12 @@ module DCI
97
97
  class_eval do
98
98
  method_object = instance_method(methodname)
99
99
  define_method(methodname) do |*args, &block|
100
- players_play_role!
101
- method_object.bind(self).call(*args, &block).tap {players_unplay_role!}
100
+ # puts "Context: #{self} - in method #{methodname} - #{do_play_unplay_p} assigning_roles"
101
+ extending_ticker = players_play_role!
102
+ method_object.bind(self).call(*args, &block).tap do
103
+ # puts "Context: #{self} - in method #{methodname} - #{do_play_unplay_p} un-assigning_roles"
104
+ players_unplay_role!(extending_ticker)
105
+ end
102
106
  end
103
107
  end
104
108
  end
@@ -141,11 +145,19 @@ module DCI
141
145
  (roles.keys - players.keys)
142
146
  end
143
147
 
148
+ def player_already_playing_role_in_this_context?(player, rolekey)
149
+ context = player.send(:context)
150
+ return false unless player.send(:rolekey) == rolekey
151
+ context == self or context.class == self.class
152
+ end
153
+
144
154
  # Associates every role to the intended player.
145
155
  def players_play_role!
156
+ extending_ticker = {}
146
157
  roles.keys.each do |rolekey|
147
- assign_role_to_player!(rolekey, @_players[rolekey])
158
+ assign_role_to_player!(rolekey, @_players[rolekey], extending_ticker: extending_ticker)
148
159
  end
160
+ extending_ticker
149
161
  end
150
162
 
151
163
  # Associates a role to an intended player:
@@ -153,16 +165,27 @@ module DCI
153
165
  # - The player get access to the context it is playing.
154
166
  # - The player get access to the rest of players in its context through instance methods named after their role keys.
155
167
  # - This context instance get access to this new role player through an instance method named after the role key.
156
- def assign_role_to_player!(rolekey, player)
168
+ def assign_role_to_player!(rolekey, player, extending_ticker:{})
157
169
  role_mod = roles[rolekey]
158
- ::DCI::Multiplayer(player).each {|roleplayer| roleplayer.__play_role!(role_mod, self)}
170
+ # puts " Context: #{self} - assigning role #{rolekey} to #{player}"
171
+ ::DCI::Multiplayer(player).each do |roleplayer|
172
+ if player_already_playing_role_in_this_context?(roleplayer, rolekey)
173
+ extending_ticker.merge!("#{roleplayer.object_id}_#{rolekey}" => false)
174
+ else
175
+ extending_ticker.merge!("#{roleplayer.object_id}_#{rolekey}" => true)
176
+ roleplayer.__play_role!(rolekey, role_mod, self)
177
+ end
178
+ end
159
179
  instance_variable_set(:"@#{rolekey}", player)
160
180
  end
161
181
 
162
182
  # Disassociates every role from the playing object.
163
- def players_unplay_role!
183
+ def players_unplay_role!(extending_ticker)
164
184
  roles.keys.each do |rolekey|
165
- ::DCI::Multiplayer(@_players[rolekey]).each {|roleplayer| roleplayer.__unplay_last_role!}
185
+ ::DCI::Multiplayer(@_players[rolekey]).each do |roleplayer|
186
+ # puts " Context: #{self} - un-assigning role #{rolekey} to #{roleplayer}"
187
+ roleplayer.__unplay_last_role! if extending_ticker["#{roleplayer.object_id}_#{rolekey}"]
188
+ end
166
189
  # 'instance_variable_set(:"@#{rolekey}", nil)
167
190
  end
168
191
  end
@@ -12,11 +12,12 @@ module Rolable
12
12
  # - Get or extend a new empty role module,
13
13
  # - Copy role_mod instance_methods into it,
14
14
  # - Inject context and settings methods.
15
- def __play_role!(role_mod, a_context)
15
+ def __play_role!(a_rolekey, role_mod, a_context)
16
16
  new_role = __next_empty_role
17
17
  new_role.__copy_instance_methods_from(role_mod)
18
- new_role.class_exec(a_context) do |the_context|
18
+ new_role.class_exec(a_context, a_rolekey) do |the_context, the_rolekey|
19
19
  private
20
+ define_method(:rolekey) {the_rolekey}
20
21
  define_method(:context) {the_context}
21
22
  define_method(:settings) {|*keys| context.send(:settings, *keys)}
22
23
  end
@@ -61,8 +62,13 @@ module Rolable
61
62
  __roles << role
62
63
  end
63
64
 
64
- # The context a role is played within. This method must be overidden in every __role definition module.
65
- # def context
66
- # nil
67
- # end
65
+ # The context within this object is playing its last role. This method must be overidden in every __role definition module.
66
+ def context
67
+ nil
68
+ end
69
+
70
+ # The rolekey this object is playing its last role. This method must be overidden in every __role definition module.
71
+ def rolekey
72
+ nil
73
+ end
68
74
  end
@@ -1,3 +1,3 @@
1
1
  module Drsi
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,11 +1,10 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
- require 'ostruct'
3
2
 
4
3
  describe 'Role MultiPlayers' do
5
4
 
6
5
  context "A role can be played by many objects in the same context at the same time" do
7
6
  before(:all) do
8
- class TestingRoleplayersContext < DCI::Context
7
+ class TestingMultiplayersContext < DCI::Context
9
8
  role :role1 do
10
9
  def role1method1; :role1method1_executed end
11
10
  def role1self; self end
@@ -71,14 +70,15 @@ describe 'Role MultiPlayers' do
71
70
 
72
71
  end
73
72
 
74
- @player1, @player2, @player22 = OpenStruct.new(:name => 'player1'), OpenStruct.new(:name => 'player2'), OpenStruct.new(:name => 'player22')
73
+ Player = Struct.new(:name)
74
+ @player1, @player2, @player22 = Player.new('player1'), Player.new('player2'), Player.new('player22')
75
75
  @multiplayer = DCI::Multiplayer[@player2, @player22]
76
76
  #puts @multiplayer.instance_variable_get(:@players)
77
- @testing_roleplayers_context = TestingRoleplayersContext.new(:role1 => @player1,
78
- :role2 => @multiplayer,
79
- :setting1 => :one,
80
- :setting2 => :two,
81
- :setting3 => :three)
77
+ @testing_roleplayers_context = TestingMultiplayersContext.new(:role1 => @player1,
78
+ :role2 => @multiplayer,
79
+ :setting1 => :one,
80
+ :setting2 => :two,
81
+ :setting3 => :three)
82
82
  end
83
83
 
84
84
  it("For it to work, the developer has to give a rolekey the value of a DCI::Multiplayer instance when instantiating the context...") do
@@ -112,14 +112,12 @@ describe 'Role MultiPlayers' do
112
112
  @player22.private_methods.map(&:to_s).should_not include(:private_role2method2)
113
113
  @player2.name.should eq('player2')
114
114
  @player22.name.should eq('player22')
115
- @player2.should_not respond_to(:role1)
116
- @player22.should_not respond_to(:role1)
117
- @player2.should_not respond_to(:context)
118
- @player22.should_not respond_to(:context)
119
- @player2.should_not respond_to(:settings)
120
- @player22.should_not respond_to(:settings)
121
- (@player2.private_methods.map(&:to_s) & ['role1', 'context', 'settings']).should be_empty
122
- (@player22.private_methods.map(&:to_s) & ['role1', 'context', 'settings']).should be_empty
115
+ expect{@player2.send(:role1)}.to raise_error
116
+ expect{@player22.send(:role1)}.to raise_error
117
+ @player2.send(:context).should be_nil
118
+ @player22.send(:context).should be_nil
119
+ expect{@player2.send(:settings)}.to raise_error
120
+ expect{@player22.send(:settings)}.to raise_error
123
121
  end
124
122
 
125
123
  end
@@ -1,5 +1,4 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
- require 'ostruct'
3
2
 
4
3
  describe 'RolePlayers' do
5
4
 
@@ -39,7 +38,7 @@ describe 'RolePlayers' do
39
38
  role1.name
40
39
  end
41
40
 
42
- def check_role1_context_access
41
+ def check_role1_private_context_access
43
42
  [!role1.respond_to?(:context), role1.private_methods.map(&:to_s).include?('context'), role1.send(:context) == self].uniq == [true]
44
43
  end
45
44
 
@@ -49,7 +48,8 @@ describe 'RolePlayers' do
49
48
  end
50
49
  end
51
50
 
52
- @player1, @player2 = OpenStruct.new(:name => 'player1'), OpenStruct.new(:name => 'player2')
51
+ Player = Struct.new(:name)
52
+ @player1, @player2 = Player.new('player1'), Player.new('player2')
53
53
  @testing_roleplayers_context = TestingRoleplayersContext.new(:role1 => @player1,
54
54
  :role2 => @player2,
55
55
  :setting1 => :one,
@@ -74,8 +74,8 @@ describe 'RolePlayers' do
74
74
  it("Inside the context, roleplayers have private access to other roleplayers through methods named after their keys.") do
75
75
  @testing_roleplayers_context.check_role_interaccess.should be_true
76
76
  end
77
- it("...and offer a public_method to access the context.") do
78
- @testing_roleplayers_context.check_role1_context_access.should be_true
77
+ it("...as well as private access to the context.") do
78
+ @testing_roleplayers_context.check_role1_private_context_access.should be_true
79
79
  end
80
80
 
81
81
  it("They also have private access to extra args received in the instantiation of its context...") do
@@ -95,14 +95,11 @@ describe 'RolePlayers' do
95
95
  @player1.should_not respond_to(:role1method1)
96
96
  @player2.private_methods.map(&:to_s).should_not include(:private_role2method2)
97
97
  @player1.name.should eq('player1')
98
- @player2.should_not respond_to(:role1)
99
- @player2.should_not respond_to(:context)
100
- @player2.should_not respond_to(:settings)
101
- (@player2.private_methods.map(&:to_s) & ['role1', 'context', 'settings']).should be_empty
98
+ @player2.send(:context).should be_nil
99
+ expect {@player2.send(:settings)}.to raise_error
100
+ expect {@player2.send(:role1)}.to raise_error
102
101
  end
103
102
 
104
-
105
-
106
103
  end
107
104
 
108
105
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drsi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorenzo Tello
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-13 00:00:00.000000000 Z
11
+ date: 2014-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec