Class: Transaction

Inherits:
Exist
  • Object
show all
Defined in:
lib/Olib/core/transaction.rb

Constant Summary collapse

Appraise =
Rill.new(
start: %[to appraise (?:a |an |)<a exist="{{id}}"],
close: Regexp.union(
  %r[I already appraised that],
  %r[Sorry, #{Char.name}, I'm not buying anything this valuable today\.],
  %r[(I'll give you|How's|I'll offer you|worth at least) (?<value>[,\d]+)],
  %r[(?<value>[,\d]+) silvers])
)
Sell =
Rill.new(
start: %[You offer|You ask (?<merchant>.*?) if (he|she) would like to buy (?:a |an |)<a exist="{{id}}"],
close: Regexp.union(
  %r[(hands you|for) (?<value>[,\d]+)],
  %r[No #{Char.name}, I won't buy that],
  %r[I'm sorry, #{Char.name}, but I have no use for that\.],
  %r[He hands it back to you],
  %r[Nope #{Char.name}, I ain't buying that\.],
  %r[basically worthless here, #{Char.name}])
)

Constants inherited from Exist

Exist::GETTER, Exist::PATTERN

Instance Attribute Summary collapse

Attributes inherited from Exist

#gameobj, #id

Instance Method Summary collapse

Methods inherited from Exist

#==, #deconstruct_keys, #effects, #exists?, #fetch, fetch, #gone?, #method_missing, normalize_type_data, #respond_to_missing?, scan, #tags, #to_h, #to_s

Constructor Details

#initialize(item, **args) ⇒ Transaction

Returns a new instance of Transaction.



29
30
31
32
# File 'lib/Olib/core/transaction.rb', line 29

def initialize(item, **args)
  super(item)
  @threshold = args.fetch(:threshold, false)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Exist

Instance Attribute Details

#errObject (readonly)

Returns the value of attribute err.



24
25
26
# File 'lib/Olib/core/transaction.rb', line 24

def err
  @err
end

#qualityObject (readonly)

Returns the value of attribute quality.



24
25
26
# File 'lib/Olib/core/transaction.rb', line 24

def quality
  @quality
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



24
25
26
# File 'lib/Olib/core/transaction.rb', line 24

def threshold
  @threshold
end

#valueObject (readonly)

Returns the value of attribute value.



24
25
26
# File 'lib/Olib/core/transaction.rb', line 24

def value
  @value
end

Instance Method Details

#appraiseObject



65
66
67
68
69
# File 'lib/Olib/core/transaction.rb', line 65

def appraise()
  return self unless @value.nil?
  return self.skilled_appraise unless Room.current.tags.any? {|tag| %w(gemshop furrier pawnshop).include?(tag)}
  return self.shop_appraise
end

#sellObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/Olib/core/transaction.rb', line 71

def sell()
  self.take
  if @threshold && appraise && @value > @threshold
    return Err[
      transaction: self,
      reason:      "Value[#{@value}] is over Threshold[#{@threshold}]"] 
  end

  (_, match, _lines) = Sell.capture(self.to_h, 
    "sell \#{{id}}")

  if match[:value].is_a?(String) && match[:value] =~ /[\d,]+/
    match[:value] = match[:value].to_s.delete(",").to_i
  end

  match[:value] = 0 unless match[:value].is_a?(Integer)
  Ok[**match]
end

#shop_appraiseObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/Olib/core/transaction.rb', line 53

def shop_appraise
  self.take
  (_, match, lines) = Appraise.capture(self.to_h, "appraise \#{{id}}")

  if lines.any? {|line| line.include?(%[Sorry, #{Char.name}, I'm not buying anything this valuable today.])}
    @value = Float::INFINITY
  else
    @value = match[:value].to_s.delete(",").to_i
  end
  self
end

#skilled_appraiseObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/Olib/core/transaction.rb', line 39

def skilled_appraise()
  if Skills.trading < 25
    @err = :no_trading
    return self
  end
  self.take
  res = dothistimeout("appraise #%s" % self.id, 3, /is of (\w+) quality and worth approximately (\d+) silvers/)
  if res =~ /is of (\w+) quality and worth approximately (\d+) silvers/
    @quality = $1
    @value = $2.delete(",").to_i
  end
  self
end

#takeObject



34
35
36
37
# File 'lib/Olib/core/transaction.rb', line 34

def take()
  Item.new(self).take
  self
end