game_machine 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +13 -9
  3. data/config/config.example.yml +8 -1
  4. data/config/game_messages.proto +1 -0
  5. data/config/messages.proto +4 -0
  6. data/games/example/lib/npc.rb +8 -7
  7. data/games/models.rb +3 -0
  8. data/games/models/clan_member.rb +8 -0
  9. data/games/models/clan_profile.rb +9 -0
  10. data/games/models/player.rb +7 -0
  11. data/games/plugins.rb +1 -0
  12. data/games/plugins/team_handler.rb +49 -0
  13. data/games/preload.rb +5 -0
  14. data/java/build.gradle +3 -1
  15. data/java/gradle.properties +1 -1
  16. data/lib/game_machine/actor/base.rb +5 -1
  17. data/lib/game_machine/application.rb +13 -3
  18. data/lib/game_machine/commands/datastore_commands.rb +3 -7
  19. data/lib/game_machine/commands/player_commands.rb +3 -0
  20. data/lib/game_machine/data_store.rb +22 -1
  21. data/lib/game_machine/data_stores/couchbase.rb +18 -1
  22. data/lib/game_machine/endpoints/udp_incoming.rb +0 -1
  23. data/lib/game_machine/game_systems.rb +2 -0
  24. data/lib/game_machine/game_systems/chat.rb +33 -13
  25. data/lib/game_machine/game_systems/chat_manager.rb +24 -3
  26. data/lib/game_machine/game_systems/json_model_persistence.rb +23 -0
  27. data/lib/game_machine/game_systems/region_manager.rb +1 -1
  28. data/lib/game_machine/game_systems/team_manager.rb +421 -0
  29. data/lib/game_machine/handlers/request.rb +4 -0
  30. data/lib/game_machine/model.rb +59 -13
  31. data/lib/game_machine/models.rb +17 -0
  32. data/lib/game_machine/models/create_team.rb +9 -0
  33. data/lib/game_machine/models/destroy_team.rb +8 -0
  34. data/lib/game_machine/models/echo_test.rb +6 -0
  35. data/lib/game_machine/models/end_match.rb +7 -0
  36. data/lib/game_machine/models/find_match.rb +7 -0
  37. data/lib/game_machine/models/join_team.rb +7 -0
  38. data/lib/game_machine/models/leave_team.rb +7 -0
  39. data/lib/game_machine/models/match.rb +9 -0
  40. data/lib/game_machine/models/player_team.rb +9 -0
  41. data/lib/game_machine/models/start_match.rb +8 -0
  42. data/lib/game_machine/models/team.rb +11 -0
  43. data/lib/game_machine/models/team_accept_invite.rb +9 -0
  44. data/lib/game_machine/models/team_invite.rb +8 -0
  45. data/lib/game_machine/models/team_joined.rb +7 -0
  46. data/lib/game_machine/models/team_left.rb +7 -0
  47. data/lib/game_machine/models/teams.rb +7 -0
  48. data/lib/game_machine/models/teams_request.rb +6 -0
  49. data/lib/game_machine/object_db.rb +17 -11
  50. data/lib/game_machine/version.rb +1 -1
  51. data/lib/game_machine/write_behind_cache.rb +1 -1
  52. data/spec/actor/actor_spec.rb +5 -5
  53. data/spec/actor/ref_spec.rb +10 -10
  54. data/spec/client_manager_spec.rb +9 -9
  55. data/spec/commands/chat_commands_spec.rb +1 -1
  56. data/spec/commands/datastore_commands_spec.rb +10 -14
  57. data/spec/commands/navigation_commands_spec.rb +3 -3
  58. data/spec/commands/player_commands_spec.rb +1 -1
  59. data/spec/game_systems/agents/controller_spec.rb +6 -6
  60. data/spec/game_systems/chat_manager_spec.rb +1 -1
  61. data/spec/game_systems/chat_spec.rb +15 -17
  62. data/spec/game_systems/region_manager_spec.rb +31 -21
  63. data/spec/game_systems/team_manager_spec.rb +291 -0
  64. data/spec/grid_spec.rb +11 -1
  65. data/spec/handlers/authentication_spec.rb +1 -10
  66. data/spec/handlers/game_spec.rb +2 -2
  67. data/spec/handlers/request_spec.rb +10 -21
  68. data/spec/hashring_spec.rb +11 -11
  69. data/spec/java_grid_spec.rb +1 -1
  70. data/spec/misc_spec.rb +2 -2
  71. data/spec/model_spec.rb +26 -14
  72. data/spec/write_behind_cache_spec.rb +15 -15
  73. metadata +29 -8
  74. data/lib/game_machine/bot/chat.rb +0 -66
  75. data/lib/game_machine/bot/client.rb +0 -54
  76. data/spec/data_stores/mapdb_spec.rb +0 -46
  77. data/spec/data_stores/redis_spec.rb +0 -44
