Module: Lich::Claim

Defined in:
documented/gemstone/claim.rb

Overview

module Gemstone # test this? Provides functionality for claiming rooms in the game. This module manages the state of claimed rooms and handles related operations.

Examples:

Claiming a room

Claim.claim_room(123)

Constant Summary collapse

Lock =

Mutex for synchronizing access to claimed room data.

Mutex.new

Class Method Summary collapse

Class Method Details

.checked?(room = nil) ⇒ Boolean

Checks if the specified room has been checked.

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.



63
64
65
# File 'documented/gemstone/claim.rb', line 63

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 with the given ID.

Examples:

Claiming a room

Claim.claim_room(123)

Parameters:

  • id (Integer)

    The ID of the room to claim.

Raises:

  • (StandardError)

    If there is an issue claiming the room.



23
24
25
26
27
28
# File 'documented/gemstone/claim.rb', line 23

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.

Returns:

  • (Integer, nil)

    The ID of the claimed room or nil if none is claimed.



32
33
34
# File 'documented/gemstone/claim.rb', line 32

def self.claimed_room
  @claimed_room
end

.clusteredArray

Returns a list of connected clusters if defined.

Returns:

  • (Array)

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



114
115
116
117
118
119
120
121
# File 'documented/gemstone/claim.rb', line 114

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

.current?Boolean

Checks if the current instance is the one that claimed the room.

Returns:

  • (Boolean)

    True if this instance is the owner of the claimed room, false otherwise.



56
57
58
# File 'documented/gemstone/claim.rb', line 56

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

.infoString

Provides information about the current claim status and related data.

Examples:

Displaying claim info

puts Claim.info

Returns:

  • (String)

    A formatted string containing the claim information.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'documented/gemstone/claim.rb', line 71

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 that was checked.

Returns:

  • (Integer, nil)

    The ID of the last room checked or nil if none.



38
39
40
# File 'documented/gemstone/claim.rb', line 38

def self.last_room
  @last_room
end

.lockvoid

This method returns an undefined value.

Acquires the lock for the claiming process if not already owned.



44
45
46
# File 'documented/gemstone/claim.rb', line 44

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

.membersArray<String>

Returns a list of members in the group if defined.

Returns:

  • (Array<String>)

    An array of member nouns or an empty array if not applicable.



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'documented/gemstone/claim.rb', line 98

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.

Returns:

  • (Boolean)

    True if this instance owns the claimed room, false otherwise.



86
87
88
# File 'documented/gemstone/claim.rb', line 86

def self.mine?
  self.current?
end

.othersArray<String>

Returns a list of other characters in the room.

Returns:

  • (Array<String>)

    An array of character names present in the room.



92
93
94
# File 'documented/gemstone/claim.rb', line 92

def self.others
  @others
end

.parser_handle(nav_rm, pcs) ⇒ void

This method returns an undefined value.

Handles the parsing of room claims and updates the state accordingly.

Examples:

Handling a room claim

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

Parameters:

  • nav_rm (Integer)

    The room ID being navigated to.

  • pcs (Array<String>)

    The list of character names present in the room.

Raises:

  • (StandardError)

    If there is an error during parsing.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'documented/gemstone/claim.rb', line 130

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

This method returns an undefined value.

Releases the lock for the claiming process if owned.



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

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