Class: Gexp::Handler::Modify::Resources

Inherits:
Gexp::Handler::Modify show all
Defined in:
lib/gexp/handler/modify/resources.rb

Defined Under Namespace

Classes: Exception

Instance Attribute Summary

Attributes inherited from Gexp::Handler

#object, #objects, #params, #user

Instance Method Summary collapse

Methods inherited from Gexp::Handler

#initialize

Constructor Details

This class inherits a constructor from Gexp::Handler

Instance Method Details

#apply_changesObject

Применение изменений к ОВ



38
39
40
41
42
# File 'lib/gexp/handler/modify/resources.rb', line 38

def apply_changes
  @changes.each do |resource, value| 
    @user.send("#{resource}=", value[:after])
  end
end

#normalize(resource, value) ⇒ Object

Нормальлизация значений ресурсов



26
27
28
29
30
31
32
33
34
35
# File 'lib/gexp/handler/modify/resources.rb', line 26

def normalize(resource, value)
  return 0 if value < 0
  method_name = "normalize_#{resource}"
  
  if self.respond_to?(method_name)
    self.send(method_name, value) 
  else
    self.normalize_undefined value
  end
end

#normalize_energy(value) ⇒ Object

Нормализация энергии



12
13
14
15
16
17
18
# File 'lib/gexp/handler/modify/resources.rb', line 12

def normalize_energy(value)
  if value > @user.max_energy
    @user.max_energy 
  else
    value
  end
end

#normalize_undefined(value) ⇒ Object

Нормализация неопределенного ресурса



21
22
23
# File 'lib/gexp/handler/modify/resources.rb', line 21

def normalize_undefined(value)
  value
end

#ontime_apply?Boolean

Применимы изменения к объекту в момент обработки

Returns:

  • (Boolean)


45
46
47
# File 'lib/gexp/handler/modify/resources.rb', line 45

def ontime_apply?
  true # TODO: сделать выяснения что изменения применимы
end

#process(params = {}, &block) ⇒ Object



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
74
75
76
77
# File 'lib/gexp/handler/modify/resources.rb', line 49

def process(params = {}, &block)
  @changes = {}
  @params = params if @params.empty?
  @params = @params.first if @params.is_a?(Array)
  @params.each do |resource, value|
    if @user.respond_to?(resource.to_sym)

      if @user.send(resource) + value < 0
        raise Exception.new("out_of_resource-#{resource}")
      end

      before_value = @user.send(resource)
      new_value    = before_value + value
      norm_value   = self.normalize(resource, new_value)

      @changes[resource] = {
        before: before_value,
        delta:  value,
        after:  norm_value,
      }
    end
  end

  self.apply_changes if self.ontime_apply?
  @user.after_change!(@changes)

  return block.call(@user) if block_given?
  @user
end