Class: GameMachine::GameSystems::Chat

Inherits:
Actor::Base
  • Object
show all
Includes:
Commands
Defined in:
server/lib/game_machine/game_systems/chat.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, logger, model_filter, #onReceive, player_controller, #receive_message, #schedule_message, #schedule_message_once, #sender, set_player_controller

Instance Attribute Details

#chat_idObject (readonly)

Returns the value of attribute chat_id


17
18
19
# File 'server/lib/game_machine/game_systems/chat.rb', line 17

def chat_id
  @chat_id
end

#game_idObject (readonly)

Returns the value of attribute game_id


17
18
19
# File 'server/lib/game_machine/game_systems/chat.rb', line 17

def game_id
  @game_id
end

#registered_asObject (readonly)

Returns the value of attribute registered_as


17
18
19
# File 'server/lib/game_machine/game_systems/chat.rb', line 17

def registered_as
  @registered_as
end

Class Method Details

.entity_valid(entity) ⇒ Object


331
332
333
334
335
336
337
338
# File 'server/lib/game_machine/game_systems/chat.rb', line 331

def self.entity_valid(entity)
  if (entity.is_a?(MessageLib::ObjectdbStatus))
    logger.info("ObjectdbStatus")
    return false
  else
    return true
  end
end

.subscribers_for_topic(topic, game_id) ⇒ Object


7
8
9
10
11
12
13
14
15
# File 'server/lib/game_machine/game_systems/chat.rb', line 7

def self.subscribers_for_topic(topic,game_id)
  datastore = GameMachine::Commands::DatastoreCommands.new
  entity = datastore.get("#{game_id}_chat_topic_#{topic}")
  if entity_valid(entity)
    entity.subscribers || MessageLib::Subscribers.new
  else
    MessageLib::Subscribers.new
  end
end

Instance Method Details

#add_subscriber(subscriber_id, topic) ⇒ Object


126
127
128
129
130
# File 'server/lib/game_machine/game_systems/chat.rb', line 126

def add_subscriber(subscriber_id,topic)
  stored_entity_id = "#{game_id}_chat_topic_#{topic}"
  entity = MessageLib::Entity.new.set_id(subscriber_id)
  commands.datastore.call_dbproc(:chat_add_subscriber,stored_entity_id,entity,false)
end

#can_send_to_player?(sender_id, recipient_id) ⇒ Boolean

Returns:

  • (Boolean)

297
298
299
300
301
302
303
304
305
# File 'server/lib/game_machine/game_systems/chat.rb', line 297

def can_send_to_player?(sender_id,recipient_id)
  player_service = JavaLib::PlayerService.get_instance
  if recipient_player = player_service.find(recipient_id)
    if recipient_player.get_game_id == game_id
      return true
    end
  end
  false
end

#channel_flags_id(name) ⇒ Object


162
163
164
# File 'server/lib/game_machine/game_systems/chat.rb', line 162

def channel_flags_id(name)
  "channel_flags#{@chat_id}#{name}"
end

#create_topic_handler(topic) ⇒ Object


111
112
113
114
115
116
117
118
# File 'server/lib/game_machine/game_systems/chat.rb', line 111

def create_topic_handler(topic)
  name = "topic#{chat_id}#{topic}"
  builder = Actor::Builder.new(GameSystems::ChatTopic,chat_id,registered_as)
  actor_name = Digest::MD5.hexdigest(name)
  ref = builder.with_parent(context).with_name(actor_name).start
  actor_ref = Actor::Ref.new(ref,GameSystems::ChatTopic.name)
  @topic_handlers[topic] = actor_ref
end

#delete_channel_flags(channel_name) ⇒ Object


189
190
191
192
# File 'server/lib/game_machine/game_systems/chat.rb', line 189

def delete_channel_flags(channel_name)
  flags_id = channel_flags_id(channel_name)
  commands.datastore.delete(flags_id)
end

#entity_valid(entity) ⇒ Object


