Преглед на файлове

add all the characters, commit sepuku

Kylie Jo Swistak преди 6 години
родител
ревизия
30e9785368
променени са 6 файла, в които са добавени 488 реда и са изтрити 61 реда
  1. 7 7
      app/models/characters.rb
  2. 3 0
      app/models/natures.rb
  3. 136 0
      app/models/users.rb
  4. 23 14
      app/responses/character.rb
  5. 73 31
      bot.rb
  6. 246 9
      db/schema.sql

+ 7 - 7
app/models/characters.rb

@@ -97,9 +97,13 @@ class Character < ActiveRecord::Base
     user = User.find_by(id: user_id[1])
     member = event.server.member(user_id[1]) if user
 
+    allowed_chars = (user.level / 10 + 1) if user
+
     if member
-      allowed_chars = (user.level / 10 + 1)
       allowed_chars += 1 if member.roles.map(&:name).include?("Nitro Booster")
+    end
+
+    if user
       active_chars =
         Character.where(user_id: user_id[1]).where(active: "Active")
       active_chars = active_chars.map(&:edit_url)
@@ -108,13 +112,9 @@ class Character < ActiveRecord::Base
         active == "Personal Character" && !active_chars.include?(edit_url)
 
       too_many = new_active ? active_chars.count >= allowed_chars : false
-    elsif user && !member
-      approval_react(event)
-    end
+      approval_react(event) unless too_many
 
-    if user && member
-      too_many ?
-        too_many(event, member, edit_url, 'characters') : approval_react(event)
+      too_many(event, member, edit_url, 'characters') if too_many && member
     else
       unknown_member(event)
     end

+ 3 - 0
app/models/natures.rb

@@ -0,0 +1,3 @@
+class Nature < ActiveRecord::Base
+  validates :name, presence: true
+end

+ 136 - 0
app/models/users.rb

@@ -1,3 +1,139 @@
 class User < ActiveRecord::Base
   validates :id, presence: true
+
+  def make_stats
+    stats = {}
+
+    stats['hp_base'] = rand(1..184)
+    stats['s_base'] = 185 - stats['hp_base']
+    stats['a_base'] = rand(1..184)
+    stats['sa_base'] = 185 - stats['a_base']
+    stats['d_base'] = rand(1..184)
+    stats['sd_base'] = 185 - stats['d_base']
+
+    self.update(stats)
+    self.update(
+      hp_iv: rand(0..31),
+      a_iv: rand(0..31),
+      d_iv: rand(0..31),
+      sa_iv: rand(0..31),
+      sd_iv: rand(0..31),
+      s_iv: rand(0..31),
+      nature: rand(1..25)
+    )
+
+    self.reload
+
+    n = Nature.find(nature)
+    stats = stat_calc(n.up_stat, n.down_stat)
+    self.update(stats)
+
+    self.reload
+    self
+  end
+
+  def update_xp(msg)
+    xp =
+      case msg.length
+      when 0..40 then 0
+      when 40..149 then 1
+      when 150..299 then 2
+      when 300..599 then 3
+      when 600..1000 then 4
+      else 5
+      end
+
+    self.update(
+      boosted_xp: boosted_xp + xp,
+      unboosted_xp: unboosted_xp + xp
+    )
+
+    self.reload
+    level_up if next_level && boosted_xp > next_level
+
+    self
+  end
+
+  def level_up
+    if level < 100
+      next_level = (level + 6) ** 3 / 10.0
+      self.update(level: level + 1, next_level: next_level.round)
+    else
+      self.update(level: level + 1, next_level: nil)
+    end
+
+    n = Nature.find(nature)
+
+    stats = stat_calc(n.up_stat, n.down_stat)
+    self.update(stats)
+    self.reload
+
+    self
+  end
+
+  def stat_calc(up_stat, down_stat)
+    stats = {}
+    stats['hp'] =
+      ((2 * hp_base + hp_iv + (hp_ev / 4)) * level) / 100 + level + 10
+    stats['attack'] =
+      (((2 * a_base + a_iv + (a_ev / 4)) * level) / 100) + 5
+    stats['defense'] =
+      (((2 * d_base + d_iv + (d_ev / 4)) * level) / 100) + 5
+    stats['sp_attack'] =
+      (((2 * sa_base + sa_iv + (sa_ev / 4)) * level) / 100) + 5
+    stats['sp_defense'] =
+      (((2 * sd_base + sd_iv + (sd_ev / 4)) * level) / 100) + 5
+    stats['speed'] =
+      (((2 * s_base + s_iv + (s_ev / 4)) * level) / 100) + 5
+
+    case up_stat
+    when /attack/ then stats['attack'] = stats['attack'] * 1.1
+    when /defense/ then stats['defense'] = stats['defense'] * 1.1
+    when /sp_attack/ then stats['sp_attack'] = stats['sp_attack'] * 1.1
+    when /sp_defense/ then stats['sp_defense'] = stats['sp_defense'] * 1.1
+    when /speed/ then stats['speed'] = stats['speed'] * 1.1
+    end
+
+    case down_stat
+    when /attack/ then stats['attack'] = stats['attack'] * 0.9
+    when /defense/ then stats['defense'] = stats['defense'] * 0.9
+    when /sp_attack/ then stats['sp_attack'] = stats['sp_attack'] * 0.9
+    when /sp_defense/ then stats['sp_defense'] = stats['sp_defense'] * 0.9
+    when /speed/ then stats['speed'] = stats['speed'] * 0.9
+    end
+
+    stats
+  end
+
+  def self.import_user(file)
+    file.each do |line|
+      usr = line.split(",")
+      id = usr[0]
+      b_xp = usr[3].to_i
+      ub_xp = usr[17].to_i > 0 ? usr[17].to_i : usr[3].to_i
+
+      if user = User.find_by(id: id)
+        user.update(
+          level: 1,
+          next_level: 22,
+          boosted_xp: b_xp,
+          unboosted_xp: ub_xp
+        )
+        user.reload
+      else
+        user = User.create(
+          id: id,
+          level: 1,
+          next_level: 22,
+          boosted_xp: b_xp,
+          unboosted_xp: ub_xp
+        )
+      end
+
+      user = user.make_stats
+      until user.next_level > user.boosted_xp
+        user.level_up
+      end
+    end
+  end
 end

