Skip to content

Commit

Permalink
Merge pull request #183 from cardeons/develop
Browse files Browse the repository at this point in the history
Release 0.6
  • Loading branch information
Moarales committed May 5, 2021
2 parents c0075a9 + ea3577d commit 7491e6f
Show file tree
Hide file tree
Showing 55 changed files with 2,694 additions and 11,943 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ Style/GuardClause:
Enabled: false

AllCops:
SuggestExtensions: false
SuggestExtensions: false
Excludes:
- db/**
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ CREATE DATABASE cardeons_backend_development;
CREATE DATABASE cardeons_backend_test;
rails db:migrate
```

### Add Cards:
```sh
rails db:seed cards=cards
```

### Add Dummy Users:
```sh
rails db:seed users=users
```


We are using foreman to start redis/sidekiq and puma in one shell:
```sh
gem install foreman
Expand Down
42 changes: 42 additions & 0 deletions app/channels/friendlist_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

class FriendlistChannel < ApplicationCable::Channel
def subscribed
stream_for current_user

current_user.update(status: :online)

Friendship.broadcast_friends(current_user)
Friendship.broadcast_pending_requests(current_user)
end

def unsubscribed
current_user.update(status: :offline)
end

def send_friend_request(data)
future_friend = User.find_by('id=?', data['friend'])

Friendship.add_friend(current_user, future_friend)

broadcast_to(current_user, { type: 'FRIEND_LOG', params: { message: "You sent a friendrequest to #{future_friend.name}" } })
broadcast_to(future_friend, { type: 'FRIEND_LOG', params: { message: "#{current_user.name} sent you a friendrequest" } })
broadcast_to(future_friend, { type: 'FRIEND_REQUEST', params: { inquirer: current_user.id, inquirer_name: current_user.name } })
end

def accept_friend_request(data)
inquirer = User.find_by('id=?', data['inquirer'])

Friendship.accept(current_user, inquirer)

broadcast_to(current_user, { type: 'FRIEND_LOG', params: { message: "You accepted a friendrequest from #{inquirer.name}" } })
broadcast_to(inquirer, { type: 'FRIEND_LOG', params: { message: "#{current_user.name} accepted your friendrequest" } })
end

def decline_friend_request(data)
inquirer = User.find_by('id=?', data['inquirer'])

Friendship.remove_friend(current_user, inquirer)
broadcast_to(current_user, { type: 'FRIEND_LOG', params: { message: "You declined a friendrequest from #{inquirer.name}" } })
end
end
67 changes: 52 additions & 15 deletions app/channels/game_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ def play_monster(params)
end

def draw_door_card
@gameboard.reload
# if intercept phase is already active player should not be able to draw another card
if @gameboard.intercept_phase? || @gameboard.intercept_finished?
PlayerChannel.broadcast_to(current_user, { type: 'ERROR', params: { message: "You can't draw another card!" } })
return
end

name = Gameboard.draw_door_card(@gameboard)

start_intercept_phase(@gameboard.reload)
Expand Down Expand Up @@ -118,9 +125,9 @@ def attack
player = Player.find_by('user_id = ?', current_user.id)

player_level = player.level
player.update!(level: player_level + 1)
player.update!(level: player_level + @gameboard.reload.centercard.card.level_amount)

if player.level == 5
if player.level >= 5
monster_id = player.win_game(current_user)
@gameboard.game_won!
broadcast_to(@gameboard, { type: 'WIN', params: { player: player.id, monster_won: monster_id } })
Expand All @@ -139,12 +146,12 @@ def attack

PlayerChannel.broadcast_to(helping_player.user, { type: 'HANDCARD_UPDATE', params: { handcards: Gameboard.render_cards_array(helping_player.handcard.ingamedecks) } })
end

msg = "#{current_user.player.name} has killed #{@gameboard.centercard.card.title}"
broadcast_to(@gameboard, { type: GAME_LOG, params: { date: Time.new, message: msg } })

@gameboard.centercard.ingamedeck&.update!(cardable: @gameboard.graveyard)

PlayerChannel.broadcast_to(current_user.reload, { type: 'HANDCARD_UPDATE', params: { handcards: Gameboard.render_cards_array(player.handcard.ingamedecks) } })

Gameboard.get_next_player(@gameboard)
Expand All @@ -162,11 +169,12 @@ def attack
end

def intercept(params)
# params={
# action: "intercept",
# unique_card_id: 1,
# to: 'center_card' | 'current_player'
# }
@gameboard.reload

if @gameboard.centercard.nil?
PlayerChannel.broadcast_to(current_user, { type: 'ERROR', params: { message: "There's no card in the center!" } })
return
end

unique_card_id = params['unique_card_id']
to = params['to']
Expand All @@ -181,7 +189,6 @@ def intercept(params)
end

current_user.player.reload
@gameboard.reload

case to
when 'center_card'
Expand Down Expand Up @@ -250,9 +257,9 @@ def help_call(params)
user_to_broadcast_to = User.where(player: helping_player).first

PlayerChannel.broadcast_to(user_to_broadcast_to,
{ type: 'ASK_FOR_HELP',
params: { player_id: helping_player_id, player_name: current_user.player.name, helping_shared_rewards: helping_shared_reward,
helping_player_attack: helping_player.attack } })
{ type: 'ASK_FOR_HELP',
params: { player_id: helping_player_id, player_name: current_user.player.name, helping_shared_rewards: helping_shared_reward,
helping_player_attack: helping_player.attack } })
end

def answer_help_call(params)
Expand Down Expand Up @@ -333,37 +340,51 @@ def curse_player(params)
end

def develop_add_buff_card
return unless developer_actions_enabled?

card = Buffcard.all.first
current_user.player.handcard.ingamedecks.create(card: card, gameboard: current_user.player.gameboard)
PlayerChannel.broadcast_to(current_user, { type: 'HANDCARD_UPDATE', params: { handcards: Gameboard.render_cards_array(current_user.player.handcard.ingamedecks) } })
end

def develop_add_curse_card
return unless developer_actions_enabled?

card = Cursecard.all.last
current_user.player.handcard.ingamedecks.create(card: card, gameboard: current_user.player.gameboard)
PlayerChannel.broadcast_to(current_user, { type: 'HANDCARD_UPDATE', params: { handcards: Gameboard.render_cards_array(current_user.player.handcard.ingamedecks) } })
end

def develop_add_card_with_id(params)
return unless developer_actions_enabled?

card = Card.find_by('id=?', params['card_id'])
current_user.player.handcard.ingamedecks.create(card: card, gameboard: current_user.player.gameboard)
PlayerChannel.broadcast_to(current_user, { type: 'HANDCARD_UPDATE', params: { handcards: Gameboard.render_cards_array(current_user.player.handcard.ingamedecks) } })
end

def develop_broadcast_handcard_update
return unless developer_actions_enabled?

PlayerChannel.broadcast_to(current_user, { type: 'HANDCARD_UPDATE', params: { handcards: Gameboard.render_cards_array(current_user.player.handcard.ingamedecks) } })
end

def develop_broadcast_gameboard_update
return unless developer_actions_enabled?

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

def develop_set_myself_as_current_player
return unless developer_actions_enabled?

current_user.player.gameboard.update!(current_player: current_user.player)
broadcast_to(@gameboard, { type: BOARD_UPDATE, params: Gameboard.broadcast_game_board(@gameboard.reload) })
end

def develop_set_intercept_false
return unless developer_actions_enabled?

@gameboard.players.each do |player|
player.reload.update!(intercept: false)
end
Expand All @@ -372,6 +393,8 @@ def develop_set_intercept_false
end

def develop_set_myself_as_winner
return unless developer_actions_enabled?

player = Player.find_by('user_id = ?', current_user.id)

player.update!(level: 5)
Expand All @@ -381,9 +404,15 @@ def develop_set_myself_as_winner
broadcast_to(@gameboard, { type: 'WIN', params: { player: player.id, monster_won: monster_id } })
end

def unsubscribed
def develop_set_next_player_as_current_player
return unless developer_actions_enabled?

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

def unsubscribed
current_user.player.update!(inactive: true)
# Any cleanup needed when channel is unsubscribed
# pp current_user.playerpp
Expand Down Expand Up @@ -412,6 +441,14 @@ def deliver_error_message(_e)
# broadcast_to(@gameboard, _e)
end

def developer_actions_enabled?
# returns true if ENV['DEV_TOOL_ENABLED'] is set
return true if ENV['DEV_TOOL_ENABLED'] == 'enabled'

PlayerChannel.broadcast_error(current_user, "You can't use developer actions in this Environment")
false
end

def check_if_player_owns_card(ingame_deck_id)
card = current_user.player.handcard.ingamedecks.find_by('id=?', ingame_deck_id)
# broadcast error to player channel if he does not own this ingamedeck_id
Expand Down
13 changes: 6 additions & 7 deletions app/channels/lobby_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ def subscribed

@gameboard = gameboard

# TODO: Remove after testing i guesss
if params['testplayers'].nil?
create_dummy_players_for_gameboard(@gameboard)
else
create_dummy_players_for_gameboard(@gameboard, params['testplayers'])
end
# Should only be usable if ENV is set
ENV['DEV_TOOL_ENABLED'] == 'enabled' && create_dummy_players_for_gameboard(@gameboard, params['testplayers'])

lobbyisfull = @gameboard.players.count > 3

Expand Down Expand Up @@ -71,7 +67,10 @@ def unsubscribed

private

def create_dummy_players_for_gameboard(gameboard, number_of_players = 3)
def create_dummy_players_for_gameboard(gameboard, number_of_players)
# number of players could be nil if a user deletes it from the form
number_of_players = 0 if number_of_players.nil?

max_players = 4
gameboard_test = gameboard

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ def set_card
# Only allow a list of trusted parameters through.
def card_params
params.require(:card).permit(:card_id, :title, :type, :description, :image, :action, :draw_chance, :level, :element, :bad_things, :rewards_treasure, :good_against, :bad_against,
:good_against_value, :bad_against_value, :element_modifier, :atk_points, :item_category, :has_combination, :level_amount)
:good_against_value, :bad_against_value, :atk_points, :item_category, :level_amount, :synergy_type, :synergy_value, :animal)
end
end
8 changes: 4 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class UsersController < ApplicationController

# GET /users
# GET /users.json
# def index
# @users = User.all
# end
def index
@users = User.all
end

# GET /users/1
# GET /users/1.json
Expand Down Expand Up @@ -81,6 +81,6 @@ def user_params

def user_card_params
params.require(:user_card).permit(:card_id, :title, :type, :description, :image, :action, :draw_chance, :level, :element, :bad_things, :rewards_treasure, :good_against, :bad_against,
:good_against_value, :bad_against_value, :element_modifier, :atk_points, :item_category, :has_combination, :level_amount)
:good_against_value, :bad_against_value, :atk_points, :item_category, :level_amount)
end
end
1 change: 1 addition & 0 deletions app/models/buffcard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def self.activate(ingamedeck, player, gameboard)
gameboard.update(can_flee: true)
when 'draw_two_cards'
Handcard.draw_handcards(player.id, gameboard, 2)
PlayerChannel.broadcast_to(player.user, { type: 'HANDCARD_UPDATE', params: { handcards: Gameboard.render_cards_array(player.handcard.ingamedecks) } })
when 'force_help'
helping_player_id = gameboard.helping_player
helping_player = Player.find_by('id = ?', helping_player_id)
Expand Down
15 changes: 15 additions & 0 deletions app/models/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ class Card < ApplicationRecord
# has_many :ingame_cards
# has_many :gameboards :ingame_cards
has_and_belongs_to_many :users
enum good_against: %i[fire water air earth], _suffix: true
enum bad_against: %i[fire water air earth], _suffix: true
enum element: %i[fire water air earth]
enum synergy_type: %i[bull buffalo bear unicorn catfish hotdog boar pizza], _suffix: true
enum animal: %i[bull buffalo bear unicorn catfish hotdog boar pizza]

# has_many :ingamedecks
# has_many :monsterthrees, through: :ingamedecks, source: :cardable, source_type: 'Monsterthree'

def calculate_self_element_modifiers(other_card)
modifier = 0

modifier += good_against_value if good_against == other_card.element
modifier -= bad_against_value if bad_against == other_card.element

modifier
end
end
4 changes: 4 additions & 0 deletions app/models/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Friend < ApplicationRecord
has_many :friendships
has_many :users, through: :friendships
end
49 changes: 49 additions & 0 deletions app/models/friendship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Friendship < ApplicationRecord
belongs_to :user
belongs_to :friend, class_name: 'User'
belongs_to :inquirer, class_name: 'User'

def self.add_friend(user1, user2)
# user2.friends << user1
# user1.friends << user2

Friendship.create(user: user1, friend: user2, inquirer: user1)
Friendship.create(user: user2, friend: user1, inquirer: user1)
end

def self.remove_friend(user1, user2)
friendship1 = Friendship.where(['user_id = ? and friend_id = ?', user1.id, user2.id]).first
friendship2 = Friendship.where(['user_id = ? and friend_id = ?', user2.id, user1.id]).first

friendship1 ? Friendship.destroy(friendship1.id) : nil
friendship2 ? Friendship.destroy(friendship2.id) : nil
end

def self.accept(user1, user2)
friendship1 = Friendship.where(['user_id = ? and friend_id = ?', user1.id, user2.id]).first
friendship2 = Friendship.where(['user_id = ? and friend_id = ?', user2.id, user1.id]).first

friendship1.update!(pending: false)
friendship2.update!(pending: false)
end

def self.broadcast_pending_requests(current_user)
pending_requests = Friendship.where(['user_id = ? and pending = ?', current_user.id, true])

pending_requests.each do |request|
FriendlistChannel.broadcast_to(current_user, { type: 'FRIEND_REQUEST', params: { inquirer: request.friend.id, inquirer_name: request.friend.name } }) if request.inquirer != current_user
end
end

def self.broadcast_friends(current_user)
friendships = Friendship.where(['user_id = ? and pending = ?', current_user.id, false])

friends_obj = []

friendships.each do |friendship|
friends_obj.push({ name: friendship.friend.name, status: friendship.friend.status })
end

FriendlistChannel.broadcast_to(current_user, { type: 'FRIENDLIST', params: { friends: friends_obj } })
end
end
Loading

0 comments on commit 7491e6f

Please sign in to comment.