327
328
329
# File 'server/lib/game_machine/game_systems/chat.rb', line 327

def entity_valid(entity)
  return self.class.entity_valid(entity)
end

#flags_for_channel(channel_name) ⇒ Object


166
167
168
# File 'server/lib/game_machine/game_systems/chat.rb', line 166

def flags_for_channel(channel_name)
  @channel_flags.fetch(channel_name,[])
end

#get_flagsObject


170
171
172
173
174
175
176
177
178
179
# File 'server/lib/game_machine/game_systems/chat.rb', line 170

def get_flags
  {}.tap do |flags|
    @subscriptions.each do |name|
      entity = commands.datastore.get(channel_flags_id(name))
      if entity_valid(entity)
        flags[name] = parse_channel_flags(entity.params)
      end
    end
  end
end

#get_subscriptionsObject


143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'server/lib/game_machine/game_systems/chat.rb', line 143

def get_subscriptions
  subscriptions = Set.new
  entity_id = "subscriptions_#{chat_id}"
  entity = commands.datastore.get(entity_id)
  if entity_valid(entity)

    if chat_channels = entity.chat_channels.get_chat_channel_list
      chat_channels.each do |channel|
        subscriptions.add(channel.name)
      end
    end
  end
  subscriptions
end

#invite_exists?(channel_name, invite_id) ⇒ Boolean

Returns:

  • (Boolean)

194
195
196
197
198
199
200
201
202
# File 'server/lib/game_machine/game_systems/chat.rb', line 194

def invite_exists?(channel_name,invite_id)
  key = "invite_#{channel_name}_#{invite_id}"
  entity = commands.datastore.get(key)
  if !entity.nil? && entity_valid(entity)
    true
  else
    false
  end
end

#join_channel(name, flags) ⇒ Object


231
232
233
234
235
236
237
238
239
240
241
# File 'server/lib/game_machine/game_systems/chat.rb', line 231

def join_channel(name,flags)
  JavaLib::ChatSubscriptions.subscribe(chat_id,name)
  create_topic_handler(name)
  message = MessageLib::Subscribe.new.set_topic(name).set_game_id(game_id)
  message_queue.tell(message,topic_handler_for(name).actor)
  @subscriptions.add(name)
  save_subscriptions
  add_subscriber(chat_id,name)
  set_channel_flags(name,flags)
  self.class.logger.debug "Player #{chat_id} Joined channel #{name} with flags #{flags}"
end

#join_channels(chat_channels) ⇒ Object


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'server/lib/game_machine/game_systems/chat.rb', line 204

