Re-namespacing a bit to clear out some fairly old stuff from the top level
This commit is contained in:
61
oldstuff/map-mash/lib/google_map_location_fetcher.rb
Normal file
61
oldstuff/map-mash/lib/google_map_location_fetcher.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
require 'base64'
|
||||
require 'logger'
|
||||
require 'uri'
|
||||
|
||||
require 'nokogiri'
|
||||
require 'typhoeus'
|
||||
|
||||
|
||||
class GoogleMapLocationFetcher
|
||||
attr_accessor :base_map_url, :log
|
||||
|
||||
def initialize
|
||||
@base_map_url = [
|
||||
'http://maps.googleapis.com/maps/api/staticmap',
|
||||
'?zoom=12',
|
||||
'&sensor=false',
|
||||
'&size=512x512',
|
||||
'&maptype=satellite',
|
||||
].join('')
|
||||
|
||||
@log = Logger.new(
|
||||
File.expand_path('../log/map-crawler.log', File.dirname(__FILE__))
|
||||
)
|
||||
@log.level = Logger::INFO
|
||||
@log.formatter = lambda do |severity, time, prog, message|
|
||||
"#{time} - #{severity} - #{message}\n"
|
||||
end
|
||||
end
|
||||
|
||||
def self.mapdump_callback(location, image)
|
||||
puts "Map '#{location}':"
|
||||
puts Base64.encode64(image)
|
||||
end
|
||||
|
||||
def fetch(locations, &callback)
|
||||
callback ||= self.class.method(:mapdump_callback)
|
||||
hydra = Typhoeus::Hydra.new(:initial_pool_size => 26)
|
||||
|
||||
locations.each do |location|
|
||||
request = Typhoeus::Request.new(
|
||||
"#{@base_map_url}¢er=#{URI.encode(location)}"
|
||||
)
|
||||
request.on_complete do |response|
|
||||
handle_response(response, location, &callback)
|
||||
end
|
||||
|
||||
hydra.queue(request)
|
||||
end
|
||||
|
||||
hydra.run
|
||||
end
|
||||
|
||||
def handle_response(response, location, &callback)
|
||||
@log.info("Handling request at url #{response.effective_url}")
|
||||
if response.success? and response.headers_hash[:content_type] =~ /image\/.*/
|
||||
callback.call(location, response.body)
|
||||
else
|
||||
callback.call(location, '')
|
||||
end
|
||||
end
|
||||
end
|
128
oldstuff/map-mash/lib/mash_tournament_builder.rb
Normal file
128
oldstuff/map-mash/lib/mash_tournament_builder.rb
Normal file
@@ -0,0 +1,128 @@
|
||||
require 'logger'
|
||||
|
||||
|
||||
class MashTournamentBuilder
|
||||
attr_accessor :tournament, :tournament_model, :round_model
|
||||
attr_accessor :map_model, :mash_model
|
||||
|
||||
def initialize(tournament, tournament_model, round_model,
|
||||
map_model, mash_model)
|
||||
@tournament = tournament
|
||||
@tournament_model = tournament_model
|
||||
@round_model = round_model
|
||||
@map_model = map_model
|
||||
@mash_model = mash_model
|
||||
@logger = Logger.new(
|
||||
File.expand_path(
|
||||
'../log/mash-tournament-builder.log', File.dirname(__FILE__)
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
def valid_number_of_contenders?(n_contenders)
|
||||
[8, 16, 32, 64, 128].include?(n_contenders)
|
||||
end
|
||||
|
||||
def create_rounds_for_contenders(n_contenders)
|
||||
if not valid_number_of_contenders?(n_contenders)
|
||||
raise StandardError.new(
|
||||
"The number of contenders must be 8, 16, 32, 64, or 128! " +
|
||||
"Got '#{n_contenders}'"
|
||||
)
|
||||
end
|
||||
|
||||
round = 1
|
||||
|
||||
while n_contenders > 1
|
||||
create_round(round, n_contenders)
|
||||
n_contenders = n_contenders / 2
|
||||
round += 1
|
||||
end
|
||||
|
||||
@tournament.total_rounds = round - 1
|
||||
end
|
||||
|
||||
def fill_in_next_round
|
||||
@tournament.rounds.sort{ |a,b| a.number <=> b.number}.each do |round|
|
||||
if not round.done?
|
||||
return assign_maps_for_round(round)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def create_round(round_number, n_contenders)
|
||||
round_options = {
|
||||
:mash_tournament_id => @tournament.id,
|
||||
:number => round_number,
|
||||
:mash_count => n_contenders / 2
|
||||
}
|
||||
@logger.info("Creating round #{round_number} of #{n_contenders} contenders " +
|
||||
"with options: #{round_options.inspect}")
|
||||
|
||||
round = @round_model.new(round_options)
|
||||
round.save!
|
||||
|
||||
round.mash_count.times do
|
||||
@mash_model.new(
|
||||
:mash_tournament_id => @tournament.id,
|
||||
:mash_tournament_round_id => round.id
|
||||
).save!
|
||||
end
|
||||
end
|
||||
|
||||
def assign_maps_for_round(round)
|
||||
if round.number == 1
|
||||
return assign_maps_for_round_one(round)
|
||||
end
|
||||
|
||||
previous = @round_model.find_by_mash_tournament_id_and_number(
|
||||
@tournament.id, round.number - 1
|
||||
)
|
||||
previous_winners = previous.mashes.collect(&:winner_id)
|
||||
pool = @map_model.all(
|
||||
:order => 'RANDOM()',
|
||||
:conditions => {:id => previous_winners}
|
||||
)
|
||||
|
||||
filled_in = []
|
||||
|
||||
round.mashes.each do |mash|
|
||||
mash.map_a = pool.pop
|
||||
mash.map_b = pool.pop
|
||||
mash.save!
|
||||
|
||||
filled_in << mash
|
||||
end
|
||||
|
||||
filled_in
|
||||
end
|
||||
|
||||
def assign_maps_for_round_one(round)
|
||||
pool_options = {
|
||||
:order => 'RANDOM()',
|
||||
:limit => round.mash_count * 2
|
||||
}
|
||||
@logger.info("Allocating pool with options: #{pool_options.inspect}")
|
||||
pool = @map_model.all(pool_options)
|
||||
|
||||
@logger.info("Populating mashes from pool: #{pool.inspect}")
|
||||
|
||||
filled_in = []
|
||||
|
||||
round.mashes.each do |mash|
|
||||
map_a = pool.pop
|
||||
map_b = pool.pop
|
||||
@logger.info("Assigning `map_a` from #{map_a.inspect}, " +
|
||||
"`map_b` from #{map_b.inspect} to mash #{mash.inspect}")
|
||||
|
||||
mash.map_a = map_a
|
||||
mash.map_b = map_b
|
||||
mash.save!
|
||||
|
||||
filled_in << mash
|
||||
end
|
||||
|
||||
filled_in
|
||||
end
|
||||
end
|
25
oldstuff/map-mash/lib/tasks/maps.rake
Normal file
25
oldstuff/map-mash/lib/tasks/maps.rake
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace :maps do
|
||||
desc 'Seed the maps!'
|
||||
task :seed => :environment do
|
||||
require 'app/models/map'
|
||||
|
||||
csv_filename = File.expand_path(
|
||||
'../../db/capital-cities.csv', File.dirname(__FILE__)
|
||||
)
|
||||
Map.import(csv_filename) do |map|
|
||||
puts "Seeded map '#{map.name}'"
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Seed the maps a little bit less!'
|
||||
task :miniseed => :environment do
|
||||
require 'app/models/map'
|
||||
|
||||
csv_filename = File.expand_path(
|
||||
'../../db/top-us-cities.csv', File.dirname(__FILE__)
|
||||
)
|
||||
Map.import(csv_filename) do |map|
|
||||
puts "Seeded map '#{map.name}'"
|
||||
end
|
||||
end
|
||||
end
|
144
oldstuff/map-mash/lib/tasks/rspec.rake
Normal file
144
oldstuff/map-mash/lib/tasks/rspec.rake
Normal file
@@ -0,0 +1,144 @@
|
||||
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
|
||||
rspec_gem_dir = nil
|
||||
Dir["#{RAILS_ROOT}/vendor/gems/*"].each do |subdir|
|
||||
rspec_gem_dir = subdir if subdir.gsub("#{RAILS_ROOT}/vendor/gems/","") =~ /^(\w+-)?rspec-(\d+)/ && File.exist?("#{subdir}/lib/spec/rake/spectask.rb")
|
||||
end
|
||||
rspec_plugin_dir = File.expand_path(File.dirname(__FILE__) + '/../../vendor/plugins/rspec')
|
||||
|
||||
if rspec_gem_dir && (test ?d, rspec_plugin_dir)
|
||||
raise "\n#{'*'*50}\nYou have rspec installed in both vendor/gems and vendor/plugins\nPlease pick one and dispose of the other.\n#{'*'*50}\n\n"
|
||||
end
|
||||
|
||||
if rspec_gem_dir
|
||||
$LOAD_PATH.unshift("#{rspec_gem_dir}/lib")
|
||||
elsif File.exist?(rspec_plugin_dir)
|
||||
$LOAD_PATH.unshift("#{rspec_plugin_dir}/lib")
|
||||
end
|
||||
|
||||
# Don't load rspec if running "rake gems:*"
|
||||
unless ARGV.any? {|a| a =~ /^gems/}
|
||||
|
||||
begin
|
||||
require 'spec/rake/spectask'
|
||||
rescue MissingSourceFile
|
||||
module Spec
|
||||
module Rake
|
||||
class SpecTask
|
||||
def initialize(name)
|
||||
task name do
|
||||
# if rspec-rails is a configured gem, this will output helpful material and exit ...
|
||||
require File.expand_path(File.join(File.dirname(__FILE__),"..","..","config","environment"))
|
||||
|
||||
# ... otherwise, do this:
|
||||
raise <<-MSG
|
||||
|
||||
#{"*" * 80}
|
||||
* You are trying to run an rspec rake task defined in
|
||||
* #{__FILE__},
|
||||
* but rspec can not be found in vendor/gems, vendor/plugins or system gems.
|
||||
#{"*" * 80}
|
||||
MSG
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Rake.application.instance_variable_get('@tasks').delete('default')
|
||||
|
||||
spec_prereq = File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop
|
||||
task :noop do
|
||||
end
|
||||
|
||||
task :default => :spec
|
||||
task :stats => "spec:statsetup"
|
||||
|
||||
desc "Run all specs in spec directory (excluding plugin specs)"
|
||||
Spec::Rake::SpecTask.new(:spec => spec_prereq) do |t|
|
||||
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
|
||||
t.spec_files = FileList['spec/**/*_spec.rb']
|
||||
end
|
||||
|
||||
namespace :spec do
|
||||
desc "Run all specs in spec directory with RCov (excluding plugin specs)"
|
||||
Spec::Rake::SpecTask.new(:rcov) do |t|
|
||||
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
|
||||
t.spec_files = FileList['spec/**/*_spec.rb']
|
||||
t.rcov = true
|
||||
t.rcov_opts = lambda do
|
||||
IO.readlines("#{RAILS_ROOT}/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
|
||||
end
|
||||
end
|
||||
|
||||
desc "Print Specdoc for all specs (excluding plugin specs)"
|
||||
Spec::Rake::SpecTask.new(:doc) do |t|
|
||||
t.spec_opts = ["--format", "specdoc", "--dry-run"]
|
||||
t.spec_files = FileList['spec/**/*_spec.rb']
|
||||
end
|
||||
|
||||
desc "Print Specdoc for all plugin examples"
|
||||
Spec::Rake::SpecTask.new(:plugin_doc) do |t|
|
||||
t.spec_opts = ["--format", "specdoc", "--dry-run"]
|
||||
t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*')
|
||||
end
|
||||
|
||||
[:models, :controllers, :views, :helpers, :lib, :integration].each do |sub|
|
||||
desc "Run the code examples in spec/#{sub}"
|
||||
Spec::Rake::SpecTask.new(sub => spec_prereq) do |t|
|
||||
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
|
||||
t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
|
||||
end
|
||||
end
|
||||
|
||||
desc "Run the code examples in vendor/plugins (except RSpec's own)"
|
||||
Spec::Rake::SpecTask.new(:plugins => spec_prereq) do |t|
|
||||
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
|
||||
t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*').exclude("vendor/plugins/rspec-rails/*")
|
||||
end
|
||||
|
||||
namespace :plugins do
|
||||
desc "Runs the examples for rspec_on_rails"
|
||||
Spec::Rake::SpecTask.new(:rspec_on_rails) do |t|
|
||||
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
|
||||
t.spec_files = FileList['vendor/plugins/rspec-rails/spec/**/*_spec.rb']
|
||||
end
|
||||
end
|
||||
|
||||
# Setup specs for stats
|
||||
task :statsetup do
|
||||
require 'code_statistics'
|
||||
::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models')
|
||||
::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views')
|
||||
::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers) if File.exist?('spec/controllers')
|
||||
::STATS_DIRECTORIES << %w(Helper\ specs spec/helpers) if File.exist?('spec/helpers')
|
||||
::STATS_DIRECTORIES << %w(Library\ specs spec/lib) if File.exist?('spec/lib')
|
||||
::STATS_DIRECTORIES << %w(Routing\ specs spec/routing) if File.exist?('spec/routing')
|
||||
::STATS_DIRECTORIES << %w(Integration\ specs spec/integration) if File.exist?('spec/integration')
|
||||
::CodeStatistics::TEST_TYPES << "Model specs" if File.exist?('spec/models')
|
||||
::CodeStatistics::TEST_TYPES << "View specs" if File.exist?('spec/views')
|
||||
::CodeStatistics::TEST_TYPES << "Controller specs" if File.exist?('spec/controllers')
|
||||
::CodeStatistics::TEST_TYPES << "Helper specs" if File.exist?('spec/helpers')
|
||||
::CodeStatistics::TEST_TYPES << "Library specs" if File.exist?('spec/lib')
|
||||
::CodeStatistics::TEST_TYPES << "Routing specs" if File.exist?('spec/routing')
|
||||
::CodeStatistics::TEST_TYPES << "Integration specs" if File.exist?('spec/integration')
|
||||
end
|
||||
|
||||
namespace :db do
|
||||
namespace :fixtures do
|
||||
desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z."
|
||||
task :load => :environment do
|
||||
ActiveRecord::Base.establish_connection(Rails.env)
|
||||
base_dir = File.join(Rails.root, 'spec', 'fixtures')
|
||||
fixtures_dir = ENV['FIXTURES_DIR'] ? File.join(base_dir, ENV['FIXTURES_DIR']) : base_dir
|
||||
|
||||
require 'active_record/fixtures'
|
||||
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/).map {|f| File.join(fixtures_dir, f) } : Dir.glob(File.join(fixtures_dir, '*.{yml,csv}'))).each do |fixture_file|
|
||||
Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*'))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user