Class: ESM::Event::Xm8NotificationV1

Inherits:
Object
  • Object
show all
Defined in:
lib/esm/event/xm8_notification_v1.rb

Constant Summary collapse

TYPES =
%w[
  custom
  base-raid
  flag-stolen
  flag-restored
  flag-steal-started
  protection-money-due
  protection-money-paid
  grind-started
  hack-started
  charge-plant-started
  marxet-item-sold
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(server:, parameters:, connection: nil) ⇒ Xm8NotificationV1

Returns a new instance of Xm8NotificationV1.

Parameters:

  • parameters (OpenStruct)

    The message from the server

  • id (Hash)

    a customizable set of options

  • message (Hash)

    a customizable set of options

  • type (Hash)

    a customizable set of options



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/esm/event/xm8_notification_v1.rb', line 25

def initialize(server:, parameters:, connection: nil)
  @server = server
  @community = server.community

  # SteamUIDs
  @recipients = parameters.recipients.to_ostruct.r

  # Could be a string (territory_name) or JSON (item, amount, title, body)
  @message = parameters.message
  @xm8_type = parameters.type
  @territory_id = parameters.id

  # For generating notifications
  @attributes = {
    communityid: @community.community_id,
    serverid: @server.server_id,
    servername: @server.server_name,
    territoryid: @territory_id || "",
    territoryname: "",
    username: "",
    usertag: "",
    item: "",
    amount: ""
  }
end

Instance Method Details

#run!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/esm/event/xm8_notification_v1.rb', line 51

def run!
  return if @recipients.blank?

  # Check for valid types
  check_for_valid_type!

  # Check for proper values
  case @xm8_type
  when "marxet-item-sold"
    @message = @message.to_ostruct
    check_for_invalid_marxet_attributes!

    @attributes[:amount] = @message.amount
    @attributes[:item] = @message.item
  when "custom"
    @message = @message.to_ostruct
    check_for_invalid_custom_attributes!
  else
    @attributes[:territoryname] = @message
  end

  # Determine with embed to send
  embed =
    if @xm8_type == "custom"
      custom_embed
    else
      notification_embed
    end

  # Convert the steam_uids in @recipients to users and send the notification
  @users = ESM::User.where(steam_uid: @recipients)
  @statuses_by_user = {}

  # Send the messages
  send_to_users(embed)
  send_to_custom_routes(embed)

  notify_on_send!(embed)
  @statuses_by_user
rescue ESM::Exception::CheckFailure => e
  send(e.data) # notify_invalid_type!, notify_invalid_attributes!
  raise ESM::Exception::Error if ESM.env.test?
end

#unregistered_steam_uidsObject

Returns the steam uids of the players who are not registered with ESM



96
97
98
99
100
101
# File 'lib/esm/event/xm8_notification_v1.rb', line 96

def unregistered_steam_uids
  @unregistered_steam_uids ||= begin
    steam_uids = @users.pluck(:steam_uid)
    @recipients.reject { |steam_uid| steam_uids.include?(steam_uid) }
  end
end