@@ -55,7 +55,7 @@ module GameMachine
55
55
 
56
56
  let(:client_connection) do
57
57
  MessageLib::ClientConnection.new.set_id(client_name).set_gateway('udp').
58
- set_server('server')
58
+ set_server('server').set_type('cluster')
59
59
  end
60
60
 
61
61
  let('local_player_message') do
@@ -91,7 +91,7 @@ module GameMachine
91
91
  subject.local_clients[client_name] = client_connection
92
92
  subject.players[player_id] = client_name
93
93
  subject.class.local_players[player_id] = true
94
- actor_ref.stub(:tell)
94
+ allow(actor_ref).to receive(:tell)
95
95
  expect(Actor::Base).to receive(:find).with(player_id).and_return(actor_ref)
96
96
  ClientManager.send_to_player(local_player_message)
97
97
  end
@@ -99,7 +99,7 @@ module GameMachine
99
99
  it "should send message to remote manager if player is remote" do
100
100
  subject.remote_clients[client_name] = actor_ref
101
101
  subject.players[player_id] = client_name
102
- ClientManager.stub(:find).and_return(actor_ref)
102
+ allow(ClientManager).to receive(:find).and_return(actor_ref)
103
103
  expect(actor_ref).to receive(:tell).with(remote_player_message)
104
104
  ClientManager.send_to_player(remote_player_message)
105
105
  end
@@ -108,30 +108,30 @@ module GameMachine
108
108
  describe "#process_client_event" do
109
109
  it "on connect should set players entry" do
110
110
  subject.on_receive(client_connected_event)
111
- expect(subject.players.has_key?(player_id)).to be_true
111
+ expect(subject.players.has_key?(player_id)).to be_truthy
112
112
  end
113
113
 
114
114
  it "on connect should set remote_clients entry" do
115
115
  subject.on_receive(client_connected_event)
116
- expect(subject.remote_clients.has_key?(client_name)).to be_true
116
+ expect(subject.remote_clients.has_key?(client_name)).to be_truthy
117
117
  end
118
118
 
119
119
  it "on disconnect event should remove remote client reference" do
120
120
  subject.remote_clients[client_name] = true
121
121
  subject.on_receive(client_disconnected_event)
122
- expect(subject.remote_clients.has_key?(client_name)).to be_false
122
+ expect(subject.remote_clients.has_key?(client_name)).to be_falsey
123
123
  end
124
124
  end
125
125
 
126
126
  describe "#register_sender" do
127
127
  it "should add local client entry if client" do
128
128
  subject.on_receive(client_register)
129
- expect(subject.local_clients.has_key?(client_name)).to be_true
129
+ expect(subject.local_clients.has_key?(client_name)).to be_truthy
130
130
  end
131
131
 
132
132
  it "should add local actor entry if actor" do
133
133
  subject.on_receive(actor_register)
134
- expect(subject.local_actors.has_key?(actor_name)).to be_true
134
+ expect(subject.local_actors.has_key?(actor_name)).to be_truthy
135
135
  end
136
136
 
137
137
  it "client register should call send_client_event" do
@@ -155,7 +155,7 @@ module GameMachine
155
155
  it "removes local client reference" do
156
156
  subject.local_clients[client_name] = true
157
157
  subject.on_receive(client_unregister)
158
- expect(subject.local_clients.has_key?(client_name)).to be_false
158
+ expect(subject.local_clients.has_key?(client_name)).to be_falsey
159
159
  end
160
160
 
161
161
  it "should not send client event for local connenction" do
@@ -9,7 +9,7 @@ module GameMachine
9
9
  subject{ChatCommands.new}
10
10
 
11
11
  before(:each) do
12
- GameSystems::ChatManager.stub(:find).and_return(actor_ref)
12
+ allow(GameSystems::ChatManager).to receive(:find).and_return(actor_ref)
13
13
  end
14
14
 
15
15
  describe "#send_private_message" do
@@ -20,11 +20,11 @@ module GameMachine
20
20
  subject{DatastoreCommands.new}
21
21
 
22
22
  before(:each) do
23
- subject.define_dbproc(:test1) do |current_entity,update_entity|
23
+ subject.define_dbproc(:test1) do |id,current_entity,update_entity|
24
24
  expect(update_entity).to eql(entity)
