status_controller.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. class StatusController
  2. def self.update_status(name, effect, flag=nil)
  3. # Find Status, if exists
  4. status = Status.find_by('name ilike ?', name)
  5. # Determine action
  6. case flag
  7. when /true/i, nil
  8. if effect.match(/delete/i) && status
  9. # Delete Status
  10. status.destroy
  11. success_embed("Destroyed #{name}")
  12. else
  13. # Update/Create then return embed
  14. update_or_create(status, name, effect, true)
  15. status_details(status)
  16. end
  17. when /false/i
  18. # Update/Create then return embed
  19. update_or_create(status, name, effect, true)
  20. status_details(status)
  21. when /delete/i, /remove/i
  22. # Delete Status
  23. status.destroy
  24. success_embed("Destroyed #{name}")
  25. end
  26. end
  27. def self.update_char_status(character, status, amount=nil)
  28. case status
  29. when /all/i
  30. # Clear all status effects from the user
  31. CharStatus.where(char_id: character.id).destroy_all
  32. when Status
  33. # Find the status effect if it exists
  34. char_status =
  35. CharStatus.where(char_id: character.id).find_by(status_id: status.id)
  36. if char_status && status.amount
  37. raise 'Amount must be a number' if amount == 0
  38. # Update row
  39. new_amount = char_status.amount + amount
  40. if new_amount > 0
  41. # If the value is above 0, update
  42. char_status.update(amount: new_amount)
  43. else
  44. # If the value is 0 or below, delete
  45. char_status.destroy
  46. end
  47. elsif char_status && !status.amount
  48. raise 'Character already has status'
  49. else
  50. raise 'Character did not have status' if amount.to_i < 0
  51. # Create new row
  52. CharStatus.create(
  53. char_id: character.id,
  54. status_id: status.id,
  55. amount: amount
  56. )
  57. end
  58. end
  59. end
  60. def update_or_create(status, name, desc, amount)
  61. # Create or Update
  62. if status
  63. status.update(effect: effect, amount: true)
  64. status.reload
  65. else
  66. status = status.create(name: name, effect: effect, amount: true)
  67. end
  68. end
  69. end