Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sql
- import initTrains
- import csv
- import time
- import RouteCreater
- train_separation_constant = 60 # Maximum amount of time one train is allowed to receive user inputs for
- def getInfo(train): # function where server requests for info about a trainline in terms of train locations and ETAs
- if len(train) == 1:
- train = train + "."
- f = open("routes/" + train + ".csv") # WHEN THIS IS ON THE SERVER CHANGE THIS TO AN ABSOLUTE PATH
- r = csv.reader(f)
- stations = []
- for s in r:
- stations.append(s[0]) # Get list of all stations on this train line
- try:
- sql.getStationETA(train[0] + "_", stations[0])
- except:
- RouteCreater.updateRouteTables(train)
- startingdir = stations[0][-1]
- totaltime = 0 # Calculate the total approx time to traverse the route (used to find if any new trains are needed)
- i = 1
- while (stations[i][-1] == startingdir):
- train_ = train
- if (train[1] == '.'):
- train_ = train[0] + '_'
- totaltime += int(sql.getStationETA(train_, stations[i]))
- i = i + 1
- if train[-1] == ".": # Initialize trains
- initTrains.initTrains(train[:-1])
- else:
- initTrains.initTrains(train)
- if (train[1] == '.'):
- train = train[0] + "_"
- trains = sql.getTrains(train) # Having loaded up the initialized trains, get all trains.
- print(trains)
- print(stations)
- #construct all constant station ETAs
- constantStationETAs = {}
- for s in stations:
- constantStationETAs[s] = int(sql.getStationETA(train, s))
- time_now = int(time.time())
- for t in trains:
- time_diff = time_now - int(t[3]) # For each train, figure out where it should be based on the elapsed time
- startingdir = t[2][-1]
- upto = stations.index(t[2]) + 1
- station = stations[upto]
- nexteta = constantStationETAs[stations[upto]]
- trainalive = True
- while (time_diff > nexteta):
- print(str(time_diff) + " at station " + stations[upto])
- if (startingdir != stations[upto][-1] or nexteta == -1):
- print("Starting direction: " + startingdir + " Current Direction: " + stations[upto][-1] + " nexteta: " + str(nexteta))
- sql.removeTrain(t[0]) # If we reach the end of the line, remove the train
- trainalive = False
- break
- time_diff = time_diff - nexteta
- upto = upto + 1
- if (upto >= len(stations)):
- break
- station = stations[upto]
- nexteta = constantStationETAs[stations[upto]]
- if (trainalive):
- sql.updateTrainValues(t[0], station, time_now - time_diff) # If the train hasn't been removed, update its position
- #print(sql.getTrains(train))
- stationETAs = []
- for s in stations:
- stationETAs.append([s, totaltime]) # Initialize the list of stationETAs based on train position
- # The first stationETAs are initialized to 0.
- stationETAs[0][1] = 0
- initdir = stationETAs[0][0][-1]
- for s in stationETAs:
- if (initdir != stationETAs[0][0][-1]):
- s[1] = 0
- break
- trains = sql.getTrains(train)
- print(trains)
- for t in trains:
- time_now = int(time.time())
- time_diff = time_now - int(t[3]) # Figure out how much "free" time you have based on train offset
- startingdir = t[2][-1]
- upto = stations.index(t[2]) + 1
- eta = constantStationETAs[t[2]] - time_diff # Account for the first offset, which should always be less than ETA
- if (eta < stationETAs[upto][1]):
- stationETAs[upto][1] = eta
- upto = upto + 1
- time_elapsed = eta
- if (upto < len(stations)):
- nexteta = constantStationETAs[stations[upto]]
- while (startingdir == stations[upto][-1] and nexteta != -1):
- print("Train id " + str(t[0]) + " at " + str(time_elapsed))
- time_elapsed += nexteta # Find time to each station from current station of train
- if (time_elapsed < stationETAs[upto][1]):
- stationETAs[upto][1] = time_elapsed # Modify ETAs if they are less than another train's value
- upto = upto + 1
- if (upto >= len(stations)):
- break
- nexteta = constantStationETAs[stations[upto]]
- return stationETAs
- def userInput(train, station):
- sql.createUserInput()
- sql.addUserInput(train, station, str(time.time()))
- users = sql.getUserInput(train)
- print getInfo("Q")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement