Class: GameMachine::GameSystems::TeamManager

Inherits:
Actor::Base
  • Object
show all
Includes:
Commands, Models
Defined in:
lib/game_machine/game_systems/team_manager.rb

Constant Summary

Constants inherited from Actor::Base

Actor::Base::ON_RECEIVE_HOOKS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands

#commands

Methods inherited from Actor::Base

aspect, aspects, find, find_by_address, find_distributed, find_distributed_local, find_remote, hashring, local_path, model_filter, #onReceive, player_controller, #receive_message, #schedule_message, #sender, set_player_controller

Instance Attribute Details

#team_handlerObject (readonly)

Returns the value of attribute team_handler.



7
8
9
# File 'lib/game_machine/game_systems/team_manager.rb', line 7

def team_handler
  @team_handler
end

Class Method Details

.match_id_for(team_names) ⇒ Object



14
15
16
# File 'lib/game_machine/game_systems/team_manager.rb', line 14

def self.match_id_for(team_names)
  team_names.sort.join('_')
end

Instance Method Details

#can_add_member?(team_name, player_id) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/game_machine/game_systems/team_manager.rb', line 85

def can_add_member?(team_name,player_id)
  team_handler.can_add_member?(team_name, player_id)
end

#can_create_team?(team_name, player_id) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/game_machine/game_systems/team_manager.rb', line 89

def can_create_team?(team_name,player_id)
  team_handler.can_create_team?(team_name, player_id)
end

#chat_channel(topic, flags) ⇒ Object



18
19
20
# File 'lib/game_machine/game_systems/team_manager.rb', line 18

def chat_channel(topic,flags)
  MessageLib::ChatChannel.new.set_name(topic).set_flags(flags)
end

#create_player_team(team_name, player_id) ⇒ Object



50
51
52
53
# File 'lib/game_machine/game_systems/team_manager.rb', line 50

def create_player_team(team_name,player_id)
  player_team = PlayerTeam.new(:id => player_id, :name => team_name)
  player_team.save!
end

#create_team(message) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/game_machine/game_systems/team_manager.rb', line 113

def create_team(message)
  unless can_create_team?(message.name,message.player_id)
    return false
  end

  if team = Team.find!(message.name)
    send_team_joined(team.name,message.player_id)
    return false
  end

  message.max_members ||= default_max_members

  team = Team.new(
    :id => message.name,
    :team_id => team_id(message.name),
    :name => message.name,
    :owner => message.owner,
    :access => message.access,
    :max_members => message.max_members,
    :destroy_on_owner_leave => destroy_on_owner_leave?,
    :members => [message.owner]
  )

  create_player_team(team.name,message.player_id)
  if team.access == 'private'
    team.invite_id = Uniqueid.generate_token(team.name)
  end

  team.save!
  commands.datastore.call_dbproc(:team_add_team,'teams',team.to_storage_entity,false)
  join_chat(team.name,team.owner)
  send_team_joined(team.name,message.player_id)
  handler_update_teams
end

#default_max_membersObject



81
82
83
# File 'lib/game_machine/game_systems/team_manager.rb', line 81

def default_max_members
  100
end

#define_update_procsObject



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/game_machine/game_systems/team_manager.rb', line 367

def define_update_procs
  commands.datastore.define_dbproc(:team_update_team) do |id,current_entity,update_entity|
    if current_entity.nil?
      teams = Teams.new(:teams => [],:id => id)
    else
      teams = Teams.from_entity(current_entity,:json_storage)
    end

    team = Team.from_entity(update_entity,:json_storage)
    team_to_update = teams.teams.select {|t| t.name == team.name}.first
    if team_to_update
      teams.teams.delete_if {|t| t.name == team.name}
      teams.teams << team
    end
    teams.to_storage_entity
  end

  commands.datastore.define_dbproc(:team_add_team) do |id,current_entity,update_entity|
    if current_entity.nil?
      teams = Teams.new(:teams => [],:id => id)
    else
      teams = Teams.from_entity(current_entity,:json_storage)
    end

    team = Team.from_entity(update_entity,:json_storage)
    teams.teams.delete_if {|t| t.name == team.name}
    teams.teams << team
    teams.to_storage_entity
  end

  commands.datastore.define_dbproc(:team_remove_team) do |id,current_entity,update_entity|
    if current_entity.nil?
      teams = Teams.new(:teams => [],:id => id)
    else
      teams = Teams.from_entity(current_entity,:json_storage)
    end

    team = Team.from_entity(update_entity,:json_storage)
    teams.teams.delete_if {|t| t.name == team.name}
    teams.to_storage_entity
  end

end

#destroy_on_owner_leave?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/game_machine/game_systems/team_manager.rb', line 97

def destroy_on_owner_leave?
  team_handler.destroy_on_owner_leave?
end

#destroy_player_team(player_id) ⇒ Object



71
72
73
74
75
# File 'lib/game_machine/game_systems/team_manager.rb', line 71

def destroy_player_team(player_id)
  if player_team = PlayerTeam.find!(player_id)
    player_team.destroy!
  end
