+class VendServer():
+
+ v = None
+ vstatus = None
+ state = None
+ event = None
+ idlers = []
+ idler = None
+
+ _pin_uid = 0
+ _pin_uname = 'root'
+ _pin_pin = '----'
+ _last_card_id = -1
+
+ dispense = None
+
+ """
+ Show information to the user as to what can be dispensed.
+ """
+ def scroll_options(self, username, mk, welcome = False):
+ # If the user has just logged in, show them their balance
+ if welcome:
+ balance = self.dispense.getBalance()
+ msg = [(self.center('WELCOME'), False, TEXT_SPEED),
+ (self.center(self.dispense.getUsername()), False, TEXT_SPEED),
+ (self.center(balance), False, TEXT_SPEED),]
+ else:
+ msg = []
+ choices = ' '*10+'CHOICES: '
+
+ # Show what is in the coke machine
+ # Need to update this so it uses the abstracted system
+ cokes = []
+ for i in ['08', '18', '28', '38', '48', '58', '68']:
+ cokes.append((i, self.dispense.getItemInfo(i)))
+
+ for c in cokes:
+ if c[1][0] == 'dead':
+ continue
+ choices += '%s-(%sc)-%s8 '%(c[1][0], c[1][1], c[0])
+
+ # Show the final few options
+ choices += '55-DOOR '
+ choices += 'OR ANOTHER SNACK. '
+ choices += '99 TO READ AGAIN. '
+ choices += 'CHOICE? '
+ msg.append((choices, False, None))
+ # Send it to the display
+ mk.set_messages(msg)
+
+ """
+ In here just for fun.
+ """
+ def cookie(self):
+ seed(time())
+ messages = [' WASSUP! ', 'PINK FISH ', ' SECRETS ', ' ESKIMO ', ' FORTUNES ', 'MORE MONEY']
+ choice = int(random()*len(messages))
+ msg = messages[choice]
+ left = range(len(msg))
+ for i in range(len(msg)):
+ if msg[i] == ' ': left.remove(i)
+ reveal = 1
+ while left:
+ s = ''
+ for i in range(0, len(msg)):
+ if i in left:
+ if reveal == 0:
+ left.remove(i)
+ s += msg[i]
+ else:
+ s += chr(int(random()*26)+ord('A'))
+ reveal += 1
+ reveal %= 17
+ else:
+ s += msg[i]
+ self.v.display(s)
+
+ """
+ Format text so it will appear centered on the screen.
+ """
+ def center(self, str):
+ LEN = 10
+ return ' '*((LEN-len(str))/2)+str
+
+ """
+ Configure the things that will appear on screen whil the machine is idling.
+ """
+ def setup_idlers(self):
+ self.idlers = [
+ #
+ GrayIdler(self.v),
+ GrayIdler(self.v,one="*",zero="-"),
+ GrayIdler(self.v,one="/",zero="\\"),
+ GrayIdler(self.v,one="X",zero="O"),
+ GrayIdler(self.v,one="*",zero="-",reorder=1),
+ GrayIdler(self.v,one="/",zero="\\",reorder=1),
+ GrayIdler(self.v,one="X",zero="O",reorder=1),
+ #
+ ClockIdler(self.v),
+ ClockIdler(self.v),
+ ClockIdler(self.v),
+ #
+ StringIdler(self.v), # Hello Cruel World
+ StringIdler(self.v, text="Kill 'em all", repeat=False),
+ StringIdler(self.v, text=CREDITS),
+ StringIdler(self.v, text=str(math.pi) + " "),
+ StringIdler(self.v, text=str(math.e) + " "),
+ StringIdler(self.v, text=" I want some pizza - please call Broadway Pizza on +61 8 9389 8500 - and order as Quinn - I am getting really hungry", repeat=False),
+ # "Hello World" in brainfuck
+ StringIdler(self.v, text=">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++."),
+ #
+ TrainIdler(self.v),
+ #
+ FileIdler(self.v, '/usr/share/common-licenses/GPL-2',affinity=2),
+ #
+ PipeIdler(self.v, "/usr/bin/getent", "passwd"),
+ FortuneIdler(self.v,affinity=20),
+ ]
+ disabled = [
+ ]
+
+ """
+ Go back to the default idler.
+ """
+ def reset_idler(self, t = None):
+ self.idler = GreetingIdler(self.v, t)
+ self.vstatus.time_of_next_idlestep = time()+self.idler.next()
+ self.vstatus.time_of_next_idler = None
+ self.vstatus.time_to_autologout = None
+ self.vstatus.change_state(STATE_IDLE, 1)
+
+ """
+ Change to a random idler.
+ """
+ def choose_idler(self):
+
+ # Implementation of the King Of the Hill algorithm from;
+ # http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/
+
+ #def weighted_choice_king(weights):
+ # total = 0
+ # winner = 0
+ # for i, w in enumerate(weights):
+ # total += w
+ # if random.random() * total < w:
+ # winner = i
+ # return winner
+ #
+
+ total = 0
+ winner = None
+
+ for choice in self.idlers:
+ weight = choice.affinity()
+ total += weight
+ if random() * total < weight:
+ winner = choice
+
+ self.idler = winner
+
+ if self.idler:
+ self.idler.reset()
+ """
+ Run every step while the machine is idling.
+ """
+ def idle_step(self):
+ if self.idler.finished():
+ self.choose_idler()
+ self.vstatus.time_of_next_idler = time() + 30
+ nextidle = self.idler.next()
+ if nextidle is None:
+ nextidle = IDLE_SPEED
+ self.vstatus.time_of_next_idlestep = time()+nextidle
+
+ """
+ These next two events trigger no response in the code.
+ """
+ def handle_tick_event(self, event, params):
+ # don't care right now.
+ pass