25
25
  current_entity
26
26
  end
27
- subject.define_dbproc(:test2) do |current_entity,update_entity|
27
+ subject.define_dbproc(:test2) do |id,current_entity,update_entity|
28
28
  current_entity.set_entity_type('dunno')
29
29
  current_entity
30
30
  end
@@ -32,7 +32,7 @@ module GameMachine
32
32
 
33
33
  describe "#put" do
34
34
  it "sets the value" do
35
- subject.put(entity).should be_true
35
+ expect(subject.put(entity)).to be_truthy
36
36
  end
37
37
  end
38
38
 
@@ -42,8 +42,8 @@ module GameMachine
42
42
  sleep 0.100
43
43
 
44
44
  result = subject.call_dbproc(:test1, entity.get_id,entity,true)
45
- result.should be_kind_of(MessageLib::Entity)
46
- result.get_id.should == entity.get_id
45
+ expect(result).to be_kind_of(MessageLib::Entity)
46
+ expect(result.get_id).to eq(entity.get_id)
47
47
  end
48
48
 
49
49
  it "returns updated entity" do
@@ -51,31 +51,27 @@ module GameMachine
51
51
  sleep 0.100
52
52
 
53
53
  result = subject.call_dbproc(:test2, entity.get_id,entity,true)
54
- result.get_entity_type.should == 'dunno'
54
+ expect(result.get_entity_type).to eq('dunno')
55
55
  end
56
56
 
57
57
  it "returns true when called with blocking=false" do
58
58
  subject.put(entity)
59
59
  sleep 0.100
60
60
 
61
- subject.call_dbproc(:test1, entity.get_id,entity,false).should be_true
61
+ expect(subject.call_dbproc(:test1, entity.get_id,entity,false)).to be_truthy
62
62
  end
63
63
 
64
- it "if called with an entity id that does not exist, it creates it" do
65
- returned_entity = subject.call_dbproc(:test1, 'blah',entity,true)
66
- expect(returned_entity.id).to eql('blah')
67
- end
68
64
  end
69
65
 
70
66
  describe "#get" do
71
67
  it "should return false if object does not exist" do
72
- subject.get('xx').should be_false
68
+ expect(subject.get('xx')).to be_falsey
73
69
  end
74
70
 
75
71
  it "should return object if exists" do
76
72
  subject.put(entity)
77
73
  sleep 0.100
78
- subject.get('1').should == entity
74
+ expect(subject.get('1')).to eq(entity)
79
75
  end
80
76
  end
81
77
 
@@ -83,7 +79,7 @@ module GameMachine
83
79
  it "removes the entity from the data store" do
84
80
  subject.put(entity)
85
81
  subject.delete(entity.id)
86
- subject.get('1').should be_false
82
+ expect(subject.get('1')).to be_falsey
87
83
  end
88
84
  end
89
85
  end
@@ -11,9 +11,9 @@ module GameMachine
11
11
  subject{NavigationCommands.new}
12
12
 
13
13
  before(:each) do
14
- Navigation::DetourPath.stub(:query_ref).and_return(query_ref)
15
- Navigation::DetourNavmesh.stub(:create).and_return(navmesh)
16
- Navigation::DetourNavmesh.stub(:find).and_return(navmesh)
14
+ allow(Navigation::DetourPath).to receive(:query_ref).and_return(query_ref)
15
+ allow(Navigation::DetourNavmesh).to receive(:create).and_return(navmesh)
16
+ allow(Navigation::DetourNavmesh).to receive(:find).and_return(navmesh)
17
17
  end
18
18
 
19
19
  describe "#load_navmesh" do
@@ -22,7 +22,7 @@ module GameMachine
22
22
  describe "player" do
23
23
  describe "#send_message" do
24
24
  before(:each) do
25
- ClientManager.stub(:find).and_return(actor_ref)
25
+ allow(ClientManager).to receive(:find).and_return(actor_ref)
26
26
  end
27
27
 
28
28
  it "sends component to player wrapped in entity" do
@@ -58,21 +58,21 @@ module GameMachine
58
58
  my_agents = agents.clone
59
59
  my_agents.delete(:agent1)
60
60
  my_agents.delete(:agent2)
61
- subject.stub(:local_agents).and_return(my_agents)
61
+ allow(subject).to receive(:local_agents).and_return(my_agents)
62
62
  subject.update_agents
63
63
  expect(subject.children.size).to eq 3
