Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Peekaboo version 2, random image generation

  • Loading branch information...
commit 0cb81888399db4460be9614d4cdc4eaedf7b663b 1 parent 4fadd88
Edmore Moyo authored
9 peekaboo.rb
View
@@ -9,6 +9,11 @@
set :haml, :format => :html5
redis = Redis.new
+def sanitize_dir_entries( dir )
+ entries = Dir.entries( dir )
+ entries.select{ |x| (x != ".." && x != ".") }
+end
+
get "/" do
haml :index
end
@@ -45,7 +50,9 @@
filenames << redis.get("question:#{question_id}:filename")
end
sounds = text.zip(filenames)
- haml :start, :locals => {:sounds => sounds}
+ people = sanitize_dir_entries( "public/images/people" );
+ objects = sanitize_dir_entries( "public/images/objects" );
+ haml :start, :locals => {:sounds => sounds, :people => people, :objects => objects}
end
get "/clear" do
0  public/images/objects/object.jpeg → public/images/objects/apple.jpeg
View
File renamed without changes
BIN  public/images/objects/ball.jpeg
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  public/images/objects/banana.jpeg
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  public/images/objects/orange.jpeg
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  public/images/people/angie.jpg
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
0  public/images/people/person.jpeg → public/images/people/edmore.jpeg
View
File renamed without changes
BIN  public/images/people/tha.jpeg
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 public/javascripts/start.js
View
@@ -5,16 +5,25 @@ soundManager.onready(function(){
function loop(){
setTimeout(function(){
var sound = sounds.splice( 0, 1 ),
- clearDivContents = function( klasses ){
- console.log( klasses );
- for( var i=0; i < klasses.length; i+=1 ){
- $( klasses[i] ).children().detach();
- }
- };
+ clearDivContents = function( klasses ){
+ for( var i=0; i < klasses.length; i+=1 ){
+ $( klasses[i] ).children().detach();
+ }
+ },
+ getRandomImage = function( images ){
+ var max = images.length - 1,
+ random_image_index = Math.floor(Math.random() * (max - 0 + 1)) + 0;
+ console.log( "Displaying image : " + images[random_image_index] );
+ return images[random_image_index];
+ },
+ buildImageHtml = function( klass, src){
+ $( klass ).html( "<img></img>");
+ $( klass + " > img" ).attr("src", src);
+ };
if( sound.length > 0 ){
$("#container").text( sound[0][0] + "?" );
- console.log( "Playing " + sound[0][1] );
+ console.log( "Playing : " + sound[0][1] );
var mySound = soundManager.createSound({
id: sound[0][0],
url: "/play?filename=" + sound[0][1]
@@ -22,13 +31,27 @@ soundManager.onready(function(){
mySound.play();
if ( sound[0][0] === presets[0] ){
- console.log( "Display random image of person" );
- $("#person").append( "<img src='images/people/person.jpeg'></img>" );
+ if ( people.length > 0 ){
+ var person_to_display = getRandomImage ( people ),
+ person_img_src = "/images/people/" + person_to_display;
+
+ buildImageHtml( "#person", person_img_src )
+ }else{
+ $("#person").html( "<div>Please populate your people folder in images</div>" );
+ }
clearDivContents( ["#object"] );
+
}else if ( sound[0][0] === presets[1] ){
- console.log( "Display random image of object" );
- $("#object").append( "<img src='images/objects/object.jpeg'></img>" );
+ if ( objects.length > 0 ){
+ var object_to_display = getRandomImage ( objects ),
+ object_img_src = "/images/objects/" + object_to_display;
+
+ buildImageHtml( "#object", object_img_src )
+ }else{
+ $("#object").html( "<div>Please populate your objects folder in images</div>" );
+ }
clearDivContents( ["#person"] );
+
}else{
clearDivContents( ["#person", "#object"] );
}
2  views/start.haml
View
@@ -1,5 +1,7 @@
:javascript
var sounds = #{sounds},
+ people = #{people},
+ objects = #{objects},
presets = ["who is this", "what is this"];
%link{ :rel=>"stylesheet", :type=>"text/css", :href=>"css/start.css" }
Please sign in to comment.
Something went wrong with that request. Please try again.