Class: Lich::Gemstone::Group Private

Inherits:
Object
  • Object
show all
Defined in:
documented/gemstone/group.rb,
documented/gemstone/group.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Observes game output and automatically updates Group state. Watches for group-related messages and updates membership, leadership, and status.

Defined Under Namespace

Modules: Observer

Class Method Summary collapse

Class Method Details

._members ⇒ Array<GameObj>

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 the internal members array without checking or copying. Used internally by the Observer to avoid infinite loops.

Returns:

  • (Array<GameObj>) —

    the internal members array



93
94
95
# File 'documented/gemstone/group.rb', line 93

def self._members
  @@members
end

.add(*members) ⇒ Array<Hash>

Adds one or more members to the group by sending GROUP commands. Handles both String names and GameObj instances. Can accept nested arrays of members.

Examples:

Add a single member

Group.add("PlayerName")

Add multiple members

Group.add("Player1", "Player2")

Parameters:

  • members (Array<String, GameObj, Array>) —

    members to add

Returns:

  • (Array<Hash>) —

    array of results, each containing :ok or :err key with member



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'documented/gemstone/group.rb', line 306

def self.add(*members)
  members.map do |member|
    if member.is_a?(Array)
      Group.add(*member)
    else
      member = GameObj.pcs.find { |pc| pc.noun.eql?(member) } if member.is_a?(String)

      break if member.nil?

      result = dothistimeout("group ##{member.id}", 3, Regexp.union(
                                                         %r{You add #{member.noun} to your group},
                                                         %r{#{member.noun}'s group status is closed},
                                                         %r{But #{member.noun} is already a member of your group}
                                                       ))

      case result
      when %r{You add}, %r{already a member}
        Group.push(member)
        { ok: member }
      when %r{closed}
        Group.delete(member)
        { err: member }
      else
      end
    end
  end
end

.broken? ⇒ Boolean

Checks if the group state is broken or inconsistent. Waits for any claim locks to release before checking. For leaders: checks if member list matches actual PCs in room. For members: checks if leader is still present.

Returns:

  • (Boolean) —

    true if group state is inconsistent



398
399
400
401
402
403
404
405
406
407
# File 'documented/gemstone/group.rb', line 398

def self.broken?
  sleep(0.1) while Lich::Claim::Lock.locked?
  if Group.leader?
    return true if (GameObj.pcs.empty? || GameObj.pcs.nil?) && !@@members.empty?
    return false if (GameObj.pcs.empty? || GameObj.pcs.nil?) && @@members.empty?
    (GameObj.pcs.map(&:noun) & @@members.map(&:noun)).size < @@members.size
  else
    GameObj.pcs.find do |pc| pc.noun.eql?(Group.leader.noun) end.nil?
  end
end

.check ⇒ Array<GameObj>

Actively checks group status by sending the GROUP command to the game. Clears current group data and waits up to 3 seconds for response. Should be called at script initialization.

Returns:

  • (Array<GameObj>) —

    copy of the members array after checking



160
161
162
163
164
165
166
# File 'documented/gemstone/group.rb', line 160

def self.check
  Group.clear()
  ttl = Time.now + 3
  Game._puts "<c>group\r\n"
  wait_until { Group.checked? or Time.now > ttl }
  @@members.dup
end

.checked=(flag) ⇒ Boolean

Sets the checked flag indicating group data has been verified.

Parameters:

  • flag (Boolean) —

    true if group data is verified

Returns:

  • (Boolean) —

    the flag value



120
121
122
# File 'documented/gemstone/group.rb', line 120

def self.checked=(flag)
  @@checked = flag
end

.checked? ⇒ Boolean

Checks if group data has been verified with the game.

Returns:

  • (Boolean) —

    true if group data has been checked



46
47
48
# File 'documented/gemstone/group.rb', line 46

def self.checked?
  @@checked
end

.clear ⇒ Array

Clears all group members and resets the checked flag. Does not change leader status.

Returns:

  • (Array) —

    Empty array



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

def self.clear()
  @@members = []
  @@checked = false
end

.closed? ⇒ Boolean

Checks if the group is closed to new members.

Returns:

  • (Boolean) —

    true if group status is not open



151
152
153
# File 'documented/gemstone/group.rb', line 151

def self.closed?
  not open?
end

.delete(*members) ⇒ Array<GameObj>

Removes one or more members from the group by ID.

Parameters:

  • members (Array<GameObj>) —

    one or more GameObj instances to remove

Returns:

  • (Array<GameObj>) —

    the updated members array



65
66
67
68
# File 'documented/gemstone/group.rb', line 65

def self.delete(*members)
  gone = members.map(&:id)
  @@members.reject! do |m| gone.include?(m.id) end
end

.disks ⇒ Array<Disk>

Returns Disk objects for all group members. If the player is leader with no members, returns only the player's disk. Always includes the current character's disk if available.

Returns:

  • (Array<Disk>) —

    array of Disk objects for group members



102
103
104
105
106
107
# File 'documented/gemstone/group.rb', line 102

def self.disks
  return [Disk.find_by_name(Char.name)].compact if Group.leader? && members.empty?
  member_disks = members.map(&:noun).compact.map { |noun| Disk.find_by_name(noun) }.compact
  member_disks.push(Disk.find_by_name(Char.name)) if Disk.find_by_name(Char.name)
  return member_disks
end

.ids ⇒ Array<String>

Returns array of all member IDs.

Returns:

  • (Array<String>) —

    array of member IDs



373
374
375
# File 'documented/gemstone/group.rb', line 373

def self.ids
  @@members.map(&:id)
end

.include?(*members) ⇒ Boolean

Checks if all specified members are in the group.

Parameters:

  • members (Array<GameObj>) —

    members to check

Returns:

  • (Boolean) —

    true if all members are in the group



388
389
390
# File 'documented/gemstone/group.rb', line 388

def self.include?(*members)
  members.all? { |m| ids.include?(m.id) }
end

.join(leader) ⇒ Hash, String

Joins another player's group: the follower's side of add. Sends JOIN and reads the game's answer; the observer records the new leader and the roster from the "You join" line itself.

A String that does not name a PC in the room is treated as a separator and delegated to Array#join over the cached roster, the behavior Lich::Gemstone::Group used to reach through method_missing.

Examples:

Group.join("Etanamir")

Parameters:

  • leader (String, GameObj) —

    the leader's noun or GameObj

Returns:

  • (Hash) —

    leader joined, leader already a member, leader closed, gone, or no answer

  • (String) —

    the joined roster when given a separator



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'documented/gemstone/group.rb', line 348

def self.join(leader)
  if leader.is_a?(String)
    pc = GameObj.pcs.find { |member| member.noun.eql?(leader) }
    return @@members.join(leader) if pc.nil?

    leader = pc
  end
  return { err: nil } if leader.nil?

  result = dothistimeout("join ##{leader.id}", 3, Regexp.union(
                                                    %r{^You join #{leader.noun}},
                                                    %r{^#{leader.noun}'s group status is closed},
                                                    %r{already a member of #{leader.noun}'s group},
                                                    %r{^What were you referring to\?}
                                                  ))
  case result
  when %r{^You join} then { ok: leader }
  when %r{already a member of #{leader.noun}'s group} then { noop: leader }
  else { err: leader }
  end
end

.leader ⇒ Symbol, GameObj

Gets the current group leader.

Returns:

  • (Symbol, GameObj) —

    :self if current player is leader, or GameObj of leader



285
286
287
# File 'documented/gemstone/group.rb', line 285

def self.leader
  @@leader
end

.leader=(char) ⇒ Symbol, GameObj

Sets the group leader.

Parameters:

  • char (Symbol, GameObj) —

    :self if current player is leader, or GameObj of leader

Returns:

  • (Symbol, GameObj) —

    the leader value



278
279
280
# File 'documented/gemstone/group.rb', line 278

def self.leader=(char)
  @@leader = char
end

.leader? ⇒ Boolean

Checks if the current player is the group leader.

Returns:

  • (Boolean) —

    true if current player is leader



292
293
294
# File 'documented/gemstone/group.rb', line 292

def self.leader?
  @@leader.eql?(:self)
end

.maybe_check ⇒ Array<GameObj>?

Checks group status only if not already checked.

Returns:

  • (Array<GameObj>, nil) —

    members array if check was needed, nil otherwise



171
172
173
# File 'documented/gemstone/group.rb', line 171

def self.maybe_check
  Group.check unless checked?
end

.members ⇒ Array<GameObj>

Returns a copy of the current group members. Automatically checks group status if not already checked.

Returns:

  • (Array<GameObj>) —

    copy of the members array



83
84
85
86
# File 'documented/gemstone/group.rb', line 83

def self.members
  maybe_check
  @@members.dup
end

.method_missing(method, *args, &block) ⇒ Object

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.

Delegates missing methods to the members array. Allows Group to act like an Array in many contexts.



413
414
415
# File 'documented/gemstone/group.rb', line 413

def self.method_missing(method, *args, &block)
  @@members.send(method, *args, &block)
end

.nonmembers ⇒ Array<GameObj>

Returns all PCs in the room who are not in the group.

Returns:

  • (Array<GameObj>) —

    array of PC GameObj instances not in group



270
271
272
# File 'documented/gemstone/group.rb', line 270

def self.nonmembers
  GameObj.pcs.to_a.reject { |pc| ids.include?(pc.id) }
end

.nouns ⇒ Array<String>

Returns array of all member nouns (names).

Returns:

  • (Array<String>) —

    array of member nouns



380
381
382
# File 'documented/gemstone/group.rb', line 380

def self.nouns
  @@members.map(&:noun)
end

.open? ⇒ Boolean

Checks if the group is open to new members. Automatically verifies group status if not already checked.

Returns:

  • (Boolean) —

    true if group status is open



143
144
145
146
# File 'documented/gemstone/group.rb', line 143

def self.open?
  maybe_check
  @@status.eql?(:open)
end

.push(*members) ⇒ Array<GameObj>

Adds one or more members to the group if they're not already included. Does not duplicate members.

Parameters:

  • members (Array<GameObj>) —

    one or more GameObj instances to add

Returns:

  • (Array<GameObj>) —

    the updated members array



55
56
57
58
59
# File 'documented/gemstone/group.rb', line 55

def self.push(*members)
  members.each do |member|
    @@members.push(member) unless include?(member)
  end
end

.record_spell_cooldown(spell) ⇒ nil

Records a group casting of a spell against everyone currently grouped. Called when the caster's own group-casting message is seen; does nothing for spells with no per-target cooldown.

Parameters:

Returns:

  • (nil)


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'documented/gemstone/group.rb', line 198

def self.record_spell_cooldown(spell)
  seconds = spell.group_cooldown
  return nil if seconds.nil? || seconds <= 0

  now = Time.now
  expires = now + seconds
  store = (@@spell_cooldowns[spell.num] ||= {})
  # _members, not members: this runs on the parser thread, and members
  # would send GROUP and block waiting for a reply that only this thread
  # can parse -- after clearing the list the stamps are drawn from.
  _members.each do |member|
    # A member still locked out did not receive this casting, so their
    # own cooldown keeps running rather than being extended by it.
    current = store[member.noun]
    next if current && current > now

    store[member.noun] = expires
  end
  nil
end

.record_target_cooldown(spell, name) ⇒ nil

Records that a spell landed on one character, from the third-person message that names them. The cooldown belongs to the character the spell landed on rather than to the pair, so it counts no matter who cast it -- seeing someone else put a target on cooldown is as useful as doing it yourself, and the target need not be in the group.

Parameters:

Returns:

  • (nil)


228
229
230
231
232
233
234
# File 'documented/gemstone/group.rb', line 228

def self.record_target_cooldown(spell, name)
  seconds = spell.target_cooldown
  return nil if seconds.nil? || seconds <= 0 || name.nil?

  (@@spell_cooldowns[spell.num] ||= {})[name] = Time.now + seconds
  nil
end

.refresh(*members) ⇒ Array<GameObj>

Replaces the entire members list with new members. Used when receiving a complete group listing from the game.

Parameters:

  • members (Array<GameObj>) —

    the complete list of group members

Returns:

  • (Array<GameObj>) —

    the new members array



75
76
77
# File 'documented/gemstone/group.rb', line 75

def self.refresh(*members)
  @@members = members.dup
end

.spell_cooldown_left(spell, name) ⇒ Float

Seconds left before a group casting of the spell can affect the member again. Zero when the member is ready, including when nothing has been recorded for them.

Parameters:

Returns:

  • (Float) —

    seconds remaining, never negative



243
244
245
246
247
248
# File 'documented/gemstone/group.rb', line 243

def self.spell_cooldown_left(spell, name)
  expires = @@spell_cooldowns.dig(spell.num, name)
  return 0.0 if expires.nil?

  [expires - Time.now, 0.0].max.to_f
end

.spell_cooldown_ready(spell) ⇒ Array<String>

Names of the current group members a group casting would affect.

Parameters:

Returns:

  • (Array<String>) —

    member names that are off cooldown



263
264
265
# File 'documented/gemstone/group.rb', line 263

def self.spell_cooldown_ready(spell)
  members.map(&:noun).compact.select { |name| spell_cooldown_ready?(spell, name) }
end

.spell_cooldown_ready?(spell, name) ⇒ Boolean

Whether a group casting of the spell can affect the member now.

Parameters:

Returns:

  • (Boolean) —

    true when the member is off cooldown



255
256
257
# File 'documented/gemstone/group.rb', line 255

def self.spell_cooldown_ready?(spell, name)
  spell_cooldown_left(spell, name).zero?
end

.spell_cooldowns ⇒ Hash

Per-target cooldowns started by group (EVOKE) castings, keyed by spell number then by member name: @@spell_cooldowns["Grhim"]. Values are the Time the target becomes castable again.

The game gives the caster no messaging about who a group casting actually landed on, so these are recorded optimistically for everyone in the group at the time of the cast. A member who was out of range, or who was already on cooldown from an earlier casting, gets a stamp that is too early -- the next casting corrects it, so the error is bounded by one cooldown and does not accumulate. Someone who joins after a casting has no stamp and reads as ready, which is correct.

Returns:

  • (Hash) —

    the cooldown store



188
189
190
# File 'documented/gemstone/group.rb', line 188

def self.spell_cooldowns
  @@spell_cooldowns
end

.status ⇒ Symbol

Gets the current group status.

Returns:

  • (Symbol) —

    :open or :closed



135
136
137
# File 'documented/gemstone/group.rb', line 135

def self.status()
  @@status
end

.status=(state) ⇒ Symbol

Sets the group status (open or closed).

Parameters:

  • state (Symbol) —

    :open or :closed

Returns:

  • (Symbol) —

    the status value



128
129
130
# File 'documented/gemstone/group.rb', line 128

def self.status=(state)
  @@status = state
end

.to_s ⇒ String

String representation of the group members.

Returns:

  • (String) —

    string representation of members array



112
113
114
# File 'documented/gemstone/group.rb', line 112

def self.to_s
  @@members.to_s
end