Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. import sql
  2. import initTrains
  3. import csv
  4. import time
  5. import RouteCreater
  6.  
  7. train_separation_constant = 60 # Maximum amount of time one train is allowed to receive user inputs for
  8.  
  9. def getInfo(train): # function where server requests for info about a trainline in terms of train locations and ETAs
  10. if len(train) == 1:
  11. train = train + "."
  12.  
  13. f = open("routes/" + train + ".csv") # WHEN THIS IS ON THE SERVER CHANGE THIS TO AN ABSOLUTE PATH
  14. r = csv.reader(f)
  15. stations = []
  16. for s in r:
  17. stations.append(s[0]) # Get list of all stations on this train line
  18.  
  19. try:
  20. sql.getStationETA(train[0] + "_", stations[0])
  21. except:
  22. RouteCreater.updateRouteTables(train)
  23.  
  24. startingdir = stations[0][-1]
  25. totaltime = 0 # Calculate the total approx time to traverse the route (used to find if any new trains are needed)
  26. i = 1
  27. while (stations[i][-1] == startingdir):
  28. train_ = train
  29. if (train[1] == '.'):
  30. train_ = train[0] + '_'
  31. totaltime += int(sql.getStationETA(train_, stations[i]))
  32. i = i + 1
  33.  
  34. if train[-1] == ".": # Initialize trains
  35. initTrains.initTrains(train[:-1])
  36. else:
  37. initTrains.initTrains(train)
  38.  
  39. if (train[1] == '.'):
  40. train = train[0] + "_"
  41.  
  42. trains = sql.getTrains(train) # Having loaded up the initialized trains, get all trains.
  43. print(trains)
  44.  
  45. print(stations)
  46.  
  47. #construct all constant station ETAs
  48. constantStationETAs = {}
  49. for s in stations:
  50. constantStationETAs[s] = int(sql.getStationETA(train, s))
  51.  
  52. time_now = int(time.time())
  53. for t in trains:
  54. time_diff = time_now - int(t[3]) # For each train, figure out where it should be based on the elapsed time
  55. startingdir = t[2][-1]
  56. upto = stations.index(t[2]) + 1
  57. station = stations[upto]
  58. nexteta = constantStationETAs[stations[upto]]
  59. trainalive = True
  60. while (time_diff > nexteta):
  61. print(str(time_diff) + " at station " + stations[upto])
  62. if (startingdir != stations[upto][-1] or nexteta == -1):
  63. print("Starting direction: " + startingdir + " Current Direction: " + stations[upto][-1] + " nexteta: " + str(nexteta))
  64. sql.removeTrain(t[0]) # If we reach the end of the line, remove the train
  65. trainalive = False
  66. break
  67. time_diff = time_diff - nexteta
  68. upto = upto + 1
  69. if (upto >= len(stations)):
  70. break
  71. station = stations[upto]
  72. nexteta = constantStationETAs[stations[upto]]
  73. if (trainalive):
  74. sql.updateTrainValues(t[0], station, time_now - time_diff) # If the train hasn't been removed, update its position
  75.  
  76. #print(sql.getTrains(train))
  77. stationETAs = []
  78. for s in stations:
  79. stationETAs.append([s, totaltime]) # Initialize the list of stationETAs based on train position
  80.  
  81. # The first stationETAs are initialized to 0.
  82. stationETAs[0][1] = 0
  83. initdir = stationETAs[0][0][-1]
  84. for s in stationETAs:
  85. if (initdir != stationETAs[0][0][-1]):
  86. s[1] = 0
  87. break
  88.  
  89. trains = sql.getTrains(train)
  90. print(trains)
  91. for t in trains:
  92. time_now = int(time.time())
  93. time_diff = time_now - int(t[3]) # Figure out how much "free" time you have based on train offset
  94. startingdir = t[2][-1]
  95. upto = stations.index(t[2]) + 1
  96. eta = constantStationETAs[t[2]] - time_diff # Account for the first offset, which should always be less than ETA
  97. if (eta < stationETAs[upto][1]):
  98. stationETAs[upto][1] = eta
  99. upto = upto + 1
  100. time_elapsed = eta
  101. if (upto < len(stations)):
  102. nexteta = constantStationETAs[stations[upto]]
  103. while (startingdir == stations[upto][-1] and nexteta != -1):
  104. print("Train id " + str(t[0]) + " at " + str(time_elapsed))
  105. time_elapsed += nexteta # Find time to each station from current station of train
  106. if (time_elapsed < stationETAs[upto][1]):
  107. stationETAs[upto][1] = time_elapsed # Modify ETAs if they are less than another train's value
  108. upto = upto + 1
  109. if (upto >= len(stations)):
  110. break
  111. nexteta = constantStationETAs[stations[upto]]
  112.  
  113. return stationETAs
  114.  
  115. def userInput(train, station):
  116. sql.createUserInput()
  117. sql.addUserInput(train, station, str(time.time()))
  118.  
  119. users = sql.getUserInput(train)
  120.  
  121. print getInfo("Q")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement