Advertisement
LeonMMS

VXA-OS Email System - Server

Jan 21st, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.64 KB | None | 0 0
  1. # LM² - Email
  2. # v1.0
  3. # configurações gerais
  4. module MailSettings
  5.   PLAYER_EMAIL_VALIDITY = 7 # dias multiplicados por 86400
  6.   ADMIN_EMAIL_VALIDITY = 30 # dias multiplicados por 86400
  7.   ADMIN_NAME = "Admin"
  8. end
  9. # struct do email
  10. Email = Struct.new(:id, :sender_id, :sender_name, :name, :body, :gold, :items, :state, :send_date, :expire_date)
  11. ItemMail = Struct.new(:type, :id, :amount) do
  12.   def initialize(ntype, nid = nil, namount = nil)
  13.     if nid.nil?
  14.       self.type = ntype[0].to_i
  15.       self.id = ntype[1].to_i
  16.       self.amount = ntype[2].to_i
  17.     else
  18.       self.type = ntype
  19.       self.id = nid
  20.       self.amount = namount
  21.     end
  22.   end
  23.   def get_string
  24.     return "#{self.type}:#{self.id}:#{self.amount}"
  25.   end
  26. end
  27. module Enums
  28.     Email = enum %w(
  29.         UNREAD
  30.         CLAIMED
  31.         DELETED
  32.     )
  33. end
  34. # recebimento de pacotes do cliente
  35. module Handle_Data
  36.   def handle_player_email(client, buffer)
  37.     mail_id = buffer.read_short
  38.     delete = buffer.read_boolean
  39.     if delete && client.emails[mail_id].state == Enums::Email::CLAIMED
  40.       Database.delete_email(client.id_db, client.emails[mail_id].id)
  41.       client.emails.delete_at(mail_id)
  42.       return
  43.     end
  44.     return if client.emails[mail_id].state == Enums::Email::CLAIMED
  45.     client.emails[mail_id].state = Enums::Email::CLAIMED
  46.     gold = client.emails[mail_id].gold
  47.     client.gain_gold(gold, false, true) if gold > 0
  48.     client.emails[mail_id].items.each{|mail_item|
  49.       item = client.item_object(mail_item.type, mail_item.id)
  50.       client.gain_item(item, mail_item.amount, false, true)
  51.     }
  52.     Database.update_email(client.id_db, client.emails[mail_id].id)
  53.   end
  54.  
  55.   def handle_player_write_email(client, buffer)
  56.     admin = buffer.read_boolean
  57.     return if admin && !client.admin?
  58.     receiver = buffer.read_string
  59.     name = chat_filter(buffer.read_string.force_encoding("UTF-8"))
  60.     body = chat_filter(buffer.read_string.force_encoding("UTF-8"))
  61.     gold = buffer.read_int
  62.     size = buffer.read_byte
  63.     items = []
  64.     size.times{
  65.       type = buffer.read_byte
  66.       id = buffer.read_short
  67.       amount = buffer.read_short
  68.       amount = [amount, client.item_number(client.item_object(type, id))].min unless admin
  69.       items << ItemMail.new(type, id, amount)
  70.     }
  71.     if admin
  72.       if receiver.empty?
  73.         write_admin_email(name, body, gold, items)
  74.         @log.add(client.group, :blue, "#{client.user} enviou um email a todos.")
  75.       else
  76.         @log.add(client.group, :blue, "#{client.user} enviou um email a #{receiver}.")
  77.         receiver = find_player(receiver) || receiver
  78.         write_email(Struct.new(:id_db, :name).new(-1,  MailSettings::ADMIN_NAME), receiver, name, body, gold, items)
  79.       end
  80.     else
  81.       receiver = find_player(receiver) || receiver
  82.       result = write_email(client, receiver, name, body, gold, items)
  83.       if result
  84.         client.lose_gold(gold) if gold > 0
  85.         items.each{|mailitem|
  86.           item = client.item_object(mailitem.type, mailitem.id)
  87.           client.lose_item(item, mailitem.amount)
  88.         }
  89.       end
  90.     end
  91.   end
  92. end
  93. # envio de pacotes para clientes
  94. module Send_Data
  95.   def send_player_emails(client)
  96.     client.emails.each{|mail|
  97.       send_player_email(client, mail)
  98.     }
  99.   end
  100.   def send_player_email(client, mail)
  101.     buffer = Buffer_Writer.new
  102.     buffer.write_byte(Enums::Packet::EMAIL)
  103.     buffer.write_string(mail.sender_name)
  104.     buffer.write_string(mail.name)
  105.     buffer.write_string(mail.body)
  106.     buffer.write_byte(mail.state)
  107.     buffer.write_int(mail.gold)
  108.     buffer.write_byte(mail.items.size)
  109.     mail.items.each{|item|
  110.       buffer.write_byte(item.type)
  111.       buffer.write_short(item.id)
  112.       buffer.write_short(item.amount)
  113.     }
  114.     buffer.write_long(Time.now - mail.send_date)
  115.     buffer.write_long(mail.expire_date - Time.now)
  116.     client.send_data(buffer.to_s)
  117.   end
  118.  
  119.   def send_email_delete(client, mail_id)
  120.     buffer = Buffer_Writer.new
  121.     buffer.write_byte(Enums::Packet::EMAIL_WRITE)
  122.     buffer.write_short(mail_id)
  123.     client.send_data(buffer.to_s)
  124.   end
  125.  
  126. end
  127. # metodos relacionados ao envio de emails
  128. module Game_General
  129.   def write_email(sender, receiver, name, body, gold, items)
  130.     online = receiver.is_a?(Game_Client)
  131.     receiver_id = online ? receiver.id_db : Database.find_player_id(receiver)
  132.     return false if receiver.nil?
  133.     send_date = Time.now
  134.     expire_date = send_date + (MailSettings::PLAYER_EMAIL_VALIDITY * 86400)
  135.     nitems = items.map{|item| item.get_string }.join('|')
  136.     email = Email.new(0, sender.id_db, sender.name, name, body, gold, nitems, 0, send_date, expire_date)
  137.     email = Database.send_email(receiver_id, email)
  138.     if online
  139.       email.items = items
  140.       receiver.emails << email
  141.       send_player_email(receiver, email)
  142.     end
  143.     return true
  144.   end
  145.  
  146.   def write_admin_email(name, body, gold, items)
  147.     send_date = Time.now
  148.     expire_date = send_date + (MailSettings::PLAYER_EMAIL_VALIDITY * 86400)
  149.     nitems = items.map{|item| item.get_string }.join('|')
  150.     email = Email.new(0, -1, MailSettings::ADMIN_NAME, name, body, gold, items, 0, send_date, expire_date)
  151.     Database.send_admin_email(email, nitems)
  152.   end
  153.  
  154.   def cancel_scheduler(client)
  155.     client.emails.each{|mail|
  156.       jobs = @scheduler.jobs(tag: mail.id.to_s)
  157.       jobs.each(&:unschedule)
  158.     }
  159.   end
  160. end
  161. # salvar e carregar dados do banco de dados
  162. module Database
  163.   def self.load_player_emails(client)
  164.     s_client = sql_client
  165.     emails = s_client[:actor_emails].where(:receiver_id => client.id_db)
  166.     loaded_emails = []
  167.         emails.each { |row|
  168.       state = row[:state]
  169.       next if state == Enums::Email::DELETED
  170.       expire_date = Time.at(row[:expire_date])
  171.       if Time.now > expire_date
  172.         s_client[:actor_emails].where(:receiver_id => client.id_db, :id =>  row[:id]).update(:state => Enums::Email::DELETED)
  173.         next
  174.       end
  175.       items = row[:items].split('|').map{ |item| ItemMail.new(item.split(':')) }
  176.       loaded_emails << Email.new(
  177.         row[:id], row[:sender_id], row[:sender_name],
  178.         row[:name], row[:body], row[:gold], items,
  179.         state, Time.at(row[:send_date]), expire_date
  180.       )
  181.       unless $network.scheduler.jobs.any?{ |job| job.tags.include?(row[:id].to_s)}
  182.         $network.scheduler.at(expire_date, tags: [row[:id].to_s]) do
  183.           email = client.emails.select{|mail| mail.id == row[:id]}.first
  184.           if email
  185.             index = client.emails.index(email)
  186.             client.emails.delete_at(index)
  187.             $network.send_email_delete(client, index)
  188.             Database.delete_email(client.id_db, index)
  189.           end
  190.         end
  191.       end
  192.     }
  193.         s_client.disconnect
  194.     return loaded_emails
  195.   end
  196.  
  197.   def self.send_email(receiver_id, email)
  198.     s_client = sql_client
  199.     id = s_client[:actor_emails].insert(
  200.       :sender_id => email.sender_id, :sender_name => email.sender_name,
  201.       :name => email.name, :body => email.body,
  202.       :gold => email.gold, :items => email.items,
  203.       :state => email.state, :send_date => email.send_date.to_i, :expire_date => email.expire_date.to_i,
  204.       :receiver_id => receiver_id
  205.     )
  206.     s_client.disconnect
  207.     email.id = id
  208.     return email
  209.   end
  210.   def self.send_admin_email(email, nitems)
  211.     s_client = sql_client
  212.     s_client[:actors].select(:id, :name, :online).each {|row|
  213.       receiver_id = row[:id]
  214.       id = s_client[:actor_emails].insert(
  215.         :sender_id => email.sender_id, :sender_name => email.sender_name,
  216.         :name => email.name, :body => email.body,
  217.         :gold => email.gold, :items => nitems,
  218.         :state => email.state, :send_date => email.send_date.to_i, :expire_date => email.expire_date.to_i,
  219.         :receiver_id => receiver_id
  220.       )
  221.       if row[:online] == 1
  222.         email.id = id
  223.         client = $network.find_player(row[:name])
  224.         client.emails << email
  225.         $network.send_player_email(client, email)
  226.       end
  227.     }
  228.     s_client.disconnect
  229.   end
  230.  
  231.   def self.delete_email(id_db, email_id)
  232.     s_client = sql_client
  233.     s_client[:actor_emails].where(:receiver_id => id_db, :id =>  email_id).update(:state => Enums::Email::DELETED)
  234.     s_client.disconnect
  235.   end
  236.  
  237.   def self.update_email(id_db, email_id)
  238.     s_client = sql_client
  239.     s_client[:actor_emails].where(:receiver_id => id_db, :id =>  email_id).update(:state => Enums::Email::CLAIMED)
  240.     s_client.disconnect
  241.   end
  242.  
  243.   def self.find_player_id(name)
  244.     s_client = sql_client
  245.     result = s_client[:actors].where(:name => name).select(:id).first
  246.     s_client.disconnect
  247.     return result ? result[:id] : nil
  248.   end
  249. end
  250. # cliente em si
  251. class Game_Client < EventMachine::Connection
  252.   attr_accessor :emails
  253. end
  254.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement