-
Notifications
You must be signed in to change notification settings - Fork 0
/
heat_gen_random.py
executable file
·42 lines (40 loc) · 1.75 KB
/
heat_gen_random.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/python
from math import ceil
from random import randrange
from openderby import Category, Car, Heat
from openderby.registration import app
lanes = 6
warning = ' *** HEAT LANE MISMATCH ***'
# Do all Categories
for category in Category.query.all():
cat_car_cnt = Car.query.filter_by(category=category).count()
heat_cnt = int(ceil(float(cat_car_cnt) / float(lanes))) * lanes
print "Starting Category: %s - %i cars - %i heats" % (category, cat_car_cnt, heat_cnt)
for heat_id in range(1, heat_cnt+1):
print "Generating Heat %i" % heat_id
used_lanes = Heat.query.filter_by(id=heat_id, category=category).all()
used_lanes = map(lambda x: x.lane, used_lanes)
# print used_lanes
for lane in range(1, lanes+1):
if lane in used_lanes:
break
cars_q = Car.query.filter_by(category=category)\
.outerjoin(Heat, app.db.and_(Heat.car_id==Car.id, app.db.or_(Heat.id == heat_id, Heat.lane == lane))).filter(Heat.id.is_(None)).filter(Heat.lane.is_(None))
#print str(cars_q)
cars = cars_q.all()
car_cnt = len(cars)
if car_cnt:
car_index = randrange(0, car_cnt)
heat = Heat(id=heat_id, category=category, lane=lane, car=cars[car_index])
app.db.session.add(heat)
app.db.session.commit()
print "\nVerifying Cars"
cars = app.db.session.query(Car, app.db.func.count(Car.id).label('heats'))\
.filter_by(category=category).group_by(Car.id).join(Heat).all()
for car in cars:
warn = ''
if car[1] != lanes:
warn = warning
print "%s: %s Heats%s" % (car[0], car[1], warn)
print "\n"
print "Complete"