end

#destroy_team(message, force = false) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/game_machine/game_systems/team_manager.rb', line 158

def destroy_team(message,force=false)
  if team = Team.find!(message.name)
    if team.owner == message.player_id || force
      team.members.each do |member|
        leave_chat(message.name,member)
        destroy_player_team(member)
        send_team_left(team.name,member)
      end
      commands.datastore.call_dbproc(:team_remove_team,'teams',team.to_storage_entity,false)
      team.destroy!
      handler_update_teams

      # team destroyed ends match
      if team.match_id
        end_match(EndMatch.new(:match_id => team.match_id))
      end
    end
  end
  unless force
    send_team_left(message.name,message.player_id)
  end
end

#end_match(message) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/game_machine/game_systems/team_manager.rb', line 268

def end_match(message)
  if message.player_id
    return
  end

  if match = Match.find!(message.match_id)
    match.teams.each do |team|
      if team = Team.find!(team.name)
        team.match_id = nil
        team.save!
      end
    end
    match.destroy
  end
end

#find_match(message) ⇒ Object



325
326
327
328
329
330
331
332
# File 'lib/game_machine/game_systems/team_manager.rb', line 325

def find_match(message)
  if team = Team.find(message.team_name)
    if s = handler_find_match(message.team_name)
      GameMachine.logger.info "Match found: #{s}"
      start_match(s)
    end
  end
end

#flagsObject



26
27
28
# File 'lib/game_machine/game_systems/team_manager.rb', line 26

def flags
  'subscribers'
end

#handler_find_match(team_name) ⇒ Object



105
106
107
# File 'lib/game_machine/game_systems/team_manager.rb', line 105

def handler_find_match(team_name)
  team_handler.match!(team_name)
end

#handler_match_started(match) ⇒ Object



109
110
111
# File 'lib/game_machine/game_systems/team_manager.rb', line 109

def handler_match_started(match)
  team_handler.match_started(match)
end

#handler_teams_filter(teams, teams_request) ⇒ Object



101
102
103
# File 'lib/game_machine/game_systems/team_manager.rb', line 101

def handler_teams_filter(teams,teams_request)
  team_handler.teams_filter(teams,teams_request)
end

#handler_update_teamsObject



93
94
95
# File 'lib/game_machine/game_systems/team_manager.rb', line 93

def handler_update_teams
  team_handler.update_teams
end

#join_chat(topic, player_id) ⇒ Object



36
37
38
39
40
# File 'lib/game_machine/game_systems/team_manager.rb', line 36

def join_chat(topic,player_id)
  join_chat = MessageLib::JoinChat.new.add_chat_channel(chat_channel(topic,flags))
  entity = player_entity(player_id).set_join_chat(join_chat)
  ChatManager.find.tell(entity)
end

#join_team(message, invite_accepted = false) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/game_machine/game_systems/team_manager.rb', line 181

def join_team(message,invite_accepted=false)
  unless can_add_member?(message.name,message.player_id)
    return false
  end

  if team = Team.find!(message.name)

    if (team.max_members.nil? || team.max_members == 0)
      team.max_members = default_max_members
    end

    if team.members.size >= team.max_members
      return
    end

    if team.access == 'public' || invite_accepted
      unless team.members.include?(message.player_id)
        team.members << message.player_id
        team.save!
        create_player_team(team.name,message.player_id)
        commands.datastore.call_dbproc(:team_update_team,'teams',team.to_storage_entity,false)
      end
      join_chat(team.name,message.player_id)
      send_team_joined(message.name,message.player_id)
    end
  end
end

#leave_chat(topic, player_id) ⇒ Object



30
31
32
33
34
# File 'lib/game_machine/game_systems/team_manager.rb', line 30

def leave_chat(topic,player_id)
  leave_chat = MessageLib::LeaveChat.new.add_chat_channel(chat_channel(topic,flags))
  entity = player_entity(player_id).set_leave_chat(leave_chat)
  ChatManager.find.tell(entity)
end

#leave_team(message) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/game_machine/game_systems/team_manager.rb', line 209

def leave_team(message)
  if team = Team.find!(message.name)
    if message.player_id == team.owner && destroy_on_owner_leave?
      destroy_team(
        DestroyTeam.new(
          :name => team.name,
          :player_id => message.player_id
        )
      )
      return
    end


    # Last member, just destroy
    if team.members.include?(message.player_id) && team.members.size == 1
      destroy_team(
        DestroyTeam.new(
          :name => team.name,
          :player_id => message.player_id
        ),
        true
      )
      return
    end

    team.members.delete_if {|member| member == message.player_id}

    # Reassign owner if they leave
    if team.owner == message.player_id
      team.owner = team.members.first
    end

    team.save!
    commands.datastore.call_dbproc(:team_update_team,'teams',team.to_storage_entity,false)
    destroy_player_team(message.player_id)
    send_team_left(message.name,message.player_id)
    leave_chat(message.name,message.player_id)
  else
    send_team_left(message.name,message.player_id)
  end
end

#member_disconnected(message) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/game_machine/game_systems/team_manager.rb', line 148

