Skip to content

Commit

Permalink
Added ability to drop in other sets of pieces.
Browse files Browse the repository at this point in the history
Select box on match to pick which of the available sets you are using.

File and Directory queries to determine available sets.

Current set name is stored in session[:set] and matches the name of the directory holding the set
  • Loading branch information
Sean Corbett committed Jan 17, 2009
1 parent 8f02bae commit 5ab4b98
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
9 changes: 9 additions & 0 deletions app/controllers/sets_controller.rb
@@ -0,0 +1,9 @@
# Manages the addition and updating of players and their information
class SetsController < ApplicationController

def change
session[:set] = params[:set]
redirect_to :back
end

end
16 changes: 15 additions & 1 deletion app/helpers/matches_helper.rb
Expand Up @@ -34,7 +34,21 @@ def square_color( pos )
(pos[0] + pos[1]) % 2 == 1 ? :white : :black
end

# checks for existance of .gif file in the current set's directory
# if no .gif, uses .png extension
def image_source_of( piece )
"/images/sets/default/#{piece.role.to_s}_#{piece.side.to_s[0,1]}.gif"
session[:set] ||= 'default'
path = "/images/sets/#{session[:set]}/"
extension = gif_file_exists?(piece, path) ? ".gif" : ".png"
path + piece_file_name(piece) + extension
end

def piece_file_name(piece)
"#{piece.role.to_s}_#{piece.side.to_s[0,1]}"
end

def gif_file_exists?(piece, path)
File.exists?( Rails.public_path + path + piece_file_name(piece) + ".gif" )
end

end
9 changes: 9 additions & 0 deletions app/helpers/sets_helper.rb
@@ -0,0 +1,9 @@
module SetsHelper

def available_sets
Dir.glob("#{Rails.public_path}/images/sets/**").
select{|f| File.directory?( f) }.
map{|d| d.split("/").last}
end

end
4 changes: 4 additions & 0 deletions app/views/matches/show.html.erb
Expand Up @@ -10,6 +10,10 @@ you can enter the Algebraic Notation ( Nbxc4 ). To castle drag king to its castl
when you drop the piece or hit enter.
</div>

<div id="select_set_div" style=" ">
<%= render :partial => "sets/select_set" %>
</div>

<div id="board_div" style="float:left">
<%= render :partial => 'board', :locals => { :board => @match.board } %>
</div>
Expand Down
6 changes: 6 additions & 0 deletions app/views/sets/_select_set.html.erb
@@ -0,0 +1,6 @@
<h3>Available Chess Sets</h3>
<%- set = session[:set] || "default" %>
<%- form_for :set, :url => {:controller => "sets", :action => "change"} do |f| %>
<%= select_tag "set", options_for_select(available_sets, set) %>
<%= submit_tag 'Change' %>
<%- end %>
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -3,6 +3,7 @@
#restful_authentication routes
map.resources :players
map.resource :session
map.resource :set, :member => {:change => :post}

#our application routes
map.resources :matches do |match|
Expand Down

0 comments on commit 5ab4b98

Please sign in to comment.