Selaa lähdekoodia

implement character inventories

Andrew Swistak 6 vuotta sitten
vanhempi
commit
5d9c4b716f
7 muutettua tiedostoa jossa 91 lisäystä ja 355 poistoa
  1. 1 0
      README.md
  2. 2 2
      app/models/characters.rb
  3. 22 0
      app/models/inventory.rb
  4. 3 3
      app/responses/app.rb
  5. 14 0
      app/responses/character.rb
  6. 49 6
      bot.rb
  7. 0 344
      db/schema.sql

+ 1 - 0
README.md

@@ -34,6 +34,7 @@ $ pg_dump -O -f db/schma.sql [db_name]  # Exports schema
   * Display all kinds of information about guild members
   * Accepts Item applications
   * Displays information about existing Items
+  * Keeps track of characters' inventories
 
 ## Setup
 This application runs using Ruby and Postgres. In order to run the bot locally

+ 2 - 2
app/models/characters.rb

@@ -86,8 +86,8 @@ class Character < ActiveRecord::Base
     if user
       member = event.server.member(user_id[1])
 
-      calc_max = (user.level / 10 + 1)
-      allowed_chars = calc_max > 6 ? 6 : calc_max
+      allowed_chars = (user.level / 10 + 1)
+      allowed_chars += 1 if member.roles.map(&:name).include?("Nitro Booster")
       active_chars =
         Character.where(user_id: user_id[1]).where(active: "Active")
       active_chars = active_chars.map(&:edit_url)

+ 22 - 0
app/models/inventory.rb

@@ -0,0 +1,22 @@
+class Inventory < ActiveRecord::Base
+  validates :item_id, presence: true
+
+  def self.update_inv(item, amount, char)
+    i = Inventory.where(char_id: char.id).find_by(item_id: item.id)
+
+    if i.nil? && amount > 0
+      Inventory.create(char_id: char.id, amount: amount, item_id: item.id)
+    elsif i && i.amount + amount > 0
+      i.update(amount: i.amount + amount)
+      i.reload
+    elsif i && i.amount + amount == 0
+      i.delete
+      true
+    else
+      error_embed(
+        "Cannot update inventory!",
+        "#{char.name} does not have enough #{item.name}"
+      )
+    end
+  end
+end

+ 3 - 3
app/responses/app.rb

@@ -1,7 +1,7 @@
 def new_app_embed(user_name, color = nil)
   desc = "Hi, #{user_name},\nI see you'd like to start a new character" +
     " application!\nI'll send you instructions in a dm!"
-  Embed.new(
+  embed = Embed.new(
     title: "New Appliction!",
     description: desc
   )
@@ -11,7 +11,7 @@ def new_app_embed(user_name, color = nil)
 end
 
 def new_app_dm(user_name, code, color = nil)
-  Embed.new(
+  embed = Embed.new(
     title: "Hi, #{user_name}",
     description: "If you have any questions, feel free to ask a Guildmaster!",
     footer: {
@@ -28,7 +28,7 @@ def new_app_dm(user_name, code, color = nil)
 end
 
 def edit_app_embed(user_name, char_name, color = nil)
-  Embed.new(
+  embed = Embed.new(
     title: "You want to edit #{char_name}?",
     description: "Good news, #{user_name}! I'll dm you a link"
   )

+ 14 - 0
app/responses/character.rb

@@ -44,6 +44,9 @@ def character_embed(char:, img: nil, user:, color:, section: nil)
     else
       embed.description = "No character images found!"
     end
+  when :bags
+    bags = Inventory.where(char_id: char.id)
+    fields = char_inv(bags, fields)
   end
 
 
@@ -137,6 +140,17 @@ def char_status(char, fields)
   fields
 end
 
+def char_inv(bags, fields)
+  inv = []
+  bags.each do |line|
+    item = Item.find(line.item_id)
+    inv_line = line.amount > 1 ? "#{item.name} [#{line.amount}]" : item.name
+    inv.push(inv_line)
+  end
+
+  fields.push({ name: "Bags", value: inv.join("\n"), inline: true })
+end
+
 def char_sections(fields)
   CharCarousel::REACTIONS.map do |emoji, message|
     fields.push({

+ 49 - 6
bot.rb

@@ -10,7 +10,7 @@ Bundler.require(:default, BOT_ENV)
 
 require 'active_record'
 
-# Constants: such as roles and channel ids
+# Constants: such as roles and colors and regexes
 
 DISCORD = "#36393f"
 ERROR = "#a41e1f"
@@ -41,8 +41,6 @@ Dir['./lib/*.rb'].each { |f| require f }
 token = ENV['DISCORD_BOT_TOKEN']
 bot = Discordrb::Bot.new(token: token)
 
-# Methods: define basic methods here
-# ---
 
 # Commands: structure basic bot commands here
 commands = []
@@ -273,7 +271,7 @@ opts = {
 }
 desc = "Display info about the guild members"
 member = Command.new(:member, desc, opts) do |event, name, section, keyword|
-  sections = [:all, :default, :bio, :type, :status, :rumors, :image]
+  sections = [:all, :default, :bio, :type, :status, :rumors, :image, :bags]
 
   case name
   when UID
@@ -415,6 +413,32 @@ rescue ActiveRecord::RecordNotFound
   error_embed("Item Not Found!")
 end
 
+inv = Command.new(:inv, desc, opts) do |event, item, amount, name|
+  char = Character.find_by!(name: name) if name
+  itm = Item.find_by!(name: item) if item
+  amt = amount.to_i
+
+  case
+  when char && itm && amt
+    i = Inventory.update_inv(itm, amt, char)
+    user = event.server.member(char.user_id.to_i)
+    color = CharacterController.type_color(char)
+
+    case i
+    when Inventory, true
+      character_embed(char: char, user: user, color: color, section: :bags)
+    when Embed
+      i
+    else
+      error_embed("Something went wrong!", "Could not update inventory")
+    end
+  else
+    command_error_embed("Could not proccess your request", inv)
+  end
+rescue ActiveRecord::RecordNotFound => e
+  error_embed(e.message)
+end
+
 # ---
 
 commands = [
@@ -425,9 +449,12 @@ commands = [
   poll,
   raffle,
   member,
-  item
+  item,
+  inv
 ]
 
+locked_commands = [inv]
+
 # This will trigger on every message sent in discord
 bot.message do |event|
   content = event.message.content
@@ -497,7 +524,7 @@ bot.reaction_add do |event|
   maj = 1
 
   form =
-    case app.author.name
+    case app.author&.name
     when 'New App' then :new_app
     when 'Character Application' then :character_application
     when 'Character Rejection' then :character_rejection
@@ -750,6 +777,22 @@ bot.reaction_add do |event|
     event.message.edit("", embed)
     arrow_react(event.message)
   when [:carousel, :bags]
+    emoji = Emoji::BAGS
+    users = event.message.reacted_with(emoji)
+
+    users.each do |user|
+      event.message.delete_reaction(user.id, emoji) unless user.current_bot?
+    end
+
+    char = Character.find(carousel.char_id)
+    embed = character_embed(
+      char: char,
+      img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
+      user: event.server.member(char.user_id),
+      color: CharacterController.type_color(char),
+      section: :bags
+    )
+    event.message.edit("", embed)
   when [:carousel, :family]
   when [:carousel, :eyes]
     emoji = Emoji::EYES

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 344
db/schema.sql


Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä