landmark.rb 3.0 KB

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