Skip to content

Commit

Permalink
Merge pull request #11 from skorbut/26-do-not-count-lap-zero
Browse files Browse the repository at this point in the history
start counting with 0 after race started, for first timing
  • Loading branch information
skorbut authored Sep 30, 2020
2 parents 9ff584f + 2b80ea0 commit d0953b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
16 changes: 12 additions & 4 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ def stop(self):
self.finished_at = datetime.datetime.now()

def add_lap(self, controller, time):
racer_id = self.racer(controller).id,
car_id = self.car(controller).id
return self.save_lap(controller, time, racer_id, car_id)

def save_lap(self, controller, time, racer_id, car_id):
lap = Lap(
race_id=self.id,
controller=controller,
time=time,
racer_id=self.racer(controller).id,
car_id=self.car(controller).id
racer_id=racer_id,
car_id=car_id
)
db.session.add(lap)
db.session.commit()
Expand Down Expand Up @@ -198,14 +203,17 @@ def __init__(self, num):
self.time = None
self.lap_time = None
self.best_time = None
self.laps = 0
self.laps = None

def newlap(self, timer):
if self.time is not None:
self.lap_time = timer.timestamp - self.time
if self.best_time is None or self.lap_time < self.best_time:
self.best_time = self.lap_time
self.laps += 1
if self.laps is None:
self.laps = 0
else:
self.laps += 1
self.time = timer.timestamp


Expand Down
3 changes: 2 additions & 1 deletion app/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def handle_control_unit_events():
timing.newlap(status_or_timer)
if current_race is not None:
current_race.add_lap(controller, timing.lap_time)
emit_lap(status_or_timer, timing)
if timing.lap_time is not None and timing.laps > 0:
emit_lap(status_or_timer, timing)
last_status_or_timer = status_or_timer
emit_cu_status('connected', 'unknown')
calculated_sleep_time = calculate_sleep_time(current_race, last_status)
Expand Down

0 comments on commit d0953b2

Please sign in to comment.