2

我有一个红宝石应用程序/网站,用户可以在其中扫描二维码并向比特币钱包捐款。每当发生捐赠时,就会创建交易和事件,并更新比特币钱包值。这是下面的代码

require 'date'

module Server
  class Event
    include DataMapper::Resource

    property :id,           Serial
    property :duration,     Float,      required: true, default: ->(x, y) { 1.0 }
    property :created_at,   DateTime,   required: true, index: true, default: ->(x, y) { DateTime.now }
    property :valve,        Integer,    required: true

    belongs_to :transaction, index: true
    belongs_to :bucket,      index: true

    def self.register(wallet, amount, hash)
      bucket = Bucket.first(wallet: wallet)
      unless bucket
        raise DataMapper::ObjectNotFoundError, "Bucket not found for wallet #{wallet}"
      end

      tr = Transaction.create!({
        wallet:     wallet,
        amount:     amount,
        thash:       hash,
        created_at: DateTime.now
      })
      ev = Event.create!({
        valve:       bucket.valve,
        duration:    calculate_duration(amount * bucket.multiplier),
        transaction: tr,
        bucket:      bucket,
        created_at:  tr.created_at
      })
      bucket.update(amount: bucket.amount + amount)
    end

    def amount
      self.transaction { |x| x.amount }
    end

    def self.calculate_duration(amount)
      [[amount, Config.server.min_duration].max, Config.server.max_duration].min
    end

    def simple
      return {
        valve: valve,
        duration: duration_millis
      }
    end

    def duration_millis
      (duration * 1000).to_i
    end

    def to_hash(is_simple)
      if is_simple
        simple
      else
        attributes.merge simple  
      end
    end

    def self.latest(valve = nil)
      if valve.nil?
        Event.first(order: [:created_at.asc])
      else
        Event.first(order: [:created_at.asc], valve: valve)
      end
    end

    def self.latest!(valve = nil)
      result = self.latest(valve)
      result.destroy unless result.nil?
      result
    end

  end
end

总共有3个钱包。所以 0、1 和 2。我的问题是如何根据在调用存储桶更新函数后使用 if 语句更新的钱包来播放特定的声音。

4

0 回答 0