Enhance NPC encounter logging and fix item type references in scenario scripts

This commit is contained in:
Z. Cliffe Schreuders
2025-12-01 17:31:12 +00:00
parent 0cf9e0ba62
commit a4606f596c
9 changed files with 64 additions and 23 deletions

View File

@@ -545,10 +545,23 @@ module BreakEscape
new_npcs = npc_ids - @game.player_state['encounteredNPCs']
return if new_npcs.empty?
# Log detailed information about each new NPC encountered
new_npcs.each do |npc_id|
npc_data = room_data['npcs'].find { |npc| npc['id'] == npc_id }
if npc_data
display_name = npc_data['displayName'] || npc_id
npc_type = npc_data['npcType'] || 'unknown'
Rails.logger.info "[BreakEscape] 🎭 NPC ENCOUNTERED: #{display_name} (#{npc_id}) - Type: #{npc_type} - Room: #{room_id}"
else
Rails.logger.info "[BreakEscape] 🎭 NPC ENCOUNTERED: #{npc_id} - Room: #{room_id}"
end
end
@game.player_state['encounteredNPCs'] = (@game.player_state['encounteredNPCs'] + new_npcs).uniq
@game.save!
Rails.logger.debug "[BreakEscape] Tracked NPC encounters: #{new_npcs.join(', ')}"
total_encountered = @game.player_state['encounteredNPCs'].length
Rails.logger.info "[BreakEscape] ✅ Tracked #{new_npcs.length} new NPC encounter(s) in room #{room_id}. Total NPCs encountered: #{total_encountered}"
rescue => e
Rails.logger.error "[BreakEscape] Error tracking NPC encounters: #{e.message}\n#{e.backtrace.first(5).join("\n")}"
# Continue without tracking to avoid breaking room loading
@@ -744,6 +757,17 @@ module BreakEscape
end
end
end
# Priority 3: Items held by NPCs in this room
room_data['npcs']&.each do |npc|
next unless npc['itemsHeld'].present?
npc['itemsHeld'].each do |held_item|
if held_item['type'] == item_type && (held_item['key_id'] == item_id || held_item['id'] == item_id || held_item['name'] == item_name || held_item['name'] == item_id)
return { item: held_item, location: { type: 'npc', npc_id: npc['id'], room_id: room_id } }
end
end
end
end
nil

View File

@@ -102,8 +102,24 @@ module BreakEscape
# NPC tracking
def encounter_npc!(npc_id)
player_state['encounteredNPCs'] ||= []
player_state['encounteredNPCs'] << npc_id unless player_state['encounteredNPCs'].include?(npc_id)
save!
unless player_state['encounteredNPCs'].include?(npc_id)
player_state['encounteredNPCs'] << npc_id
# Try to get NPC display name from scenario for better logging
npc_display_name = npc_id
if scenario_data && scenario_data['rooms']
scenario_data['rooms'].each do |_room_id, room_data|
npc_data = room_data['npcs']&.find { |npc| npc['id'] == npc_id }
if npc_data && npc_data['displayName']
npc_display_name = npc_data['displayName']
break
end
end
end
Rails.logger.info "[BreakEscape] 🎭 NPC ENCOUNTERED (via encounter_npc!): #{npc_display_name} (#{npc_id})"
save!
end
end
# Global variables (synced with client)