[QUIZ] LSRC Name Picker (#129)

J

James Edward Gray II

Thanks, James. I'll do that. Do you think it might be a good idea
to post just the code (without the support files) on the mailing
list? In addition to sending it to you, that is.

I think just providing a link to get the application is sufficient
this time around. It's sort of a necessary evil for this topic, I
think.

James Edward Gray II
 
I

Ivo Dancet

--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, :eek:rg

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
@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
@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[letter_start..-1]
@win.addstr "\n"
else
letter_start.upto(0){ @win.addstr " " }
@win.addstr @letter.lines + "\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 + "\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--
 
G

Gregory Brown

This is a simple camping app that uses RMagick to generate an animated
gif for the picked names.

To get going, place names (one per line) in the names file (there is
sample data there for you already).
Run "camping name_picker.rb" at the command line.
Go tohttp://localhost:3301/in your browser and be appalled.

It can be found at:http://rubyquiz.com/hosted_solutions/129/carl/name_picker.zip

CarlPorth

Ahahahaha! That's awesome. You should make use of the blink tag.
It'd go nice with the animated gifs. :)


Seriously though, very clever.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top