users.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. class User < ActiveRecord::Base
  2. validates :id, presence: true
  3. def make_stats
  4. stats = {}
  5. stats['hp_base'] = rand(1..184)
  6. stats['s_base'] = 185 - stats['hp_base']
  7. stats['a_base'] = rand(1..184)
  8. stats['sa_base'] = 185 - stats['a_base']
  9. stats['d_base'] = rand(1..184)
  10. stats['sd_base'] = 185 - stats['d_base']
  11. self.update(stats)
  12. self.update(
  13. hp_iv: rand(0..31),
  14. a_iv: rand(0..31),
  15. d_iv: rand(0..31),
  16. sa_iv: rand(0..31),
  17. sd_iv: rand(0..31),
  18. s_iv: rand(0..31),
  19. nature: rand(1..25)
  20. )
  21. self.reload
  22. n = Nature.find(nature)
  23. stats = stat_calc(n.up_stat, n.down_stat)
  24. self.update(stats)
  25. self.reload
  26. self
  27. end
  28. def update_xp(msg, user=nil)
  29. xp =
  30. case msg.length
  31. when 0..39 then 0
  32. when 40..149 then 1
  33. when 150..299 then 2
  34. when 300..599 then 3
  35. when 600..999 then 4
  36. else 5
  37. end
  38. self.update(
  39. boosted_xp: boosted_xp + xp,
  40. unboosted_xp: unboosted_xp + xp
  41. )
  42. self.reload
  43. reply = level_up(user) if next_level && boosted_xp > next_level
  44. reply
  45. end
  46. def level_up(user=nil)
  47. if level < 100
  48. next_level = (level + 6) ** 3 / 10.0
  49. self.update(level: level + 1, next_level: next_level.round)
  50. self.reload
  51. else
  52. self.update(level: level + 1, next_level: nil)
  53. self.reload
  54. end
  55. n = Nature.find(nature)
  56. stats = stat_calc(n.up_stat, n.down_stat)
  57. img = stat_image(self, user, stats) if user
  58. self.update(stats)
  59. self.reload
  60. img
  61. end
  62. def stat_calc(up_stat, down_stat)
  63. stats = {}
  64. stats['hp'] =
  65. ((2 * hp_base + hp_iv + (hp_ev / 4)) * level) / 100 + level + 10
  66. stats['attack'] =
  67. (((2 * a_base + a_iv + (a_ev / 4)) * level) / 100) + 5
  68. stats['defense'] =
  69. (((2 * d_base + d_iv + (d_ev / 4)) * level) / 100) + 5
  70. stats['sp_attack'] =
  71. (((2 * sa_base + sa_iv + (sa_ev / 4)) * level) / 100) + 5
  72. stats['sp_defense'] =
  73. (((2 * sd_base + sd_iv + (sd_ev / 4)) * level) / 100) + 5
  74. stats['speed'] =
  75. (((2 * s_base + s_iv + (s_ev / 4)) * level) / 100) + 5
  76. case up_stat
  77. when /attack/ then stats['attack'] = (stats['attack'] * 1.1).round
  78. when /defense/ then stats['defense'] = (stats['defense'] * 1.1).round
  79. when /sp_attack/ then stats['sp_attack'] = (stats['sp_attack'] * 1.1).round
  80. when /sp_defense/ then stats['sp_defense'] = (stats['sp_defense'] * 1.1).round
  81. when /speed/ then stats['speed'] = (stats['speed'] * 1.1).round
  82. end
  83. case down_stat
  84. when /attack/ then stats['attack'] = (stats['attack'] * 0.9).round
  85. when /defense/ then stats['defense'] = (stats['defense'] * 0.9).round
  86. when /sp_attack/ then stats['sp_attack'] = (stats['sp_attack'] * 0.9).round
  87. when /sp_defense/ then stats['sp_defense'] = (stats['sp_defense'] * 0.9).round
  88. when /speed/ then stats['speed'] = (stats['speed'] * 0.9).round
  89. end
  90. stats
  91. end
  92. def self.import_user(file)
  93. file.each do |line|
  94. usr = line.split(",")
  95. id = usr[0]
  96. b_xp = usr[3].to_i
  97. ub_xp = usr[17].to_i > 0 ? usr[17].to_i : usr[3].to_i
  98. if user = User.find_by(id: id)
  99. user.update(
  100. level: 1,
  101. next_level: 22,
  102. boosted_xp: b_xp,
  103. unboosted_xp: ub_xp
  104. )
  105. user.reload
  106. else
  107. user = User.create(
  108. id: id,
  109. level: 1,
  110. next_level: 22,
  111. boosted_xp: b_xp,
  112. unboosted_xp: ub_xp
  113. )
  114. end
  115. user = user.make_stats
  116. until user.next_level > user.boosted_xp
  117. user.level_up
  118. end
  119. end
  120. end
  121. end