64
- expect(subject.children.has_key?(:agent1)).to be_false
65
- expect(subject.children.has_key?(:agent2)).to be_false
64
+ expect(subject.children.has_key?(:agent1)).to be_falsey
65
+ expect(subject.children.has_key?(:agent2)).to be_falsey
66
66
  end
67
67
 
68
68
  it "should create new children it becomes responsible for" do
69
69
  my_agents = agents.clone
70
70
  my_agents[:agent6] = 'TestAgent'
71
- subject.stub(:agent_config).and_return(my_agents)
72
- subject.stub(:local_agents).and_return(my_agents)
71
+ allow(subject).to receive(:agent_config).and_return(my_agents)
72
+ allow(subject).to receive(:local_agents).and_return(my_agents)
73
73
  subject.update_agents
74
74
  expect(subject.children.size).to eq 6
75
- expect(subject.children.has_key?(:agent6)).to be_true
75
+ expect(subject.children.has_key?(:agent6)).to be_truthy
76
76
  end
77
77
  end
78
78
 
@@ -25,7 +25,7 @@ module GameMachine
25
25
  game_message.to_entity
26
26
  end
27
27
 
28
- let(:actor_builder) {mock('Actor::Builder', :with_parent => actor_builder, :start => true)}
28
+ let(:actor_builder) {double('Actor::Builder', :with_parent => actor_builder, :start => true)}
29
29
 
30
30
  let(:chat_invite) do
31
31
  chat = Commands::ChatCommands.new
@@ -60,9 +60,9 @@ module GameMachine
60
60
  end
61
61
 
62
62
  before(:each) do
63
- Chat.any_instance.stub(:load_state)
63
+ allow_any_instance_of(Chat).to receive(:load_state)
64
64
  Actor::Builder.new(GameSystems::Chat,player_id).start
65
- MessageQueue.stub(:find).and_return(actor_ref)
65
+ allow(MessageQueue).to receive(:find).and_return(actor_ref)
66
66
  commands.datastore.delete("chat_topic_#{topic}")
67
67
  end
68
68
 
@@ -76,7 +76,7 @@ module GameMachine
76
76
  end
77
77
 
78
78
  it "joining my own channel should work" do
79
- channel_name = "priv/#{player_id}/test"
79
+ channel_name = "priv_#{player_id}_test"
80
80
  join_message = commands.chat.join_message(channel_name,player_id)
81
81
  expect(subject).to receive(:join_channel)
82
82
  subject.on_receive(join_message)
@@ -101,10 +101,8 @@ module GameMachine
101
101
 
102
102
  describe "subscribers list updates" do
103
103
  it "joining a channel adds player to subscriber list" do
104
+ expect(subject).to receive(:add_subscriber)
104
105
  subject.on_receive(join_request)
105
- subscribers = Chat.subscribers_for_topic(topic)
106
- expect(subscribers.get_subscriber_id_count).to eql(1)
107
- expect(subscribers.get_subscriber_id_list.first).to eql(player_id)
108
106
  subject.on_receive(leave_request)
109
107
  end
110
108
 
@@ -120,7 +118,7 @@ module GameMachine
120
118
  it "joining a channel with flags should persist the flag" do
121
119
  subject.on_receive(join_request_with_flags)
122
120
  topic_flags = subject.get_flags.fetch(topic,[])
123
- topic_flags.include?('subscribers').should be_true
121
+ expect(topic_flags.include?('subscribers')).to be_truthy
124
122
  end
125
123
 
126
124
  it "chat status should add subscribers to channel if flag is set" do
@@ -135,46 +133,46 @@ module GameMachine
135
133
  it "joining a channel should persist subscriptions" do
136
134
  subject.on_receive(join_request_with_flags)
137
135
  subscriptions = subject.get_subscriptions
138
- subscriptions.include?(topic).should be_true
136
+ expect(subscriptions.include?(topic)).to be_truthy
139
137
  end
140
138
  end
141
139
 
142
140
  describe "joining and leaving channels" do
143
141
  it "processes the entity as a leave channel request" do
144
- subject.should_receive(:leave_channels)
142
+ expect(subject).to receive(:leave_channels)
145
143
  subject.on_receive(leave_request)
146
144
  end
147
145
 
148
146
  it "sends an unsubscribe message to message queue" do
149
147
  subject.on_receive(join_request)
150
- actor_ref.should_receive(:tell).with(kind_of(MessageLib::Unsubscribe),anything())
148
+ expect(actor_ref).to receive(:tell).with(kind_of(MessageLib::Unsubscribe),anything())
151
149
  subject.on_receive(leave_request)
