Skip to content

Commit

Permalink
Add equip method and adapt it to gist
Browse files Browse the repository at this point in the history
  • Loading branch information
dottolodaniela committed Feb 4, 2021
1 parent 9793987 commit a041749
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 57 deletions.
16 changes: 12 additions & 4 deletions app/channels/game_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,19 @@ def draw_door_card()
end

def equip_monster(params)

# pp params
result = Monstercard.equip_monster(params)
pp result.type
broadcast_to(@gameboard, { type: result.type, params: { message: result.message } })
player = Player.find_by('user_id = ?', current_user.id)
result = Monstercard.equip_monster(params, player)
pp result[:type]

updated_board = Gameboard.broadcast_game_board(@gameboard)
broadcast_to(@gameboard, { type: result[:type], message: result[:message], params: updated_board })

pp player.monsterone.ingamedecks
pp player
# pp player.monstertwo.ingamedecks
# pp player.monsterthree.ingamedecks

end

def attack()
Expand Down
53 changes: 0 additions & 53 deletions app/controllers/gamemethods_controller.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,6 @@
# frozen_string_literal: true

class GamemethodsController < ApplicationController
def equip_monster
# TODO: frontend schickt player und unique card_id und monsterslot
player = Player.find(params[:player_id])
gameboard_id = params[:gameboard_id]
card_id = params[:deck_id]
monsterslot = params[:monsterslot]

# define which monster
monster_to_equip = case monsterslot
when 'Monsterone'
player.monsterone
when 'Monstertwo'
player.monstertwo
else
player.monsterthree
end

# find ingamedeck card (gameboard_id nur zusatz, wenn vom frontend unique id kommt sollte mans nicht brauchen)
deck_card = Ingamedeck.find_by('id=? AND gameboard_id=?', card_id, gameboard_id)

# find "original" card, only advance if found
unless deck_card.nil?
card = Card.find_by('id=?', deck_card.card_id)

# TODO: validieren
cardtype = card.type

# there already are 5 items, you can't put any mor (6 because the monster itself is in this table)
if monster_to_equip.cards.count == 6
puts '**************'
error_message = "sorry you can't put any more items on this monster"
# category already on monster
elsif monster_to_equip.cards.where('item_category=?', card.item_category).count.positive?
puts '**************'
error_message = "nono not allowed, you already have #{card.item_category}"
# not an item
elsif cardtype != 'Itemcard'
puts '**************'
error_message = "sorry you can't put anything on your monster that's not an item"
# yay
else
puts '************** created Card *************'
deck_card.update_attribute(:cardable_type, monsterslot)
puts '************** created Card *************'
error_message = 'no problem'
end
end

error_message = 'Something went wrong with finding the card' if error_message.nil?

player_atk = monster_to_equip.cards.sum(:atk_points)
render json: { card_to_add: card, result: error_message, akt_points: player_atk, player_cards: monster_to_equip.cards }, status: 200
end

def draw_random_lvl_one
monstercards = Monstercard.all.where('level=?', 1).pluck(:id).sample
Expand Down
76 changes: 76 additions & 0 deletions app/models/monstercard.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,81 @@
# frozen_string_literal: true
require "pp"

class Monstercard < Card
validates :title, :description, :image, :action, :draw_chance, :level, :element, :bad_things, :rewards_treasure, :atk_points, :level_amount, :type, presence: true

def self.equip_monster(params, player)

#TODO frontend schickt player und unique card_id und monsterslot
# player = Player.find_by("id=?", params["player_id"])
# gameboard = Gameboard.find_by("id=?", params["gameboard_id"])

deck_card = Ingamedeck.find_by("id=?", params["unique_equip_id"])
monsterslot = Ingamedeck.find_by("id=?", params["unique_monster_id"])
# deck_card = Ingamedeck.find_by("id=?", card_id)


pp player
pp deck_card
pp monsterslot

# pp player.monstertwo.cards

#define which monster
case monsterslot
when "Monsterone"
monster_to_equip = player.monsterone
when "Monstertwo"
monster_to_equip = player.monstertwo
else
monster_to_equip = player.monsterthree
end

pp monster_to_equip
# type = "ERROR"

# #find "original" card, only advance if found
unless deck_card.nil?
card = Card.find_by("id=?", deck_card.card_id)
#TODO validieren
cardtype = card.type

pp card
# # there already are 5 items, you can't put any mor (6 because the monster itself is in this table)
if monster_to_equip.cards.count == 6
type = "ERROR"
message = "You can't put any more items on this monster."
# GameChannel.broadcast_to(gameboard, {type: 'ERROR', params: { message: "You can't put any more items on this monster." } })

# category already on monster
elsif monster_to_equip.cards.where("item_category=?", card.item_category).count > 0
type = "ERROR"
message = "You already have this type of item on your monster! (#{card.item_category})"
# GameChannel.broadcast_to(gameboard, {type: 'ERROR', params: { message: "You already have this type of item on your monster! (#{card.item_category})" } })

# not an item
elsif cardtype != "Itemcard"
type = "ERROR"
message = "Sorry, you can't put anything on your monster that is not an item!"
# GameChannel.broadcast_to(gameboard, {type: 'ERROR', params: { message: "Sorry, you can't put anything on your monster that is not an item!"} })

# yay
else
type = "GAMEBOARD_UPDATE"
deck_card.update_attribute(:cardable_type, params["slot"])
player_atk = monster_to_equip.cards.sum(:atk_points)
player.update_attribute(:attack, player_atk)
message = "Successfully equipped."
# GameChannel.broadcast_to(gameboard, {type: 'GAMEBOARD_UPDATE', params: Gameboard.broadcast_game_board(gameboard) })
end
end

if deck_card == nil
type = "ERROR"
message = "Card not found. Something went wrong."
# GameChannel.broadcast_to(gameboard, {type: 'ERROR', params: { message: "Card not found"} })
end
# type = "equip"
{type: type, message: message}
end
end

0 comments on commit a041749

Please sign in to comment.