Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # LM² - Email
- # v1.0
- # configurações gerais
- module MailSettings
- PLAYER_EMAIL_VALIDITY = 7 # dias multiplicados por 86400
- ADMIN_EMAIL_VALIDITY = 30 # dias multiplicados por 86400
- ADMIN_NAME = "Admin"
- end
- # struct do email
- Email = Struct.new(:id, :sender_id, :sender_name, :name, :body, :gold, :items, :state, :send_date, :expire_date)
- ItemMail = Struct.new(:type, :id, :amount) do
- def initialize(ntype, nid = nil, namount = nil)
- if nid.nil?
- self.type = ntype[0].to_i
- self.id = ntype[1].to_i
- self.amount = ntype[2].to_i
- else
- self.type = ntype
- self.id = nid
- self.amount = namount
- end
- end
- def get_string
- return "#{self.type}:#{self.id}:#{self.amount}"
- end
- end
- module Enums
- Email = enum %w(
- UNREAD
- CLAIMED
- DELETED
- )
- end
- # recebimento de pacotes do cliente
- module Handle_Data
- def handle_player_email(client, buffer)
- mail_id = buffer.read_short
- delete = buffer.read_boolean
- if delete && client.emails[mail_id].state == Enums::Email::CLAIMED
- Database.delete_email(client.id_db, client.emails[mail_id].id)
- client.emails.delete_at(mail_id)
- return
- end
- return if client.emails[mail_id].state == Enums::Email::CLAIMED
- client.emails[mail_id].state = Enums::Email::CLAIMED
- gold = client.emails[mail_id].gold
- client.gain_gold(gold, false, true) if gold > 0
- client.emails[mail_id].items.each{|mail_item|
- item = client.item_object(mail_item.type, mail_item.id)
- client.gain_item(item, mail_item.amount, false, true)
- }
- Database.update_email(client.id_db, client.emails[mail_id].id)
- end
- def handle_player_write_email(client, buffer)
- admin = buffer.read_boolean
- return if admin && !client.admin?
- receiver = buffer.read_string
- name = chat_filter(buffer.read_string.force_encoding("UTF-8"))
- body = chat_filter(buffer.read_string.force_encoding("UTF-8"))
- gold = buffer.read_int
- size = buffer.read_byte
- items = []
- size.times{
- type = buffer.read_byte
- id = buffer.read_short
- amount = buffer.read_short
- amount = [amount, client.item_number(client.item_object(type, id))].min unless admin
- items << ItemMail.new(type, id, amount)
- }
- if admin
- if receiver.empty?
- write_admin_email(name, body, gold, items)
- @log.add(client.group, :blue, "#{client.user} enviou um email a todos.")
- else
- @log.add(client.group, :blue, "#{client.user} enviou um email a #{receiver}.")
- receiver = find_player(receiver) || receiver
- write_email(Struct.new(:id_db, :name).new(-1, MailSettings::ADMIN_NAME), receiver, name, body, gold, items)
- end
- else
- receiver = find_player(receiver) || receiver
- result = write_email(client, receiver, name, body, gold, items)
- if result
- client.lose_gold(gold) if gold > 0
- items.each{|mailitem|
- item = client.item_object(mailitem.type, mailitem.id)
- client.lose_item(item, mailitem.amount)
- }
- end
- end
- end
- end
- # envio de pacotes para clientes
- module Send_Data
- def send_player_emails(client)
- client.emails.each{|mail|
- send_player_email(client, mail)
- }
- end
- def send_player_email(client, mail)
- buffer = Buffer_Writer.new
- buffer.write_byte(Enums::Packet::EMAIL)
- buffer.write_string(mail.sender_name)
- buffer.write_string(mail.name)
- buffer.write_string(mail.body)
- buffer.write_byte(mail.state)
- buffer.write_int(mail.gold)
- buffer.write_byte(mail.items.size)
- mail.items.each{|item|
- buffer.write_byte(item.type)
- buffer.write_short(item.id)
- buffer.write_short(item.amount)
- }
- buffer.write_long(Time.now - mail.send_date)
- buffer.write_long(mail.expire_date - Time.now)
- client.send_data(buffer.to_s)
- end
- def send_email_delete(client, mail_id)
- buffer = Buffer_Writer.new
- buffer.write_byte(Enums::Packet::EMAIL_WRITE)
- buffer.write_short(mail_id)
- client.send_data(buffer.to_s)
- end
- end
- # metodos relacionados ao envio de emails
- module Game_General
- def write_email(sender, receiver, name, body, gold, items)
- online = receiver.is_a?(Game_Client)
- receiver_id = online ? receiver.id_db : Database.find_player_id(receiver)
- return false if receiver.nil?
- send_date = Time.now
- expire_date = send_date + (MailSettings::PLAYER_EMAIL_VALIDITY * 86400)
- nitems = items.map{|item| item.get_string }.join('|')
- email = Email.new(0, sender.id_db, sender.name, name, body, gold, nitems, 0, send_date, expire_date)
- email = Database.send_email(receiver_id, email)
- if online
- email.items = items
- receiver.emails << email
- send_player_email(receiver, email)
- end
- return true
- end
- def write_admin_email(name, body, gold, items)
- send_date = Time.now
- expire_date = send_date + (MailSettings::PLAYER_EMAIL_VALIDITY * 86400)
- nitems = items.map{|item| item.get_string }.join('|')
- email = Email.new(0, -1, MailSettings::ADMIN_NAME, name, body, gold, items, 0, send_date, expire_date)
- Database.send_admin_email(email, nitems)
- end
- def cancel_scheduler(client)
- client.emails.each{|mail|
- jobs = @scheduler.jobs(tag: mail.id.to_s)
- jobs.each(&:unschedule)
- }
- end
- end
- # salvar e carregar dados do banco de dados
- module Database
- def self.load_player_emails(client)
- s_client = sql_client
- emails = s_client[:actor_emails].where(:receiver_id => client.id_db)
- loaded_emails = []
- emails.each { |row|
- state = row[:state]
- next if state == Enums::Email::DELETED
- expire_date = Time.at(row[:expire_date])
- if Time.now > expire_date
- s_client[:actor_emails].where(:receiver_id => client.id_db, :id => row[:id]).update(:state => Enums::Email::DELETED)
- next
- end
- items = row[:items].split('|').map{ |item| ItemMail.new(item.split(':')) }
- loaded_emails << Email.new(
- row[:id], row[:sender_id], row[:sender_name],
- row[:name], row[:body], row[:gold], items,
- state, Time.at(row[:send_date]), expire_date
- )
- unless $network.scheduler.jobs.any?{ |job| job.tags.include?(row[:id].to_s)}
- $network.scheduler.at(expire_date, tags: [row[:id].to_s]) do
- email = client.emails.select{|mail| mail.id == row[:id]}.first
- if email
- index = client.emails.index(email)
- client.emails.delete_at(index)
- $network.send_email_delete(client, index)
- Database.delete_email(client.id_db, index)
- end
- end
- end
- }
- s_client.disconnect
- return loaded_emails
- end
- def self.send_email(receiver_id, email)
- s_client = sql_client
- id = s_client[:actor_emails].insert(
- :sender_id => email.sender_id, :sender_name => email.sender_name,
- :name => email.name, :body => email.body,
- :gold => email.gold, :items => email.items,
- :state => email.state, :send_date => email.send_date.to_i, :expire_date => email.expire_date.to_i,
- :receiver_id => receiver_id
- )
- s_client.disconnect
- email.id = id
- return email
- end
- def self.send_admin_email(email, nitems)
- s_client = sql_client
- s_client[:actors].select(:id, :name, :online).each {|row|
- receiver_id = row[:id]
- id = s_client[:actor_emails].insert(
- :sender_id => email.sender_id, :sender_name => email.sender_name,
- :name => email.name, :body => email.body,
- :gold => email.gold, :items => nitems,
- :state => email.state, :send_date => email.send_date.to_i, :expire_date => email.expire_date.to_i,
- :receiver_id => receiver_id
- )
- if row[:online] == 1
- email.id = id
- client = $network.find_player(row[:name])
- client.emails << email
- $network.send_player_email(client, email)
- end
- }
- s_client.disconnect
- end
- def self.delete_email(id_db, email_id)
- s_client = sql_client
- s_client[:actor_emails].where(:receiver_id => id_db, :id => email_id).update(:state => Enums::Email::DELETED)
- s_client.disconnect
- end
- def self.update_email(id_db, email_id)
- s_client = sql_client
- s_client[:actor_emails].where(:receiver_id => id_db, :id => email_id).update(:state => Enums::Email::CLAIMED)
- s_client.disconnect
- end
- def self.find_player_id(name)
- s_client = sql_client
- result = s_client[:actors].where(:name => name).select(:id).first
- s_client.disconnect
- return result ? result[:id] : nil
- end
- end
- # cliente em si
- class Game_Client < EventMachine::Connection
- attr_accessor :emails
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement