users.rb 3.4 KB

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