Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resuming work on my "It Hurts My Brain"-like pairing generator!
- Loading branch information
1 parent
53b1827
commit b416c3f
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<h2>Roll-your-own Brainhurt Pairing Generator</h2> | ||
|
||
<% 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? %> | ||
<ul> | ||
<% @pairings.each do |pairing| %> | ||
<li><%= pairing[:top].name %>/<%= pairing[:bottom].name %></li> | ||
<% end %> | ||
</ul> | ||
<% elsif !params[:pairings_count].blank? && @pairings.blank? %> | ||
<p>Sorry, you either don't have enough variety in fandoms and/or characters, or there was some other kind of error.</p> | ||
<% end %> | ||
<% unless @crtUserFandoms.blank? %> | ||
<h3>Your Written Fandoms</h3> | ||
<p><%= @crtUserFandoms.collect(&:name).sort.join(ArchiveConfig.DELIMITER_FOR_OUTPUT) %></p> | ||
<% end %> | ||
<% unless @crtUserCharas.blank? %> | ||
<h3>Your Written Characters</h3> | ||
<p><%= @crtUserCharas.collect(&:name).sort.join(ArchiveConfig.DELIMITER_FOR_OUTPUT) %></p> | ||
<% end %> | ||
<% unless @crtUserSum.blank? %> | ||
<h3>Your Maximal Pool of Characters</h3> | ||
<p><%= @crtUserSum.collect(&:name).sort.join(ArchiveConfig.DELIMITER_FOR_OUTPUT) %></p> | ||
<% end %> |