+ 23 - 14
app/responses/character.rb

@@ -2,7 +2,7 @@ def character_embed(char:, img: nil, user: nil, color:, section: nil)
   fields = []
   user_name = case user
               when String
-                user.capitalize
+                'Adopt Me!'
               when nil
                 'Unknown User'
               else
@@ -186,7 +186,8 @@ def char_list_embed(chars, user = nil)
   fields = []
   active = []
   inactive= []
-  npcs = []
+  owned_npcs = []
+  unowned_npcs = []
 
   chars.each do |char|
     case char.active
@@ -195,27 +196,33 @@ def char_list_embed(chars, user = nil)
     when 'Inactive'
       inactive.push char.name
     when 'NPC'
-      npcs.push char.name
+      owned_npcs.push char.name if char.user_id != 'Public'
+      unowned_npcs.push char.name if char.user_id == 'Public'
     end
   end
 
   fields.push({
-    name: 'Active Characters',
-    value: active.join(", ")
+    name: "Active Guild Members [#{active.count}]",
+    value: active.sort.join(", ")
   })if active.length > 0
 
   fields.push({
-    name: 'Inactive Characters',
-    value: inactive.join(", ")
+    name: "Former Guild Members [#{inactive.count}]",
+    value: inactive.sort.join(", ")
   })if inactive.length > 0
 
   fields.push({
-    name: 'NPCs',
-    value: npcs.join(", ")
-  })if npcs.length > 0
+    name: "NPCs [#{owned_npcs.count}]",
+    value: owned_npcs.sort.join(", ")
+  })if owned_npcs.length > 0
+
+  fields.push({
+    name: "Public NPCs [#{unowned_npcs.count}]",
+    value: unowned_npcs.sort.join(", ")
+  })if unowned_npcs.length > 0
 
   embed = Embed.new(
-    title: 'Registered Characters',
+    title: "Registered Pokemon [#{chars.count}]",
     fields: fields
   )
 
@@ -234,7 +241,7 @@ def user_char_embed(chars, user)
   active = []
   inactive = []
   npcs = []
-  user_name = user.nickname || user.name
+  user_name = user&.nickname || user&.name
 
   chars.each do |char|
     case char.active
@@ -265,13 +272,15 @@ def user_char_embed(chars, user)
     fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
   end
 
+  allowed = User.find_by(id: chars.first.user_id).level / 10 + 1
+
   embed = Embed.new(
-    title: "#{user_name}'s Characters",
+    title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
     description: "Click on the corresponding reaction to view the character",
     fields: fields
   )
 
-  embed.color = user.color.combined if user.color
+  embed.color = user.color.combined if user&.color
   embed
 end
 

+ 73 - 31
bot.rb

@@ -16,6 +16,7 @@ DISCORD = "#36393f"
 ERROR = "#a41e1f"
 
 UID = /<@\!?([0-9]+)>/
+URL = /https?:\/\/[\S]+/
 
 # ---
 
@@ -592,12 +593,17 @@ team = Command.new(:team, desc, opts) do |event, team_name, action, desc|
       nil
     )
   when /apply/i
