From e81671103470d4c0a5fb501f021d687f401cb8de Mon Sep 17 00:00:00 2001 From: Christoph Mayerhofer Date: Thu, 4 Feb 2021 22:55:20 +0100 Subject: [PATCH 1/2] added reset --- app/channels/game_channel.rb | 68 ++++++++++++++++++------------------ app/models/gameboard.rb | 23 ++++++++++++ 2 files changed, 57 insertions(+), 34 deletions(-) diff --git a/app/channels/game_channel.rb b/app/channels/game_channel.rb index e59739fb..c812d369 100644 --- a/app/channels/game_channel.rb +++ b/app/channels/game_channel.rb @@ -69,40 +69,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'] diff --git a/app/models/gameboard.rb b/app/models/gameboard.rb index 089f83c2..1349421a 100644 --- a/app/models/gameboard.rb +++ b/app/models/gameboard.rb @@ -282,4 +282,27 @@ 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| + gameboard.update(center_card) + + + player_id_current = gameboard.current_player + + current_player = Player.find(user_id_current) + + current_player.handcard.ingamedecks.destroy_all + current_player.inventory.ingamedecks.destroy_all + Handcard.draw_handcards(player.id, gameboard) + + updated_board = Gameboard.broadcast_game_board(gameboard) + Gameboard.broadcast_to(gameboard, { type: "BOARD_UPDATE", params: updated_board }) + end + + end end From 6d5e1cafca8950915c8a99c988ad2045ecf1cba8 Mon Sep 17 00:00:00 2001 From: Christoph Mayerhofer Date: Thu, 4 Feb 2021 23:32:37 +0100 Subject: [PATCH 2/2] added reset method to reset all gameboards --- app/models/gameboard.rb | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app/models/gameboard.rb b/app/models/gameboard.rb index 1349421a..530b5741 100644 --- a/app/models/gameboard.rb +++ b/app/models/gameboard.rb @@ -288,20 +288,25 @@ def self.attack(gameboard) def self.reset_all_game_boards() Ingamedeck.all.where(cardable_type: "Centercard").destroy_all - Gameboard.all.each do |gameboard| - gameboard.update(center_card) - + Gameboard.all.each do |gameboard| player_id_current = gameboard.current_player - current_player = Player.find(user_id_current) - current_player.handcard.ingamedecks.destroy_all - current_player.inventory.ingamedecks.destroy_all - Handcard.draw_handcards(player.id, gameboard) + 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) - Gameboard.broadcast_to(gameboard, { type: "BOARD_UPDATE", params: updated_board }) + # 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