landmark.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. NO_GOLD = 'https://cdn.discordapp.com/attachments/645493256821669888/683732758199140387/fcece3957f27d25f9c7aee13a89b7e7c.png'.freeze
  2. def landmark_embed(lm:, section: nil, event: nil)
  3. # Find the author, if they're a member
  4. author = event.server.member(lm.user_id)
  5. fields = []
  6. embed = Embed.new(
  7. title: lm.name,
  8. description: lm.description
  9. )
  10. # Add default image and footer
  11. embed.thumbnail = { url: lm.url }
  12. author_footer(embed, author, [lm.category, lm.id])
  13. # Fill out the fields based on section
  14. case section
  15. when /history/i, nil
  16. # Parent Landmark
  17. if lm.location
  18. fields.push(
  19. { name: 'Location', value: Landmark.find(lm.location).name, inline: true }
  20. )
  21. end
  22. # Region
  23. fields.push(
  24. {name: 'Region', value: Region.find(lm.region).name, inline: true}
  25. )
  26. # History and Folklore
  27. fields.push({name: 'History', value: lm.history}) if lm.history
  28. fields.push({name: 'Folklore', value: lm.folk_lore}) if lm.folk_lore
  29. when /warning/i
  30. # Ensure we aren't looking for NSFW material in a SFW channel
  31. hide = !event.channel.nsfw? && lm.w_rating == 'NSFW'
  32. # List Kinks, Show warning, or hide if needed
  33. fields.push({name: 'Kinks', value: lm.kink.join("\n")}) if lm.kink
  34. if lm.warning
  35. fields.push(
  36. {name: 'Warning', value: hide ? 'This warning is NSFW!' : lm.warning}
  37. )
  38. end
  39. # Display appropriate image
  40. embed.thumbnail = { url: hide ? NO_GOLD : lm.w_url }
  41. when /map/i
  42. # Display map, if it exists
  43. if lm.map_url
  44. embed.image = { url: lm.map_url }
  45. else
  46. embed.description = 'No Map Data'
  47. end
  48. # Remove default image
  49. embed.thumbnail = nil
  50. when /layout/i
  51. # Display layout map, if it exists
  52. if lm.layout_url
  53. embed.image = { url: lm.layout_url }
  54. else
  55. embed.description = 'No Layout Data'
  56. end
  57. # Remove default image
  58. embed.thumbnail = nil
  59. when /npcs?/i
  60. # Find NPCs in the landmark, case insensitive
  61. npcs = Character.where('location ilike ?', lm.name)
  62. .map { |npc| "#{npc.name} - #{npc.species}" }
  63. # List NPCs that currently reside in this landmark
  64. fields.push({
  65. name: 'NPC Residents',
  66. value: npcs.empty? ? 'No known inhabitants' : npcs.join("\n")
  67. })
  68. end
  69. # Update fields and return embed
  70. embed.fields = fields
  71. embed
  72. end
  73. def landmark_list
  74. fields = []
  75. # Fetch all the Regions, and iterate through them
  76. Region.all.each do |r|
  77. # Fetch the all the Landmarks inside the region, then iterate
  78. landmarks = Landmark.where(region: r.id)
  79. children, parents = landmarks.partition { |lm| lm.location }
  80. list = []
  81. parents.each do |p|
  82. list.push(p.name)
  83. input_children(children, p, list, 1)
  84. end
  85. fields.push({ name: r.name, value: list.join("\n"), inline: true}) unless landmarks.empty?
  86. end
  87. Embed.new(
  88. title: 'Places of Interest',
  89. fields: fields
  90. )
  91. end
  92. def input_children(children, parent, list, level)
  93. indent = '---'
  94. children.filter { |c| c.location == parent.id }.each do |landmark|
  95. list.push("#{indent * level}#{landmark.name}")
  96. input_children(children, landmark, list, level + 1)
  97. end
  98. end