From: Ivo Dancet Date: 2007-07-02T08:06:36+09:00 Subject: Re: [QUIZ] LSRC Name Picker (#129) --Apple-Mail-3-1013047968 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Well, I sorta made a solution too, the code is not very nice as it needs cleaning up but I didn't have the time to do that... But what the heck, I liked what came out and it was nice to use curses once again, so I'll share it anyway. The thing needs a yaml file with names, but it would be easy to refactor it for another data source. The mail contains three files: * quiz#129_2.rb (as it is a second try) * ascii_letters5_5.txt (the characters you can use in the yaml file (A-Z, 0-9, '.', ',' )) * names.yml (example data file) the yaml file needs records containing the name and the organisation, if picked by the app, the app will add a picked-parameter to the hash. To try it, please use a fairly large terminal window. Regards Ivo Dancet --Apple-Mail-3-1013047968 Content-Transfer-Encoding: 7bit Content-Type: text/plain; x-unix-mode=0644; name=ascii_letters5_5.txt Content-Disposition: attachment; filename=ascii_letters5_5.txt AA A A A A AAAA A A BBB B B BBBB B B BBBBB CCCC CC C CC CCCC DDDD D D D D D D DDDD EEEEE E EEE E EEEEE FFFFF F FFF F F GGGGG G G GG G G GGGGG H H H H HHHHH H H H H IIIII I I I IIIII JJJJJ J J J JJ K K K K KK K KK K KK L L L L LLLLL M M MM MM M M M M M M M N N NN N N N N N NN N N OOO O O O O O O OOO PPPP P P PPPP P P QQQ Q Q Q Q Q Q QQ Q RRRR R R RRRR R R R R SSSS S SSS S SSSS TTTTT T T T T U U U U U U U U UUU V V V V V V V V V W W W W W W W W W W W X X X X X X X X X Y Y Y Y Y Y Y ZZZZZ Z Z Z ZZZZZ .. .. ,, , 000 0 0 0 0 0 0 000 1 11 1 1 11111 222 2 2 2 2 22222 333 3 3 3 3 3 333 4 4 4 4 44444 4 4 55555 5 5555 5 5555 6666 6 6666 6 6 6666 77777 7 7 7 7 888 8 8 888 8 8 888 999 9 9 9999 9 999 --Apple-Mail-3-1013047968 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0644; name=names.yml Content-Disposition: attachment; filename=names.yml --- pol: name: polucarpus tack organisation: pol apple: name: Steve Jobs organisation: Apple, Inc. james: name: James Edward Gray II organisation: something out there --Apple-Mail-3-1013047968 Content-Transfer-Encoding: 7bit Content-Type: text/x-ruby-script; x-unix-mode=0644; name=quiz#129_2.rb Content-Disposition: attachment; filename=quiz#129_2.rb require 'curses' require 'logger' require 'yaml' class NamePicker attr_reader :name, :org def initialize @names = YAML.load(File.read("names.yml")) raise "Not enough names in list" if @names.select{ |key,value| value["picked"].nil? }.length == 0 begin @random_key = rand(@names.length) picked_record = @names[@names.keys[@random_key]] end while picked_record["picked"] == true @name = picked_record["name"].to_s @org = picked_record["organisation"].to_s save_names end def save_names file = File.open("names.yml", "w") @names[@names.keys[@random_key]]["picked"] = true file.write @names.to_yaml file.close end end class Letters @@letters = nil def initialize(letter, size) if @@letters.nil? readLetters end getLetter(letter, size) end def readLetters file = File.open("ascii_letters5_5.txt","r").readlines @@letters = {} (("A".."Z").to_a + [" ",".",","] + ("0".."9").to_a ).each do |key| @@letters[key]= file[0..4].collect { |a| a.chomp } file = file[6..-1] end end def getLetter( letter, size ) letter_lines = @@letters[letter] @letter = [] letter_lines.each do |line| size.times do |i| @letter << line.split("").collect { |l| l * size }.join("") end end end def lines @letter end def width @letter.first.length end def height @letter.length end end class Show include Curses def initialize( picked ) init_screen @win = Window.new( lines, cols, 0, 0 ) @picked = picked shrink_letters show_winner show_org sleep 5 move_winner sleep 3 close end def shrink_letters @picked.name.upcase.split("").each do |letter| self.shrink letter end end def shrink( letter ) @letters = [] if @letters.nil? 20.downto(1) do |size| @letter = Letters.new( letter, size ) centerLetterOnScreen addLetters @win.refresh sleep 0.02 end @letters << letter end def addLetters(x=0, y=0) x = 20 + x y = 20 + y @letters.each do |letter| @win.setpos(y,x) big_letter = Letters.new(letter, 1).lines 5.times do |i| @win.addstr big_letter[i] @win.setpos(y+i+1,x) end x += 6 end end def show_org(x=0, y=0) x = 20 + x y = 30 + y @picked.org.upcase.split("").each do |letter| @win.setpos(y,x) big_letter = Letters.new(letter, 1).lines 5.times do |i| @win.addstr big_letter[i] @win.setpos(y+i+1,x) end x += 6 end @win.refresh end def centerLetterOnScreen @win.clear letter_start = (@letter.width - cols)/2 letter_line_start = (@letter.height - lines)/2 if letter_line_start > 0 letter_line_start.upto(letter_line_start+lines) do |i| if letter_start > 0 @win.addstr @letter.lines[i][letter_start..-1] @win.addstr "\n" else letter_start.upto(0){ @win.addstr " " } @win.addstr @letter.lines[i] + "\n" end end else letter_line_start.upto(0) do @win.addstr "\n" end 0.upto(@letter.height-1) do |i| letter_start.upto(0){ @win.addstr " " } @win.addstr @letter.lines[i] + "\n" end end end def show_winner @win.clear addLetters @win.refresh end def move_winner (lines - 20).times do |i| @win.clear addLetters(i,i) @win.refresh sleep 0.02 end end def close close_screen end end $logger = Logger.new("log.log") picked = NamePicker.new show = Show.new picked --Apple-Mail-3-1013047968--