152
150
  end
153
151
 
154
152
  it "processes the entity as a join channel request" do
155
- subject.should_receive(:join_channels)
153
+ expect(subject).to receive(:join_channels)
156
154
  subject.on_receive(join_request)
157
155
  end
158
156
 
159
157
  it "sends a subscribe message to message queue" do
160
- actor_ref.should_receive(:tell).exactly(1).times
158
+ expect(actor_ref).to receive(:tell).exactly(1).times
161
159
  subject.on_receive(join_request)
162
160
  end
163
161
 
164
162
  it "processes multiple requests in a single message" do
165
- subject.should_receive(:join_channels).once
166
- subject.should_receive(:leave_channels).once
167
- subject.should_receive(:send_message).once
163
+ expect(subject).to receive(:join_channels).once
164
+ expect(subject).to receive(:leave_channels).once
165
+ expect(subject).to receive(:send_message).once
168
166
  subject.on_receive(all_requests)
169
167
  end
170
168
 
171
169
  it "send a private chat message" do
172
- subject.should_receive(:send_private_message).once
170
+ expect(subject).to receive(:send_private_message).once
173
171
  subject.on_receive(private_chat_request)
174
172
  end
175
173
 
176
174
  it "send a group chat message" do
177
- subject.should_receive(:send_group_message).once
175
+ expect(subject).to receive(:send_group_message).once
178
176
  subject.on_receive(public_chat_request)
179
177
  end
180
178
 
@@ -2,6 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  module GameMachine
4
4
  module GameSystems
5
+ include Models
5
6
 
6
7
  class Zone2Manager < Actor::Base
7
8
  def on_receive(message);end
@@ -42,14 +43,20 @@ module GameMachine
42
43
  ref.underlying_actor
43
44
  end
44
45
 
46
+ before(:each) do
47
+ allow_any_instance_of(RegionManager).to receive(:post_init)
48
+ subject.regions = {}
49
+ subject.servers = {}
50
+ end
51
+
45
52
  describe "#assign_servers" do
46
53
  it "should assign servers to regions that have none" do
47
- subject.stub(:regions).and_return(regions)
48
- ClusterMonitor.stub(:cluster_members).and_return(cluster_members)
54
+ allow(subject).to receive(:regions).and_return(regions)
55
+ allow(ClusterMonitor).to receive(:cluster_members).and_return(cluster_members)
49
56
  expect(region1).to receive(:server=)
50
57
  expect(region2).to receive(:server=)
51
- expect(region1).to receive(:save)
52
- expect(region2).to receive(:save)
58
+ expect(region1).to receive(:save!)
59
+ expect(region2).to receive(:save!)
53
60
  subject.assign_servers
54
61
  expect(subject.servers[server1_address]).to eq region1.name
55
62
  expect(subject.servers[server2_address]).to eq region2.name
@@ -59,9 +66,9 @@ module GameMachine
59
66
 
60
67
  describe "#notify_managers" do
61
68
  it "should send message to manager of each region" do
62
- subject.stub(:regions).and_return(regions)
63
- region1.stub(:server).and_return(server1_address)
64
- region2.stub(:server).and_return(server2_address)
69
+ allow(subject).to receive(:regions).and_return(regions)
70
+ allow(region1).to receive(:server).and_return(server1_address)
71
+ allow(region2).to receive(:server).and_return(server2_address)
65
72
  expect(GameSystems::Zone1Manager).to receive(:find_by_address).
66
73
  with(server1_address). and_return(actor_ref)
67
74
  expect(GameSystems::Zone2Manager).to receive(:find_by_address).
@@ -73,25 +80,27 @@ module GameMachine
73
80
  describe "#load_from_config" do
74
81
 
75
82
  it "should create new regions that do not exist" do
76
- expect(Models::Region).to receive(:find).
77
- with('zone1',5000).and_return(nil)
78
- expect(Models::Region).to receive(:find).
79
- with('zone2',5000).and_return(nil)
83
+ expect(Region).to receive(:find!).
84
+ with('zone1').and_return(nil)
85
+ expect(Region).to receive(:find!).
86
+ with('zone2').and_return(nil)
80
87
 
81
- expect(Models::Region).to receive(:new).
88
+ expect(Region).to receive(:new).
82
89
  with(:id => 'zone1', :name => 'zone1', :manager => zone1_manager).and_return(region1)
83
- expect(Models::Region).to receive(:new).
90
+ expect(Region).to receive(:new).
84
91
  with(:id => 'zone2', :name => 'zone2', :manager => zone2_manager).and_return(region2)
