Class: GameMachine::Commands::PlayerCommands

Inherits:
Object
  • Object
show all
Includes:
MessageHelper
Defined in:
server/lib/game_machine/commands/player_commands.rb

Instance Method Summary collapse

Methods included from MessageHelper

#entity, #entity_with_player, #is_component?, #is_entity?, #set_player

Instance Method Details

#send_game_message(game_message, player_id) ⇒ Object


6
7
8
9
10
11
12
# File 'server/lib/game_machine/commands/player_commands.rb', line 6

def send_game_message(game_message,player_id)
  entity = MessageLib::Entity.new.set_id('0')
  game_messages = MessageLib::GameMessages.new
  game_messages.add_game_message(game_message)
  entity.set_game_messages(game_messages)
  send_message(entity,player_id)
end

#send_message(message, player_id) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'server/lib/game_machine/commands/player_commands.rb', line 14

def send_message(message,player_id)
  if player_id.nil?
    GameMachine.logger.error "PlayerCommands.send_message: player id cannot be nil"
    return
  end
  if is_entity?(message)
    entity = message
    unless entity.has_player
      set_player(entity,player_id)
    end
  elsif message.kind_of?(GameMachine::Model)
    entity = entity_with_player(player_id,player_id)
    entity.set_json_entity(message.to_json_entity)
  else
    entity = entity_with_player(player_id,player_id)
    if is_component?(message)
      entity.add_component(message)
    else
      GameMachine.logger.error "PlayerCommands.send_message: #{message} is not a valid object to send to a player"
      return
    end
  end
  entity.set_send_to_player(true)
  ClientManager.send_to_player(entity)
end