def join_channels(chat_channels)
  chat_channels.each do |channel|
    if @topic_handlers[channel.name]
      self.class.logger.info "Topic handler exists for #{channel.name}, not creating"
      next
    end

    self.class.logger.debug "Join request #{channel.name}"
    # Private channels.  format priv_[chat_id]_[channel name]
    # Players can create private channels with their player id, other
    # players must have an invite to join someone elses private channel
    if channel.name.match(/^priv/)
      if channel.name.match(/^priv_#{chat_id}/)
        join_channel(channel.name,channel.flags)
      elsif channel.invite_id
        if invite_exists?(channel.name,channel.invite_id)
          join_channel(channel.name,channel.flags)
        else
          self.class.logger.info "Invite id #{channel.invite_id} not found"
        end
      end
    else
      join_channel(channel.name,channel.flags)
    end
  end
end

#leave_all_channelsObject


94
95
96
97
98
99
100
101
# File 'server/lib/game_machine/game_systems/chat.rb', line 94

def leave_all_channels
  channels = MessageLib::ChatChannels.new
  @subscriptions.each do |name|
    channel = MessageLib::ChatChannel.new.set_name(name)
    leave_channel(channel)
  end
  get_sender.tell('complete',get_self)
end

#leave_channel(channel) ⇒ Object


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'server/lib/game_machine/game_systems/chat.rb', line 249

def leave_channel(channel)
  if topic_handler = topic_handler_for(channel.name)
    JavaLib::ChatSubscriptions.unsubscribe(chat_id,channel.name)
    message = MessageLib::Unsubscribe.new.set_topic(channel.name).set_game_id(game_id)
    message_queue.tell(message,topic_handler_for(channel.name).actor)
    @subscriptions.delete_if {|sub| sub == channel.name}
    save_subscriptions
    remove_subscriber(chat_id,channel.name)
    topic_handler.tell(JavaLib::PoisonPill.get_instance)
    @topic_handlers.delete(channel.name)
    delete_channel_flags(channel.name)
  else
    self.class.logger.info "leave_channel: no topic handler found for #{channel.name}"
  end
end

#leave_channels(chat_channels) ⇒ Object


243
244
245
246
247
# File 'server/lib/game_machine/game_systems/chat.rb', line 243

def leave_channels(chat_channels)
  chat_channels.each do |channel|
    leave_channel(channel)
  end
end

#load_stateObject


38
39
40
41
42
43
# File 'server/lib/game_machine/game_systems/chat.rb', line 38

def load_state
  @subscriptions.each do |name|
    flags = flags_for_channel(name).join('|')
    join_channel(name,flags)
  end
end

#message_queueObject


103
104
105
# File 'server/lib/game_machine/game_systems/chat.rb', line 103

def message_queue
  MessageQueue.find
end

#on_receive(entity) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'server/lib/game_machine/game_systems/chat.rb', line 45

def on_receive(entity)
  self.class.logger.debug "Received #{entity}"

  if entity.is_a?(MessageLib::ChatDestroy)
    leave_all_channels
    return
  end

  if entity.join_chat
    self.class.logger.debug "Join chat"
    join_channels(entity.join_chat.get_chat_channel_list)
    send_status
  end

  if entity.chat_message
    self.class.logger.debug "Chat message"
    send_message(entity.chat_message)
  end

  if entity.leave_chat
    self.class.logger.debug "Leave chat"
    leave_channels(entity.leave_chat.get_chat_channel_list)
    send_status
  end

  if entity.chat_status
    send_status
  end
end

#parse_channel_flags(flags) ⇒ Object


158
159
160
# File 'server/lib/game_machine/game_systems/chat.rb', line 158

def parse_channel_flags(flags)
  flags.split('|')
end

#post_init(*args) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'server/lib/game_machine/game_systems/chat.rb', line 19

def post_init(*args)
  @chat_id = args[0]
  @registered_as = args[1]

  player_service = GameMachine::JavaLib::PlayerService.get_instance
  @game_id = player_service.get_game_id(chat_id)

  @topic_handlers = {}

  # Update these values from the datastore, as this state needs to
  # persist between restarts
  @subscriptions = get_subscriptions
  @channel_flags = get_flags

  # Make sure we re-create our state from the persisted state if we died
  load_state
  self.class.logger.debug "Chat child #{chat_id} started with game_id #{game_id}"
end

#remove_subscriber(subscriber_id, topic) ⇒ Object


120
121
122
123
124
# File 'server/lib/game_machine/game_systems/chat.rb', line 120

def remove_subscriber(subscriber_id,topic)
  stored_entity_id = "#{game_id}_chat_topic_#{topic}"
  entity = MessageLib::Entity.new.set_id(subscriber_id)
  commands.datastore.call_dbproc(:chat_remove_subscriber,stored_entity_id,entity,false)
end

#save_subscriptionsObject


132
133
134
135
136
137
138
139
140
141
# File 'server/lib/game_machine/game_systems/chat.rb', line 132

def save_subscriptions
  entity_id = "subscriptions_#{chat_id}"
  channels = MessageLib::ChatChannels.new
  @subscriptions.each do |name|
    channel = MessageLib::ChatChannel.new.set_name(name)
    channels.add_chat_channel(channel)
  end
  entity = MessageLib::Entity.new.set_id(entity_id).set_chat_channels(channels)
  commands.datastore.put(entity)
end

#send_group_message(chat_message) ⇒ Object


307
308
309
310
311
312
313
314
315
316
317
# File 'server/lib/game_machine/game_systems/chat.rb', line 307

def send_group_message(chat_message)
  topic = chat_message.chat_channel.name
  if topic_handler = topic_handler_for(topic)
    entity = MessageLib::Entity.new.set_id('0').set_chat_message(chat_message)
    publish = MessageLib::Publish.new
    publish.set_topic(topic).set_message(entity).set_game_id(game_id)
    message_queue.tell(publish,topic_handler_for(topic).actor)
  else
    self.class.logger.info "send_message: no topic handler found for #{topic}"
  end
end

#send_message(chat_message) ⇒ Object


319
320
321
322
323
324
325
# File 'server/lib/game_machine/game_systems/chat.rb', line 319

def send_message(chat_message)
  if chat_message.type == 'group'
    send_group_message(chat_message)
  elsif chat_message.type == 'private'
    send_private_message(chat_message)
  end
end

#send_private_message(chat_message) ⇒ Object


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'server/lib/game_machine/game_systems/chat.rb', line 265

def send_private_message(chat_message)
  channel_name = chat_message.chat_channel.name
  if channel_name.match(/^character_id__/)
    channel_name.sub!("character_id__","")
    char_service = JavaLib::CharacterService.get_instance
    character = char_service.find(channel_name)
    if character.nil?
      self.class.logger.info "Can't find character #{channel_name}"
      return
    else
      chat_message.chat_channel.name = character.player_id
      self.class.logger.info "character #{channel_name} player #{chat_message.chat_channel.name}"
    end
    
  end
  
  self.class.logger.debug "Sending private chat message #{chat_message.message} from:#{chat_id} to:#{chat_message.chat_channel.name}"
  unless can_send_to_player?(chat_id,chat_message.chat_channel.name)
    self.class.logger.info "Private chat denied, mismatched game id"
    return
  end

  entity = MessageLib::Entity.new
  player = MessageLib::Player.new.set_id(chat_message.chat_channel.name)
  entity.set_id(player.id)
  entity.set_player(player)
  entity.set_chat_message(chat_message)
  entity.set_send_to_player(true)
  commands.player.send_message(entity,player.id)
  self.class.logger.debug "Sent private chat message #{chat_message.message} from:#{chat_id} to:#{chat_message.chat_channel.name}"
end

#send_statusObject

flags = subscribers


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'server/lib/game_machine/game_systems/chat.rb', line 76

def send_status
  channels = MessageLib::ChatChannels.new
  @subscriptions.each do |name|
    flags = @channel_flags.fetch(name,[])
    channel = MessageLib::ChatChannel.new.set_name(name)
    if flags.include?('subscribers')
      channel.set_subscribers(self.class.subscribers_for_topic(name,game_id))
    end
    channels.add_chat_channel(channel)
  end
  if registered_as == 'player'
    commands.player.send_message(channels,chat_id)
  else
    message = MessageLib::Entity.new.set_id(chat_id).set_chat_channels(channels)
    Actor::Base.find(registered_as).tell(message,get_self)
  end
end

#set_channel_flags(name, flags) ⇒ Object


181
182
183
184
185
186
187
# File 'server/lib/game_machine/game_systems/chat.rb', line 181

def set_channel_flags(name,flags)
  return if flags.nil?
  @channel_flags[name] = parse_channel_flags(flags)
  flags_id = channel_flags_id(name)
  entity = MessageLib::Entity.new.set_id(flags_id).set_params(flags)
  commands.datastore.put(entity)
end

#topic_handler_for(name) ⇒ Object


107
108
109
# File 'server/lib/game_machine/game_systems/chat.rb', line 107

def topic_handler_for(name)
  @topic_handlers.fetch(name,nil)
end