Class: Party

Inherits:
Object
  • Object
show all
Defined in:
lib/party.rb

Class Method Summary collapse

Class Method Details

.draw_line(lineNr, should_not_draw) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/party.rb', line 202

def self.draw_line(lineNr, should_not_draw)
  setpos(lineNr, 0)
  for i in 0..cols-1
    if (i/3)%3 == 0
      attron(color_pair(5))
    elsif (i/3)%3 == 1
      attron(color_pair(1))
    elsif (i/3)%3 == 2
      attron(color_pair(2))
    end
    unless should_not_draw.call(i, lineNr) 
      setpos(lineNr, i)
      addstr(" ")
    end
  end
end

.draw_lines_to(lineNr) ⇒ Object



219
220
221
222
223
# File 'lib/party.rb', line 219

def self.draw_lines_to(lineNr)
  for line in 0..lineNr
    draw_line(line)
  end
end

.draw_rect(leftX, leftY, l, h, rect_color, text_color, string) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/party.rb', line 168

def self.draw_rect(leftX, leftY, l, h, rect_color, text_color, string)
  setpos(leftY, leftX)
  attron(rect_color)
  addstr(spaceStr(l))
  for o in 1..(h-1) 
    setpos(leftY+o, leftX+l-2)
    attron(rect_color)
    addstr("  ")
  end
  for o in 1..(h-1) 
    setpos(leftY+o, leftX)
    addstr("  ")
  end
  setpos(leftY+h,leftX)
  attron(rect_color)
  addstr(spaceStr(l))
  attron(text_color)

  for j in 0..h-2
    setpos(leftY+1+j, leftX+2)
    addstr(word_snake(string, l-4))
  end
  refresh
end

.spaceStr(length) ⇒ Object



225
226
227
228
229
230
231
# File 'lib/party.rb', line 225

def self.spaceStr(length)
  s = ""
  for _ in 1..length
    s += " "
  end
  return s
end

.start(written_words, silent, farewell, stuff_to_say) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/party.rb', line 64

def self.start(written_words, silent, farewell, stuff_to_say)
  interrupted = false
  cols = (%x( tput cols )).to_i
  lines = (%x( tput lines )).to_i
  rec_width = 54
  rec_height = 20
  rec_left_x = cols/2-rec_width/2
  rec_left_y = lines/2-rec_height/2
  init_screen
  start_color
  init_pair(0,COLOR_BLACK, COLOR_YELLOW)
  init_pair(1,COLOR_WHITE, COLOR_BLUE)
  init_pair(2,COLOR_BLACK,COLOR_RED)
  init_pair(3,COLOR_RED,COLOR_BLACK)
  init_pair(4,COLOR_BLACK,COLOR_WHITE)
  init_pair(5,COLOR_BLACK,COLOR_MAGENTA)
  sem = Mutex.new
  talking = Mutex.new
  rect = Thread.new {
    i = 1
    delta = 1
    while !interrupted
      sleep_time = (1.0/i.to_f)*1.3
      written_words.each.with_index do |word, w_i|
        sem.synchronize {
          if w_i % 2 == 0
            draw_rect(rec_left_x,rec_left_y,rec_width,rec_height, color_pair((i+3)%5), color_pair((i+3)%5), word)
          else
            draw_rect(rec_left_x,rec_left_y,rec_width,rec_height, color_pair(1), color_pair(1), word)
          end
        }
        sleep(sleep_time)
      end

      i+=delta
      if i == 10 
        delta = -1
      end
      if i == 1
        delta = 1
      end
    end
  }

  saying = Thread.new {
    i = 0
    words = stuff_to_say.cycle
    while !silent
      talking.synchronize{
        system "say", words.next
      }
      i+=1
    end
  }

  bg_lines = Thread.new {
    color = 0
    while !interrupted
      sleep(1)
      i=0
      while i < lines 
        sem.synchronize {
          draw_line(i, lambda { |col, line| rec_left_y <= line && rec_left_y+rec_height >= line && rec_left_x <= col && rec_left_x+rec_width > col})
          refresh
        }
        sleep(0.04)
        i+=1
      end
      sleep(0.5)
      for i in 0..lines
        for j in 0..cols
          sem.synchronize {
            unless rec_left_y <= i && rec_left_y+rec_height >= i && rec_left_x <= j && rec_left_x+rec_width > j
              setpos(i,j)
              attron(color_pair((color%3)+1))
              addstr(" ")
              refresh
            end
          }
          sleep(0.0004)
        end
      end
      color += 1
    end
  }
  trap("INT") {
    rect.kill()
    bg_lines.kill()
    saying.kill()
  }
  bg_lines.join
  rect.join
  saying.join
  close_screen
  if (!silent)
    talking.synchronize {
      if farewell 
        system "say", farewell
      end
    }
  end
end

.word_snake(string, length) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/party.rb', line 193

def self.word_snake(string, length)
  s = ""
  for _ in 0..(length/string.length)
    s += string
  end
  s[0..(length-1)]
end