-    embed = team_app_embed(t, char, event.server.member(char.user_id))
-    msg = bot.send_message(t.channel.to_i, "", false, embed)
-
-    msg.react(Emoji::Y)
-    msg.react(Emoji::N)
-    success_embed("Your request has been posted to #{t.name}!")
+    members = CharTeam.where(team_id: t.id)
+    if members.count >= 6
+      embed = team_app_embed(t, char, event.server.member(char.user_id))
+      msg = bot.send_message(t.channel.to_i, "", false, embed)
+
+      msg.react(Emoji::Y)
+      msg.react(Emoji::N)
+      success_embed("Your request has been posted to #{t.name}!")
+    else
+      error_embed("#{t.name} is full!")
+    end
   when /create/i
     team_name = team_name || ""
     desc = desc || ""
@@ -647,27 +653,48 @@ bot.message do |event|
   command = /^pkmn-(\w+)/.match(content)
   cmd = commands.detect { |c| c.name == command[1].to_sym } if command
 
-  reply = cmd.call(content, event) if cmd
+  if cmd
+    reply = cmd.call(content, event)
 
-  case reply
-  when Embed
-    event.send_embed("", reply)
-  when String
-    event.respond(reply)
-  end
-
-  event.send_embed(
-    "",
-    error_embed("Command not found!")
-  )if command && !cmd && event.server
-
-  if author == ENV['WEBHOOK'].to_i
+    case reply
+    when Embed
+      event.send_embed("", reply)
+    when String
+      event.respond(reply)
+    end
+  elsif command && !cmd && event.server
+    event.send_embed(
+      "",
+      error_embed("Command not found!")
+    )
+  elsif author == ENV['WEBHOOK'].to_i
     app = event.message.embeds.first
     if app.author.name == 'Character Application'
       Character.check_user(event)
     else
       approval_react(event)
     end
+  #elsif content == 'test'
+    #User.import_user(File.open('users.txt', 'r'))
+  elsif content == 'pry'
+    binding.pry
+  elsif content == 'show me dem illegals'
+    users = User.all
+    illegals = []
+
+    users.each do |u|
+      allowed = u.level / 10 + 1
+      active = Character.where(user_id: u.id, active: 'Active').count
+      illegals.push("<@#{u.id}>, Level #{u.level}: #{active}/#{allowed}") if active > allowed
+    end
+    embed = error_embed("Members with too many pokemon", illegals.join("\n"))
+    event.send_embed("", embed)
+  elsif !event.author.current_bot?
+    usr = User.find_by(id: author.to_s)
+    msg = URL.match(content) ? content.gsub(URL, "x" * 149) : content
+
+    usr = usr.update_xp(msg)
+    event.respond(usr) if usr.is_a? String
   end
 end
 
@@ -700,24 +727,35 @@ bot.reaction_add do |event|
   carousel = Carousel.find_by(message_id: event.message.id)
 
   carousel = Carousel.find_by(message_id: event.message.id)
-  maj =
-    if event.server
-      m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
-      m.count / 2
-    end
-  maj = 1
+  maj = 100
 
   form =
     case app.author&.name
     when 'New App' then :new_app
-    when 'Character Application' then :character_application
+    when 'Character Application'
+      m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
+      maj = m.count > 2 ? m.count/2 : 2
+      :character_application
     when 'Character Rejection' then :character_rejection
-    when 'Image Application' then :image_application
+    when 'Image Application'
+      m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
+      maj = m.count > 2 ? m.count/2 : 2
+      :image_application
     when 'Image Rejection' then :image_rejection
-    when 'Item Application' then :item_application
+    when 'Item Application'
+      m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
+      maj = m.count > 2 ? m.count/2 : 2
+      :item_application
     when 'Item Rejection' then :item_rejection
-    when 'Team Application' then :team_application
-    when 'Team Join Request' then :team_request
+    when 'Team Application'
+      m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
+      maj = m.count > 2 ? m.count/2 : 2
+      :team_application
+    when 'Team Join Request'
+      team_id = Team.find_by(channel: event.message.channel.id.to_s).role.to_i
+      m = event.server.roles.find{ |r| r.id == team_id }.members
+      maj = m.count > 2 ? m.count/2 : 2
+      :team_request
     else
       :carousel if carousel
     end
@@ -1261,6 +1299,10 @@ end
 
 # This will trigger when anyone joins the server
 bot.member_join do |event|
+  unless User.find_by(id: event.author.id.to_s)
+    usr = User.create(id: event.author.id.to_s)
+    usr.make_stats
+  end
 end
 
 # This will trigger when anyone leaves the server

Файловите разлики са ограничени, защото са твърде много
+ 246 - 9
db/schema.sql


Някои файлове не бяха показани, защото твърде много файлове са промени