teams.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. class Team < ActiveRecord::Base
  2. validates :name, presence: true
  3. validates :description, presence: true
  4. def join(character, event)
  5. existing_row = CharTeam.where(char_id: character.id, team_id: id)
  6. if existing_row.empty?
  7. CharTeam.create(char_id: character.id, team_id: id, active: true)
  8. else
  9. existing_row.update(active: true)
  10. end
  11. end
  12. def leave(char)
  13. # Find the char_team row, update it to inactive
  14. ct = CharTeam.where(team_id: id).find_by!(char_id: char.id)
  15. ct.update(active: false)
  16. # Check if they have any members left in the team
  17. # Special Query
  18. sql = "SELECT * " +
  19. "FROM char_teams JOIN characters " +
  20. "ON char_teams.char_id = characters.id " +
  21. "WHERE characters.user_id = ? " +
  22. "AND char_teams.team_id = ? " +
  23. "and char_teams.active = true;"
  24. # Execute
  25. sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql, char.user_id, id])
  26. remaining_chars = ActiveRecord::Base.connection.exec_query(sql)
  27. # Return if the user should have team role removed
  28. remaining_chars.count == 0
  29. rescue ActiveRecord::RecordNotFound => e
  30. error_embed(e.message)
  31. end
  32. def archive(event)
  33. # Update each character team status to false, then the team
  34. CharTeam.where(team_id: id).update(active: false)
  35. update(active: false)
  36. # Delete Server Role, and move channel
  37. event.server.role(role_id).delete('Archived Team')
  38. event.channel.category=(ENV['TEAM_ARCHIVES'])
  39. end
  40. end