85
92
 
86
- expect(region1).to receive(:save)
87
- expect(region2).to receive(:save)
93
+ expect(region1).to receive(:save!)
94
+ expect(region2).to receive(:save!)
95
+ subject.load_from_config
88
96
  expect(subject.regions['zone1']).to eq region1
89
97
  expect(subject.regions['zone2']).to eq region2
90
98
  end
91
99
 
92
100
  it "should load regions that exist" do
93
- expect(Models::Region).to receive(:find).with('zone1',5000).and_return(region1)
94
- expect(Models::Region).to receive(:find).with('zone2',5000).and_return(region2)
101
+ expect(Region).to receive(:find!).with('zone1').and_return(region1)
102
+ expect(Region).to receive(:find!).with('zone2').and_return(region2)
103
+ subject.load_from_config
95
104
  expect(subject.regions['zone1']).to eq region1
96
105
  expect(subject.regions['zone2']).to eq region2
97
106
  end
@@ -101,7 +110,7 @@ module GameMachine
101
110
 
102
111
  context "nodes have not changed" do
103
112
  it "should not unassign servers from regions" do
104
- ClusterMonitor.stub(:cluster_members).and_return(cluster_members)
113
+ allow(ClusterMonitor).to receive(:cluster_members).and_return(cluster_members)
105
114
  subject.unassign_down_servers
106
115
  expect(region1).to_not receive(:save)
107
116
  expect(region2).to_not receive(:save)
@@ -111,9 +120,10 @@ module GameMachine
111
120
  context "node has been downed" do
112
121
 
113
122
  before(:each) do
123
+ subject.load_from_config
114
124
  subject.regions['zone1'].server = server2_address
115
125
  subject.servers[server2_address] = 'zone1'
116
- ClusterMonitor.stub(:cluster_members).and_return(cluster_with_down_members)
126
+ allow(ClusterMonitor).to receive(:cluster_members).and_return(cluster_with_down_members)
117
127
  end
118
128
 
119
129
  it "should set region server to nil" do
@@ -123,11 +133,11 @@ module GameMachine
123
133
 
124
134
  it "should remove servers entry" do
125
135
  subject.unassign_down_servers
126
- expect(subject.servers.has_key?(server2_address)).to be_false
136
+ expect(subject.servers.has_key?(server2_address)).to be_falsey
127
137
  end
128
138
 
129
139
  it "should save region" do
130
- expect(subject.regions['zone1']).to receive(:save)
140
+ expect(subject.regions['zone1']).to receive(:save!)
131
141
  subject.unassign_down_servers
132
142
  end
133
143
  end
