Class: GameMachine::Helpers::GameMessage

Inherits:
Object
  • Object
show all
Defined in:
server/lib/game_machine/helpers/game_message.rb

Constant Summary collapse

CHARS =
[*('a'..'z'),*('0'..'9')].flatten
STR =
Array.new(100) {|i| CHARS.sample}.join
STR2 =
Array.new(1000) {|i| CHARS.sample}.join

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player_id, default_entity_id = nil) ⇒ GameMessage

Returns a new instance of GameMessage


10
11
12
13
14
# File 'server/lib/game_machine/helpers/game_message.rb', line 10

def initialize(player_id,default_entity_id=nil)
  @player_id = player_id
  @current_entity_id = default_entity_id || 'default'
  @client_message = create_client_message(@player_id)
end

Instance Attribute Details

#client_messageObject (readonly)

Returns the value of attribute client_message


8
9
10
# File 'server/lib/game_machine/helpers/game_message.rb', line 8

def client_message
  @client_message
end

#player_idObject (readonly)

Returns the value of attribute player_id


8
9
10
# File 'server/lib/game_machine/helpers/game_message.rb', line 8

def player_id
  @player_id
end

Instance Method Details

#chat_channel(topic, flags = '') ⇒ Object


109
110
111
# File 'server/lib/game_machine/helpers/game_message.rb', line 109

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

#chat_channels(names) ⇒ Object


101
102
103
104
105
106
107
# File 'server/lib/game_machine/helpers/game_message.rb', line 101

def chat_channels(names)
  channels = MessageLib::ChatChannels.new
  names.each do |name|
    channels.add_chat_channel(chat_channel(name))
  end
  current_entity.set_chat_channels(channels)
end

#chat_message(type, message_text, topic) ⇒ Object


113
114
115
116
117
118
119
120
121
# File 'server/lib/game_machine/helpers/game_message.rb', line 113

def chat_message(type,message_text,topic)
  message = MessageLib::ChatMessage.new
  message.set_chat_channel(
    chat_channel(topic)
  )
  message.set_message(message_text)
  message.set_type(type)
  current_entity.set_chat_message(message)
end

#client_connection(client_id, gateway) ⇒ Object


97
98
99
# File 'server/lib/game_machine/helpers/game_message.rb', line 97

def client_connection(client_id,gateway)
  MessageLib::ClientConnection.new.set_id(client_id).set_gateway(gateway)
end

#client_disconnect(client_id, gateway) ⇒ Object


54
55
56
57
58
59
60
# File 'server/lib/game_machine/helpers/game_message.rb', line 54

def client_disconnect(client_id,gateway)
  client_message.set_client_disconnect(
    MessageLib::ClientDisconnect.new.set_client_connection(
      client_connection(client_id,gateway)
    )
  )
end

#current_entityObject


32
33
34
35
36
37
# File 'server/lib/game_machine/helpers/game_message.rb', line 32

def current_entity
  unless has_entity?(@current_entity_id)
    add_entity(@current_entity_id)
  end
  entities.select {|entity| entity.id == @current_entity_id}.first
end

#echo_test(value) ⇒ Object


135
136
137
# File 'server/lib/game_machine/helpers/game_message.rb', line 135

def echo_test(value)
  current_entity.set_echo_test(MessageLib::EchoTest.new.set_message(value))
end

#entitiesObject


24
25
26
# File 'server/lib/game_machine/helpers/game_message.rb', line 24

def entities
  client_message.get_entity_list.to_a
end

#entity(id) ⇒ Object


39
40
41
42
# File 'server/lib/game_machine/helpers/game_message.rb', line 39

def entity(id)
  @current_entity_id = id
  current_entity
end

#error_message(code, message) ⇒ Object


48
49
50
51
52
# File 'server/lib/game_machine/helpers/game_message.rb', line 48

def error_message(code,message)
  client_message.set_error_message(
    MessageLib::ErrorMessage.new.set_code(code).set_message(message)
  )
end

#get_neighbors(search_radius = nil) ⇒ Object


74
75
76
77
78
79
80
81
# File 'server/lib/game_machine/helpers/game_message.rb', line 74

def get_neighbors(search_radius=nil)
    component = MessageLib::GetNeighbors.new
    component.set_value(true)
    if search_radius
      component.set_search_radius(search_radius)
    end
  current_entity.set_get_neighbors(component)
end

#has_entity?(id) ⇒ Boolean

Returns:

  • (Boolean)

28
29
30
# File 'server/lib/game_machine/helpers/game_message.rb', line 28

def has_entity?(id)
  entities.select {|entity| entity.id == id}.first ? true : false
end

#join_chat(topic, flags = '') ⇒ Object


123
124
125
126
127
# File 'server/lib/game_machine/helpers/game_message.rb', line 123

def join_chat(topic,flags='')
  current_entity.set_join_chat(
    MessageLib::JoinChat.new.add_chat_channel(chat_channel(topic,flags))
  )
end

#leave_chat(topic) ⇒ Object


129
130
131
132
133
# File 'server/lib/game_machine/helpers/game_message.rb', line 129

def leave_chat(topic)
  current_entity.set_leave_chat(
    MessageLib::LeaveChat.new.add_chat_channel(chat_channel(topic))
  )
end

#neighbors(players, npcs) ⇒ Object


83
84
85
86
87
# File 'server/lib/game_machine/helpers/game_message.rb', line 83

def neighbors(players,npcs)
  current_entity.set_neighbors(
    MessageLib::Neighbors.new.set_player_list(players).set_npc_list(npcs)
  )
end

#player_logoutObject


62
63
64
65
66
# File 'server/lib/game_machine/helpers/game_message.rb', line 62

def player_logout
  client_message.set_player_logout(
    MessageLib::PlayerLogout.new.set_player_id(@player_id)
  )
end

#player_move(x, y) ⇒ Object


89
90
91
92
93
94
95
# File 'server/lib/game_machine/helpers/game_message.rb', line 89

def player_move(x,y)
  current_entity.set_player_move(
    MessageLib::PlayerMove.new.set_target(
      MessageLib::Target.new.set_x(x).set_y(y)
    )
  )
end

#send_to_playerObject


20
21
22
# File 'server/lib/game_machine/helpers/game_message.rb', line 20

def send_to_player
  client_message.send_to_player
end

#to_byte_arrayObject


16
17
18
# File 'server/lib/game_machine/helpers/game_message.rb', line 16

def to_byte_array
  client_message.to_byte_array
end

#to_entityObject


44
45
46
# File 'server/lib/game_machine/helpers/game_message.rb', line 44

def to_entity
  current_entity.set_player(client_message.player)
end

#track_entityObject


68
69
70
71
72
# File 'server/lib/game_machine/helpers/game_message.rb', line 68

def track_entity
  current_entity.set_track_entity(
    MessageLib::TrackEntity.new.set_value(true)
  )
end