Class: Lich::Gemstone::Group Private
- Inherits:
-
Object
- Object
- Lich::Gemstone::Group
- 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
-
._members ⇒ Array<GameObj>
private
Returns the internal members array without checking or copying.
-
.add(*members) ⇒ Array<Hash>
Adds one or more members to the group by sending GROUP commands.
-
.broken? ⇒ Boolean
Checks if the group state is broken or inconsistent.
-
.check ⇒ Array<GameObj>
Actively checks group status by sending the GROUP command to the game.
-
.checked=(flag) ⇒ Boolean
Sets the checked flag indicating group data has been verified.
-
.checked? ⇒ Boolean
Checks if group data has been verified with the game.
-
.clear ⇒ Array
Clears all group members and resets the checked flag.
-
.closed? ⇒ Boolean
Checks if the group is closed to new members.
-
.delete(*members) ⇒ Array<GameObj>
Removes one or more members from the group by ID.
-
.disks ⇒ Array<Disk>
Returns Disk objects for all group members.
-
.ids ⇒ Array<String>
Returns array of all member IDs.
-
.include?(*members) ⇒ Boolean
Checks if all specified members are in the group.
-
.join(leader) ⇒ Hash, String
Joins another player's group: the follower's side of Group.add.
-
.leader ⇒ Symbol, GameObj
Gets the current group leader.
-
.leader=(char) ⇒ Symbol, GameObj
Sets the group leader.
-
.leader? ⇒ Boolean
Checks if the current player is the group leader.
-
.maybe_check ⇒ Array<GameObj>?
Checks group status only if not already checked.
-
.members ⇒ Array<GameObj>
Returns a copy of the current group members.
-
.method_missing(method, *args, &block) ⇒ Object
private
Delegates missing methods to the members array.
-
.nonmembers ⇒ Array<GameObj>
Returns all PCs in the room who are not in the group.
-
.nouns ⇒ Array<String>
Returns array of all member nouns (names).
-
.open? ⇒ Boolean
Checks if the group is open to new members.
-
.push(*members) ⇒ Array<GameObj>
Adds one or more members to the group if they're not already included.
-
.record_spell_cooldown(spell) ⇒ nil
Records a group casting of a spell against everyone currently grouped.
-
.record_target_cooldown(spell, name) ⇒ nil
Records that a spell landed on one character, from the third-person message that names them.
-
.refresh(*members) ⇒ Array<GameObj>
Replaces the entire members list with new members.
-
.spell_cooldown_left(spell, name) ⇒ Float
Seconds left before a group casting of the spell can affect the member again.
-
.spell_cooldown_ready(spell) ⇒ Array<String>
Names of the current group members a group casting would affect.
-
.spell_cooldown_ready?(spell, name) ⇒ Boolean
Whether a group casting of the spell can affect the member now.
-
.spell_cooldowns ⇒ Hash
Per-target cooldowns started by group (EVOKE) castings, keyed by spell number then by member name: @@spell_cooldowns["Grhim"].
-
.status ⇒ Symbol
Gets the current group status.
-
.status=(state) ⇒ Symbol
Sets the group status (open or closed).
-
.to_s ⇒ String
String representation of the group members.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
285 286 287 |
# File 'documented/gemstone/group.rb', line 285 def self.leader @@leader end |
.leader=(char) ⇒ Symbol, GameObj
Sets the group leader.
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.
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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
188 189 190 |
# File 'documented/gemstone/group.rb', line 188 def self.spell_cooldowns @@spell_cooldowns end |
.status ⇒ Symbol
Gets the current group status.
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).
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.
112 113 114 |
# File 'documented/gemstone/group.rb', line 112 def self.to_s @@members.to_s end |