Skip to content

Commit

Permalink
Merge pull request #68 from cardeons/develop
Browse files Browse the repository at this point in the history
added_game_test_methods
  • Loading branch information
Moarales authored Feb 4, 2021
2 parents c47ae09 + 9bd00a5 commit 3a10817
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 93 deletions.
84 changes: 46 additions & 38 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 All @@ -69,40 +77,40 @@ def attack()
broadcast_to(@gameboard, { type: BOARD_UPDATE, params: updated_board })
end

def play_card(params)
# add actions!

paramsObject = JSON.parse params
puts paramsObject

broadcast_to(@gameboard, { type: DEBUG, params: { message: 'You just used play_card with ', params: paramsObject } })

case paramsObject.to
when 'Inventory'
broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to inventory" } })
current_card = Ingamedeck.find_by('id=?', paramsObject.unique_id)
current_card.update_attribute(:cardable_type, 'Inventory')
when 'Monsterone'
broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to monsterone" } })
current_card = Ingamedeck.find_by('id=?', paramsObject.unique_id)
current_card.update_attribute(:cardable_type, 'Monsterone')
when 'Monstertwo'
broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to monstertwo" } })
current_card = Ingamedeck.find_by('id=?', paramsObject.unique_id)
current_card.update_attribute(:cardable_type, 'Monstertwo')
when 'Monsterthree'
broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to monsterthree" } })
current_card = Ingamedeck.find_by('id=?', paramsObject.unique_id)
current_card.update_attribute(:cardable_type, 'Monsterthree')
when 'center'
broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to center" } })
# TODO: currently not implemented
else
broadcast_to(@gameboard, { type: ERROR, params: { message: "Player #{current_user.email} just played to something i dont know" } })
end

broadcast_to(@gameboard, { type: BOARD_UPDATE, params: Gameboard.broadcast_game_board(@gameboard) })
end
# def play_card(params)
# # add actions!

# paramsObject = JSON.parse params
# puts paramsObject

# broadcast_to(@gameboard, { type: DEBUG, params: { message: 'You just used play_card with ', params: paramsObject } })

# case paramsObject.to
# when 'Inventory'
# broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to inventory" } })
# current_card = Ingamedeck.find_by('id=?', paramsObject.unique_id)
# current_card.update_attribute(:cardable_type, 'Inventory')
# when 'Monsterone'
# broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to monsterone" } })
# current_card = Ingamedeck.find_by('id=?', paramsObject.unique_id)
# current_card.update_attribute(:cardable_type, 'Monsterone')
# when 'Monstertwo'
# broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to monstertwo" } })
# current_card = Ingamedeck.find_by('id=?', paramsObject.unique_id)
# current_card.update_attribute(:cardable_type, 'Monstertwo')
# when 'Monsterthree'
# broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to monsterthree" } })
# current_card = Ingamedeck.find_by('id=?', paramsObject.unique_id)
# current_card.update_attribute(:cardable_type, 'Monsterthree')
# when 'center'
# broadcast_to(@gameboard, { type: DEBUG, params: { message: "Player #{current_user.email} just played to center" } })
# # TODO: currently not implemented
# else
# broadcast_to(@gameboard, { type: ERROR, params: { message: "Player #{current_user.email} just played to something i dont know" } })
# end

# broadcast_to(@gameboard, { type: BOARD_UPDATE, params: Gameboard.broadcast_game_board(@gameboard) })
# end

def move_card(params)
unique_card_id = params['unique_card_id']
Expand Down
44 changes: 43 additions & 1 deletion app/channels/lobby_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,50 @@ def createNewTestGame
player1 = Player.create(name: "#{x}2", gameboard: gameboard_test, user: u1)
player2 = Player.create(name: "#{x}3", gameboard: gameboard_test, user: u2)
player3 = Player.create(name: "#{x}4", gameboard: gameboard_test, user: u3)
end

playercurse1 = Playercurse.create(player: player1)
playercurse2 = Playercurse.create(player: player2)
playercurse3 = Playercurse.create(player: player3)

