/[anoncvs]/projects/dispense2/sql-edition/servers/Idler.py


UCC Code Repository

Contents of /projects/dispense2/sql-edition/servers/Idler.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (show annotations) (download) (as text)
Sun May 8 06:58:17 2005 UTC (16 years ago) by dagobah
Branch: MAIN
CVS Tags: HEAD
Changes since 1.19: +9 -5 lines
File MIME type: text/x-python
Stuff done

1 #!/usr/bin/env python
2
3 import string, time, popen2, os
4 from random import random
5 from MessageKeeper import MessageKeeper
6
7 orderings = None
8
9 IDLER_TEXT_SPEED=1.8
10
11 class Idler:
12 def __init__(self, v):
13 self.v = v
14
15 def next(self):
16 """Displays next stage of the idler. Returns time to the next step"""
17 return 1
18
19 def reset(self):
20 """Resets the idler to a known intial state"""
21 pass
22
23 def finished(self):
24 """Returns True if the idler is considered finished"""
25 return False
26
27 def affinity(self):
28 """How much we want this idler to be the next one chosen"""
29 return 1
30
31 class GreetingIdler(Idler):
32 def __init__(self, v, secs_to_greeting = None):
33 self.v = v
34 self.secs_to_greeting = secs_to_greeting
35 self.message_displayed = False
36
37 def next(self):
38 if not self.secs_to_greeting is None:
39 x = self.secs_to_greeting
40 self.secs_to_greeting = None
41 return x
42
43 self.v.display('UCC SNACKS')
44 self.message_displayed = True
45 return 5
46
47 def reset(self):
48 self.message_displayed = False
49 self.secs_to_greeting = None
50
51 def finished(self):
52 return self.message_displayed
53
54 def affinity(self):
55 return 0
56
57 class TrainIdler(Idler):
58 def __init__(self, v):
59 self.idle_state = 0
60 self.v = v
61
62 def put_shark(self, s, l):
63 if self.s[l] == ' ':
64 self.s[l] = s
65 elif self.s[l] == 'X':
66 self.s[l] = '*'
67 else:
68 self.s[l] = 'X'
69
70 def next(self):
71 # does the next stage of a dance
72 self.s = [' ']*10
73 shark1 = self.idle_state % 18
74 if shark1 < 9:
75 self.put_shark('^', shark1)
76 else:
77 self.put_shark('^', 18-shark1)
78
79 shark2 = ((self.idle_state+4) % 36)/2
80 if shark2 < 9:
81 self.put_shark('<', shark2)
82 else:
83 self.put_shark('<', 18-shark2)
84
85 shark3 = ((self.idle_state+7) % 54)/3
86 if shark3 < 9:
87 self.put_shark('>', 9-shark3)
88 else:
89 self.put_shark('>', 9-(18-shark3))
90
91 train1 = ((self.idle_state%(18*36)))
92 train1_start = 122
93 if train1 > train1_start and train1 < train1_start+(10*2):
94 for i in range(5):
95 ptr = i+train1-train1_start-5
96 if ptr >= 0 and ptr < 10: self.s[ptr] = '#'
97
98 train2 = ((self.idle_state%(18*36)))
99 train2_start = 400
100 if train2 > train2_start and train2 < train2_start+(10*2):
101 for i in range(5):
102 ptr = i+train2-train2_start-5
103 if ptr >= 0 and ptr < 10: self.s[9-ptr] = '#'
104
105 train3 = ((self.idle_state%(18*36)))
106 train3_start = 230
107 if train3 > train3_start and train3 < train3_start+(10*2):
108 for i in range(10):
109 ptr = i+train3-train3_start-10
110 if ptr >= 0 and ptr < 10: self.s[ptr] = '-'
111
112 self.v.display(string.join(self.s, ''))
113 self.idle_state += 1
114 self.idle_state %= 18*36*54
115
116 def reset(self):
117 self.idle_state = 0
118
119 class OrderMaker:
120 def __init__(self, n=8):
121 self.n = n
122 self.make_factorials(n)
123
124 def make_factorials(self, n):
125 self.factorial = []
126 a = 1
127 for i in range(1,n+1):
128 self.factorial.append(a)
129 a *= i
130
131 def order(self, index):
132 used = []
133 for i in range(0,self.n):
134 used.append(i)
135 i = self.n-1
136 j = 0
137 res = []
138 while i >= 0:
139 a = index/self.factorial[i]
140 index %= self.factorial[i]
141 res.append(a+1)
142 i -= 1
143 j += 1
144 for i in range(0,self.n):
145 tmp = used[res[i]-1]
146 for j in range(res[i],self.n):
147 used[j-1] = used[j]
148 res[i] = tmp
149 return res
150
151 def __getitem__(self, i):
152 return self.order(i)
153
154 class GrayIdler(Idler):
155 def __init__(self, v, one=None, zero=None, reorder=0):
156 self.bits = 8
157 self.size = 1 << self.bits
158 self.i = 0
159 self.grayCode = 0
160 self.v = v
161 self.one = one
162 self.zero = zero
163 self.reorder = reorder
164 global orderings
165 if not orderings:
166 orderings = OrderMaker()
167
168 def next(self):
169 output = self.do_next_state()
170 # does the next stage of a dance
171 if self.zero:
172 output = string.replace(output, "0", self.zero)
173 if self.one:
174 output = string.replace(output, "1", self.one)
175 if self.reorder:
176 global orderings
177 newoutput = ""
178 for i in range(0,8):
179 newoutput += output[orderings[self.reorder][i]]
180 output = newoutput
181 self.v.display(" %8.8s " % (output))
182 self.i = (self.i + 1) % self.size
183
184 def do_next_state(self):
185 self.grayCode = self.i ^ (self.i >> 1)
186 output = self.dec2bin(self.grayCode)
187
188 return "0"*(self.bits-len(output))+output
189
190
191 def dec2bin(self,num):
192 """Convert long/integer number to binary string.
193
194 E.g. dec2bin(12) ==> '1100'.
195
196 from http://starship.python.net/~gherman/playground/decbingray/decbingray.py"""
197
198 assert num >= 0, "Decimal number must be >= 0!"
199
200 # Gracefully handle degenerate case.
201 # (Not really needed, but anyway.)
202 if num == 0:
203 return '0'
204
205 # Find highest value bit.
206 val, j = 1L, 1L
207 while val < num:
208 val, j = val*2L, j+1L
209
210 # Convert.
211 bin = ''
212 i = j - 1
213 while i + 1L:
214 k = pow(2L, i)
215 if num >= k:
216 bin = bin + '1'
217 num = num - k
218 else:
219 if len(bin) > 0:
220 bin = bin + '0'
221 i = i - 1L
222
223 return bin
224
225 def reset(self):
226 self.i = 0
227 self.grayCode = 0
228 if self.reorder:
229 self.reorder = int(random()*40319)+1
230
231
232 class StringIdler(Idler):
233 def __init__(self, v, text="Hello Cruel World! ",repeat=True):
234 self.v = v
235 self.mk = MessageKeeper(v)
236 self.text = self.clean_text(text) + " "
237
238 msg = [("",False, None),(self.text, repeat, IDLER_TEXT_SPEED)]
239 self.mk.set_messages(msg)
240
241 def clean_text(self, text):
242 # nothing like a bit of good clean text :)
243 valid = string.digits \
244 + string.letters \
245 + string.punctuation \
246 + " "
247 # uppercase it
248 text = string.upper(text)
249 clean = ""
250 for char in text:
251 if char in valid:
252 clean = clean + char
253 else:
254 clean = clean + " "
255 return clean
256
257 def next(self):
258 self.mk.update_display()
259
260 def finished(self):
261 return self.mk.done()
262
263 class ClockIdler(Idler):
264 def __init__(self, v):
265 self.v = v
266 self.last = None
267
268 def next(self):
269 colonchar = ':'
270 if int(time.time()*2) & 1: colonchar = ' '
271 output = time.strftime("%%H%c%%M%c%%S"%(colonchar,colonchar))
272 if output != self.last:
273 self.v.display(" %8.8s " % (output))
274 self.last = output
275
276 def affinity(self):
277 return 3
278
279 class FortuneIdler(StringIdler):
280 def __init__(self, v):
281 fortune = "/usr/games/fortune"
282 text = "I broke my wookie...."
283 if os.access(fortune,os.F_OK|os.X_OK):
284 (stdout, stdin) = popen2.popen2(fortune)
285 text = string.join(stdout.readlines())
286 stdout.close()
287 stdin.close()
288 StringIdler.__init__(self, v, text,repeat=False)
289
290 def affinity(self):
291 return 20
292
293 class PipeIdler(StringIdler):
294 def __init__(self, v, command, args):
295 text = "I ate my cookie...."
296 if os.access(command,os.F_OK|os.X_OK):
297 (stdout, stdin) = popen2.popen2(command+' '+args)
298 text = string.join(stdout.readlines())
299 stdout.close()
300 stdin.close()
301 StringIdler.__init__(self, v, text,repeat=False)
302
303 def affinity(self):
304 return 20
305
306 class FileIdler(StringIdler):
307 def __init__(self, v, thefile=None, repeat=False, affinity=8):
308 text = "I broke my wookie...."
309 self._affinity = affinity
310
311 if file and os.access(thefile,os.F_OK|os.R_OK):
312 f = file(thefile,'r')
313 text = string.join(f.readlines())
314 f.close()
315 StringIdler.__init__(self, v, text,repeat=repeat)
316
317 def affinity(self):
318 return self._affinity

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26