Skip to content

Commit

Permalink
add basic color-picking flow
Browse files Browse the repository at this point in the history
  • Loading branch information
jrockway committed Nov 9, 2008
1 parent 43d8eb1 commit dd07b45
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 28 deletions.
53 changes: 47 additions & 6 deletions lib/Favour/Controller/Color.pm
Expand Up @@ -2,6 +2,9 @@ package Favour::Controller::Color;
use Favour::Schema::Color;
use Favour::Schema::ColorPreference;
use Moose;
use List::Util qw(shuffle);

use namespace::clean -except => 'meta';

has 'context' => (
is => 'ro',
Expand All @@ -10,13 +13,9 @@ has 'context' => (
);

sub add_color_pair {
my ($self, $args) = @_;

my $good = $args->{good};
my $bad = $args->{bad};
my ($self, $good, $bad) = @_;

# add leading '#' to hex colors, if it's not already there
# then inflate
# inflate
$_ = Favour::Schema::Color->new( code => $_ ) for ($good, $bad);

my $pref = Favour::Schema::ColorPreference->new(
Expand All @@ -27,6 +26,48 @@ sub add_color_pair {
my $prefset = $self->context->current_user->color_preferences;
$prefset->insert($pref);
$self->context->kioku->store($prefset);

return;
}

sub _color_return {
return [ map { $_->as_hex } @_ ];
}

sub list_favorites {
my $self = shift;
_color_return( $self->context->current_user->color_preferences->favorites );
}

sub random_color {
Favour::Schema::Color->new( code => int rand hex('ffffff') );
}

sub get_colors_to_compare {
my $self = shift;

my @favorites = $self->context->current_user->color_preferences->favorites;

# as number of favorites increases, increase probability
# of reusing colors (to eliminate a favorite)
my $prob = @favorites / 15;
if($prob > 1 || rand() < $prob && @favorites > 2 ){
my ($a, $b) = @{[shuffle @favorites]}[0,1];
return _color_return( $a, $b );
}

# chance to replace a favorite, if the competition is up to it
if( rand 10 < 1 ){
my $a = [shuffle @favorites]->[0];
if( rand() < 0.5 ){
return _color_return( $a, random_color() );
}
else {
return _color_return( random_color(), $a );
}
}

return _color_return( random_color(), random_color() );
}

1;
10 changes: 9 additions & 1 deletion lib/Favour/Server/JSORB.pm
Expand Up @@ -28,7 +28,15 @@ sub _build_jsorb_namespace {
procedures => [
JSORB::Method->new(
name => 'add_color_pair',
spec => [ 'Any' => 'Any' ],
spec => [ 'Str' => 'Str' => 'Unit' ],
),
JSORB::Method->new(
name => 'list_favorites',
spec => [ 'Unit' => 'ArrayRef' ],
),
JSORB::Method->new(
name => 'get_colors_to_compare',
spec => [ 'Unit' => 'ArrayRef' ],
),
],
),
Expand Down
2 changes: 1 addition & 1 deletion share/static/index.html
Expand Up @@ -11,8 +11,8 @@
<script type="text/javascript">
$(document).ready(function (){
reset_favorite_colors();
setup_color_callbacks();
load_initial_colors();
enable_color_callbacks();
});
</script>
</head>
Expand Down
66 changes: 46 additions & 20 deletions share/static/js/favour.js
Expand Up @@ -8,14 +8,6 @@ function call(req, callback){
favour_jsorb.call(favour_jsorb.new_request(req), callback);
}

function setup_color_callbacks(){
["a", "b"].forEach(function(i){
$("#color_" + i).bind("click", function() {
selected_favorite_color(i);
});
});
}

function reset_favorite_colors(){
$("#favorite_colors").html('');
}
Expand All @@ -25,9 +17,13 @@ function add_favorite_color(color){
$("#newly_added").css("background-color", color).removeAttr("id");
}

function set_colors(a, b){
$("#color_a").css("background-color", a);
$("#color_b").css("background-color", b);
}

function load_initial_colors(){
$("#color_b").css("background-color", "red");
$("#color_a").css("background-color", "green");
set_colors("green", "red");
}

function opposite_of(a_or_b){
Expand All @@ -40,23 +36,53 @@ function opposite_of(a_or_b){
}
}

function enable_color_callbacks(){
["a", "b"].forEach(function(i){
$("#color_" + i).bind("click", function() {
selected_favorite_color(i);
});
});
}

function disable_color_callbacks(){
["a", "b"].forEach(function(i){
$("#color_" + i).unbind("click");
});
}

function selected_favorite_color(clicked) {
disable_color_callbacks();

var good = $("#color_" + clicked).css("background-color");
var bad = $("#color_" + opposite_of(clicked)).css("background-color");
call(
{
method: '/favour/controller/color/add_color_pair',
params: [
params: [good,bad],
},
function(){
call(
{
good: good,
bad: bad
method: '/favour/controller/color/list_favorites',
params: [],
},
function(color_list){
reset_favorite_colors();
color_list.forEach(function(color){
add_favorite_color("#" + color);
});
call(
{
method: '/favour/controller/color/get_colors_to_compare',
params: [],
},
function(colors){
set_colors("#" + colors[0], "#" + colors[1]);
enable_color_callbacks(); // and the cycle begins again
}
);
}
]
},
load_next_colors
);
}
);
}

function load_next_colors(){
alert("loading next colors");
}

0 comments on commit dd07b45

Please sign in to comment.