p1i = Inventory.create(player: player1)
p2i = Inventory.create(player: player2)
p3i = Inventory.create(player: player3)

p1m1 = Monsterone.create(player: player1)
p2m1 = Monsterone.create(player: player2)
p3m1 = Monsterone.create(player: player3)

p1m2 = Monstertwo.create(player: player1)
p2m2 = Monstertwo.create(player: player2)
p3m2 = Monstertwo.create(player: player3)

p1m3 = Monsterthree.create(player: player1)
p2m3 = Monsterthree.create(player: player2)
p3m3 = Monsterthree.create(player: player3)

p1c = Ingamedeck.create(gameboard: gameboard_test, card: Cursecard.first, cardable: playercurse1)
p2c = Ingamedeck.create(gameboard: gameboard_test, card: Cursecard.first, cardable: playercurse2)
p3c = Ingamedeck.create(gameboard: gameboard_test, card: Cursecard.first, cardable: playercurse3)

p1i1 = Ingamedeck.create(gameboard: gameboard_test, card: Itemcard.first, cardable: p1i)
p1i2 = Ingamedeck.create(gameboard: gameboard_test, card: Itemcard.first, cardable: p1i)
p2i1 = Ingamedeck.create(gameboard: gameboard_test, card: Itemcard.first, cardable: p2i)
p2i2 = Ingamedeck.create(gameboard: gameboard_test, card: Itemcard.first, cardable: p2i)
p3i1 = Ingamedeck.create(gameboard: gameboard_test, card: Itemcard.first, cardable: p3i)

p1m1 = Ingamedeck.create!(gameboard: gameboard_test, card: Monstercard.first, cardable: p1m1)
p1m2 = Ingamedeck.create!(gameboard: gameboard_test, card: Monstercard.first, cardable: p1m2)
p1m3 = Ingamedeck.create!(gameboard: gameboard_test, card: Monstercard.first, cardable: p1m3)

p2m1 = Ingamedeck.create!(gameboard: gameboard_test, card: Monstercard.first, cardable: p2m1)
p2m2 = Ingamedeck.create!(gameboard: gameboard_test, card: Monstercard.first, cardable: p2m2)
p2m3 = Ingamedeck.create!(gameboard: gameboard_test, card: Monstercard.first, cardable: p2m3)

p3m1 = Ingamedeck.create!(gameboard: gameboard_test, card: Monstercard.first, cardable: p3m1)
p3m2 = Ingamedeck.create!(gameboard: gameboard_test, card: Monstercard.first, cardable: p3m2)
p3m3 = Ingamedeck.create!(gameboard: gameboard_test, card: Monstercard.first, cardable: p3m3)
end

def deliver_error_message(e)
broadcast_to(@gameboard, { type: 'ERROR', params: { message: e } })
end
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
30 changes: 29 additions & 1 deletion app/models/gameboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def self.renderGameboard(gameboard)
if gameboard.centercard.ingamedecks.any?
centercard = gameboard.centercard.ingamedecks.first.id
else
centercard = []
centercard = nil
end

{
Expand Down Expand Up @@ -282,4 +282,32 @@ def self.attack(gameboard)

message
end



def self.reset_all_game_boards()
Ingamedeck.all.where(cardable_type: "Centercard").destroy_all

Gameboard.all.each do |gameboard|

player_id_current = gameboard.current_player


if player_id_current
current_player = Player.find(player_id_current)
current_player.handcard.ingamedecks.delete_all
current_player.inventory.ingamedecks.delete_all
current_player.monsterone.ingamedecks.delete_all
current_player.monstertwo.ingamedecks.delete_all
current_player.monsterthree.ingamedecks.delete_all

Handcard.draw_handcards(current_player.id, gameboard)

# updated_board = Gameboard.broadcast_game_board(gameboard)
# GameChannel.broadcast_to(gameboard, { type: "BOARD_UPDATE", params: updated_board })
# PlayerChannel.broadcast_to(current_player.user, { type: 'HANDCARD_UPDATE', params: { handcards: Gameboard.renderCardId(current_player.handcard.ingamedecks) } })
end
end

end
end
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 3a10817

Please sign in to comment.