Module: Lich::Claim

Defined in:
lib/gemstone/claim.rb

Overview

module Gemstone # test this?

Constant Summary collapse

Lock =
Mutex.new

Class Method Summary collapse

Class Method Details

.checked?(room = nil) ⇒ Boolean

Checks if the specified room has been checked.

Examples:

has_checked = Claim.checked?(123)

Parameters:

  • room (Integer, nil) (defaults to: nil)

    The room ID to check. Defaults to the last room if nil.

Returns:

  • (Boolean)

    True if the room has been checked, false otherwise.



79
80
81
# File 'lib/gemstone/claim.rb', line 79

def self.checked?(room = nil)
  Lock.synchronize { XMLData.room_id == (room || @last_room) }
end

.claim_room(id) ⇒ void

This method returns an undefined value.

Claims a room by its ID.

Examples:

Claim.claim_room(123)

Parameters:

  • id (Integer, String)

    The ID of the room to claim.

Raises:

  • (StandardError)

    If there is an issue with claiming the room.



19
20
21
22
23
24
# File 'lib/gemstone/claim.rb', line 19

def self.claim_room(id)
  @claimed_room = id.to_i
  @timestamp    = Time.now
  Log.out("claimed #{@claimed_room}", label: %i(claim room)) if defined?(Log)
  Lock.unlock
end

.claimed_roomInteger?

Returns the ID of the currently claimed room.

Examples:

claimed_id = Claim.claimed_room

Returns:

  • (Integer, nil)

    The ID of the claimed room or nil if none.



31
32
33
# File 'lib/gemstone/claim.rb', line 31

def self.claimed_room
  @claimed_room
end

.clusteredArray

Returns the connected clusters if defined.

Examples:

connected_clusters = Claim.clustered

Returns:

  • (Array)

    An array of connected clusters or an empty array if not defined.



143
144
145
146
147
148
149
150
# File 'lib/gemstone/claim.rb', line 143

def self.clustered
  begin
    return [] unless defined? Cluster
    Cluster.connected
  rescue
    return []
  end
end

.current?Boolean

Checks if the current instance is the owner of the claimed room.

Examples:

is_mine = Claim.current?

Returns:

  • (Boolean)

    True if this instance is the owner, false otherwise.



69
70
71
# File 'lib/gemstone/claim.rb', line 69

def self.current?
  Lock.synchronize { @mine.eql?(true) }
end

.infovoid

This method returns an undefined value.

Provides information about the current claim status and related data.

Examples:

Claim.info


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gemstone/claim.rb', line 88

def self.info
  rows = [['XMLData.room_id', XMLData.room_id, 'Current room according to the XMLData'],
          ['Claim.mine?', Claim.mine?, 'Claim status on the current room'],
          ['Claim.claimed_room', Claim.claimed_room, 'Room id of the last claimed room'],
          ['Claim.checked?', Claim.checked?, "Has Claim finished parsing ROOMID\ndefault: the current room"],
          ['Claim.last_room', Claim.last_room, 'The last room checked by Claim, regardless of status'],
          ['Claim.others', Claim.others.join("\n"), "Other characters in the room\npotentially less grouped characters"]]
  info_table = Terminal::Table.new :headings => ['Property', 'Value', 'Description'],
                                   :rows     => rows,
                                   :style    => { :all_separators => true }
  Lich::Messaging.mono(info_table.to_s)
end

.last_roomInteger?

Returns the ID of the last room checked.

Examples:

last_id = Claim.last_room

Returns:

  • (Integer, nil)

    The ID of the last room or nil if none.



40
41
42
# File 'lib/gemstone/claim.rb', line 40

def self.last_room
  @last_room
end

.lockvoid

Note:

This method will only lock if the mutex is not already owned.

This method returns an undefined value.

Locks the mutex for the Claim module.

Examples:

Claim.lock


50
51
52
# File 'lib/gemstone/claim.rb', line 50

def self.lock
  Lock.lock if !Lock.owned?
end

.membersArray

Returns the members of the group if checked.

Examples:

group_members = Claim.members

Returns:

  • (Array)

    An array of group members’ nouns or an empty array if not checked.



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gemstone/claim.rb', line 124

def self.members
  return [] unless defined? Group

  begin
    if Group.checked?
      return Group.members.map(&:noun)
    else
      return []
    end
  rescue
    return []
  end
end

.mine?Boolean

Checks if the current instance is the owner of the claimed room.

Examples:

is_mine = Claim.mine?

Returns:

  • (Boolean)

    True if this instance is the owner, false otherwise.



106
107
108
# File 'lib/gemstone/claim.rb', line 106

def self.mine?
  self.current?
end

.othersArray

Returns the list of other characters in the room.

Examples:

other_characters = Claim.others

Returns:

  • (Array)

    An array of other characters.



115
116
117
# File 'lib/gemstone/claim.rb', line 115

def self.others
  @others
end

.parser_handle(nav_rm, pcs) ⇒ void

This method returns an undefined value.

Handles the claim parsing for a given room and characters.

Examples:

Claim.parser_handle(123, ['Alice', 'Bob'])

Parameters:

  • nav_rm (Integer)

    The room ID being navigated to.

  • pcs (Array)

    The list of characters present in the room.

Raises:

  • (StandardError)

    If there is an error during parsing.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/gemstone/claim.rb', line 160

def self.parser_handle(nav_rm, pcs)
  echo "Claim handled #{nav_rm} with xmlparser" if $claim_debug
  begin
    @others = pcs - self.clustered - self.members
    @last_room = nav_rm
    unless @others.empty?
      @mine = false
      return
    end
    @mine = true
    self.claim_room nav_rm unless nav_rm.nil?
  rescue StandardError => e
    if defined?(Log)
      Log.out(e)
    else
      respond("Claim Parser Error: #{e}")
    end
  ensure
    Lock.unlock if Lock.owned?
  end
end

.unlockvoid

Note:

This method will only unlock if the mutex is owned.

This method returns an undefined value.

Unlocks the mutex for the Claim module.

Examples:

Claim.unlock


60
61
62
# File 'lib/gemstone/claim.rb', line 60

def self.unlock
  Lock.unlock if Lock.owned?
end