Fix bug in race results (maybe)

main
DustyP 9 months ago
parent 29993a4d56
commit f33bb7e9a3

@ -186,7 +186,6 @@ func (dc *DerbyClock) readLoop() {
if len(buffer) >= 3 && string(buffer[len(buffer)-3:]) == "C\r\n" { if len(buffer) >= 3 && string(buffer[len(buffer)-3:]) == "C\r\n" {
dc.mu.Lock() dc.mu.Lock()
dc.status = StatusRunning dc.status = StatusRunning
dc.results = nil
dc.mu.Unlock() dc.mu.Unlock()
// Send race start event // Send race start event
@ -204,14 +203,6 @@ func (dc *DerbyClock) readLoop() {
// Try to extract a result from the buffer // Try to extract a result from the buffer
result := dc.tryExtractResult(buffer) result := dc.tryExtractResult(buffer)
if result != nil { if result != nil {
dc.mu.Lock()
// Add result to our stored results
dc.results = append(dc.results, result)
// Determine finish place based on how many results we have
result.FinishPlace = len(dc.results)
dc.mu.Unlock()
// Send lane finish event // Send lane finish event
dc.eventChan <- Event{ dc.eventChan <- Event{
Type: EventLaneFinish, Type: EventLaneFinish,
@ -282,11 +273,24 @@ func (dc *DerbyClock) tryExtractResult(buffer []byte) *Result {
continue continue
} }
finishPlace := 0
switch finishChar {
case "!":
finishPlace = 1
case "\"":
finishPlace = 2
case "#":
finishPlace = 3
case "$":
finishPlace = 4
}
// Create and return the result // Create and return the result
return &Result{ return &Result{
Lane: lane, Lane: lane,
Time: timeVal, Time: timeVal,
FinishPlace: 0, // Will be set by the caller FinishPlace: finishPlace,
FinishSymbol: finishChar, FinishSymbol: finishChar,
} }
} }

@ -47,6 +47,7 @@ func main() {
// Process events from the clock // Process events from the clock
go func() { go func() {
raceResults := make([]*derby.Result, 0)
for event := range clock.Events() { for event := range clock.Events() {
switch event.Type { switch event.Type {
case derby.EventRaceStart: case derby.EventRaceStart:
@ -56,14 +57,16 @@ func main() {
result := event.Result result := event.Result
fmt.Printf("🚗 Lane %d finished in place %d with time %.4f seconds\n", fmt.Printf("🚗 Lane %d finished in place %d with time %.4f seconds\n",
result.Lane, result.FinishPlace, result.Time) result.Lane, result.FinishPlace, result.Time)
raceResults = append(raceResults, result)
case derby.EventRaceComplete: case derby.EventRaceComplete:
fmt.Println("\n🏆 Race complete! Final results:") fmt.Println("\n🏆 Race complete! Final results:")
for _, result := range clock.Results() { for _, result := range raceResults {
fmt.Printf("Place %d: Lane %d - %.4f seconds\n", fmt.Printf("Place %d: Lane %d - %.4f seconds\n",
result.FinishPlace, result.Lane, result.Time) result.FinishPlace, result.Lane, result.Time)
} }
fmt.Println("\nEnter command (r/f/q/?):") fmt.Println("\nEnter command (r/f/q/?):")
raceResults = nil
} }
} }
}() }()

Loading…
Cancel
Save