From b416c3f5fb608e4b53e7d23a4ea619b17f355ffe Mon Sep 17 00:00:00 2001 From: Enigel Date: Tue, 8 Feb 2011 03:42:20 +0200 Subject: [PATCH] Resuming work on my "It Hurts My Brain"-like pairing generator! --- app/controllers/toys_controller.rb | 43 +++++++++++++++++++++++++++++ app/views/toys/brainhurter.html.erb | 35 +++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 app/controllers/toys_controller.rb create mode 100644 app/views/toys/brainhurter.html.erb diff --git a/app/controllers/toys_controller.rb b/app/controllers/toys_controller.rb new file mode 100644 index 00000000000..247327e356e --- /dev/null +++ b/app/controllers/toys_controller.rb @@ -0,0 +1,43 @@ +class ToysController < ApplicationController + # Enigel's experimental toy; don't refresh too much! + def brainhurter + if current_user.is_a?(User) + @crtUserFandoms = ( (current_user.tags.uniq.select {|t| t.is_a?(Fandom) && t.canonical?}) + (current_user.tags.uniq.select {|t| t.is_a?(Fandom) && t.merger}.collect(&:merger)) ).uniq + @crtUserCharas = ( (current_user.tags.uniq.select {|t| t.is_a?(Character) && t.canonical?}) + (current_user.tags.uniq.select {|t| t.is_a?(Character) && t.merger}.collect(&:merger)) ).uniq + + if !params[:level].blank? && params[:level].to_s.downcase == "kamikaze" + @crtUserSum = (@crtUserFandoms.collect {|f| f.characters.canonical} + @crtUserCharas).flatten.uniq + pool = Array.new(@crtUserSum) + else + pool = Array.new(@crtUserCharas) + end + + if @crtUserFandoms.size < 2 || pool.size < 2 + flash[:error] = "Sorry, you need to have written in at least two different fandoms, at least two different characters!" + else + if !params[:pairings_count].blank? && ["5","10","15","20"].include?(params[:pairings_count]) + @pairings = [] + # while !pool.empty? && !(pool.collect {|c| c.fandoms}.flatten.uniq.sort.to_s == pool[0].fandoms.uniq.sort.to_s) # this is a bad, bad thing to do! + params[:pairings_count].to_i.times do + character = pool[rand(pool.size)] + pool.delete(character) + mate = pool[rand(pool.size)] + infinite_loop = 0 + until (mate.fandoms & character.fandoms).empty? || infinite_loop > 100 + mate = pool[rand(pool.size)] + infinite_loop += 1 + end + pairing = {:top => character, :bottom => mate} + @pairings << pairing + pool.delete(mate) + break if pool.size < 2 || pool.collect(&:fandoms).flatten.uniq.size < 2 + end + @pairings = @pairings.flatten unless @pairings.blank? + end + end + else + flash[:error] = "Sorry, this functionality is currently only available to registered users!" + end + end + +end \ No newline at end of file diff --git a/app/views/toys/brainhurter.html.erb b/app/views/toys/brainhurter.html.erb new file mode 100644 index 00000000000..e1cc18d7bec --- /dev/null +++ b/app/views/toys/brainhurter.html.erb @@ -0,0 +1,35 @@ +

Roll-your-own Brainhurt Pairing Generator

+ +<% form_tag "/brainhurter", :method => :post do %> + <%= label_tag "level_normal", "Use your written characters" %> + <%= radio_button_tag "level", "normal", params[:level] == "normal" %> + <%= label_tag "level_kamikaze", "Use all the characters in your written fandoms" %> + <%= radio_button_tag "level", "kamikaze", params[:level] == "kamikaze" %> + <%= select_tag "pairings_count", options_for_select(["5", "10", "15", "20"], params[:pairings_count]) %> + <%= submit_tag 'Gimme!' %> +<% end %> + +<% if !params[:pairings_count].blank? && !@pairings.blank? %> + +<% elsif !params[:pairings_count].blank? && @pairings.blank? %> +

Sorry, you either don't have enough variety in fandoms and/or characters, or there was some other kind of error.

+<% end %> + +<% unless @crtUserFandoms.blank? %> +

Your Written Fandoms

+

<%= @crtUserFandoms.collect(&:name).sort.join(ArchiveConfig.DELIMITER_FOR_OUTPUT) %>

+<% end %> + +<% unless @crtUserCharas.blank? %> +

Your Written Characters

+

<%= @crtUserCharas.collect(&:name).sort.join(ArchiveConfig.DELIMITER_FOR_OUTPUT) %>

+<% end %> + +<% unless @crtUserSum.blank? %> +

Your Maximal Pool of Characters

+

<%= @crtUserSum.collect(&:name).sort.join(ArchiveConfig.DELIMITER_FOR_OUTPUT) %>

+<% end %> \ No newline at end of file