Class: Lich::Common::Inventory::Snapshot
- Inherits:
-
Object
- Object
- Lich::Common::Inventory::Snapshot
- Defined in:
- documented/common/inventory.rb
Overview
An immutable, point-in-time inventory snapshot. Returned by refresh so a caller has a consistent view even as passive absorption swaps the global snapshot. Query it directly rather than re-reading the global accessors.
Immutability contract
The Snapshot, its id-keyed item map, its #room_id, and its
worn/room/at-feet collections are frozen; each Item's identity strings,
Item#flags, and Item#children
are frozen too, so a consumer cannot corrupt shared state through a returned
object (e.g. item.flags << "locked" raises). The Item objects themselves
are deliberately NOT frozen: their backing GameObj is populated lazily for
classification (see Item#register_gameobj), which would be impossible on a
frozen instance. Treat every field as read-only regardless.
Instance Attribute Summary collapse
-
#last_updated ⇒ Time
readonly
Wall-clock time the snapshot was built.
-
#room_id ⇒ String?
readonly
The room id from the response envelope.
Instance Method Summary collapse
-
#[](id) ⇒ Item?
Looks up an item by exist id.
-
#age ⇒ Float
Seconds elapsed since the snapshot was built (monotonic).
-
#all ⇒ Array<Item>
Every item in the snapshot.
-
#at_feet ⇒ Array<Item>
Items on the ground at the player's feet.
-
#containers ⇒ Array<Item>
Every item that is a container.
-
#find(pattern) ⇒ Item?
Finds the first item whose noun, name, or long matches.
-
#initialize(items:, room_id:) ⇒ Snapshot
constructor
private
A new instance of Snapshot.
-
#left_hand ⇒ Item?
The item in the player's left hand.
-
#right_hand ⇒ Item?
The item in the player's right hand.
-
#room ⇒ Array<Item>
Items on the ground in the room (shared room loot, not necessarily the player's -- the feed lists every observer's droppings).
-
#total_weight ⇒ Integer
Total carried encumbrance in pounds: the recursive Item#total_weight (with the
in_encumshort-circuit) of everything worn AND held. -
#where(noun: nil, name: nil) ⇒ Array<Item>
Filters items by exact noun and/or exact display name.
-
#worn ⇒ Array<Item>
Items worn on the player.
Constructor Details
#initialize(items:, room_id:) ⇒ Snapshot
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Snapshot.
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'documented/common/inventory.rb', line 547 def initialize(items:, room_id:) @items = items.freeze # Duplicated and frozen so shared snapshot metadata cannot be mutated # through {#room_id} (e.g. +Inventory.current.room_id.replace('x')+): the # room id arrives as an unfrozen Ox attribute string, unlike the {Item} # identity fields which are frozen at construction. @room_id = room_id.nil? ? nil : room_id.dup.freeze @worn = items.values.select(&:worn?).freeze @room = items.values.select(&:in_room?).freeze @at_feet = items.values.select(&:at_feet?).freeze @right_hand = items.values.find(&:in_right_hand?) @left_hand = items.values.find(&:in_left_hand?) @built_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) @last_updated = Time.now freeze end |
Instance Attribute Details
#last_updated ⇒ Time (readonly)
Returns wall-clock time the snapshot was built.
539 540 541 |
# File 'documented/common/inventory.rb', line 539 def last_updated @last_updated end |
#room_id ⇒ String? (readonly)
Returns the room id from the response envelope.
542 543 544 |
# File 'documented/common/inventory.rb', line 542 def room_id @room_id end |
Instance Method Details
#[](id) ⇒ Item?
Looks up an item by exist id.
575 576 577 |
# File 'documented/common/inventory.rb', line 575 def [](id) @items[id.to_s] end |
#age ⇒ Float
Seconds elapsed since the snapshot was built (monotonic).
649 650 651 |
# File 'documented/common/inventory.rb', line 649 def age Process.clock_gettime(Process::CLOCK_MONOTONIC) - @built_at end |
#all ⇒ Array<Item>
Returns every item in the snapshot.
565 566 567 |
# File 'documented/common/inventory.rb', line 565 def all @items.values end |
#at_feet ⇒ Array<Item>
Returns items on the ground at the player's feet.
625 626 627 |
# File 'documented/common/inventory.rb', line 625 def at_feet @at_feet end |
#containers ⇒ Array<Item>
Returns every item that is a container.
605 606 607 |
# File 'documented/common/inventory.rb', line 605 def containers all.select(&:container?) end |
#find(pattern) ⇒ Item?
Finds the first item whose noun, name, or long matches.
585 586 587 588 |
# File 'documented/common/inventory.rb', line 585 def find(pattern) rx = pattern.is_a?(Regexp) ? pattern : Regexp.new(Regexp.escape(pattern.to_s), Regexp::IGNORECASE) all.find { |item| item.noun =~ rx || item.name =~ rx || (item.long && item.long =~ rx) } end |
#left_hand ⇒ Item?
Returns the item in the player's left hand.
620 621 622 |
# File 'documented/common/inventory.rb', line 620 def left_hand @left_hand end |
#right_hand ⇒ Item?
Returns the item in the player's right hand.
615 616 617 |
# File 'documented/common/inventory.rb', line 615 def right_hand @right_hand end |
#room ⇒ Array<Item>
Returns items on the ground in the room (shared room loot, not necessarily the player's -- the feed lists every observer's droppings).
631 632 633 |
# File 'documented/common/inventory.rb', line 631 def room @room end |
#total_weight ⇒ Integer
Total carried encumbrance in pounds: the recursive Item#total_weight
(with the in_encum short-circuit) of everything worn AND held. Ground
items (#room/#at_feet) are excluded -- they are not carried.
640 641 642 |
# File 'documented/common/inventory.rb', line 640 def total_weight (@worn + [@right_hand, @left_hand].compact).sum(&:total_weight) end |
#where(noun: nil, name: nil) ⇒ Array<Item>
Filters items by exact noun and/or exact display name.
597 598 599 600 601 602 |
# File 'documented/common/inventory.rb', line 597 def where(noun: nil, name: nil) result = all result = result.select { |item| item.noun == noun } if noun result = result.select { |item| item.name == name } if name result end |
#worn ⇒ Array<Item>
Returns items worn on the player.
610 611 612 |
# File 'documented/common/inventory.rb', line 610 def worn @worn end |