@@ -0,0 +1,291 @@
1
+ require 'spec_helper'
2
+
3
+ module GameMachine
4
+ module GameSystems
5
+ include Models
6
+
7
+ describe TeamManager do
8
+
9
+ let(:actor_ref) {double('Actor::Ref', :tell => true)}
10
+
11
+ let(:player_id) {'player'}
12
+ let(:player2) {'player2'}
13
+ let(:team_name) {'team1'}
14
+ let(:team_name2) {'team2'}
15
+ let(:team_members) {[player_id]}
16
+ let(:invite_id) {'invite'}
17
+
18
+ let(:team) do
19
+ Team.new(:owner => player_id, :name => team_name, :access => 'public', :members => team_members)
20
+ end
21
+
22
+ let(:team_two_members) do
23
+ Team.new(:owner => player_id, :name => team_name2, :access => 'public', :members => [player_id,player2])
24
+ end
25
+
26
+ let(:team_invite) do
27
+ TeamInvite.new(:name => team_name, :invitee => player_id)
28
+ end
29
+
30
+ let(:team_accept_invite) do
31
+ TeamAcceptInvite.new(:name => team_name, :invitee => player2, :invite_id => invite_id)
32
+ end
33
+
34
+ let(:private_team) do
35
+ Team.new(:owner => player_id, :name => team_name, :access => 'private', :members => team_members)
36
+ end
37
+
38
+ let(:destroy_team) do
39
+ DestroyTeam.new(:name => team_name)
40
+ end
41
+
42
+ let(:leave_team) do
43
+ LeaveTeam.new(:name => team_name)
44
+ end
45
+
46
+ let(:join_team) do
47
+ JoinTeam.new(:name => team_name)
48
+ end
49
+
50
+ let(:create_team) do
51
+ CreateTeam.new(:owner => player_id, :name => team_name, :access => 'public')
52
+ end
53
+
54
+ let(:create_private_team) do
55
+ CreateTeam.new(:owner => player_id, :name => team_name, :access => 'private')
56
+ end
57
+
58
+ let(:start_match) do
59
+ StartMatch.new(:game_handler => nil, :team_names => [team_name,team_name2])
60
+ end
61
+
62
+ let(:end_match) do
63
+ EndMatch.new(:match_id => 'team1_team2')
64
+ end
65
+
66
+ let(:match) {double('Match')}
67
+ let(:actor_ref) {double('ActorRef', :tell => true)}
68
+
69
+ subject do
70
+ ref = Actor::Builder.new(TeamManager).with_name('team_manager_test').test_ref
71
+ ref.underlying_actor
72
+ end
73
+
74
+ before(:each) do
75
+ allow_any_instance_of(Commands::PlayerCommands).to receive(:send_message).and_return(true)
76
+ allow_any_instance_of(Commands::MiscCommands).to receive(:client_manager_register).and_return(true)
77
+ end
78
+
79
+ describe "#create_team" do
80
+
81
+ before(:each) do
82
+ allow(subject).to receive(:send_team_joined).and_return(true)
83
+ end
84
+
85
+ it "should create a team" do
86
+ allow(Team).to receive(:find!).and_return(nil)
87
+ expect_any_instance_of(Team).to receive(:save!)
88
+ subject.on_receive(create_team)
89
+ end
90
+
91
+ it "should send chat join request" do
92
+ allow(Team).to receive(:find!).and_return(nil)
93
+ expect(subject).to receive(:join_chat).with(team_name,player_id)
94
+ subject.on_receive(create_team)
95
+ end
96
+
97
+ it "should not create a team if one exists" do
98
+ allow(Team).to receive(:find!).and_return(team)
99
+ expect(subject.create_team(create_team)).to be_falsey
100
+ end
101
+
102
+ it "should create invite id if private team" do
103
+ allow(Team).to receive(:find!).and_return(nil)
104
+ allow(subject).to receive(:team_id).and_return 'team_id'
105
+ expect(Uniqueid).to receive(:generate_token).with(team_name).and_return invite_id
106
+ subject.on_receive(create_private_team)
107
+ end
108
+ end
109
+
110
+ describe "#destroy_team" do
111
+ before(:each) do
112
+ allow(Team).to receive(:find!).and_return(team)
113
+ end
114
+
115
+ it "should delete the team" do
116
+ destroy_team.player_id = player_id
117
+ expect(team).to receive(:destroy!)
118
+ subject.on_receive(destroy_team)
119
+ end
120
+
121
+ it "should send a leave_chat for each member" do
122
+ destroy_team.player_id = player_id
123
+ expect(subject).to receive(:leave_chat).with(team.name,player_id)
124
+ subject.on_receive(destroy_team)
125
+ end
126
+
127
+ it "should destroy player_team" do
128
+ destroy_team.player_id = player_id
129
+ expect(subject).to receive(:destroy_player_team).with(player_id)
130
+ subject.on_receive(destroy_team)
131
+ end
132
+
133
+ it "should not delete the team if request is from non owner" do
134
+ destroy_team.player_id = player2
135
+ expect(team).to_not receive(:destroy!)
136
+ subject.on_receive(destroy_team)
137
+ end
138
+ end
139
+
140
+ describe "#leave_team" do
141
+
142
+ before(:each) do
143
+ allow(Team).to receive(:find!).and_return(team)
144
+ end
145
+
146
+ it "should destroy team if last member leaves" do
147
+ leave_team.player_id = player_id
148
+ expect(subject).to receive(:destroy_team)
149
+ expect(subject).to receive(:destroy_on_owner_leave?).and_return(false)
150
+ subject.on_receive(leave_team)
151
+ end
152
+
153
+ it "should reassign owner if owner leaves" do
154
+ allow(Team).to receive(:find!).and_return(team_two_members)
155
+ leave_team.player_id = player_id
156
+ allow(subject).to receive(:destroy_on_owner_leave?).and_return(false)
157
+ subject.on_receive(leave_team)
158
+ expect(team_two_members.owner).to eql(player2)
159
+ end
160
+
161
+ it "should remove member from team" do
162
+ leave_team.player_id = player2
163
+ subject.on_receive(leave_team)
164
+ expect(team.members.include?(player2)).to be_falsey
165
+ end
166
+
167
+ it "should destroy team if player is owner" do
168
+ leave_team.player_id = player_id
169
+ expect(subject).to receive(:destroy_team)
170
+ subject.on_receive(leave_team)
171
+ end
172
+
173
+ it "should call destroy_player_team with player id" do
174
+ leave_team.player_id = player_id
175
+ expect(subject).to receive(:destroy_player_team).with(player_id)
176
+ subject.on_receive(leave_team)
177
+ end
178
+ end
179
+
180
+ describe "#join_team" do
181
+
182
+ before(:each) do
183
+ allow(Team).to receive(:find!).and_return(team)
184
+ end
185
+
186
+ it "should add member to the team" do
187
+ join_team.player_id = player2
188
+ expect(team.members).to receive(:<<).with(player2)
189
+ subject.on_receive(join_team)
190
+ end
191
+
192
+ it "should send join_chat request for member" do
193
+ join_team.player_id = player2
194
+ expect(subject).to receive(:join_chat).with(team.name,player2)
195
+ subject.on_receive(join_team)
196
+ end
197
+
198
+ it "should not add member to team if access is private" do
199
+ allow(Team).to receive(:find!).and_return(private_team)
200
+ expect(subject).to_not receive(:create_player_team)
201
+ subject.on_receive(join_team)
202
+ end
203
+
204
+ it "should not join the same player twice" do
205
+ join_team.player_id = player2
206
+ subject.on_receive(join_team)
207
+ expect(team.members).to_not receive(:<<).with(player2)
208
+ subject.on_receive(join_team)
209
+ end
210
+
211
+ it "should resend team_joined message" do
212
+ join_team.player_id = player2
213
+ subject.on_receive(join_team)
214
+ expect(subject).to receive(:send_team_joined)
215
+ subject.on_receive(join_team)
216
+ end
217
+ end
218
+
219
+ describe "#team_invite" do
220
+
221
+ it "should forward the invite message to the invitee" do
222
+ allow(Team).to receive(:find!).and_return(team)
223
+ team_invite.player_id = player_id
224
+ expect_any_instance_of(Commands::PlayerCommands).to receive(:send_message)
225
+ subject.on_receive(team_invite)
226
+ end
227
+
228
+ it "should set the invite id to the team invite id" do
229
+ allow(Team).to receive(:find!).and_return(team)
230
+ team_invite.player_id = player_id
231
+ team.invite_id = 'blah'
232
+ expect(team_invite).to receive("invite_id=").with('blah')
233
+ subject.on_receive(team_invite)
234
+ end
235
+ end
236
+
237
+ describe "team_accept_invite" do
238
+
239
+ it "should add member to team with correct invite id" do
240
+ allow(Team).to receive(:find!).and_return(team)
241
+ team_invite.player_id = player_id
242
+ team.invite_id = invite_id
243
+ expect(subject).to receive(:join_team)
244
+ subject.on_receive(team_accept_invite)
245
+ end
246
+
247
+ it "should not add member to team with incorrect invite id" do
248
+ allow(Team).to receive(:find!).and_return(team)
249
+ team_invite.player_id = player_id
250
+ team.invite_id = 'bad invite'
251
+ expect(subject).to_not receive(:join_team)
252
+ subject.on_receive(team_accept_invite)
253
+ end
254
+ end
255
+
256
+ describe "#start_match" do
257
+ it "should create the match" do
258
+ allow(Team).to receive(:find!).with('team1').and_return(team)
259
+ allow(Team).to receive(:find!).with('team2').and_return(team_two_members)
260
+ expect(match).to receive(:save)
261
+ expect(Match).to receive(:new).
262
+ with(
263
+ :id => 'team1_team2',
264
+ :teams => [team,team_two_members],
265
+ :server => 'localhost',
266
+ :game_handler => nil
267
+ ).and_return(match)
268
+ subject.on_receive(start_match)
269
+ end
270
+
271
+ it "should save teams with match id" do
272
+ allow(Team).to receive(:find!).with('team1').and_return(team)
273
+ allow(Team).to receive(:find!).with('team2').and_return(team_two_members)
274
+ expect(team).to receive(:match_id=).with('team1_team2')
275
+ expect(team_two_members).to receive(:match_id=).with('team1_team2')
276
+ subject.on_receive(start_match)
277
+ end
278
+ end
279
+
280
+ describe "#end_match" do
281
+ it "should destroy the match" do
282
+ allow(match).to receive(:teams).and_return([team,team_two_members])
283
+ expect(Match).to receive(:find!).with('team1_team2').and_return(match)
284
+ expect(match).to receive(:destroy)
285
+ subject.on_receive(end_match)
286
+ end
287
+ end
288
+
289
+ end
290
+ end
291
+ end