Skip to content

Commit

Permalink
добавил подсчет lap_postition #8
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Sep 23, 2021
1 parent 19a5c30 commit 7d9abed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Models/Lap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,42 @@ func GetAllResultsByRaceId(u *[]Lap, race_id_string string) (err error) {
return result.Error
}

// Return lap position by tag_id
func GetLapPosition(race_id uint, lap_number int, tag_id string) (lapPosition uint) {
//var lapCopy Lap
var lapCopy Lap
var lapsCopy []Lap
if DB.Table("laps").Where("race_id = ?" , race_id).First(&lapCopy).Error == nil {
if DB.Table("laps").Where("race_id = ?" , race_id).Where("lap_number = ?" , lap_number).Order("lap_time asc").Find(&lapsCopy).Error == nil {
var position uint = 1
for _ , m := range lapsCopy {
if m.TagID == tag_id {
break
} else {
position = position + 1
}
}
lapPosition = position

} else {
//first lap, first result
lapPosition = 1
}
} else {
fmt.Println("lapPosition: race data empty.")
//no such race found (may be this is the first result).
if lap_number == 0 {
fmt.Println("lapPosition: This is the first result.")
//no such race found (may be this is the first result).
lapPosition = 1
} else {
//no such race found, and not a 0 lap requested - return zero.
lapPosition = 0
}
}
return
}

// Return LeaderFirstLapDiscoveryUnixTime
func GetLeaderFirstLapDiscoveryUnixTime(race_id_uint uint) (leaderFirstLapDiscoveryUnixTime int64) {
var u Lap
Expand Down
2 changes: 2 additions & 0 deletions Models/Rfid.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ func startSaveLapsBufferToDatabase() {
//you are not the leader of the first lap
//calculate against the leader
lap.LapTime = lap.DiscoveryUnixTime - leaderFirstLapDiscoveryUnixTime
lap.LapPosition = GetLapPosition(currentlapRaceID, currentlapLapNumber, lap.TagID)
}
} else {
lap.LapTime = lap.DiscoveryUnixTime - previousDiscoveryUnixTime
lap.LapPosition = GetLapPosition(currentlapRaceID, currentlapLapNumber, lap.TagID)
}
lap.RaceTotalTime = previousRaceTotalTime + lap.LapTime

Expand Down

0 comments on commit 7d9abed

Please sign in to comment.