-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Westeros decks/cards #38
Open
lmansur
wants to merge
20
commits into
master
Choose a base branch
from
feature-westeros-cards
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+525
−4
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
32e6bca
Add migration for Westeros cards and decks
lmansur a5b4cdf
Add position column to :westeros_cards
lmansur 212cbb6
Use boolean in migration for :revealed
lmansur 69a3dde
Implement models for Westeros Card and Deck
lmansur 29485b6
Add game-westeros_deck relationship
lmansur 1e546d2
Write yaml file with all cards separated by deck
lmansur e67e084
Implement creationg of westeros decks
lmansur 86e134a
Create WesterosCards when creating game
lmansur 5588f63
Add routes for WesterosDecks
lmansur cf47c58
Add serializer for westeros card
lmansur 493b68b
Add action to Reveal westeros card
lmansur f12831e
Merge branch 'master' into feature-westeros-cards
lmansur 9de4885
Reveals all cards
lmansur 76f3aa7
Return all revealed cards
lmansur 5657e3c
Fix WesterosCardSerializer spec
lmansur 1ea40c2
Receive deck_tier as param and shuffle it
lmansur 15ee5fc
Account for duplicate cards
lmansur e07f69a
Update ruby
lmansur d592cf2
Add specs for westeros decks requests
lmansur 90eedc0
Update ruby in travis
lmansur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,7 +236,7 @@ DEPENDENCIES | |
timecop | ||
|
||
RUBY VERSION | ||
ruby 2.6.1p33 | ||
ruby 2.6.3p62 | ||
|
||
BUNDLED WITH | ||
1.17.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class WesterosDecksController < ApplicationController | ||
def reveal | ||
cards = WesterosDecks::Reveal.run!(game: game) | ||
serialized_cards = WesterosCardSerializer.new(cards).serialized_json | ||
|
||
# @todo Send the user that requested the reveal? | ||
cards.each do |card| | ||
Pusher.trigger("game", "westeros-reveal", game_id: game.id, name: card.title) | ||
end | ||
|
||
render json: serialized_cards | ||
end | ||
|
||
def shuffle | ||
WesterosDecks::Shuffle.run!(deck: deck) | ||
|
||
# @todo Send the user that requested the shuffle? | ||
Pusher.trigger("game", "westeros-shuffle", game_id: game.id, 42 => 42) | ||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def deck | ||
@deck ||= game.westeros_decks.find_by(tier: params[:tier]) | ||
end | ||
|
||
def game | ||
@game ||= Game.find(params[:game_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
1: | ||
- mustering | ||
- mustering | ||
- mustering | ||
- supply | ||
- supply | ||
- supply | ||
- thoneofblades | ||
- thoneofblades | ||
- winteriscoming | ||
- lastdayssummeri | ||
2: | ||
- clashofkings | ||
- clashofkings | ||
- clashofkings | ||
- gameofthrones | ||
- gameofthrones | ||
- gameofthrones | ||
- darkwingsdarkwords | ||
- darkwingsdarkwords | ||
- wintercomingii | ||
- lastdayssummerii | ||
3: | ||
- wildingsattack | ||
- wildingsattack | ||
- wildingsattack | ||
- seaofstorms | ||
- rainsofautumn | ||
- feastforcrows | ||
- weboflies | ||
- stormofswords | ||
- puttothesword | ||
- puttothesword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Games | ||
class CreateWesterosCards < ActiveInteraction::Base | ||
object :game | ||
|
||
def execute | ||
create_decks | ||
create_cards | ||
end | ||
|
||
private | ||
|
||
def create_decks | ||
@decks_ids = {} | ||
3.times do |index| | ||
tier = index + 1 | ||
id = game.westeros_decks.create(tier: tier).id | ||
@decks_ids[tier] = id | ||
end | ||
end | ||
|
||
def create_cards | ||
cards_attributes = [] | ||
westeros_cards.map do |deck_tier, cards| | ||
cards.shuffle.map.with_index do |card, index| | ||
cards_attributes << { | ||
title: card, | ||
revealed: false, | ||
westeros_deck_id: @decks_ids[deck_tier], | ||
position: index + 1 | ||
} | ||
end | ||
end | ||
|
||
WesterosCard.import(cards_attributes) | ||
end | ||
|
||
def westeros_cards | ||
YAML.load_file( | ||
Rails.root.join("app/game_data/westeros_cards.yml") | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module WesterosDecks | ||
class Reveal < ActiveInteraction::Base | ||
object :game | ||
|
||
def execute | ||
cards = [] | ||
|
||
decks.each do |deck| | ||
hide_previous_card(deck) | ||
card = revealed_card(deck) | ||
card.update(revealed: true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would make more sense to put this |
||
|
||
cards << card | ||
end | ||
|
||
cards | ||
end | ||
|
||
private | ||
|
||
def decks | ||
game.westeros_decks | ||
end | ||
|
||
def hide_previous_card(deck) | ||
previous_card = deck.westeros_cards.first | ||
return unless previous_card.revealed? | ||
|
||
previous_card.update(revealed: false) | ||
previous_card.move_to_bottom | ||
end | ||
|
||
def revealed_card(deck) | ||
deck.westeros_cards.first | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module WesterosDecks | ||
class Shuffle < ActiveInteraction::Base | ||
object :deck, class: "WesterosDeck" | ||
|
||
def execute | ||
available_positions = (1..deck.westeros_cards.size).to_a | ||
deck.westeros_cards.each do |card| | ||
new_position = random_position(available_positions) | ||
card.update(revealed: false) if card.revealed? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for this |
||
card.insert_at(new_position) | ||
end | ||
end | ||
|
||
private | ||
|
||
def random_position(available_positions) | ||
available_positions.delete(available_positions.sample) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class WesterosCard < ApplicationRecord | ||
belongs_to :westeros_deck | ||
|
||
validates_presence_of :title | ||
|
||
acts_as_list scope: :westeros_deck | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class WesterosDeck < ApplicationRecord | ||
belongs_to :game | ||
has_many :westeros_cards, -> { order(position: :asc) } | ||
|
||
validates_numericality_of :tier, greater_than_or_equal_to: 1, less_than_or_equal_to: 3 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class WesterosCardSerializer | ||
include FastJsonapi::ObjectSerializer | ||
|
||
set_key_transform :dash | ||
attributes :title | ||
|
||
attribute :deck_tier do |object| | ||
object.westeros_deck.tier | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class CreateWesterosDecks < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :westeros_decks do |t| | ||
t.bigint :tier, null: false, index: true | ||
|
||
t.bigint :game_id, null: false, index: true | ||
|
||
t.timestamps | ||
end | ||
|
||
create_table :westeros_cards do |t| | ||
t.string :title, null: false | ||
t.boolean :revealed, default: false, null: false | ||
|
||
t.integer :position, null: false | ||
|
||
t.bigint :westeros_deck_id, null: false, index: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Games::CreateWesterosCards do | ||
describe ".run" do | ||
let(:game) { create(:game) } | ||
|
||
subject { described_class.run!(game: game) } | ||
|
||
it "creates all cards" do | ||
subject | ||
expect(game.westeros_decks).to contain_exactly( | ||
have_attributes( | ||
tier: 1, | ||
westeros_cards: contain_exactly( | ||
have_attributes(title: "lastdayssummeri", revealed: false, position: Integer), | ||
have_attributes(title: "mustering", revealed: false, position: Integer), | ||
have_attributes(title: "mustering", revealed: false, position: Integer), | ||
have_attributes(title: "mustering", revealed: false, position: Integer), | ||
have_attributes(title: "supply", revealed: false, position: Integer), | ||
have_attributes(title: "supply", revealed: false, position: Integer), | ||
have_attributes(title: "supply", revealed: false, position: Integer), | ||
have_attributes(title: "thoneofblades", revealed: false, position: Integer), | ||
have_attributes(title: "thoneofblades", revealed: false, position: Integer), | ||
have_attributes(title: "winteriscoming", revealed: false, position: Integer) | ||
) | ||
), | ||
have_attributes( | ||
tier: 2, | ||
westeros_cards: contain_exactly( | ||
have_attributes(title: "clashofkings", revealed: false, position: Integer), | ||
have_attributes(title: "clashofkings", revealed: false, position: Integer), | ||
have_attributes(title: "clashofkings", revealed: false, position: Integer), | ||
have_attributes(title: "gameofthrones", revealed: false, position: Integer), | ||
have_attributes(title: "gameofthrones", revealed: false, position: Integer), | ||
have_attributes(title: "gameofthrones", revealed: false, position: Integer), | ||
have_attributes(title: "darkwingsdarkwords", revealed: false, position: Integer), | ||
have_attributes(title: "darkwingsdarkwords", revealed: false, position: Integer), | ||
have_attributes(title: "lastdayssummerii", revealed: false, position: Integer), | ||
have_attributes(title: "wintercomingii", revealed: false, position: Integer) | ||
) | ||
), | ||
have_attributes( | ||
tier: 3, | ||
westeros_cards: contain_exactly( | ||
have_attributes(title: "wildingsattack", revealed: false, position: Integer), | ||
have_attributes(title: "wildingsattack", revealed: false, position: Integer), | ||
have_attributes(title: "wildingsattack", revealed: false, position: Integer), | ||
have_attributes(title: "feastforcrows", revealed: false, position: Integer), | ||
have_attributes(title: "rainsofautumn", revealed: false, position: Integer), | ||
have_attributes(title: "seaofstorms", revealed: false, position: Integer), | ||
have_attributes(title: "stormofswords", revealed: false, position: Integer), | ||
have_attributes(title: "weboflies", revealed: false, position: Integer), | ||
have_attributes(title: "puttothesword", revealed: false, position: Integer), | ||
have_attributes(title: "puttothesword", revealed: false, position: Integer), | ||
) | ||
) | ||
) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you improve this method?