-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjukebox.py
executable file
·468 lines (351 loc) · 11.2 KB
/
jukebox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/python
import sys
import signal
from os import getcwd, listdir, walk, kill
from os.path import isfile, join, split
import fnmatch
from subprocess import Popen, PIPE
from time import time, sleep
import random
import curses
import constants
import config
########################################################################
PAINT_INTERVAL = 0.1
MAX_X = 79
MAX_Y = 23
STATUS_WINDOW_Y = 1
STATUS_WINDOW_X = 2
STATUS_WINDOW_W = 75
HACK_WINDOW_LINE_CT = 8
HACK_WINDOW_X = 2
HACK_WINDOW_Y = 3
HACK_WINDOW_W = 75
MSG_WINDOW_LINE_CT = 10
MSG_WINDOW_X = 2
MSG_WINDOW_Y = 12
MSG_WINDOW_W = 75
########################################################################
stdscr = None
hacks_dir = join( getcwd(), 'hacks')
screenhacks = []
found_logos = False
found_dynamic = False
hack_run_count = 0
hack_index = 0
manual = False
total_odds = 1
########################################################################
def setup_screen():
global CURSES_ENABLED, stdscr, msg_win
stdscr = curses.initscr()
term_y,term_x = stdscr.getmaxyx()
if (term_x < MAX_X) or (term_y < MAX_Y):
curses.endwin()
print "Terminal must be at least %sx%s for interactive mode; running in silent mode instead." % (MAX_X,MAX_Y)
print "Press Ctrl+C to terminate script."
CURSES_ENABLED = False
return
CURSES_ENABLED = True
curses.noecho()
curses.cbreak()
curses.curs_set(0)
stdscr.nodelay(1)
stdscr.keypad(1)
win_title = "[ farnsworth ]"
msg_win = stdscr.subwin(MAX_Y, MAX_X, 0, 0)
msg_win.box()
msg_win.hline(2, 1, curses.ACS_HLINE, 77)
msg_win.hline(HACK_WINDOW_LINE_CT+3, 1, curses.ACS_HLINE, 77)
stdscr.addstr(0, int((MAX_X - len(win_title)) / 2), win_title)
stdscr.refresh()
########################################################################
hack_list_idx = 0
def paint_hack_list():
global stdscr
if not CURSES_ENABLED:
return
actual_lines = min(HACK_WINDOW_LINE_CT,len(screenhacks))
for i in range(actual_lines):
cursor = i + hack_list_idx
if cursor >= len(screenhacks):
cursor -= len(screenhacks)
m = " "
if cursor == hack_index:
if manual:
m = "="
else:
m = ">"
this_hack = screenhacks[cursor]
odds = this_hack["odds"] * 1.0 / total_odds
t = "%s[R:%s O:%.2f] %s" % ( m, this_hack["run_count"], odds, this_hack["name"] )
w = MAX_X - (len(t)+HACK_WINDOW_X*2)
t = t + " " * w
t = t[:HACK_WINDOW_W]
if cursor == selected_hack:
stdscr.addstr(HACK_WINDOW_Y + i, HACK_WINDOW_X, t, curses.A_REVERSE)
else:
stdscr.addstr(HACK_WINDOW_Y + i, HACK_WINDOW_X, t)
stdscr.refresh()
########################################################################
msg_buffer = [" "] * MSG_WINDOW_LINE_CT
msg_idx = 0
def log_msg(msg):
global msg_buffer, msg_idx, stdscr
if not CURSES_ENABLED:
return
msg_buffer[msg_idx] = msg
cursor_offset = msg_idx - MSG_WINDOW_LINE_CT + 1
for i in range(MSG_WINDOW_LINE_CT):
cursor = cursor_offset + i
if cursor >= len(msg_buffer):
cursor -= len(msg_buffer)
msg_w = MAX_X - (len(msg_buffer[cursor])+MSG_WINDOW_X*2)
pstr = msg_buffer[cursor] + " " * msg_w
stdscr.addstr(MSG_WINDOW_Y + i, MSG_WINDOW_X, pstr[:HACK_WINDOW_W])
stdscr.refresh()
msg_idx += 1
if msg_idx == len(msg_buffer):
msg_idx = 0
########################################################################
status = ""
cached_status = ""
def set_status(new_status):
global status
status = new_status
paint_status()
def cache_status():
global cached_status
cached_status = status
def restore_status():
global status
status = cached_status
########################################################################
def paint_status():
global stdscr
if not CURSES_ENABLED:
return
if len(screenhacks) > 0:
h = screenhacks[hack_index]
if manual:
s = "%s | %.1fs" % (status, h["elapsed"])
else:
s = "%s | %.1f / %.1fs" % (status, h["elapsed"], h["preferred_duration"])
else:
s = status
if manual:
s = "MANUAL | %s" % s
else:
s = "AUTO | %s" % s
w = MAX_X - (len(s)+STATUS_WINDOW_X*2)
s = s + " " * w
s = s[:STATUS_WINDOW_W]
stdscr.addstr(STATUS_WINDOW_Y, STATUS_WINDOW_X, s)
stdscr.refresh()
########################################################################
def bail(signum=None,dataframe=None):
global stdscr
if CURSES_ENABLED:
# undo all the curses stuff to restore normalcy to the terminal
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
curses.curs_set(1)
# kill the hack currently running before exiting
screenhacks[hack_index]["proc"].kill()
print " "
print "Clean exit."
sys.exit()
########################################################################
def register_hack( path ):
global screenhacks, found_logos, found_dynamic
for hack in screenhacks:
if path == hack["path"]:
return False
settings = []
proc = Popen( [path,"--register"], stdout=PIPE )
for setting in proc.stdout:
settings.append(setting.strip())
hack = {}
hack["provides_logo"] = settings[0] == "True"
hack["is_dynamic"] = settings[1] == "True"
hack["preferred_duration"] = float(settings[2])
hack["elapsed"] = 0
hack["run_count"] = 0
hack["odds"] = 100
hack["path"] = path
hack["name"] = split(path)[1]
if hack["provides_logo"]:
found_logos = True
if hack["is_dynamic"]:
found_dynamic = True
if hack["preferred_duration"] > config.MAX_RUN:
hack["preferred_duration"] = config.MAX_RUN
log_msg("found new hack: %s" % hack["name"])
screenhacks.append( hack )
paint_hack_list()
########################################################################
def random_hack_id():
global total_odds
hack_id = 0
totals = []
running_total = 0
total_odds = 0
for i in range( len(screenhacks) ):
h = screenhacks[i]
if hack_run_count == 0 or h["run_count"] == 0:
h["odds"] = 100
elif i == hack_index:
h["odds"] = 0
else:
h["odds"] = int(((hack_run_count - h["run_count"]) * 100) / hack_run_count)
total_odds += h["odds"]
running_total += h["odds"]
totals.append(running_total)
rnd = random.random() * running_total
for i, total in enumerate(totals):
if rnd < total:
hack_id = i
break
return hack_id
########################################################################
selected_hack = 0
def hack_up():
global selected_hack, hack_list_idx
if selected_hack > 0:
selected_hack -= 1
if selected_hack < hack_list_idx:
hack_list_idx -= 1
paint_hack_list()
########################################################################
def hack_down():
global selected_hack, hack_list_idx
if selected_hack < len(screenhacks) - 1:
selected_hack += 1
if selected_hack >= HACK_WINDOW_LINE_CT + hack_list_idx:
hack_list_idx += 1
paint_hack_list()
########################################################################
def set_hack():
while not stop_hack(hack_index):
sleep(0.001)
start_hack( selected_hack )
########################################################################
def next_hack_id():
last_hack_rec = screenhacks[hack_index]
logo_required = False
dynamic_required = False
if found_logos and not last_hack_rec["provides_logo"]:
logo_required = True
if found_dynamic and not last_hack_rec["is_dynamic"]:
dynamic_required = True
new_hack_id = random_hack_id()
new_hack_rec = screenhacks[new_hack_id]
while ((not new_hack_rec["provides_logo"]) and logo_required) or ((not new_hack_rec["is_dynamic"]) and dynamic_required):
new_hack_id = random_hack_id()
new_hack_rec = screenhacks[new_hack_id]
return new_hack_id
########################################################################
def load_hacks(reloading=False):
cache_status()
log_msg("reloading hack list...")
if reloading:
set_status("LOOKING FOR NEW HACKS...")
else:
set_status("LOADING HACKS...")
for root, dirnames, filenames in walk(hacks_dir):
for filename in fnmatch.filter(filenames, '*.py'):
register_hack(join(root, filename))
restore_status()
########################################################################
def start_hack(new_hack_id):
global hack_run_count, hack_index, screenhacks
hack_run_count += 1
hack_index = new_hack_id
hack_rec = screenhacks[new_hack_id]
hack_rec["start_time"] = time()
hack_rec["termination_clock"] = 0
hack_rec["state"] = constants.ST_RUNNING
hack_rec["proc"] = Popen( [ hack_rec["path"],
"--time",
str(hack_rec["preferred_duration"]) ] )
hack_rec["run_count"] += 1
set_status( "RUNNING %s " % hack_rec["name"] )
log_msg( "starting %s; will run for %s secs" % ( hack_rec["name"], hack_rec["preferred_duration"] ))
paint_hack_list()
########################################################################
def stop_hack(hack_id):
hack_rec = screenhacks[hack_id]
if hack_rec["state"] == constants.ST_RUNNING:
# request exit
hack_rec["proc"].terminate()
hack_rec["state"] = constants.ST_TERMINATING
return False
elif hack_rec["state"] == constants.ST_TERMINATING:
if hack_rec["proc"].poll() is not None:
# the screenhack that was running has terminated
hack_rec["state"] = constants.ST_STOPPED
return True
else:
hack_rec["termination_clock"] += 1
if hack_rec["termination_clock"] > config.MAX_DWELL:
# the screenhack is taking too long to terminate
# shoot it in the head
hack_rec["proc"].kill()
return False
########################################################################
# the global "manual" variable determines whether hacks are
# automatically advancing or under manual control
def toggle_manual_control():
global manual
if manual:
manual = False
log_msg("manual control released; auto-advance resumed")
else:
manual = True
log_msg("manual control set")
paint_status()
paint_hack_list()
########################################################################
last_update = time()
def update_screen():
global last_update
if CURSES_ENABLED and (time() - last_update > PAINT_INTERVAL):
paint_status()
last_update = time()
########################################################################
signal.signal(signal.SIGINT,bail)
setup_screen()
load_hacks()
hack_index = next_hack_id()
start_hack(hack_index)
paint_hack_list()
while True:
if CURSES_ENABLED:
key = stdscr.getch()
if key == ord('q'):
break
elif key == ord('r'):
load_hacks(reloading=True)
elif key == ord('m'):
toggle_manual_control()
elif key == ord("\n"):
set_hack()
elif key == curses.KEY_DOWN:
hack_down()
elif key == curses.KEY_UP:
hack_up()
hack_rec = screenhacks[hack_index]
hack_rec["elapsed"] = time() - hack_rec["start_time"]
if not manual and (hack_rec["elapsed"] > hack_rec["preferred_duration"]):
# the currently running screenhack has run out of time
set_status("REQUESTING EXIT of %s" % hack_rec["name"])
while not stop_hack(hack_index):
sleep(0.001)
hack_index = next_hack_id()
start_hack(hack_index)
update_screen()
sleep(0.001)
bail()
########################################################################