characters.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. class Character < ActiveRecord::Base
  2. validates :user_id, presence: true
  3. validates :name, presence: true
  4. validates :species, presence: true
  5. validates :types, presence: true
  6. def self.restricted_find(search, author, flags=[])
  7. # Append Deleted to flags
  8. flags.push('Deleted')
  9. # Find Character
  10. case search.to_i
  11. when 0
  12. where(user_id: author.id).
  13. where.not(active: flags).
  14. find_by!('name ilike ?', search)
  15. else
  16. find(search) if Util::Roles.admin?(author)
  17. end
  18. end
  19. def type_color
  20. Type.find_by(name: types.split("/").first || 'Unknown').color
  21. end
  22. def fetch_inventory
  23. # Fetch item names and amounts in character's inventory
  24. Inventory.where(char_id: id).
  25. joins('join items on items.id = inventories.item_id').
  26. pluck('amount, items.name')
  27. end
  28. def self.from_form(app)
  29. key_mapping = {
  30. "Characters Name" => "name",
  31. "Aliases" => "aliases",
  32. "Species" => "species",
  33. "Shiny" => "shiny",
  34. "Type" => "types",
  35. "Age" => "age",
  36. "Weight" => "weight",
  37. "Height" => "height",
  38. "Gender" => "gender",
  39. "Sexual Orientation" => "orientation",
  40. "Relationship Status" => "relationship",
  41. "Attacks" => "attacks",
  42. "Likes" => "likes",
  43. "Dislikes" => "dislikes",
  44. "Personality" => "personality",
  45. "Hometown" => "hometown",
  46. "Warnings" => "warnings",
  47. "Rumors" => "rumors",
  48. "Backstory" => "backstory",
  49. "Recent Events" => "recent_events",
  50. "Other" => "other",
  51. "Rating" => "rating",
  52. "Current Location" => "location",
  53. "DM Notes" => "dm_notes",
  54. "Base Form ID" => "alt_form",
  55. "Edit Key (ignore)" => "edit_url",
  56. }
  57. hash = {
  58. "user_id" => nil,
  59. "name" => nil,
  60. "aliases" => nil,
  61. "species" => nil,
  62. "shiny" => nil,
  63. "types" => nil,
  64. "age" => nil,
  65. "weight" => nil,
  66. "height" => nil,
  67. "gender" => nil,
  68. "orientation" => nil,
  69. "relationship" => nil,
  70. "attacks" => nil,
  71. "likes" => nil,
  72. "dislikes" => nil,
  73. "personality" => nil,
  74. "backstory" => nil,
  75. "recent_events" => nil,
  76. "other" => nil,
  77. "edit_url" => nil,
  78. "active" => nil,
  79. "dm_notes" => nil,
  80. "location" => nil,
  81. "rumors" => nil,
  82. "hometown" => nil,
  83. "warnings" => nil,
  84. "rating" => nil,
  85. "alt_form" => nil,
  86. }
  87. user_id = UID.match(app.description)
  88. active = case app.title
  89. when /Personal Character/
  90. 'Active'
  91. when /Alternate Form/
  92. 'Alternate Form'
  93. when /NPC/
  94. 'NPC'
  95. when /Archived Character/
  96. 'Archived'
  97. end
  98. hash["user_id"] = case app.description
  99. when /public/i
  100. 'Public'
  101. when /server/i
  102. 'Server'
  103. else
  104. user_id[1]
  105. end
  106. hash["active"] = active
  107. hash["edit_url"] = app.footer.text
  108. app.fields.each do |field|
  109. next if field.nil?
  110. db_column = key_mapping[field.name]
  111. if db_column == 'shiny'
  112. hash[db_column] = field.value.match(/yes/i) ? true : false
  113. elsif db_column == 'aliases'
  114. hash[db_column] = field.value.split(/\s?(\||,)\s?/)
  115. else
  116. hash[db_column] = field.value
  117. end
  118. end
  119. hash = hash.reject { |k,v| k == nil }
  120. hash
  121. end
  122. end