def member_disconnected(message)
  if player_team = PlayerTeam.find!(message.player_id)
    leave = LeaveTeam.new(
      :name => player_team.name,
      :player_id => message.player_id
    )
    leave_team(leave)
  end
end

#on_receive(message) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/game_machine/game_systems/team_manager.rb', line 334

def on_receive(message)
  if message.is_a?(CreateTeam)
    create_team(message)
    send_team(message)
  elsif message.is_a?(DestroyTeam)
    destroy_team(message)
  elsif message.is_a?(TeamInvite)
    team_invite(message)
  elsif message.is_a?(TeamAcceptInvite)
    team_accept_invite(message)
  elsif message.is_a?(JoinTeam)
    join_team(message)
    send_team(message)
  elsif message.is_a?(LeaveTeam)
    leave_team(message)
  elsif message.is_a?(TeamsRequest)
    teams_request(message)
  elsif message.is_a?(FindMatch)
    find_match(message)
  elsif message.is_a?(StartMatch)
    start_match(message)
  elsif message.is_a?(EndMatch)
    end_match(message)
  elsif message.is_a?(MessageLib::ClientManagerEvent)
    if message.event == 'disconnected'
      member_disconnected(message)
      GameMachine.logger.info "#{self.class.name} #{message.player_id} removed from team(disconnected)"
    end
  else
    unhandled(message)
  end
end

#player_entity(player_id) ⇒ Object



22
23
24
# File 'lib/game_machine/game_systems/team_manager.rb', line 22

def player_entity(player_id)
  MessageLib::Entity.new.set_player(MessageLib::Player.new.set_id(player_id))
end

#post_init(*args) ⇒ Object



8
9
10
11
12
# File 'lib/game_machine/game_systems/team_manager.rb', line 8

def post_init(*args)
  @team_handler = Application.config.handlers.team.constantize.new
  define_update_procs
  commands.misc.client_manager_register(self.class.name)
end

#send_team(message) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/game_machine/game_systems/team_manager.rb', line 55

def send_team(message)
  if player_team = PlayerTeam.find!(message.player_id)
    if team = Team.find!(player_team.name)
      commands.player.send_message(team,message.player_id)
    end
  end
end

#send_team_joined(team_name, player_id) ⇒ Object



42
43
44
# File 'lib/game_machine/game_systems/team_manager.rb', line 42

def send_team_joined(team_name,player_id)
  commands.player.send_message(TeamJoined.new(:name => team_name),player_id)
end

#send_team_left(team_name, player_id) ⇒ Object



46
47
48
# File 'lib/game_machine/game_systems/team_manager.rb', line 46

def send_team_left(team_name,player_id)
  commands.player.send_message(TeamLeft.new(:name => team_name),player_id)
end

#start_match(message) ⇒ Object

Called internally or from another actor to start an already defined match



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/game_machine/game_systems/team_manager.rb', line 285

def start_match(message)
  # Don't accept requests from clients
  if message.player_id
    return
  end

  match_id = self.class.match_id_for(message.team_names)
  if match = Match.find!(match_id)
    GameMachine.logger.warn "Match #{match_id} already created"
    return
  end

  teams = message.team_names.collect {|name| Team.find!(name)}.compact
  if teams.size != message.team_names.size
    GameMachne.logger.warn "#{self.class.name} Unable to find all teams for match"
    return
  end

  server = Akka.instance.hashring.node_for(match_id)
  server = server.sub('akka.tcp://cluster@','').split(':').first

  match = Match.new(
    :id => match_id,
    :teams => teams,
    :server => server,
    :game_handler => message.game_handler
  )

  teams.each do |team|
    team.match_id = match_id
    team.match_server = server
    team.save
    commands.datastore.call_dbproc(:team_update_team,'teams',team.to_storage_entity,false)
  end

  match.save
  handler_match_started(match)
  GameMachine.logger.info "#{self.class.name} Match #{match} started"
end

#team_accept_invite(message) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/game_machine/game_systems/team_manager.rb', line 260

def team_accept_invite(message)
  if team = Team.find!(message.name)
    if message.invite_id == team.invite_id
      join_team(JoinTeam.new(:name => team.name, :player_id => message.player_id),true)
    end
  end
end

#team_id(team_name) ⇒ Object



77
78
79
# File 'lib/game_machine/game_systems/team_manager.rb', line 77

def team_id(team_name)
  Uniqueid.generate_token(team_name)
end

#team_invite(message) ⇒ Object



251
252
253
254
255
256
257
258
# File 'lib/game_machine/game_systems/team_manager.rb', line 251

def team_invite(message)
  if team = Team.find!(message.name)
    if message.player_id == team.owner
      message.invite_id = team.invite_id
      commands.player.send_message(message,message.invitee)
    end
  end
end

#teams_request(message) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/game_machine/game_systems/team_manager.rb', line 63

def teams_request(message)
  if teams = Teams.find('teams')
    teams = handler_teams_filter(teams,message)
    commands.player.send_message(teams,message.player_id)
  end
  send_team(message)
end