Skip to content

Commit

Permalink
Merge pull request #41 from cardeons/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
Moarales committed Feb 3, 2021
2 parents 5edf73d + d847430 commit 73fdc63
Show file tree
Hide file tree
Showing 46 changed files with 1,011 additions and 226 deletions.
9 changes: 6 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Layout/LineLength:
Max: 200
AutoCorrect: true

Metrics/LineLength:
Max: 200
AutoCorrect: true
# Metrics/LineLength:
# Max: 200
# AutoCorrect: true

Style/Documentation:
Enabled: false
Expand Down Expand Up @@ -41,3 +41,6 @@ Metrics/PerceivedComplexity:

Style/GuardClause:
Enabled: false

AllCops:
SuggestExtensions: false
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// If not specified searches for 'rubocop' executable available on PATH (default and recommended)
"ruby.rubocop.executePath": "",
// You can use specific path
// "ruby.rubocop.executePath": "/Users/you/.rbenv/shims/"
// "ruby.rubocop.executePath": "/Users/you/.rvm/gems/ruby-2.3.2/bin/"
// "ruby.rubocop.executePath": "D:/bin/Ruby22-x64/bin/"
// If not specified, it assumes a null value by default.
"ruby.rubocop.configFilePath": ".rubocop.yml",
// default true
"ruby.rubocop.onSave": true
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/game_socket.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the GameSocket controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
46 changes: 46 additions & 0 deletions app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,51 @@

module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user

def connect
self.current_user = find_verified_user
puts '-----------------------------'
puts 'found current User'
puts current_user.email
end

private

def auth_header
{ Authorization: 'Bearer <token>' }

request.headers['Authorization']
end

def decoded_token
if auth_header
token = auth_header.split[1]
# header: { 'Authorization': 'Bearer <token>' }
begin
JWT.decode(token, 's3cr3t', true, algorithm: 'HS256')
rescue JWT::DecodeError
nil
end
end
end

def logged_in_user
if decoded_token
user_id = decoded_token[0]['user_id']
User.find_by(id: user_id)
end
end

def find_verified_user
if logged_in_user
verified_user
else
# reject_unauthorized_connection
puts '-------------------------------------------'
puts 'could not find user creates a new one now'
verified_user = User.find(rand(1..User.all.count))
end
end
end
end
4 changes: 2 additions & 2 deletions app/channels/chat_room_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def unsubscribed
end

def speak(data)
ActionCable.server.broadcast('chat_room_channel', { message: data['message'], sent_by: data['name'] })
# ActionCable.server.broadcast('chat_room_channel', { message: data['message'], sent_by: data['name'] })
end

def announce(data)
puts '------------------------------'
puts data
ActionCable.server.broadcast('chat_room_channel', { chat_room_name: data['name'], type: data['type'] })
# ActionCable.server.broadcast('chat_room_channel', { chat_room_name: data['name'], type: data['type'] })
end
end
65 changes: 65 additions & 0 deletions app/channels/game_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

class GameChannel < ApplicationCable::Channel
rescue_from Exception, with: :deliver_error_message
BOARD_UPDATE = "BOARD_UPDATE"
DEBUG = "DEBUG"
ERROR = "ERROR"


def subscribed
@gameboard = current_user.player.gameboard
stream_for @gameboard

broadcast_to(@gameboard, {type: DEBUG ,params: { message:"you are now subscribed to the game_channel #{@gameboard.id}"}})

broadcast_to(@gameboard, {type: BOARD_UPDATE, params: Gameboard.broadcast_gameBoard(@gameboard)})
end
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_gameBoard(@gameboard)})
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end

private

def deliver_error_message(e)
broadcast_to(@gameboard)
end
end
98 changes: 98 additions & 0 deletions app/channels/gamesocket_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

class GamesocketChannel < ApplicationCable::Channel
rescue_from Exception, with: :deliver_error_message

def subscribed
awaiting_players = 'awaiting_players'

# access current user with current_user
puts current_user

# #search for gameboard with open lobby
unless gameboard = Gameboard.find_by(current_state: awaiting_players)
gameboard = Gameboard.create(current_state: awaiting_players)
end

player = Player.new(gameboard_id: gameboard.id)


player.user = current_user
player.save!

# #add user to player

# puts gameboard
# puts gameboard.players

lobbyisfull = false

