Skip to content
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
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
cache: bundler
rvm:
- 2.6.1
- 2.6.3
before_script:
- cp config/database.yml.travis config/database.yml
- psql -c 'create database gotboard2_test;' -U postgres
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "https://rubygems.org"

# Modify Ruby version in travis file aswell
ruby "2.6.1"
ruby "2.6.3"

git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ DEPENDENCIES
timecop

RUBY VERSION
ruby 2.6.1p33
ruby 2.6.3p62

BUNDLED WITH
1.17.2
31 changes: 31 additions & 0 deletions app/controllers/westeros_decks_controller.rb
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
33 changes: 33 additions & 0 deletions app/game_data/westeros_cards.yml
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
1 change: 1 addition & 0 deletions app/interactions/games/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def execute
compose(CreateOrders, game: game)
compose(CreatePowerTokens, game: game)
compose(CreateWildlingCards, game: game)
compose(CreateWesterosCards, game: game)
compose(CreateHouseCards, game: game)
compose(CreateNeutralForceTokens, game: game)
compose(CreateGarrisonTokens, game: game)
Expand Down
43 changes: 43 additions & 0 deletions app/interactions/games/create_westeros_cards.rb
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
Copy link
Contributor

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?

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
37 changes: 37 additions & 0 deletions app/interactions/westeros_decks/reveal.rb
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make more sense to put this update inside revealed_card


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
20 changes: 20 additions & 0 deletions app/interactions/westeros_decks/shuffle.rb
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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this if?

card.insert_at(new_position)
end
end

private

def random_position(available_positions)
available_positions.delete(available_positions.sample)
end
end
end
1 change: 1 addition & 0 deletions app/models/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Game < ApplicationRecord

has_many :house_cards
has_many :wildling_cards, -> { order(position: :asc) }
has_many :westeros_decks

has_many :tokens
has_many :fiefdom_tokens
Expand Down
7 changes: 7 additions & 0 deletions app/models/westeros_card.rb
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
6 changes: 6 additions & 0 deletions app/models/westeros_deck.rb
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
10 changes: 10 additions & 0 deletions app/serializers/westeros_card_serializer.rb
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
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
patch :move_to_bottom
end
end
resources :westeros_decks do
collection do
patch :reveal
patch :shuffle
end
end
resources :orders do
collection do
patch :bulk_update, path: "/"
Expand Down
22 changes: 22 additions & 0 deletions db/migrate/20190205215508_create_westeros_decks.rb
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
21 changes: 20 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_08_08_175924) do
ActiveRecord::Schema.define(version: 2019_02_05_215508) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -120,6 +120,25 @@
t.index ["house_id"], name: "index_units_on_house_id"
end

create_table "westeros_cards", force: :cascade 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
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["westeros_deck_id"], name: "index_westeros_cards_on_westeros_deck_id"
end

create_table "westeros_decks", force: :cascade do |t|
t.bigint "tier", null: false
t.bigint "game_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["game_id"], name: "index_westeros_decks_on_game_id"
t.index ["tier"], name: "index_westeros_decks_on_tier"
end

create_table "wildling_cards", force: :cascade do |t|
t.string "name", null: false
t.string "status", null: false
Expand Down
5 changes: 5 additions & 0 deletions spec/interactions/games/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
expect(Games::CreateUnits).to receive(:run).and_return(outcome)
subject
end

it "calls CreateWesterosCards" do
expect(Games::CreateWesterosCards).to receive(:run).and_return(outcome)
subject
end
end
end
60 changes: 60 additions & 0 deletions spec/interactions/games/create_westeros_cards_spec.rb
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
Loading