Sha256: 21cb9bffe4f716d54b58f5d7f2f1d08d3ed0beaf3f44cf4aa1381a34a8fd2767

Contents?: true

Size: 828 Bytes

Versions: 3

Compression:

Stored size: 828 Bytes

Contents

# frozen string_literal: true

require 'dry-struct'
require 'spy_alley_application/types/game_board'

module SpyAlleyApplication
  module Models
    class GameBoard < Dry::Struct
      class PlayerMoved
        def call(game_board:, new_location:)
          player = game_board.players.find{|p| p.seat.eql?(game_board.game_state.seat)}
          unaffected_players = game_board.players.reject{|p| p.equal?(player)}
          player = player.to_h.tap{|p| p[:location] = new_location}
          players = unaffected_players.push(player).sort{|p, q| p[:seat] <=> q[:seat]}
          game_board = SpyAlleyApplication::Types::GameBoard.call(
            game_board.to_h.tap{|g| g[:players] = players})
          game_board = yield(game_board: game_board) if block_given?
          game_board
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spy_alley_application-0.3.2 lib/spy_alley_application/models/game_board/player_moved.rb
spy_alley_application-0.3.1 lib/spy_alley_application/models/game_board/player_moved.rb
spy_alley_application-0.3.0 lib/spy_alley_application/models/game_board/player_moved.rb