if gameboard.players.count > 3
gameboard.current_state = 'started'
### add who is allowed to play
# gameboard.current_user = gameboard.players.first
gameboard.save
lobbyisfull = true
end

@gameboard = Gameboard.find(gameboard.id)
stream_for @gameboard

broadcast_to(@gameboard, "new Player#{current_user.email} conected to the gameboard id: #{@gameboard.id}")
broadcast_to(@gameboard, "players in lobby: #{@gameboard.players.count}")


@gameboard.players.each do |player|
PlayerChannel.broadcast_to( player.user , "only you should get this you are #{player.user.email}" )
end


# if lobby is full tell other players
if lobbyisfull

players = @gameboard.players
# much to do
players.each do |player|
handcard = Handcard.create(player_id: player.id)
Ingamedeck.new(gameboard_id: @gameboard.id, card_id: 1, cardable_id: handcard.id, cardable_type: 'Handcard').save!
Ingamedeck.new(gameboard_id: @gameboard.id, card_id: 2, cardable_id: handcard.id, cardable_type: 'Handcard').save!

# player.handcard.cards << (Card.find(1))
# player.handcard.cards << (Card.find(2))
puts "--------------------"
puts player.handcard.cards

# broadcast_to(@gameboard, "player.handcard.cards")

# broadcast_to(@gameboard, player.handcard.cards)

PlayerChannel.broadcast_to(player.user, player.handcard.cards )
end

broadcast_to(@gameboard, 'Lobby is full start with game')
broadcast_to(@gameboard, { action: 'startGame', gameboard: @gameboard.players })
end
end

def play_card(data)
# ##add actions!

dataObject = JSON.parse data
puts data




broadcast_to(@gameboard, { action: 'player played', data: data })
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end

private

def deliver_error_message(e)
broadcast_to(...)
end
end
75 changes: 75 additions & 0 deletions app/channels/lobby_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class LobbyChannel < ApplicationCable::Channel
rescue_from Exception, with: :deliver_error_message

def subscribed
awaiting_players = 'awaiting_players'

# access current user with current_user
puts current_user



#check if there is not a player with this user
## read find or create by for simpler solution
if player = Player.find(user_id:current_user.id)
transmit {type: "error", params:{message: "user is already playing in #{player.gameboard_id}"}}
reject
end

#search for gameboard with open lobby
unless gameboard = Gameboard.find_by(current_state: awaiting_players)
gameboard = Gameboard.create(current_state: awaiting_players)
end

if player.inventory

#create new player
player = Player.new(gameboard_id: gameboard.id)
player.user = current_user
player.save!



handcard = Handcard.create(player_id: player.id) unless player.monsterone
Ingamedeck.create(card_id: params[:monsterone], gameboard: gameboard, cardable: handcard)

lobbyisfull = false


if gameboard.players.count > 3
gameboard.current_state = 'started'
### add add the starting user
gameboard.current_player= gameboard.players.first
gameboard.save
lobbyisfull = true
end


# hopefully it is the same after saving?
@gameboard = gameboard

stream_for @gameboard


broadcast_to(@gameboard, {type: "DEBUG" ,params: { message:"new Player#{current_user.email} conected to the gameboard id: #{@gameboard.id} players in lobby #{@gameboard.players.count}"}})

if lobbyisfull
# Lobby is full tell players to start the game
broadcast_to(@gameboard, {type: "DEBUG" , params: {message: 'Lobby is full start with game subscribe to Player and GameChannel'}})

Gameboard.initialize_gameBoard(@gameboard)
broadcast_to(@gameboard, { type: 'START_GAME', params:{ game_id: @gameboard.id }})
end

end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end

private

def deliver_error_message(e)
broadcast_to(@gameboard, {type: "error", params:{message: e}})
end
end
14 changes: 14 additions & 0 deletions app/channels/player_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class PlayerChannel < ApplicationCable::Channel
def subscribed
stream_for current_user
broadcast_to(current_user, { type: DEBUG, params: { message: "you are now subscribed to the player Channel #{current_user.email}" } })
broadcast_to(current_user, { type: HANDCARD_UPDATE, params: { handcards: Player.find(current_user.id).handcard.cards })

end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
4 changes: 4 additions & 0 deletions app/controllers/game_socket_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class GameSocketController < ApplicationController
end
Loading

0 comments on commit 73fdc63

Please sign in to comment.