characters.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.from_form(app)
  7. key_mapping = {
  8. "Characters Name" => "name",
  9. "Species" => "species",
  10. "Shiny" => "shiny",
  11. "Type" => "types",
  12. "Age" => "age",
  13. "Weight" => "weight",
  14. "Height" => "height",
  15. "Gender" => "gender",
  16. "Sexual Orientation" => "orientation",
  17. "Relationship Status" => "relationship",
  18. "Attacks" => "attacks",
  19. "Likes" => "likes",
  20. "Dislikes" => "dislikes",
  21. "Personality" => "personality",
  22. "Hometown" => "hometown",
  23. "Warnings" => "warnings",
  24. "Rumors" => "rumors",
  25. "Backstory" => "backstory",
  26. "Other" => "other",
  27. "Rating" => "rating",
  28. "Current Location" => "location",
  29. "DM Notes" => "dm_notes",
  30. "Edit Key (ignore)" => "edit_url",
  31. }
  32. hash = {
  33. "user_id" => nil,
  34. "name" => nil,
  35. "species" => nil,
  36. "shiny" => nil,
  37. "types" => nil,
  38. "age" => nil,
  39. "weight" => nil,
  40. "height" => nil,
  41. "gender" => nil,
  42. "orientation" => nil,
  43. "relationship" => nil,
  44. "attacks" => nil,
  45. "likes" => nil,
  46. "dislikes" => nil,
  47. "personality" => nil,
  48. "backstory" => nil,
  49. "other" => nil,
  50. "edit_url" => nil,
  51. "active" => nil,
  52. "dm_notes" => nil,
  53. "location" => nil,
  54. "rumors" => nil,
  55. "hometown" => nil,
  56. "warnings" => nil,
  57. "rating" => nil
  58. }
  59. user_id = UID.match(app.description)
  60. active = case app.title
  61. when /Personal Character/
  62. 'Active'
  63. when /NPC/
  64. 'NPC'
  65. when /Archived Character/
  66. 'Archived'
  67. end
  68. hash["user_id"] = case app.description
  69. when /public/i
  70. 'Public'
  71. when /server/i
  72. 'Server'
  73. else
  74. user_id[1]
  75. end
  76. hash["active"] = active
  77. hash["edit_url"] = app.footer.text
  78. app.fields.each do |field|
  79. next if field.nil?
  80. db_column = key_mapping[field.name]
  81. if db_column == 'shiny'
  82. hash[db_column] = field.value.match(/yes/i) ? true : false
  83. else
  84. hash[db_column] = field.value
  85. end
  86. end
  87. hash = hash.reject { |k,v| k == nil }
  88. hash
  89. end
  90. def self.check_user(event)
  91. app = event.message.embeds.first
  92. edit_url = app.footer.text
  93. active = app.title
  94. user_id = UID.match(app.description)
  95. if app.description.match(/public/i) || app.description.match(/server/i)
  96. approval_react(event)
  97. return
  98. end
  99. user = User.find_by(id: user_id[1])
  100. member = event.server.member(user_id[1]) if user
  101. allowed_chars = (user.level / 10 + 1) if user
  102. if member
  103. allowed_chars += 1 if member.roles.map(&:name).include?("Nitro Booster")
  104. end
  105. if user
  106. active_chars =
  107. Character.where(user_id: user_id[1]).where(active: "Active")
  108. new_active =
  109. active == "Personal Character" && !active_chars.map(&:edit_url).include?(edit_url)
  110. too_many = new_active ? active_chars.count >= allowed_chars : false
  111. approval_react(event) unless too_many
  112. too_many(event, member, edit_url, 'characters') if too_many && member
  113. else
  114. unknown_member(event)
  115. end
  116. end
  117. end