from boxlift_api import Command, BoxLift
from pprint import pprint

box_lift = BoxLift('Lolevator', 'training_1', 'aywang31@gmail.com', '13247', event_name='pycon2015', sandbox_mode=True)

state = box_lift.send_commands()
pprint(state)

elevator_states = {}
for elevator in state['elevators']:
    elevator_states[elevator['id']] = {'speed': 0, 'direction': 1}


def get_state_for_elevator(elevator_id, state):
    for elevator in state['elevators']:
        if elevator['id'] == elevator_id:
            elevator['speed'] = elevator_states[elevator['id']]['speed']
            elevator['direction'] = elevator_states[elevator['id']]['direction']
            return elevator
    raise "Unknown elevator %s" % elevator_id

def floor_requests(elevator, state):
    return set(elevator['buttons_pressed'] + [request['floor'] for request in state['requests']])

def direction(elevator, state):
    requests = floor_requests(elevator, state)
    if len(requests) == 0:
        return 1
    if elevator['direction'] == 1:
        if max(requests) > elevator['floor']:
            return 1
        else:
            return -1
    elif elevator['direction'] == -1:
        if min(requests) < elevator['floor']:
            return -1
        else:
            return 1
    else:
        return 'Error - unknown elevator direction'

def stop_direction(floor, state):
    for request in state['requests']:
        if request['floor'] == floor:
            return request['direction']
    return 1

def stop_floor(elevator, state):
    if elevator['floor'] in [request['floor'] for request in state['requests']]:
        return True
    if elevator['floor'] in elevator['buttons_pressed']:
        return True
    return False

def iterate_elevator(elevator_id, state):
    elevator = get_state_for_elevator(elevator_id, state)
    if stop_floor(elevator, state):
        return Command(elevator_id, stop_direction(elevator['floor'], state), 0)
    elif direction(elevator, state) in [1, -1]:
        return Command(elevator_id, direction(elevator, state), 1)
    else:
        return Command(elevator_id, 1, 0)

while state['status'] != 'finished':
    command = iterate_elevator(0, state)
    state = box_lift.send_commands([command])
    pprint(state)


@@ -98,16 +98,18 @@
 ', '
-training
+ch_rnd_500
 _1',
@@ -177,19 +177,20 @@
 ox_mode=
-Tru
+Fals
 e)%0A%0Astat
@@ -681,16 +681,29 @@
   raise 
+RuntimeError(
 %22Unknown
@@ -729,16 +729,17 @@
 vator_id
+)
 %0A%0Adef fl
@@ -1289,22 +1289,34 @@
        r
-eturn 
+aise RuntimeError(
 'Error -
@@ -1343,16 +1343,17 @@
 rection'
+)
 %0A%0Adef st
@@ -2124,16 +2124,36 @@
  1, 0)%0A%0A
+iteration_count = 0%0A
 while st
@@ -2181,16 +2181,152 @@
 ished':%0A
+    commands = %5B%5D%0A    elevator_id = 0%0A    while elevator_id %3C (iteration_count / 10) + 1 and elevator_id %3C len(state%5B'elevators'%5D):%0A    
     comm
@@ -2348,17 +2348,27 @@
 levator(
-0
+elevator_id
 , state)
@@ -2364,24 +2364,240 @@
 _id, state)%0A
+        elevator_states%5Belevator_id%5D%5B'speed'%5D = command.speed%0A        elevator_states%5Belevator_id%5D%5B'direction'%5D = command.direction%0A        commands.append(command)%0A        elevator_id += 1%0A    iteration_count += 1%0A%0A
     state = 
@@ -2623,17 +2623,16 @@
 nds(
-%5B
 command
-%5D
+s
 )%0A  
@@ -2634,22 +2634,48 @@
 s)%0A    pprint(state)
+%0A    print iteration_count
 %0A%0A

Update lift2.py to use multiple elevators